From 7a8f5c96c6d2d2e0d5eca6015c846212c0f72fb4 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 8 Jul 2016 13:22:03 -0400 Subject: [PATCH] WIP for gravity forms leads entry integration. I have the gravity forms entering the data from the forms into our tables. I'm using the dymanic field setting to set the inputName for the field. This will be used to pull each field needed for the leads form entries. --- class-gfleadsfieldaddon.php | 5 + includes/class-leads-gf-field.php | 10 +- setup/frontHooks.php | 254 ++++++++++++++++++++---------- 3 files changed, 185 insertions(+), 84 deletions(-) diff --git a/class-gfleadsfieldaddon.php b/class-gfleadsfieldaddon.php index 5200920..8e2729f 100644 --- a/class-gfleadsfieldaddon.php +++ b/class-gfleadsfieldaddon.php @@ -48,6 +48,11 @@ class GFLeadsFieldAddOn extends GFAddOn { add_action( 'gform_field_appearance_settings', array( $this, 'field_appearance_settings' ), 10, 2 ); } + // # Override functions + public function get_form_editor_field_title(){ + return esc_attr__( 'Leads', 'leadsfieldaddon' ); + } + // # SCRIPTS & STYLES ----------------------------------------------------------------------------------------------- diff --git a/includes/class-leads-gf-field.php b/includes/class-leads-gf-field.php index b3746c4..fbca2a0 100644 --- a/includes/class-leads-gf-field.php +++ b/includes/class-leads-gf-field.php @@ -4,7 +4,7 @@ if ( ! class_exists( 'GFForms' ) ) { die(); } -class Leads_GF_Field extends GF_Field { +class Leads_GF_Field extends GF_Field_Checkbox { /** * @var string $type The field type. @@ -41,15 +41,15 @@ class Leads_GF_Field extends GF_Field { return array( 'label_setting', 'description_setting', - 'rules_setting', - 'placeholder_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', + //'conditional_logic_field_setting', ); } @@ -92,7 +92,6 @@ class Leads_GF_Field extends GF_Field { * @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'] ); @@ -125,6 +124,7 @@ class Leads_GF_Field extends GF_Field { return sprintf( "
%s
", $this->type, $input ); } + */ } GF_Fields::register( new Leads_GF_Field() ); diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 2205057..359adec 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -12,6 +12,8 @@ * @release frontHooks.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/dataInterests.php'; +require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php'; /* * Place Misc Hooks and Filters here. If this file exists, it will be included @@ -29,92 +31,186 @@ // 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 ) { - //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' => '' - ) - ); + if ( $v['type'] == 'leads' ) { + $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' => '' + ); + } + } + $form['fields'][$k]['inputs'] = $inputs; + $form['fields'][$k]['choices'] = $choices; } } - //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' - ), + return $form; +}); +add_action( 'gform_after_submission', function( $entry, $form ){ + //echo '
$entry: ' . print_r($entry, true) . '
'; + $mapped_keys = array( + 'fname', + 'lname', + 'org', + 'addr1', + 'addr2', + 'city', + 'state', + 'zip', + 'country', + 'email', + 'phone', + 'phone2', + 'fax', ); - $newCheckbox->choices = array( - array( - 'text' => 'Skiing', - 'value' => '1', - 'isSelected' => false, - 'price' => '' + // have to go through the $form array to see what field we need from post + $glm_leads_entry = array(); + if ( $form['fields'] ) { + foreach ( $form['fields'] as $field ) { + switch ( $field['type'] ) { + case 'website': + case 'phone': + case 'email': + case 'text': + if ( in_array( $field['inputName'], $mapped_keys ) ) { + $glm_leads_entry[$field['inputName']] = $_POST['input_' . $field['id']]; + } + break; + case 'name': + case 'address': + if ( $field->inputs ) { + foreach ( $field->inputs as $input ) { + if ( in_array( $input['name'], $mapped_keys ) ) { + $glm_leads_entry[$input['name']] = $_POST['input_' . str_replace( '.', '_', $input['id'] )]; + } + } + } + break; + case 'leads': + $leads_fields = array(); + foreach ( $_POST as $key => $value ) { + if ( preg_match( '/input_' . $field['id'] . '_(\d*)/', $key, $matches ) ) { + // grab the final digit + $leads_fields[$matches[1]] = $value; + } + } + //$glm_leads_entry['leads'] = $leads_fields; + break; + default: + break; + } + } + } + // we need to have email field + if ( !isset( $glm_leads_entry['email'] ) ) { + return; + } + // create the lead + // check first to see if the lead exists by matching email address + $result = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads + WHERE email = %s", + $glm_leads_entry['email'] ), + ARRAY_A + ); + if ( $result['id'] ) { + $lead_id = $result['id']; + // will need to update the mail_ok or member fields + } else { + // this will need mail_ok and members to be setup also + $result = $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads', // table + array( + 'email' => $glm_leads_entry['email'] + ), // data + array( '%s' ) // data format + ); + if ( $result === false ) { + die( 'lead error given' ); + } + $lead_id = $this->wpdb->insert_id; + } + if ( !$lead_id ) { + return; + } + // create the lead entry + $lead_entry = $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry', // table array( - 'text' => 'Boating', - 'value' => '2', - 'isSelected' => false, - 'price' => '' - ), + 'source_id' => $entry['form_id'], + 'lead_id' => $lead_id, + 'fname' => ( isset( $glm_leads_entry['fname'] ) ) ? $glm_leads_entry['fname'] : '', + 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '', + 'lname' => ( isset( $glm_leads_entry['lname'] ) ) ? $glm_leads_entry['lname'] : '', + 'addr1' => ( isset( $glm_leads_entry['addr1'] ) ) ? $glm_leads_entry['addr1'] : '', + 'org' => ( isset( $glm_leads_entry['org'] ) ) ? $glm_leads_entry['org'] : '', + 'city' => ( isset( $glm_leads_entry['city'] ) ) ? $glm_leads_entry['city'] : '', + 'state' => ( isset( $glm_leads_entry['state'] ) ) ? $glm_leads_entry['state'] : '', + 'zip' => ( isset( $glm_leads_entry['zip'] ) ) ? $glm_leads_entry['zip'] : '', + 'country' => ( isset( $glm_leads_entry['country'] ) ) ? $glm_leads_entry['country'] : '', + 'phone' => ( isset( $glm_leads_entry['phone'] ) ) ? $glm_leads_entry['phone'] : '', + 'phone2' => ( isset( $glm_leads_entry['phone2'] ) ) ? $glm_leads_entry['phone2'] : '', + 'fax' => ( isset( $glm_leads_entry['fax'] ) ) ? $glm_leads_entry['fax'] : '', + 'user_trace_info' => $entry['ip'], + 'date_submitted' => $entry['date_created'] + ), // data array( - 'text' => 'Hiking', - 'value' => '3', - 'isSelected' => false, - 'price' => '' - ) + '%d', + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + ) // data format ); - */ - //echo '
$newCheckbox: ' . print_r($newCheckbox, true) . '
'; - //$form['fields'][] = $newCheckbox; - - return $form; -}); -add_action( 'gform_after_submission', function( $entry, $form ){ - echo '
$entry: ' . print_r($entry, true) . '
'; - echo '
$form: ' . print_r($form, true) . '
'; - exit; -}, 10, 2); -//add_action( 'gform_pre_submission', function ( $form ) { - // echo '
$_POST: ' . print_r($_POST, true) . '
'; + if ( $lead_entry === false ) { + die( 'lead_entry error given' ); + } + $lead_entry_id = $this->wpdb->insert_id; + // create the lead to interest entries + // using $leads_fields + // first delete any they current have + if ( is_array( $leads_fields) && !empty( $leads_fields ) ) { + foreach ( $leads_fields as $interest_id => $interest_val ) { + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests', // table + array( + 'interest_id' => $interest_id, + 'lead_entry_id' => $lead_entry_id + ), // data + array( '%d', '%d' ) // data format + ); + } + } + //echo '
$glm_leads_entry: ' . print_r($glm_leads_entry, true) . '
'; + //echo '
$_POST: ' . print_r($_POST, true) . '
'; //echo '
$form: ' . print_r($form, true) . '
'; - // exit; -//}); + //exit; +}, 10, 2); -- 2.17.1