From: Steve Sutton Date: Mon, 29 Jan 2018 20:20:51 +0000 (-0500) Subject: Updating the leads plugin X-Git-Tag: v1.1.3^2~8 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=d4e358da84356f509a36a3fc6971d1fa8d7f8c53;p=WP-Plugins%2Fglm-member-db-leads.git Updating the leads plugin Needed update for getting wmta leads into our leads system. --- diff --git a/models/admin/management/leads.php b/models/admin/management/leads.php index 7465fa5..11b5194 100644 --- a/models/admin/management/leads.php +++ b/models/admin/management/leads.php @@ -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 .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + // $this->importMemberCategories(); + $result .= $this->importMemberLeads(); + break; case 'importleads': $result .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; $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 '
$interests: ' . print_r( $interests, true ) . '
'; + // 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 = '
$results: ' . print_r( $results, true ) . '
'; + 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 '
$lead_entry_id: ' . print_r( $lead_entry_id, true ) . '
'; + if ( $lead_entry_id ) { + $newEntries++; + // Get this row's terms + $terms = wp_get_post_terms( + $contact['ID'], + 'member-lead-cat' + ); + $return .= '
$terms: ' . print_r( $terms, true ) . '
'; + foreach ( $terms as $term ) { + $lead_categories[$term->term_id] = $term->name; + $int_key = array_search( $term->name, $interests ); + // echo '
';
+                        // var_dump($int_key);
+                        // echo '
'; + 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 .= '
$lead_categories: ' . print_r( $lead_categories, true ) . '
'; + } + } + } + $next = $start + $limit; + $return = "

Added $newEntries.

"; + $next_url = 'admin.php?page=glm-members-admin-menu-management' + . '&glm_action=leads&option=import&option2=importmemberleads&start=' . $next; + $return .= 'Next'; + if ( $next > $total_number ) { + $completed = true; + } + if ( !$completed ) { + $return .= ""; + } + return $return; + } + /** * importLeads * diff --git a/setup/frontHooks.php b/setup/frontHooks.php index 857787b..4a09fb9 100644 --- a/setup/frontHooks.php +++ b/setup/frontHooks.php @@ -210,6 +210,8 @@ add_action( 'gform_after_submission', function( $entry, $form ){ } } } + // echo '
$gf_interests: ' . print_r( $gf_interests, true ) . '
'; + // echo '
$_POST: ' . print_r( $_POST, true ) . '
'; // 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 '
$leads_fields: ' . print_r( $leads_fields, true ) . '
'; // 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 '
$ints: ' . print_r( $ints, true ) . '
'; // 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 '
$interest_val: ' . print_r( $interest_val, true ) . '
'; + + // 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' ); + } + } } } } diff --git a/views/admin/management/leads.html b/views/admin/management/leads.html index 7467b78..092969a 100644 --- a/views/admin/management/leads.html +++ b/views/admin/management/leads.html @@ -6,6 +6,7 @@ Settings / Source List Gravity Forms Import Leads + Import Member Leads @@ -196,6 +197,23 @@
+
+ + + + + {if !$result} + + + + {else} + + + + {/if} +
{$result}
+
+