Updating the leads plugin
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 29 Jan 2018 20:20:51 +0000 (15:20 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 29 Jan 2018 20:20:51 +0000 (15:20 -0500)
Needed update for getting wmta leads into our leads system.

models/admin/management/leads.php
setup/frontHooks.php
views/admin/management/leads.html

index 7465fa5..11b5194 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/dataInterests.php';
 require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php';
 
 /**
@@ -179,6 +180,11 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement
 
 
         switch( $option2 ) {
+        case 'importmemberleads':
+            $result .= '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+            // $this->importMemberCategories();
+            $result .= $this->importMemberLeads();
+            break;
         case 'importleads':
             $result .= '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
             $result .= $this->importLeads();
@@ -298,6 +304,205 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement
         );
     }
 
+    public function setupFormInterests()
+    {
+        $this->wpdb->insert(
+            GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'gf_interests',
+            array(
+                'interest_id' => $interest_id,
+                'gf_field_id' => $gf_field_id,
+                'form_id'     => $form_id
+            ),
+            array( '%d', '%s', '%d' )
+        );
+    }
+
+    public function importMemberCategories()
+    {
+        // Create Leads Category group_name
+        $this->wpdb->insert(
+                GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'interest_groups',
+                array( 'title' => 'Lead Categories' ),
+                array( '%s' )
+        );
+        $terms = get_terms( 'member-lead-cat' );
+
+        foreach ( $terms as $term ) {
+            $this->wpdb->insert(
+                GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'interests',
+                array( 'title' => $term->name, 'group_id' => 1 ),
+                array( '%s', '%d' )
+            );
+        }
+    }
+
+    public function resetData()
+    {
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads" );
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry" );
+        $this->wpdb->query( "DELETE FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests" );
+    }
+
+    public function importMemberLeads()
+    {
+        $limit = 10;
+        $start = ( isset( $_REQUEST['start'] ) ) ? (int)filter_var( $_REQUEST['start'] ) : 0;
+        if ( $start === 0 ) {
+            $this->resetData();
+        }
+        // Grab the interest.
+        $Interest  = new GlmDataInterests( $this->wpdb, $this->config );
+        $intData   = $Interest->getList();
+        $interests = array();
+        if ( $intData ) {
+            foreach ( $intData as $interest ) {
+                // $interests[$interest['id']] = htmlspecialchars_decode( $interest['title'] );
+                $interests[$interest['id']] = $interest['title'];
+            }
+        }
+        $newEntries = 0;
+        // echo '<pre>$interests: ' . print_r( $interests, true ) . '</pre>';
+        // exit;
+        $source_id = 1; // Source id for the form
+
+        // get the total number of posts
+        $total_number = $this->wpdb->get_var(
+            "SELECT count(ID)
+               FROM " . $this->wpdb->prefix . "posts
+              WHERE post_type = 'member_lead'
+                AND post_status = 'publish'"
+        );
+        $completed = false;
+
+        $query =
+        "SELECT p.ID,p.post_title, pm1.meta_value AS 'fname', pm2.meta_value AS 'lname',
+                pm3.meta_value AS 'email', pm4.meta_value AS 'phone', pm5.meta_value AS 'address',
+                pm7.meta_value AS 'city', pm8.meta_value AS 'state', pm9.meta_value AS 'zip', p.post_date AS create_date, p.post_status
+           FROM wp_p6qnh6_posts p
+        INNER JOIN wp_p6qnh6_postmeta AS pm1 ON pm1.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm2 ON pm2.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm3 ON pm3.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm4 ON pm4.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm5 ON pm5.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm7 ON pm7.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm8 ON pm8.post_id = p.ID
+        INNER JOIN wp_p6qnh6_postmeta AS pm9 ON pm9.post_id = p.ID
+          WHERE pm1.meta_key = 'First Name'
+            AND pm2.meta_key = 'Last Name'
+            AND pm3.meta_key = 'Email'
+            AND pm4.meta_key = 'Phone'
+            AND pm5.meta_key = 'Address Line 1'
+            AND pm7.meta_key = 'City'
+            AND pm8.meta_key = 'State'
+            AND pm9.meta_key = 'Zip Code'
+            AND p.post_type = 'member_lead'
+            AND p.post_status = 'publish'";
+        $query .= " LIMIT $limit OFFSET $start";
+
+        $results = $this->wpdb->get_results( $query, ARRAY_A );
+        $return = '';
+        $return = '<pre>$results: ' . print_r( $results, true ) . '</pre>';
+        foreach ( $results as $contact ) {
+            $lead_categories = array();
+
+            // Check to see if a lead with this email address exists.
+            $lead_id = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
+                      WHERE email = %s",
+                    $contact['email']
+                )
+            );
+            // If the lead doesn't exists create it.
+            if ( !$lead_id ) {
+                $this->wpdb->insert(
+                    GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX. 'leads',
+                    array(
+                        'email'     => $contact['email'],
+                        'mail_ok'   => 1,
+                        'member_ok' => 1,
+                        'created'   => $contact['create_date'],
+                    ),
+                    array( '%s', '%s', '%s', '%s' )
+                );
+                $lead_id = $this->wpdb->insert_id;
+            }
+            if ( $lead_id ) {
+                $this->wpdb->insert(
+                    GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX. 'lead_entry',
+                    array(
+                        'source_id'      => $source_id,
+                        'lead_id'        => $lead_id,
+                        'fname'          => $contact['fname'],
+                        'lname'          => $contact['lname'],
+                        'addr1'          => $contact['address'],
+                        'city'           => $contact['city'],
+                        'state'          => $contact['state'],
+                        'zip'            => $contact['zip'],
+                        'phone'          => $contact['phone'],
+                        'fax'            => $contact['fax'],
+                        'date_submitted' => $contact['create_date'],
+                    ),
+                    array(
+                        '%d', // source id
+                        '%d', // lead id
+                        '%s', // first name
+                        '%s', // last name
+                        '%s', // address
+                        '%s', // city
+                        '%s', // state
+                        '%s', // zip
+                        '%s', // phone
+                        '%s', // fax
+                        '%s', // create date
+                    )
+                );
+                $lead_entry_id = $this->wpdb->insert_id;
+                // echo '<pre>$lead_entry_id: ' . print_r( $lead_entry_id, true ) . '</pre>';
+                if ( $lead_entry_id ) {
+                    $newEntries++;
+                    // Get this row's terms
+                    $terms = wp_get_post_terms(
+                        $contact['ID'],
+                        'member-lead-cat'
+                    );
+                    $return .= '<pre>$terms: ' . print_r( $terms, true ) . '</pre>';
+                    foreach ( $terms as $term ) {
+                        $lead_categories[$term->term_id] = $term->name;
+                        $int_key = array_search( $term->name, $interests );
+                        // echo '<pre>';
+                        // var_dump($int_key);
+                        // echo '</pre>';
+                        if ( $int_key !== false ) {
+                            $this->wpdb->insert(
+                                GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests',
+                                array(
+                                    'interest_id'   => $int_key,
+                                    'lead_entry_id' => $lead_entry_id
+                                ),
+                                array( '%d', '%d' )
+                            );
+                        }
+                    }
+                    // $return .= '<pre>$lead_categories: ' . print_r( $lead_categories, true ) . '</pre>';
+                }
+            }
+        }
+        $next = $start + $limit;
+        $return = "<p>Added $newEntries. </p>";
+        $next_url = 'admin.php?page=glm-members-admin-menu-management'
+            .  '&glm_action=leads&option=import&option2=importmemberleads&start=' . $next;
+        $return .= '<a href="' . $next_url . '">Next</a>';
+        if ( $next > $total_number ) {
+            $completed = true;
+        }
+        if ( !$completed ) {
+            $return .= "<script> window.location.href = '" . $next_url. "'; </script>";
+        }
+        return $return;
+    }
+
     /**
      * importLeads
      *
index 857787b..4a09fb9 100644 (file)
@@ -210,6 +210,8 @@ add_action( 'gform_after_submission', function( $entry, $form ){
             }
         }
     }
+    // echo '<pre>$gf_interests: ' . print_r( $gf_interests, true ) . '</pre>';
+    // echo '<pre>$_POST: ' . print_r( $_POST, true ) . '</pre>';
     // have to go through the $form array to see what field we need from post
     $glm_leads_entry = array();
     if ( $form['fields'] ) {
@@ -303,6 +305,7 @@ add_action( 'gform_after_submission', function( $entry, $form ){
             }
         }
     }
+    // echo '<pre>$leads_fields: ' . print_r( $leads_fields, true ) . '</pre>';
     // we need to have email field
     if ( !isset( $glm_leads_entry['email'] ) || !$glm_leads_entry['email'] ) {
         return;
@@ -395,11 +398,16 @@ add_action( 'gform_after_submission', function( $entry, $form ){
             $ints[$interest['id']] = htmlspecialchars_decode( $interest['title'] );
         }
     }
+    // echo '<pre>$ints: ' . print_r( $ints, true ) . '</pre>';
     // 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_val ) {
+            // echo '<pre>$interest_val: ' . print_r( $interest_val, true ) . '</pre>';
+
+            // for one type if checkboxes
+            // Here we have the interest id as interest_val
             $int_key = array_search( $interest_val, array_keys( $ints ) );
             if ( $int_key !== false ) {
                 $isGood = $this->wpdb->insert(
@@ -413,6 +421,24 @@ add_action( 'gform_after_submission', function( $entry, $form ){
                 if ( !$isGood ) {
                     die( 'We did not get lead_interests id' );
                 }
+                // Here we now have the interest_val as pointer to
+                // the ints array, so we need the array key
+            } else {
+                // if lead type
+                $int_key = array_search( $interest_val, $ints );
+                if ( $int_key !== false ) {
+                    $isGood = $this->wpdb->insert(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests',
+                        array(
+                            'interest_id'   => $int_key,
+                            'lead_entry_id' => $lead_entry_id
+                        ),
+                        array( '%d', '%d' )
+                    );
+                    if ( !$isGood ) {
+                        die( 'We did not get lead_interests id' );
+                    }
+                }
             }
         }
     }
index 7467b78..092969a 100644 (file)
@@ -6,6 +6,7 @@
     <a id="glm-leads-sources" data-show-table="glm-table-sources" class="glm-settings-tab nav-tab {if $option2 == ''} nav-tab-active{/if}">Settings / Source List</a>
     <a id="glm-leads-import" data-show-table="glm-table-import" class="glm-settings-tab nav-tab">Gravity Forms</a>
     <a id="glm-leads-importleads" data-show-table="glm-table-importleads" class="glm-settings-tab nav-tab{if $result && $option2 == 'importleads'} nav-tab-active{/if}">Import Leads</a>
+    <a id="glm-leads-importmemberleads" data-show-table="glm-table-importmemberleads" class="glm-settings-tab nav-tab{if $result && $option2 == 'importmemberleads'} nav-tab-active{/if}">Import Member Leads</a>
 </h2>
 
 <table id="glm-table-sources" class="glm-admin-table glm-settings-table{if $option2 != ''} glm-hidden{/if}">
     </table>
 </form>
 
+<form action="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=importmemberleads" method="post">
+    <input type="hidden" name="glm_action" value="leads" />
+    <input type="hidden" name="option" value="import" />
+    <input type="hidden" name="option2" value="importmemberleads" />
+    <table id="glm-table-importmemberleads" class="glm_admin_table glm-settings-table{if $option2 != 'importmemberleads'} glm-hidden{/if}">
+        {if !$result}
+            <tr>
+                <td colspan="2"><input type="submit" value="Import Member Leads" /></td>
+            </tr>
+        {else}
+            <tr>
+                <td>{$result}</td>
+            </tr>
+        {/if}
+    </table>
+</form>
+
 <script>
 jQuery(document).ready(function($){
     /*