WIP For adding field type to gravity forms
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 7 Jul 2016 21:00:15 +0000 (17:00 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 7 Jul 2016 21:00:15 +0000 (17:00 -0400)
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 [new file with mode: 0644]
css/leads_style.css [new file with mode: 0644]
includes/class-leads-gf-field.php [new file with mode: 0644]
index.php
js/leads_script.js [new file with mode: 0644]
models/admin/management/leads.php
setup/adminHooks.php
setup/databaseScripts/create_database_V0.0.1.sql
setup/frontHooks.php
views/admin/management/leads.html

diff --git a/class-gfleadsfieldaddon.php b/class-gfleadsfieldaddon.php
new file mode 100644 (file)
index 0000000..5200920
--- /dev/null
@@ -0,0 +1,136 @@
+<?php
+
+GFForms::include_addon_framework();
+
+class GFLeadsFieldAddOn extends GFAddOn {
+
+       protected $_version = GLM_MEMBER_LEADS_FIELD_ADDON_VERSION;
+       protected $_min_gravityforms_version = '1.9';
+       protected $_slug = 'leadsfieldaddon';
+    protected $_path = 'glm-member-db-leads/index.php';
+       protected $_full_path = __FILE__;
+       protected $_title = 'Gravity Forms Leads Field Add-On';
+       protected $_short_title = 'Leads Field Add-On';
+
+       /**
+        * @var object $_instance If available, contains an instance of this class.
+        */
+       private static $_instance = null;
+
+       /**
+        * Returns an instance of this class, and stores it in the $_instance property.
+        *
+        * @return object $_instance An instance of this class.
+        */
+       public static function get_instance() {
+               if ( self::$_instance == null ) {
+                       self::$_instance = new self();
+               }
+
+               return self::$_instance;
+       }
+
+       /**
+        * Include the field early so it is available when entry exports are being performed.
+        */
+       public function pre_init() {
+               parent::pre_init();
+
+               if ( $this->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( '<h6>%s</h6>%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 ) {
+                       ?>
+                       <li class="input_class_setting field_setting">
+                               <label for="input_class_setting">
+                                       <?php esc_html_e( 'Input CSS Classes', 'leadsfieldaddon' ); ?>
+                                       <?php gform_tooltip( 'input_class_setting' ) ?>
+                               </label>
+                               <input id="input_class_setting" type="text" class="fieldwidth-1" onkeyup="SetInputClassSetting(jQuery(this).val());" onchange="SetInputClassSetting(jQuery(this).val());"/>
+                       </li>
+
+                       <?php
+               }
+       }
+
+}
diff --git a/css/leads_style.css b/css/leads_style.css
new file mode 100644 (file)
index 0000000..553b750
--- /dev/null
@@ -0,0 +1 @@
+/* placeholder for css */
\ No newline at end of file
diff --git a/includes/class-leads-gf-field.php b/includes/class-leads-gf-field.php
new file mode 100644 (file)
index 0000000..b3746c4
--- /dev/null
@@ -0,0 +1,130 @@
+<?php
+
+if ( ! class_exists( 'GFForms' ) ) {
+       die();
+}
+
+class Leads_GF_Field extends GF_Field {
+
+       /**
+        * @var string $type The field type.
+        */
+       public $type = 'leads';
+
+       /**
+        * Return the field title, for use in the form editor.
+        *
+        * @return string
+        */
+       public function get_form_editor_field_title() {
+               return esc_attr__( 'Leads', 'leadsfieldaddon' );
+       }
+
+       /**
+        * Assign the field button to the Advanced Fields group.
+        *
+        * @return array
+        */
+       public function get_form_editor_button() {
+               return array(
+                       'group' => '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 = "<input name='input_{$id}' id='{$field_id}' type='text' value='{$value}' class='{$class}' {$tabindex} {$logic_event} {$placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>";
+
+               return sprintf( "<div class='ginput_container ginput_container_%s'>%s</div>", $this->type, $input );
+       }
+}
+
+GF_Fields::register( new Leads_GF_Field() );
index 8753edf..1254036 100644 (file)
--- 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 (file)
index 0000000..9d21e86
--- /dev/null
@@ -0,0 +1 @@
+// placeholder for javascript
\ No newline at end of file
index 0e61bd3..160db77 100644 (file)
@@ -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 '<pre>$leadForms: ' . print_r($leadForms, true) . '</pre>';
+        $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
index 6e7ad8e..fa63c7d 100644 (file)
@@ -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.
-  */
+ */
+
+
index fe9cfa0..a8339e7 100644 (file)
@@ -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
index 6bac904..2205057 100644 (file)
  */
 
 // 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 '<pre>$v[inputName]: ' . print_r($v['inputName'], true) . '</pre>';
+        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 '<pre>$form: ' . print_r($form, true) . '</pre>';
+
+    //echo '<pre>$form: ' . print_r($form, true) . '</pre>';
+    /*
     $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 '<pre>$newCheckbox: ' . print_r($newCheckbox, true) . '</pre>';
-    $form['fields'][] = $newCheckbox;
+    //$form['fields'][] = $newCheckbox;
 
     return $form;
 });
@@ -63,3 +113,8 @@ add_action( 'gform_after_submission', function( $entry, $form ){
     echo '<pre>$form: ' . print_r($form, true) . '</pre>';
     exit;
 }, 10, 2);
+//add_action( 'gform_pre_submission', function ( $form ) {
+   // echo '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
+    //echo '<pre>$form: ' . print_r($form, true) . '</pre>';
+   // exit;
+//});
index ef20a3a..0915aa1 100644 (file)
@@ -1,3 +1,30 @@
 {include file='admin/management/header.html'}
 
-<H1>ADMIN LEADS MANAGEMENT VIEW STUFF GOES HERE</H1>
\ No newline at end of file
+<H1>This requires Gravity Forms to be Installed and active</H1>
+
+<table class="glm-admin-table glm-settings-table">
+    <tr>
+        <th>
+            Form Name
+        </th>
+        <th>
+            Enabled
+        </th>
+        <th>
+            Groups
+        </th>
+    </tr>
+    {foreach $gravityForms as $form}
+    <tr>
+        <td>
+            {$form.title}
+        </td>
+        <td>
+            Yes
+        </td>
+        <td>
+
+        </td>
+    </tr>
+    {/foreach}
+</table>