Add import option for Harbor Country leads.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 11 Jan 2017 20:52:27 +0000 (15:52 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 11 Jan 2017 20:52:27 +0000 (15:52 -0500)
This will need work on it to make it usable by another client.

models/admin/leads/index.php
models/admin/management/leads.php
views/admin/management/leads.html

index 07dffd9..62257f0 100644 (file)
@@ -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 '<pre>$leads: ' . print_r( $leads, true ) . '</pre>';
+                    //exit;
                     unset($listResult);
 
                     // If we have list entries - even if it's an empty list
index 6cbf262..65e2b0c 100644 (file)
@@ -155,6 +155,11 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement
 
 
         switch( $option2 ) {
+        case 'importleads':
+            $result .= '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+            $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 '<p>No lead id!</p>';
+                echo '<pre>$contact: ' . print_r( $contact, true ) . '</pre>';
+                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 '<pre>
+            New leads: ' . $new_leads . '
+            Updated Leads: ' . $updated_leads . '</pre>';
+    }
+
     /**
      * 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 '<pre>$e: ' . print_r($e, true) . '</pre>';
+            wp_die();
+        }
+    }
 }
index 86b79eb..a4cf299 100644 (file)
@@ -3,8 +3,9 @@
 <h1>This requires Gravity Forms to be Installed and active</h1>
 
 <h2 class="nav-tab-wrapper" style="margin-bottom: 1em;">
-    <a id="glm-leads-sources" data-show-table="glm-table-sources" class="glm-settings-tab nav-tab nav-tab-active">Source List</a>
+    <a id="glm-leads-sources" data-show-table="glm-table-sources" class="glm-settings-tab nav-tab {if $option2 == ''} nav-tab-active{/if}">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>
 </h2>
 
 <table id="glm-table-sources" class="glm-admin-table glm-settings-table{if $option2 != ''} glm-hidden{/if}">
     <input type="hidden" name="option" value="import" />
     <input type="hidden" name="option2" value="import" />
     <input type="hidden" name="form_id" value="{$form.id}" />
-<table id="glm-table-import" class="glm-admin-table glm-settings-table{if $option2 != import} glm-hidden{/if}">
-    {if $forms}
-    <tr>
-        <td>
-            <table>
-                <thead>
-                    <th>Form Id</th>
-                    <th>Title</th>
-                    <th>version</th>
-                </thead>
-                <tbody>
-                    {foreach $forms as $form_list}
-                    <tr>
-                        <td> {$form_list.id} </td>
-                        <td> <a href="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=import&form_id={$form_list.id}">{$form_list.title}</a> </td>
-                        <td> {$form_list.version} </td>
-                    </tr>
-                    {/foreach}
-                <tbody>
-            </table>
-        </td>
-    </tr>
-    {/if}
-    {if $form && !$importing}
-    <tr>
-        <td>
-            <table>
-                <thead>
-                    <tr>
-                        <th>Field Id</th>
-                        <th>Type</th>
-                        <th>Label</th>
-                        <th>Import Name</th>
-                        <th>&nbsp;</th>
-                    </tr>
-                </thead>
-                <tbody>
-                    {foreach $form.fields as $field}
-                    <tr>
-                        <td>{$field.id}</td>
-                        <td>{$field.type}</td>
-                        <td>{$field.label}</td>
-                        <td>{if $field.inputs && !$field.inputs.0.id}<input type="text" name="import_fields[{$field.id}]" value="{if $import_fields[$field.id]}{$import_fields[$field.id]}{/if}" />{else}&nbsp;{/if}</td>
-                        <td>
-                            {if $field.inputs}
-                                <table>
-                                    <tbody>
-                                        {foreach $field.inputs as $input}
-                                            {if !isset($input.isHidden)}
-                                            <tr>
-                                                <td>
-                                                    <label>{$input.label}</label><br>
-                                                    <input type="text" name="import_fields[{$input.id}]" value="{if $import_fields[$input.id]}{$import_fields[$input.id]}{/if}" />
-                                                </td>
-                                            </tr>
-                                            {/if}
-                                        {/foreach}
-                                    </tbody>
-                                </table>
-                            {/if}
-                            {if $field.choices && $field.type == 'checkbox'}
-                                <table>
-                                    <thead>
-                                        <tr>
-                                            <th>Choices</th>
-                                        </tr>
-                                    </thead>
-                                    <tbody>
-                                        {foreach $field.choices as $choice}
+    <table id="glm-table-import" class="glm-admin-table glm-settings-table{if $option2 != import} glm-hidden{/if}">
+        {if $forms}
+        <tr>
+            <td>
+                <table>
+                    <thead>
+                        <th>Form Id</th>
+                        <th>Title</th>
+                        <th>version</th>
+                    </thead>
+                    <tbody>
+                        {foreach $forms as $form_list}
+                        <tr>
+                            <td> {$form_list.id} </td>
+                            <td> <a href="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=import&form_id={$form_list.id}">{$form_list.title}</a> </td>
+                            <td> {$form_list.version} </td>
+                        </tr>
+                        {/foreach}
+                    <tbody>
+                </table>
+            </td>
+        </tr>
+        {/if}
+        {if $form && !$importing}
+        <tr>
+            <td>
+                <table>
+                    <thead>
+                        <tr>
+                            <th>Field Id</th>
+                            <th>Type</th>
+                            <th>Label</th>
+                            <th>Import Name</th>
+                            <th>&nbsp;</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        {foreach $form.fields as $field}
+                        <tr>
+                            <td>{$field.id}</td>
+                            <td>{$field.type}</td>
+                            <td>{$field.label}</td>
+                            <td>{if $field.inputs && !$field.inputs.0.id}<input type="text" name="import_fields[{$field.id}]" value="{if $import_fields[$field.id]}{$import_fields[$field.id]}{/if}" />{else}&nbsp;{/if}</td>
+                            <td>
+                                {if $field.inputs}
+                                    <table>
+                                        <tbody>
+                                            {foreach $field.inputs as $input}
+                                                {if !isset($input.isHidden)}
+                                                <tr>
+                                                    <td>
+                                                        <label>{$input.label}</label><br>
+                                                        <input type="text" name="import_fields[{$input.id}]" value="{if $import_fields[$input.id]}{$import_fields[$input.id]}{/if}" />
+                                                    </td>
+                                                </tr>
+                                                {/if}
+                                            {/foreach}
+                                        </tbody>
+                                    </table>
+                                {/if}
+                                {if $field.choices && $field.type == 'checkbox'}
+                                    <table>
+                                        <thead>
                                             <tr>
-                                                <td>{$choice.text}</td>
+                                                <th>Choices</th>
                                             </tr>
-                                        {/foreach}
-                                    </tbody>
-                                </table>
-                            {/if}
-                        </td>
-                    </tr>
-                    {/foreach}
-                </tbody>
-            </table>
-        </td>
-    </tr>
-    <tr>
-        <td><input type="submit" value="Submit" /></td>
-    </tr>
-    {/if}
-    {if $importing}
-    <tr>
-        <td><a href="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=import&form_id={$form.id}&importing=1&offset={$next_offset}">Next Round</a></td>
-    </tr>
-    {/if}
-    {if $import_fields}
-        {php}
-            echo '<pre>' . print_r( $import_fields, true ) . '</pre>';
-        {/php}
-    {/if}
-    {if $result}
-    <tr>
-        <td>{$result}</td>
-    </tr>
-    {/if}
-</table>
+                                        </thead>
+                                        <tbody>
+                                            {foreach $field.choices as $choice}
+                                                <tr>
+                                                    <td>{$choice.text}</td>
+                                                </tr>
+                                            {/foreach}
+                                        </tbody>
+                                    </table>
+                                {/if}
+                            </td>
+                        </tr>
+                        {/foreach}
+                    </tbody>
+                </table>
+            </td>
+        </tr>
+        <tr>
+            <td><input type="submit" value="Submit" /></td>
+        </tr>
+        {/if}
+        {if $importing}
+        <tr>
+            <td><a href="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=import&form_id={$form.id}&importing=1&offset={$next_offset}">Next Round</a></td>
+        </tr>
+        {/if}
+        {if $import_fields}
+            {php}
+                echo '<pre>' . print_r( $import_fields, true ) . '</pre>';
+            {/php}
+        {/if}
+        {if $result}
+        <tr>
+            <td>{$result}</td>
+        </tr>
+        {/if}
+    </table>
+</form>
+
+<form action="{$thisUrl}?page={$thisPage}&glm_action=leads&option=import&option2=importleads" method="post">
+    <input type="hidden" name="glm_action" value="leads" />
+    <input type="hidden" name="option" value="import" />
+    <input type="hidden" name="option2" value="importleads" />
+    <table id="glm-table-importleads" class="glm-admin-table glm-settings-table{if $option2 != 'importleads'} glm-hidden{/if}">
+        {if !$result}
+            <tr>
+                <th>Database Host:</th>
+                <td><input type="text" name="db_host" /></td>
+            </tr>
+            <tr>
+                <th>Database Name:</th>
+                <td><input type="text" name="db_name" /></td>
+            </tr>
+            <tr>
+                <th>Database User:</th>
+                <td><input type="text" name="db_user" /></td>
+            </tr>
+            <tr>
+                <th>Database Password:</th>
+                <td><input type="text" name="db_password" /></td>
+            </tr>
+            <tr>
+                <td colspan="2"><input type="submit" value="Import Leads" /></td>
+            </tr>
+        {else}
+            <tr>
+                <td>{$result}</td>
+            </tr>
+        {/if}
+    </table>
 </form>
 
 <script>