From 1eb71f433bb21ae0b8cfa1542678d84213df3ecb Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 8 Jul 2016 14:36:10 -0400 Subject: [PATCH] Integrate the group interest with the new lead field I have the forms fields going into the leads tables now. What I'm using from gravity forms is the "Paramater Name" or inputName from Advanced field setting under Allow field to be populated dynamically. I have a new field type in gravity forms Called Leads. It's under the group Advanced. When you add the field you can setup the label to how you want it to be labelled on the front end. You need to assign a group to that field by setting the paramater name for that field the same name as the group name under settings. --- includes/class-leads-gf-field.php | 33 ++++-------------- setup/frontHooks.php | 57 +++++++++++++++++++------------ 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/includes/class-leads-gf-field.php b/includes/class-leads-gf-field.php index fbca2a0..3b58975 100644 --- a/includes/class-leads-gf-field.php +++ b/includes/class-leads-gf-field.php @@ -41,6 +41,7 @@ class Leads_GF_Field extends GF_Field_Checkbox { return array( 'label_setting', 'description_setting', + 'prepopulate_field_setting', //'rules_setting', //'placeholder_setting', 'input_class_setting', @@ -92,39 +93,19 @@ class Leads_GF_Field extends GF_Field_Checkbox { * @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; + $id = $this->id; + $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; + $disabled_text = $is_form_editor ? 'disabled="disabled"' : ''; - // 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 ); + return sprintf( "
", esc_attr( $field_id ), $this->get_checkbox_choices( $value, $disabled_text, $form_id ) ); } - */ } GF_Fields::register( new Leads_GF_Field() ); diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 359adec..2596cee 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -30,31 +30,43 @@ require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php'; // Inject things into gravity forms fields - This intercepts all GravityForms before displaying them add_filter( 'gform_pre_render', function( $form ) { - - $inputs = $choices = array(); foreach ( $form['fields'] as $k => $v ) { + $inputs = $choices = array(); if ( $v['type'] == 'leads' ) { + // check the inputName to see if it matches a group $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config ); $groups = $groupData->getList(); - $interestsData = new GlmDataInterests( $this->wpdb, $this->config ); - $interests = $interestsData->getList( null, 'title' ); - //echo '
$interests: ' . print_r($interests, true) . '
'; - if ( $interests ) { - foreach ( $interests as $interest ) { - $inputs[] = array( - 'id' => $v['id'] . '.' . $interest['id'], - 'label' => $interest['title'] - ); - $choices[] = array( - 'text' => $interest['title'], - 'value' => $interest['title'], - 'isSelected' => false, - 'price' => '' - ); + //echo '
$groups: ' . print_r($groups, true) . '
'; + //echo '
$v: ' . print_r($v, true) . '
'; + if ( $groups ) { + foreach ( $groups as $group ) { + if ( $v['inputName'] == $group['title'] ) { + $interestsData = new GlmDataInterests( $this->wpdb, $this->config ); + $interests = $interestsData->getList( + "group_id = " . $group['id'], + 'title' + ); + //echo '
$interests: ' . print_r($interests, true) . '
'; + if ( $interests ) { + foreach ( $interests as $interest ) { + $inputs[] = array( + 'id' => $v['id'] . '.' . $interest['id'], + 'label' => $interest['title'] + ); + $choices[] = array( + 'text' => $interest['title'], + 'value' => $interest['title'], + 'isSelected' => false, + 'price' => '' + ); + } + } + $form['fields'][$k]['inputs'] = $inputs; + $form['fields'][$k]['choices'] = $choices; + } } } - $form['fields'][$k]['inputs'] = $inputs; - $form['fields'][$k]['choices'] = $choices; + } } @@ -116,7 +128,7 @@ add_action( 'gform_after_submission', function( $entry, $form ){ } } // we need to have email field - if ( !isset( $glm_leads_entry['email'] ) ) { + if ( !isset( $glm_leads_entry['email'] ) || !$glm_leads_entry['email'] ) { return; } // create the lead @@ -138,9 +150,10 @@ add_action( 'gform_after_submission', function( $entry, $form ){ $result = $this->wpdb->insert( GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads', // table array( - 'email' => $glm_leads_entry['email'] + 'email' => $glm_leads_entry['email'], + 'created' => $entry['date_created'], ), // data - array( '%s' ) // data format + array( '%s', '%s' ) // data format ); if ( $result === false ) { die( 'lead error given' ); -- 2.17.1