From 54f1fb2cff360de39e4324bbe44e33d5aeabbbce Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 7 Jul 2016 17:00:15 -0400 Subject: [PATCH] WIP For adding field type to gravity forms Setting up a simple field add on. Should be able to use this for adding Leads field to any gravity form. When the field exists we know to store the form data. Also this will inject the checkboxes for all of the assigned groups. They will be able to pick which group they want for the form. And can place this field anywhere on the form they want. --- class-gfleadsfieldaddon.php | 136 ++++++++++++++++++ css/leads_style.css | 1 + includes/class-leads-gf-field.php | 130 +++++++++++++++++ index.php | 21 +++ js/leads_script.js | 1 + models/admin/management/leads.php | 10 +- setup/adminHooks.php | 6 +- .../create_database_V0.0.1.sql | 11 +- setup/frontHooks.php | 71 +++++++-- views/admin/management/leads.html | 29 +++- 10 files changed, 399 insertions(+), 17 deletions(-) create mode 100644 class-gfleadsfieldaddon.php create mode 100644 css/leads_style.css create mode 100644 includes/class-leads-gf-field.php create mode 100644 js/leads_script.js diff --git a/class-gfleadsfieldaddon.php b/class-gfleadsfieldaddon.php new file mode 100644 index 0000000..5200920 --- /dev/null +++ b/class-gfleadsfieldaddon.php @@ -0,0 +1,136 @@ +is_gravityforms_supported() && class_exists( 'GF_Field' ) ) { + require_once( 'includes/class-leads-gf-field.php' ); + } + } + + public function init_admin() { + parent::init_admin(); + + add_filter( 'gform_tooltips', array( $this, 'tooltips' ) ); + add_action( 'gform_field_appearance_settings', array( $this, 'field_appearance_settings' ), 10, 2 ); + } + + + // # SCRIPTS & STYLES ----------------------------------------------------------------------------------------------- + + /** + * Include leads_script.js when the form contains a 'leads' type field. + * + * @return array + */ + public function scripts() { + $scripts = array( + array( + 'handle' => 'leads_script', + 'src' => $this->get_base_url() . '/js/leads_script.js', + 'version' => $this->_version, + 'deps' => array( 'jquery' ), + 'enqueue' => array( + array( 'field_types' => array( 'leads' ) ), + ), + ), + + ); + + return array_merge( parent::scripts(), $scripts ); + } + + /** + * Include leads_style.css when the form contains a 'leads' type field. + * + * @return array + */ + public function styles() { + $styles = array( + array( + 'handle' => 'leads_style', + 'src' => $this->get_base_url() . '/css/leads_style.css', + 'version' => $this->_version, + 'enqueue' => array( + array( 'field_types' => array( 'leads' ) ) + ) + ) + ); + + return array_merge( parent::styles(), $styles ); + } + + + // # FIELD SETTINGS ------------------------------------------------------------------------------------------------- + + /** + * Add the tooltips for the field. + * + * @param array $tooltips An associative array of tooltips where the key is the tooltip name and the value is the tooltip. + * + * @return array + */ + public function tooltips( $tooltips ) { + $leads_tooltips = array( + 'input_class_setting' => sprintf( '
%s
%s', esc_html__( 'Input CSS Classes', 'leadsfieldaddon' ), esc_html__( 'The CSS Class names to be added to the field input.', 'simplefieldaddon' ) ), + ); + + return array_merge( $tooltips, $leads_tooltips ); + } + + /** + * Add the custom setting for the Leads field to the Appearance tab. + * + * @param int $position The position the settings should be located at. + * @param int $form_id The ID of the form currently being edited. + */ + public function field_appearance_settings( $position, $form_id ) { + // Add our custom setting just before the 'Custom CSS Class' setting. + if ( $position == 250 ) { + ?> +
  • + + +
  • + + 'advanced_fields', + 'text' => $this->get_form_editor_field_title(), + ); + } + + /** + * The settings which should be available on the field in the form editor. + * + * @return array + */ + function get_form_editor_field_settings() { + return array( + 'label_setting', + 'description_setting', + 'rules_setting', + 'placeholder_setting', + 'input_class_setting', + 'css_class_setting', + 'size_setting', + 'admin_label_setting', + 'default_value_setting', + 'visibility_setting', + 'conditional_logic_field_setting', + ); + } + + /** + * Enable this field for use with conditional logic. + * + * @return bool + */ + public function is_conditional_logic_supported() { + return true; + } + + /** + * The scripts to be included in the form editor. + * + * @return string + */ + public function get_form_editor_inline_script_on_page_render() { + + // set the default field label for the leads type field + $script = sprintf( "function SetDefaultValues_leads(field) {field.label = '%s';}", $this->get_form_editor_field_title() ) . PHP_EOL; + + // initialize the fields custom settings + $script .= "jQuery(document).bind('gform_load_field_settings', function (event, field, form) {" . + "var inputClass = field.inputClass == undefined ? '' : field.inputClass;" . + "jQuery('#input_class_setting').val(inputClass);" . + "});" . PHP_EOL; + + // saving the leads setting + $script .= "function SetInputClassSetting(value) {SetFieldProperty('inputClass', value);}" . PHP_EOL; + + return $script; + } + + /** + * Define the fields inner markup. + * + * @param array $form The Form Object currently being processed. + * @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission. + * @param null|array $entry Null or the Entry Object currently being edited. + * + * @return string + */ + public function get_field_input( $form, $value = '', $entry = null ) { + $id = absint( $this->id ); + $form_id = absint( $form['id'] ); + $is_entry_detail = $this->is_entry_detail(); + $is_form_editor = $this->is_form_editor(); + + // Prepare the value of the input ID attribute. + $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; + + $value = esc_attr( $value ); + + // Get the value of the inputClass property for the current field. + $inputClass = $this->inputClass; + + // Prepare the input classes. + $size = $this->size; + $class_suffix = $is_entry_detail ? '_admin' : ''; + $class = $size . $class_suffix . ' ' . $inputClass; + + // Prepare the other input attributes. + $tabindex = $this->get_tabindex(); + $logic_event = ! $is_form_editor && ! $is_entry_detail ? $this->get_conditional_logic_event( 'keyup' ) : ''; + $placeholder_attribute = $this->get_field_placeholder_attribute(); + $required_attribute = $this->isRequired ? 'aria-required="true"' : ''; + $invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"'; + $disabled_text = $is_form_editor ? 'disabled="disabled"' : ''; + + // Prepare the input tag for this field. + $input = ""; + + return sprintf( "
    %s
    ", $this->type, $input ); + } +} + +GF_Fields::register( new Leads_GF_Field() ); diff --git a/index.php b/index.php index 8753edf..1254036 100644 --- a/index.php +++ b/index.php @@ -211,3 +211,24 @@ add_filter('glm-member-db-register-addon','glmMembersRegisterLeads', 10, 1); */ require_once(GLM_MEMBERS_LEADS_PLUGIN_SETUP_PATH.'/permissions.php'); +/** + * Setup for the custom field add on for gravity form + */ +define( 'GLM_MEMBER_LEADS_FIELD_ADDON_VERSION', '1.0' ); + +add_action( 'gform_loaded', array( 'glm_member_db_leads_bootstrap', 'load' ), 5 ); + +class glm_member_db_leads_bootstrap { + + public static function load() { + + if ( ! method_exists( 'GFForms', 'include_addon_framework' ) ) { + return; + } + + require_once( 'class-gfleadsfieldaddon.php' ); + + GFAddOn::register( 'GFLeadsFieldAddOn' ); + } + +} diff --git a/js/leads_script.js b/js/leads_script.js new file mode 100644 index 0000000..9d21e86 --- /dev/null +++ b/js/leads_script.js @@ -0,0 +1 @@ +// placeholder for javascript \ No newline at end of file diff --git a/models/admin/management/leads.php b/models/admin/management/leads.php index 0e61bd3..160db77 100644 --- a/models/admin/management/leads.php +++ b/models/admin/management/leads.php @@ -12,6 +12,7 @@ * @release leadss.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ * @link http://dev.gaslightmedia.com/ */ +require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php'; /** * GlmMembersAdmin_management_leads @@ -115,11 +116,16 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement public function modelAction($actionData = false) { - //$leadForms = GFAPI::get_forms(); - //echo '
    $leadForms: ' . print_r($leadForms, true) . '
    '; + $gravity_forms = GFAPI::get_forms(); + + $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config ); + $groups = $groupData->getList(); // Compile template data $templateData = array( + 'formData' => print_r( $gravity_forms, true ), + 'gravityForms' => $gravity_forms, + 'groups' => $groups, ); // Return status, suggested view, and data to controller diff --git a/setup/adminHooks.php b/setup/adminHooks.php index 6e7ad8e..fa63c7d 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -13,7 +13,7 @@ * @link http://dev.gaslightmedia.com/ */ -/* +/** * Place Misc Hooks and Filters here. If this file exists, it will be included * by the add-on main plugin script. * @@ -24,4 +24,6 @@ * }); * * Also note that parameters will be in the context of the main admin controller constructor. - */ + */ + + diff --git a/setup/databaseScripts/create_database_V0.0.1.sql b/setup/databaseScripts/create_database_V0.0.1.sql index fe9cfa0..a8339e7 100644 --- a/setup/databaseScripts/create_database_V0.0.1.sql +++ b/setup/databaseScripts/create_database_V0.0.1.sql @@ -1,11 +1,11 @@ --- Gaslight Media Members Database - Leads +-- Gaslight Media Members Database - Leads -- File Created: 12/02/15 15:27:15 -- Database Version: 0.0.1 -- Database Creation Script --- +-- -- This file is called to create a new set of tables for this -- add-on for the most receint database version for this add-on. --- +-- -- There should only be one such file in this directory -- -- To permit each query below to be executed separately, @@ -30,6 +30,9 @@ CREATE TABLE {prefix}sources ( id INT NOT NULL AUTO_INCREMENT, title TINYTEXT NULL, -- Title/Name of source for reference code TINYTEXT NULL, -- Code supplied by form to indicate source + form_id INT NULL, + enabled BOOLEAN NULL, + groups TEXT NULL, PRIMARY KEY (id), INDEX (code(20)) ); @@ -68,7 +71,7 @@ CREATE TABLE {prefix}lead_entry ( ---- --- Interest Groups +-- Interest Groups CREATE TABLE {prefix}interest_groups ( id INT NOT NULL AUTO_INCREMENT, title TINYTEXT NULL, -- Group name diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 6bac904..2205057 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -27,34 +27,84 @@ */ // Inject things into gravity forms fields - This intercepts all GravityForms before displaying them -add_filter( 'gform_pre_render', function($form) { +add_filter( 'gform_pre_render', function( $form ) { + foreach ( $form['fields'] as $k => $v ) { + //echo '
    $v[inputName]: ' . print_r($v['inputName'], true) . '
    '; + if ( $v['inputName'] == 'glm_interest' ) { + $form['fields'][$k]['choices'] = array( + 0 => array( + 'text' => 'Skiing', + 'value' => 'Skiing', + 'isSelected' => false, + 'price' => '' + ), + 1 => array( + 'text' => 'Boating', + 'value' => 'Boating', + 'isSelected' => false, + 'price' => '' + ), + 2 => array( + 'text' => 'Hiking', + 'value' => 'Hiking', + 'isSelected' => false, + 'price' => '' + ), + 3 => array( + 'text' => 'Biking', + 'value' => 'Biking', + 'isSelected' => false, + 'price' => '' + ) + ); + } + } //echo '
    $form: ' . print_r($form, true) . '
    '; + + //echo '
    $form: ' . print_r($form, true) . '
    '; + /* $newCheckbox = new GF_Field_Checkbox(); + $newCheckbox->adminLabel = 'glm_assoc_interest'; $newCheckbox->label = 'Interest Fields'; $newCheckbox->inputName = 'glm_assoc_interest'; + $newCheckbox->inputs = array( + array( + 'id' => '5.1', + 'label' => 'Skiing' + ), + array( + 'id' => '5.2', + 'label' => 'Boating' + ), + array( + 'id' => '5.3', + 'label' => 'Hiking' + ), + ); $newCheckbox->choices = array( - 0 => array( + array( 'text' => 'Skiing', 'value' => '1', 'isSelected' => false, - 'price' => false + 'price' => '' ), - 1 => array( + array( 'text' => 'Boating', 'value' => '2', 'isSelected' => false, - 'price' => false + 'price' => '' ), - 2 => array( + array( 'text' => 'Hiking', 'value' => '3', 'isSelected' => false, - 'price' => false + 'price' => '' ) ); + */ //echo '
    $newCheckbox: ' . print_r($newCheckbox, true) . '
    '; - $form['fields'][] = $newCheckbox; + //$form['fields'][] = $newCheckbox; return $form; }); @@ -63,3 +113,8 @@ add_action( 'gform_after_submission', function( $entry, $form ){ echo '
    $form: ' . print_r($form, true) . '
    '; exit; }, 10, 2); +//add_action( 'gform_pre_submission', function ( $form ) { + // echo '
    $_POST: ' . print_r($_POST, true) . '
    '; + //echo '
    $form: ' . print_r($form, true) . '
    '; + // exit; +//}); diff --git a/views/admin/management/leads.html b/views/admin/management/leads.html index ef20a3a..0915aa1 100644 --- a/views/admin/management/leads.html +++ b/views/admin/management/leads.html @@ -1,3 +1,30 @@ {include file='admin/management/header.html'} -

    ADMIN LEADS MANAGEMENT VIEW STUFF GOES HERE

    \ No newline at end of file +

    This requires Gravity Forms to be Installed and active

    + + + + + + + + {foreach $gravityForms as $form} + + + + + + {/foreach} +
    + Form Name + + Enabled + + Groups +
    + {$form.title} + + Yes + + +
    -- 2.17.1