bug fix
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 8 Jul 2016 19:30:12 +0000 (15:30 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 8 Jul 2016 19:30:12 +0000 (15:30 -0400)
Was adding incorrect interest id to the table.
Can't use the forms id's they're based on the choice numbers.

setup/frontHooks.php

index 2596cee..3f73913 100644 (file)
@@ -73,7 +73,7 @@ add_filter( 'gform_pre_render', function( $form ) {
     return $form;
 });
 add_action( 'gform_after_submission', function( $entry, $form ){
-    //echo '<pre>$entry: ' . print_r($entry, true) . '</pre>';
+    $leads_fields = array();
     $mapped_keys = array(
         'fname',
         'lname',
@@ -88,12 +88,21 @@ add_action( 'gform_after_submission', function( $entry, $form ){
         'phone',
         'phone2',
         'fax',
+        'mail_ok',
+        'member_ok',
     );
     // 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 'checkbox':
+                if ( in_array( $field['inputName'], $mapped_keys ) ) {
+                    if ( isset( $_POST['input_' . $field['id'] . '_1'] ) && $_POST['input_' . $field['id'] . '_1'] ) {
+                        $glm_leads_entry[$field['inputName']] = 1;
+                    }
+                }
+                break;
             case 'website':
             case 'phone':
             case 'email':
@@ -113,14 +122,12 @@ add_action( 'gform_after_submission', function( $entry, $form ){
                 }
                 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;
+                        $leads_fields[] = $value;
                     }
                 }
-                //$glm_leads_entry['leads'] = $leads_fields;
                 break;
             default:
                 break;
@@ -150,10 +157,12 @@ 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'],
-                'created' => $entry['date_created'],
+                'email'     => $glm_leads_entry['email'],
+                'created'   => $entry['date_created'],
+                'mail_ok'   => ( $glm_leads_entry['mail_ok'] ) ? 1: 0,
+                'member_ok' => ( $glm_leads_entry['member_ok'] ) ? 1: 0,
             ), // data
-            array( '%s', '%s' ) // data format
+            array( '%s', '%s', '%s', '%s' ) // data format
         );
         if ( $result === false ) {
             die( 'lead error given' );
@@ -207,23 +216,36 @@ add_action( 'gform_after_submission', function( $entry, $form ){
         die( 'lead_entry error given' );
     }
     $lead_entry_id = $this->wpdb->insert_id;
+
+    $ints = array();
+    $interestsData  = new GlmDataInterests( $this->wpdb, $this->config );
+    $interests      = $interestsData->getList();
+    if ( $interests ) {
+        foreach ( $interests as $interest ) {
+            $ints[$interest['id']] = $interest['title'];
+        }
+    }
+    //echo '<pre>$interests: ' . print_r($interests, true) . '</pre>';
+    //echo '<pre>$ints: ' . print_r($ints, true) . '</pre>';
+    //echo '<pre>$leads_fields: ' . print_r($leads_fields, true) . '</pre>';
     // create the lead to interest entries
     // using $leads_fields
     // first delete any they current have
+    //echo '<pre>$leads_fields: ' . print_r($leads_fields, true) . '</pre>';
     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
-            );
+        foreach ( $leads_fields as $interest_val ) {
+            $int_key = array_search( $interest_val, $ints );
+            //echo '<pre>$int_key: ' . print_r($int_key, true) . '</pre>';
+            if ( $int_key ) {
+                $this->wpdb->insert(
+                    GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests', // table
+                    array(
+                        'interest_id'   => $int_key,
+                        'lead_entry_id' => $lead_entry_id
+                    ), // data
+                    array( '%d', '%d' ) // data format
+                );
+            }
         }
     }
-    //echo '<pre>$glm_leads_entry: ' . print_r($glm_leads_entry, true) . '</pre>';
-    //echo '<pre>$_POST: ' . print_r($_POST, true) . '</pre>';
-    //echo '<pre>$form: ' . print_r($form, true) . '</pre>';
-    //exit;
 }, 10, 2);