Add Lead Entry Form done
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 15 Jul 2016 17:46:43 +0000 (13:46 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 15 Jul 2016 17:46:43 +0000 (13:46 -0400)
Also have put in success messages for the update insert parts.
And hide the search form when searching and add button to toggle the
form.

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

index 213e8e1..ec7447c 100644 (file)
@@ -100,6 +100,11 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
         $haveLeads           = false;
         $leadCount           = 0;
         $addingLead          = false;
+        $searching           = false;
+        $updating            = false;
+        $updating_error      = false;
+        $inserting           = false;
+        $inserting_error     = false;
 
         // Get any provided option
         if ( isset( $_REQUEST['option'] ) ) {
@@ -147,6 +152,9 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
         }
 
         if ( ( isset( $search ) && $search ) || isset( $search_params ) && $search_params ) {
+            if ( isset( $search ) && $search ) {
+                $searching = true;
+            }
             // Filter the $_POST variables from the search form.
             // Verify mm/dd/YYYY date format for the from and to dates.
             if ( !isset( $search_params ) ) {
@@ -307,30 +315,36 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                     $paging = false;
                 }
 
-                //$leads = $this->getList( $where, $order );
-                $listResult = $this->getSimpleEntriesList( $where, $order, true, 'id', $start, $limit );
-
-                // Get paging results
-                $numbDisplayed = $listResult['returned'];
-                $lastDisplayed = $listResult['last'];
-                if ($start == 1) {
-                    $prevStart = false;
-                } else {
-                    $prevStart = $start - $limit;
-                    if ($start < 1) {
-                        $start = 1;
+
+                switch ( $option ) {
+                case 'csv':
+                    $leads = $this->getList( $where, $order );
+                    break;
+                default:
+                    $listResult = $this->getSimpleEntriesList( $where, $order, true, 'id', $start, $limit );
+                    // Get paging results
+                    $numbDisplayed = $listResult['returned'];
+                    $lastDisplayed = $listResult['last'];
+                    if ($start == 1) {
+                        $prevStart = false;
+                    } else {
+                        $prevStart = $start - $limit;
+                        if ($start < 1) {
+                            $start = 1;
+                        }
+                    }
+                    if ($listResult['returned'] == $limit) {
+                        $nextStart = $start + $limit;
                     }
-                }
-                if ($listResult['returned'] == $limit) {
-                    $nextStart = $start + $limit;
-                }
 
-                // since we're doing paging, we have to break out just the member data
-                $leads = $listResult['list'];
-                unset($listResult);
+                    // since we're doing paging, we have to break out just the member data
+                    $leads = $listResult['list'];
+                    unset($listResult);
 
-                // If we have list entries - even if it's an empty list
-                $success = true;
+                    // If we have list entries - even if it's an empty list
+                    break;
+                }
+                $success   = true;
                 $haveLeads = false;
                 if ($leads !== false) {
 
@@ -342,6 +356,8 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                     }
                 }
 
+
+
             } else {
                 $leads = false;
             }
@@ -441,6 +457,7 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             break;
 
         case 'update':
+            $updating = true;
             if ( $this->config['loggedInUser']['contactUser'] ) {
                 break;
             }
@@ -463,7 +480,6 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                         ),
                         array( '%d', '%d' )
                     );
-                    var_dump( $this->wpdb->insert_id );
                 }
             }
 
@@ -473,15 +489,12 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             }
             // Get the record for this Entry.
             $entry = $this->editEntry( $this->entryId );
+            //echo '<pre>$entry: ' . print_r($entry, true) . '</pre>';
 
             // get the field from the leads table
             $leadData = new GlmDataLeads( $this->wpdb, $this->config  );
             $lead     = $leadData->getEntry( $entry['fieldData']['lead_id']['value'] );
-            if ( $lead ) {
-                $entry['fieldData']['mail_ok'] = $lead['mail_ok'];
-                $entry['fieldData']['member_ok'] = $lead['member_ok'];
-            }
-            //echo '<pre>$entry: ' . print_r($entry, true) . '</pre>';
+            //echo '<pre>$lead: ' . print_r($lead, true) . '</pre>';
 
             // Use the edit view file.
             $view = 'edit.html';
@@ -523,12 +536,149 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             }
             // Compile template data
             $templateData = array(
-                'addingLead'        => $addingLead,
-                'entry'             => $entry,
-                'lead_interests'    => $lead_interests,
+                'addingLead'     => $addingLead,
+                'entry'          => $entry,
+                'lead'           => $lead,
+                'lead_interests' => $lead_interests,
             );
             break;
 
+        case 'insert':
+            $inserting = true;
+            if ( $this->config['loggedInUser']['contactUser'] ) {
+                break;
+            }
+            $email     = filter_var( $_REQUEST['email'], FILTER_VALIDATE_EMAIL );
+            $mail_ok   = filter_var( $_REQUEST['mail_ok'], FILTER_VALIDATE_BOOLEAN );
+            $member_ok = filter_var( $_REQUEST['member_ok'], FILTER_VALIDATE_BOOLEAN );
+            if ( !$email ) {
+                die( 'Fatal Error: Email did not validate' );
+            }
+            // Check to see if this email is already in leads table
+            $lead = $this->wpdb->get_row(
+                $this->wpdb->prepare(
+                    "SELECT *
+                       FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
+                      WHERE email = %s",
+                    $email
+                ),
+                ARRAY_A
+            );
+            if ( !$lead ) {
+                // Insert new email address as new lead
+                $this->wpdb->insert(
+                    GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'leads',
+                    array(
+                        'email'     => $email,
+                        'mail_ok'   => $mail_ok,
+                        'member_ok' => $member_ok,
+                        'created'   => current_time( 'mysql', false ),
+                    ),
+                    array( '%s', '%s', '%s', '%s' )
+                );
+                // get leads id
+                $lead_id = $this->wpdb->insert_id;
+            } else {
+                $lead_id = $lead['id'];
+                echo '<pre>$lead: ' . print_r($lead, true) . '</pre>';
+            }
+            if ( !isset( $lead_id ) && !$lead_id ) {
+                die( 'Fatal Error: Cannot generate lead_id for new leads record' );
+            }
+            /* Source
+             * If there's no admin source then create one.
+             */
+            $source_id = $this->wpdb->get_var(
+                $this->wpdb->prepare(
+                    "SELECT id
+                       FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources
+                      WHERE title = %s",
+                    'Admin'
+                )
+            );
+            if ( !$source_id ) {
+                $this->wpdb->insert(
+                    GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'sources',
+                    array(
+                        'title'   => 'Admin',
+                        'enabled' => 1
+                    ),
+                    array( '%s', '%s' )
+                );
+                $source_id = $this->wpdb->insert_id;
+            }
+            if ( !$source_id ) {
+                die( 'Fatal Error: Cannot generate source_id for new source record' );
+            }
+            /*
+             * Now that we have a source_id and lead_id we can
+             * insert the entry record.
+             */
+            $fname   = filter_var( $_REQUEST['fname'], FILTER_SANITIZE_STRING );
+            $lname   = filter_var( $_REQUEST['lname'], FILTER_SANITIZE_STRING );
+            $addr1   = filter_var( $_REQUEST['addr1'], FILTER_SANITIZE_STRING );
+            $addr2   = filter_var( $_REQUEST['addr2'], FILTER_SANITIZE_STRING );
+            $city    = filter_var( $_REQUEST['city'], FILTER_SANITIZE_STRING );
+            $state   = filter_var( $_REQUEST['state'], FILTER_SANITIZE_STRING );
+            $zip     = filter_var( $_REQUEST['zip'], FILTER_SANITIZE_STRING );
+            $country = filter_var( $_REQUEST['country'], FILTER_SANITIZE_STRING );
+            $phone   = filter_var( $_REQUEST['phone'], FILTER_SANITIZE_STRING );
+            $phone2  = filter_var( $_REQUEST['phone2'], FILTER_SANITIZE_STRING );
+            $fax     = filter_var( $_REQUEST['fax'], FILTER_SANITIZE_STRING );
+            $this->wpdb->insert(
+                GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . 'lead_entry',
+                array(
+                    'date_submitted'  => current_time( 'mysql', false ) ,
+                    'user_trace_info' => $_SERVER['REMOTE_ADDR'],
+                    'lead_id'         => $lead_id,
+                    'source_id'       => $source_id,
+                    'fname'           => $fname,
+                    'lname'           => $lname,
+                    'addr1'           => $addr1,
+                    'addr2'           => $addr2,
+                    'city'            => $city,
+                    'state'           => $state,
+                    'zip'             => $zip,
+                    'country'         => $country,
+                    'phone'           => $phone,
+                    'phone2'          => $phone2,
+                    'fax'             => $fax,
+                ),
+                array(
+                    '%s',
+                    '%s',
+                    '%d',
+                    '%d',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                    '%s',
+                )
+            );
+            // Get the Entry id
+            $entry_id = $this->wpdb->insert_id;
+
+            // Add the interests to lead_interests
+            if ( isset( $_POST['interests'] ) && !empty( $_POST['interests'] ) ) {
+                foreach ( $_POST['interests'] as $interest_id => $new_interest ) {
+                    $this->wpdb->insert(
+                        GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "lead_interests",
+                        array(
+                            'lead_entry_id' => $entry_id,
+                            'interest_id'   => $interest_id
+                        ),
+                        array( '%d', '%d' )
+                    );
+                }
+            }
+
         case 'add':
             if ( $this->config['loggedInUser']['contactUser'] ) {
                 break;
@@ -621,6 +771,11 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
         $templateData['start']             = $start;
         $templateData['limit']             = $limit;
         $templateData['paging']            = $paging;
+        $templateData['searching']         = $searching;
+        $templateData['updating']          = $updating;
+        $templateData['updating_error']    = $updating_error;
+        $templateData['inserting']         = $inserting;
+        $templateData['inserting_error']   = $inserting_error;
 
 
         // Return status, any suggested view, and any data to controller
index 88ad0aa..f78701f 100644 (file)
@@ -1,19 +1,72 @@
 {include file='admin/leads/header.html'}
 
+{if $updating}<span class="glm-notice glm-flash-updated glm-right">Lead Entry Updated</span>{/if}
+{if $updating_error}<span class="glm-error glm-flash-updated glm-right">Lead Entry Update Error</span>{/if}
+{if $inserting}<span class="glm-notice glm-flash-updated glm-right">Lead Entry Added</span>{/if}
+{if $inserting_error}<span class="glm-error glm-flash-updated glm-right">Lead Entry Add Error</span>{/if}
+
 {if $entry.status}
 <form action="{$thisUrl}?page={$thisPage}" method="post">
     <input type="hidden" name="glm_action" value="{$thisAction}" />
-    <input type="hidden" name="option" value="update" />
     {if !$addingLead}
-    <input type="hidden" name="entry" value="{$entry.fieldData.id}" />
+        <input type="hidden" name="entry" value="{$entry.fieldData.id}" />
+        <input type="hidden" name="option" value="update" />
+    {else}
+        <input type="hidden" name="option" value="insert" />
     {/if}
     <input type="hidden" name="lead_id" value="{$entry.fieldData.lead_id.value}" />
+    <input type="hidden" name="source_id" value="{$entry.fieldData.source_id.value}" />
     <table class="glm-admin-table glm-lead-table">
         <tbody>
         <tr>
-            <th>Email</th>
-            <td><input type="text" name="org" value="{$entry.fieldData.lead_id.name}" /></td>
+            <th colspan="2">Lead</th>
+        </tr>
+        <tr>
+            <td colspan="2">
+                <table>
+                    <tr>
+                        <td>Email</td>
+                        <td>
+                            {if $addingLead}
+                                <input type="text" name="email" value="">
+                            {else}
+                                {$lead.email}
+                            {/if}
+                        </td>
+                    </tr>
+                    <tr>
+                        <td>Mail Ok?</td>
+                        <td>
+                            {if $addingLead}
+                                <input type="checkbox" name="mail_ok" value="1">
+                            {else}
+                                {$lead.mail_ok.name}
+                            {/if}
+                        </td>
+                    </tr>
+                    <tr>
+                        <td>Member Ok?</td>
+                        <td>
+                            {if $addingLead}
+                                <input type="checkbox" name="member_ok" value="1">
+                            {else}
+                                {$lead.member_ok.name}
+                            {/if}
+                        </td>
+                    </tr>
+                </table>
+
+            </td>
+        </tr>
+        <tr>
+            <th colspan="2">Entry</th>
+        </tr>
+        {if !$addingLead}
+        <tr>
+            <th>Date Submitted</th>
+            <td>{$entry.fieldData.date_submitted|date_format}</td>
         </tr>
+        {/if}
         <tr>
             <th>Company</th>
             <td><input type="text" name="org" value="{$entry.fieldData.org}" /></td>
             <th>Fax</th>
             <td><input type="text" name="fax" value="{$entry.fieldData.fax}" /></td>
         </tr>
-        <tr>
-            <th>Mail Ok?</th>
-            <td><label><input type="checkbox" name="mail_ok" value="1"{if $entry.fieldData.mail_ok.value == 1} checked{/if}>Yes</label></td>
-        </tr>
-        <tr>
-            <th>Member Ok?</th>
-            <td><label><input type="checkbox" name="member_ok" value="1"{if $entry.fieldData.member_ok.value == 1} checked{/if}>Yes</label></td>
-        </tr>
         <tr>
             <td colspan="2">
                 {foreach $grouped_interests as $group_name => $group}
 </form>
 {/if}
 
+<script>
+jQuery(document).ready(function($){
+    // Flash certain elements for a short time after display
+    $(".glm-flash-updated").fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500);
+});
+</script>
 {include file='admin/footer.html'}
index 961eb30..22f893c 100644 (file)
@@ -4,16 +4,22 @@
     #lead-search-form td {
         padding: 5px;
     }
+    #lead-search-form.hide {
+        display: none;
+    }
 </style>
 {if $user_can_edit_leads}
     <a class="button button-primary glm-button glm-right" href="{$thisUrl}?page={$thisPage}&option=add">Add New Lead</a>
 {/if}
+{if $searching}
+<button id="search-form-toggle">Show / Hide Search Form</button>
+{/if}
 <form action="{$thisUrl}?page={$thisPage}" method="post">
     <input type="hidden" name="search" value="1" />
     <input type="hidden" name="prevStart" value="{$prevStart}" />
     <input type="hidden" name="nextStart" value="{$nextStart}" />
     <input type="hidden" name="limit" value="{$limit}" />
-    <table style="width: 500px;" id="lead-search-form">
+    <table style="width: 500px;" id="lead-search-form"{if $searching} class="hide"{/if}>
         <tr>
             <td style="text-align: right;"><label for="glm-form-source_id">Source</label></td>
             <td>
                     </td>
                     <td> {$lead.source_id.name} </td>
                     <td> {$lead.org} </td>
-                    <td> {$lead.date_submitted} </td>
+                    <td> {$lead.date_submitted|date_format} </td>
                 </tr>
            {/foreach}
        {/if}
 jQuery(document).ready(function($){
     $('#glm-form-from_date').datepicker();
     $('#glm-form-to_date').datepicker();
+    $('#search-form-toggle').click(function(){
+        $('#lead-search-form').toggleClass('hide');
+    });
 });
 </script>