From: Steve Sutton Date: Wed, 11 Jan 2017 20:52:27 +0000 (-0500) Subject: Add import option for Harbor Country leads. X-Git-Tag: v1.1.1^2~6 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=2e81eba097c9e0243c4bfa1023b969c771ab823f;p=WP-Plugins%2Fglm-member-db-leads.git Add import option for Harbor Country leads. This will need work on it to make it usable by another client. --- diff --git a/models/admin/leads/index.php b/models/admin/leads/index.php index 07dffd9..62257f0 100644 --- a/models/admin/leads/index.php +++ b/models/admin/leads/index.php @@ -354,6 +354,8 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry // since we're doing paging, we have to break out just the member data $leads = $listResult['list']; + //echo '
$leads: ' . print_r( $leads, true ) . '
'; + //exit; unset($listResult); // If we have list entries - even if it's an empty list diff --git a/models/admin/management/leads.php b/models/admin/management/leads.php index 6cbf262..65e2b0c 100644 --- a/models/admin/management/leads.php +++ b/models/admin/management/leads.php @@ -155,6 +155,11 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement switch( $option2 ) { + case 'importleads': + $result .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $result .= $this->importLeads(); + break; + case 'import': $total_imported_leads = 0; $result = ''; @@ -266,6 +271,213 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement ); } + /** + * importLeads + * + * Import the leads from their old contact database. + * This has been written only for Harbor Country database contacts. + * + * @access public + * @return void + */ + public function importLeads() + { + $new_leads = 0; + $updated_leads = 0; + $interests = array(); + $new_interests = array(); + $interests_map = array(); + + // Connect to the postgresql database + $db_host = filter_var( $_REQUEST['db_host'], FILTER_SANITIZE_STRING ); + $db_name = filter_var( $_REQUEST['db_name'], FILTER_SANITIZE_STRING ); + $db_user = filter_var( $_REQUEST['db_user'], FILTER_SANITIZE_STRING ); + $db_password = filter_var( $_REQUEST['db_password'], FILTER_SANITIZE_STRING ); + $this->connectPostgresDb( $db_host, $db_name, $db_user, $db_password ); + + // Get list of contact interest + // From old database + $sql = " + SELECT id, header + FROM contacts.contact_inq + ORDER BY id"; + + $results = $this->dbh->query( $sql )->fetchAll( PDO::FETCH_ASSOC ); + + foreach ( $results as $row ) { + $interests[$row['id']] = $row['header']; + } + + // Get list of new interest + $sql = " + SELECT * + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "interests + ORDER BY id"; + $new_interests = $this->wpdb->get_results( $sql, ARRAY_A ); + + foreach ( $new_interests as $ints ) { + if ( $key = array_search( $ints['title'], $interests ) ) { + $interests_map[$key] = $ints['id']; + } + } + + // Get contacts + $sql = " + SELECT * + FROM contacts.contact + WHERE email != '' + AND email is not null + AND create_date >= '2014-01-01' + AND contact_type not like '%3'"; + $contacts = $this->dbh->query( $sql )->fetchAll( PDO::FETCH_ASSOC ); + foreach ( $contacts as $contact ) { + // 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'] + ) + ); + + // Add/Update lead + if ( $lead_id ) { + // Don't update lead right now + /* + $this->wpdb->update( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX. 'leads', + array( + 'mail_ok' => ( ($contact['mail_ok'] ) ? $contact['mail_ok'] : 0 ), + 'member_ok' => ( ($contact['members'] ) ? $contact['members'] : 0 ), + ), + array( 'id' => $lead_id ), + array( '%s', '%s' ), + array( '%d' ) + ); + */ + $updated_leads++; + } else { + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX. 'leads', + array( + 'email' => $contact['email'], + 'mail_ok' => ( ($contact['mail_ok'] ) ? $contact['mail_ok'] : 0 ), + 'member_ok' => ( ($contact['members'] ) ? $contact['members'] : 0 ), + 'created' => $contact['create_date'], + ), + array( '%s', '%s', '%s', '%s' ) + ); + $lead_id = $this->wpdb->insert_id; + if ( $lead_id ) { + $new_leads++; + } + } + if ( !$lead_id ) { + echo '

No lead id!

'; + echo '
$contact: ' . print_r( $contact, true ) . '
'; + wp_die(); + } + + // Add Lead Entry record + // Check to see if this lead_id has an entry with same create_date + $lead_entry_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_entry + WHERE date_submitted = %s + AND lead_id = %d", + $contact['create_date'], + $lead_id + ) + ); + if ( $lead_entry_id ) { + // Do nothing for now + // Don't want to mess with already imported entries. + } else { + // $source_id is the new source table id for the ContactType + $source_id = $matches = null; + if ( $contact['contact_type'] ) { + // get last digit from contact_type field + if ( preg_match( '%:(\d):$%', $contact['contact_type'], $matches ) ) { + if ( isset( $matches[1] ) ) { + $source_id = $matches[1]; + // TODO; Add option in import leads form for source/types + // This part here changes the source_id from Harbor + // Country so it is the correct source_id. + // Different sites will have different contact_type + // to source_id so this will need to be put into + // the first form. + if ( $source_id == 4 ) { + $source_id = 3; + } + } + } + } + $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'], + 'org' => $contact['company'], + 'addr1' => $contact['address'], + 'addr2' => $contact['address2'], + 'city' => $contact['city'], + 'state' => $contact['state'], + 'zip' => $contact['zip'], + 'phone' => $contact['phone'], + 'fax' => $contact['fax'], + 'date_submitted' => $contact['create_date'], + ), + array( + '%d', + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + ) + ); + $lead_entry_id = $this->wpdb->insert_id; + } + + if ( $lead_entry_id && $contact['interest'] ) { + // remove ending colons + $interest_string = preg_replace( '%^:|:$%', '', $contact['interest'] ); + if ( $interest_string ) { + $interest_array = explode( ':', $interest_string ); + // Add entry record for the interests + foreach ( $interest_array as $int_id ) { + $this->wpdb->insert( + GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_interests', + array( + 'interest_id' => $interests_map[$int_id], + 'lead_entry_id' => $lead_entry_id + ), + array( + '%d', + '%d' + ) + ); + } + } + } + + } + return '
+            New leads: ' . $new_leads . '
+            Updated Leads: ' . $updated_leads . '
'; + } + /** * addInterestGroup * @@ -706,4 +918,49 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement } } } + /** + * connectPostgresDb + * + * Make a connection to the given database for the site. (postgres) + * Sets the $this->dbh with the postgers database connection + * + * @param mixed $db_host + * @param mixed $db_name + * @param mixed $db_user + * @param mixed $db_password + * @access public + * @return void + */ + public function connectPostgresDb($db_host, $db_name, $db_user, $db_password) + { + $conn_str = "pgsql:"; + if ( $db_host ) { + $conn_part[] = "host={$db_host}"; + } + if ( $db_name ) { + $conn_part[] = "dbname={$db_name}"; + } + if ( $db_user ) { + $conn_part[] = "user={$db_user}"; + } + if ( $db_password ) { + $conn_part[] = "password={$db_password}"; + } + if ( !empty($conn_part) ) { + $conn_str .= implode( " ", $conn_part ); + } + $driver_options = array( + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_BOTH, + ); + try { + $this->dbh = new PDO($conn_str, null, null, $driver_options); + $this->dbh->setAttribute( + PDO::ATTR_ERRMODE, + PDO::ERRMODE_EXCEPTION + ); + } catch(PDOException $e) { + echo '
$e: ' . print_r($e, true) . '
'; + wp_die(); + } + } } diff --git a/views/admin/management/leads.html b/views/admin/management/leads.html index 86b79eb..a4cf299 100644 --- a/views/admin/management/leads.html +++ b/views/admin/management/leads.html @@ -3,8 +3,9 @@

This requires Gravity Forms to be Installed and active

@@ -28,109 +29,142 @@ -
- {if $forms} - - - - {/if} - {if $form && !$importing} - - + + {/foreach} + +
- - - - - - - - {foreach $forms as $form_list} - - - - - - {/foreach} - -
Form IdTitleversion
{$form_list.id} {$form_list.title} {$form_list.version}
-
- - - - - - - - - - - - {foreach $form.fields as $field} - - - - - - + {foreach $field.choices as $choice} + + + + {/foreach} + +
Field IdTypeLabelImport Name 
{$field.id}{$field.type}{$field.label}{if $field.inputs && !$field.inputs.0.id}{else} {/if} - {if $field.inputs} - - - {foreach $field.inputs as $input} - {if !isset($input.isHidden)} - - - - {/if} - {/foreach} - -
-
- -
- {/if} - {if $field.choices && $field.type == 'checkbox'} - - - - - - - - {foreach $field.choices as $choice} +
Choices
+ {if $forms} + + + + {/if} + {if $form && !$importing} + + - - - - - {/if} - {if $importing} - - - - {/if} - {if $import_fields} - {php} - echo '
' . print_r( $import_fields, true ) . '
'; - {/php} - {/if} - {if $result} - - - - {/if} -
+ + + + + + + + {foreach $forms as $form_list} + + + + + + {/foreach} + +
Form IdTitleversion
{$form_list.id} {$form_list.title} {$form_list.version}
+
+ + + + + + + + + + + + {foreach $form.fields as $field} + + + + + + - - {/foreach} - -
Field IdTypeLabelImport Name 
{$field.id}{$field.type}{$field.label}{if $field.inputs && !$field.inputs.0.id}{else} {/if} + {if $field.inputs} + + + {foreach $field.inputs as $input} + {if !isset($input.isHidden)} + + + + {/if} + {/foreach} + +
+
+ +
+ {/if} + {if $field.choices && $field.type == 'checkbox'} + + - + - {/foreach} - -
{$choice.text}Choices
- {/if} -
-
Next Round
{$result}
+ +
{$choice.text}
+ {/if} +
+ + + + + + {/if} + {if $importing} + + Next Round + + {/if} + {if $import_fields} + {php} + echo '
' . print_r( $import_fields, true ) . '
'; + {/php} + {/if} + {if $result} + + {$result} + + {/if} + + + +
+ + + + + {if !$result} + + + + + + + + + + + + + + + + + + + + {else} + + + + {/if} +
Database Host:
Database Name:
Database User:
Database Password:
{$result}