Working more on the add edit form.
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Jul 2016 20:59:22 +0000 (16:59 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Jul 2016 20:59:22 +0000 (16:59 -0400)
Adding to the edit template the mail_ok and member_ok fields.
Getting the info from the main leads record.

classes/data/dataLeadEntry.php
classes/data/dataLeads.php
models/admin/leads/index.php
models/admin/management/leads.php
views/admin/leads/edit.html
views/admin/leads/index.html
views/admin/management/leads.html

index ed7afe1..560685b 100644 (file)
@@ -276,4 +276,39 @@ class GlmDataLeadEntry extends GlmDataAbstract
         return $r;
     }
 
+   /*
+     * Get a simple members list - Name and ID only
+     *
+     * @return array Array of Name and ID for all members
+     * @access public
+     */
+    public function getSimpleEntriesList(
+        $where     = '',
+        $order     = '',
+        $fieldVals = true,
+        $idField   = 'id',
+        $start     = false,
+        $limit     = false
+    ) {
+
+        // Save the current fields array and make a copy
+        $fSave = $this->fields;
+
+        // Remove what we don't want from the copy and get the list
+        $this->fields = array(
+            'id'             => $fSave['id'],
+            'fname'          => $fSave['fname'],
+            'lname'          => $fSave['lname'],
+            'org'            => $fSave['org'],
+            'source_id'      => $fSave['source_id'],
+            'date_submitted' => $fSave['date_submitted'],
+        );
+
+        $entryList = $this->getList( $where, $order, $fieldVals, $idField, $start, $limit );
+
+        // Restore the fields list
+        $this->fields = $fSave;
+
+        return $entryList;
+    }
 }
index 30ccda3..0257cdc 100644 (file)
@@ -123,11 +123,25 @@ class GlmDataLeads extends GlmDataAbstract
                 'use'       => 'a',
             ),
 
+            'mail_ok' => array(
+                'field'     => 'mail_ok',
+                'type'      => 'checkbox',
+                'required'  => false,
+                'use'       => 'a',
+            ),
+
+            'member_ok' => array(
+                'field'     => 'member_ok',
+                'type'      => 'checkbox',
+                'required'  => false,
+                'use'       => 'a',
+            ),
+
          );
 
     }
 
-    /*
+    /**
      * Entry Post Processing Call-Back Method
      *
      * Perform post-processing for all result entries.
index 5868108..213e8e1 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 // Load Leads data abstract
+require_once(GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataLeads.php');
 require_once(GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataLeadEntry.php');
 require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterests.php';
 require_once GLM_MEMBERS_LEADS_PLUGIN_CLASS_PATH.'/data/dataInterestGroups.php';
@@ -84,10 +85,21 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
     public function modelAction($actionData = false)
     {
+        $where               = ' true ';
         $option              = false;
         $user_can_edit_leads = false;
         $wpUser              = $this->config['loggedInUser']['wpUser'];
         $search_id           = false;
+        $numbDisplayed       = false;
+        $lastDisplayed       = false;
+        $paging              = true;
+        $prevStart           = false;
+        $nextStart           = false;
+        $start               = 1;
+        $limit               = 20;        // Set to the number of listings per page
+        $haveLeads           = false;
+        $leadCount           = 0;
+        $addingLead          = false;
 
         // Get any provided option
         if ( isset( $_REQUEST['option'] ) ) {
@@ -156,7 +168,8 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                                 'regexp' => '%([0-9]{2})/([0-9]{2})/([0-9]{4})%'
                             )
                         ),
-                        'mail_ok' => FILTER_VALIDATE_BOOLEAN,
+                        'mail_ok'   => FILTER_VALIDATE_BOOLEAN,
+                        'member_ok' => FILTER_VALIDATE_BOOLEAN,
                         'interests' => array(
                             'filter' => FILTER_VALIDATE_BOOLEAN,
                             'flags'  => FILTER_FORCE_ARRAY
@@ -164,6 +177,13 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                     )
                 );
             }
+            // checking the booleans for empty or false
+            if ( isset( $_REQUEST['mail_ok'] ) && $_REQUEST['mail_ok'] === '0' ) {
+                $search_params['mail_ok'] = (int)0;
+            }
+            if ( isset( $_REQUEST['member_ok'] ) && $_REQUEST['member_ok'] === '0' ) {
+                $search_params['member_ok'] = (int)0;
+            }
 
             if ( $wpUser['ID'] ) {
                 $search_data = array(
@@ -191,11 +211,19 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
 
             // build the $where part
             $where_parts = array();
-            if ( $search_params['mail_ok'] ) {
+            if ( isset( $search_params['member_ok'] ) && $search_params['member_ok'] !== false ) {
+                $member_ok = ( $search_params['member_ok'] === true ) ? 'true' : 'false';
+                $where_parts[] = "T.lead_id IN (
+                    SELECT id
+                      FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
+                     WHERE member_ok = $member_ok)";
+            }
+            if ( isset( $search_params['mail_ok'] ) && $search_params['mail_ok'] !== false ) {
+                $mail_ok = ( $search_params['mail_ok'] === true ) ? 'true' : 'false';
                 $where_parts[] = "T.lead_id IN (
                     SELECT id
                       FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "leads
-                     WHERE mail_ok = true)";
+                     WHERE mail_ok = $mail_ok)";
             }
             if ( $this->config['loggedInUser']['contactUser'] ) {
                 $where_parts[] = "T.lead_id IN (
@@ -249,9 +277,71 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
                 $where_parts[] = "T.date_submitted <= STR_TO_DATE('{$search_params['to_date']}', '%m/%d/%Y')";
             }
             if ( isset( $search ) && $search ) {
-                $where = implode( ' AND ', $where_parts );
+                if ( isset( $where_parts ) && !empty( $where_parts ) ) {
+                    $where .= ' AND ' . implode( ' AND ', $where_parts );
+                }
                 $order = "T.lname, T.fname";
-                $leads = $this->getList( $where, $order );
+
+                // Check if we're doing paging
+                if (isset($_REQUEST['pageSelect'])) {
+
+                    // If request is for Next
+                    if ($_REQUEST['pageSelect'][0] == 'N') {
+                        $newStart = $_REQUEST['nextStart'] - 0;
+
+                    // Otherwise it must be Previous
+                    } else {
+                        $newStart = $_REQUEST['prevStart'] - 0;
+                    }
+
+                    if ($newStart > 0) {
+                        $start = $newStart;
+                    }
+                }
+
+                // Get count of members listed
+                $leadCount = $this->getStats($where);
+
+                // If the number of members is less than a page, don't do paging
+                if ($leadCount <= $limit) {
+                    $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;
+                    }
+                }
+                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);
+
+                // If we have list entries - even if it's an empty list
+                $success = true;
+                $haveLeads = false;
+                if ($leads !== false) {
+
+                    $success = true;
+
+                    // If we have any entries
+                    if (count($leads) > 0) {
+                        $haveLeads = true;
+                    }
+                }
+
             } else {
                 $leads = false;
             }
@@ -384,6 +474,15 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             // Get the record for this Entry.
             $entry = $this->editEntry( $this->entryId );
 
+            // 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>';
+
             // Use the edit view file.
             $view = 'edit.html';
 
@@ -424,6 +523,45 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
             }
             // Compile template data
             $templateData = array(
+                'addingLead'        => $addingLead,
+                'entry'             => $entry,
+                'lead_interests'    => $lead_interests,
+            );
+            break;
+
+        case 'add':
+            if ( $this->config['loggedInUser']['contactUser'] ) {
+                break;
+            }
+            $entry = $this->newEntry();
+            $addingLead = true;
+
+            $lead_interests = array();
+
+            // Use the edit view file.
+            $view = 'edit.html';
+
+            // Setup the $grouped_interests array for the edit page.
+            if ( $groups ) {
+                foreach ( $groups as $group ) {
+                    $intData     = new GlmDataInterests( $this->wpdb, $this->config );
+                    $group_parts = explode( '_', $group['title'] );
+                    $group_parts = array_map( function($item){ return ucfirst( $item ); }, $group_parts );
+                    $group_title = implode( ' ', $group_parts );
+                    $grouped_interests[$group_title] = $intData->getList( "group_id = " . $group['id'], 'title' );
+                }
+                if ( $grouped_interests ) {
+                    foreach ( $grouped_interests as $group_name => $interests ) {
+                        foreach ( $interests as $key => $interest ) {
+                            $grouped_interests[$group_name][$key]['selected'] = 0;
+                        }
+                    }
+                }
+            }
+
+            // Compile template data
+            $templateData = array(
+                'addingLead'        => $addingLead,
                 'entry'             => $entry,
                 'lead_interests'    => $lead_interests,
             );
@@ -474,6 +612,16 @@ class GlmMembersAdmin_leads_index extends GlmDataLeadEntry
         // Common things to place into the $templateData array
         $templateData['sources']           = $sources;
         $templateData['grouped_interests'] = $grouped_interests;
+        $templateData['haveLeads']         = $haveLeads;
+        $templateData['leadCount']         = $leadCount;
+        $templateData['numbDisplayed']     = $numbDisplayed;
+        $templateData['lastDisplayed']     = $lastDisplayed;
+        $templateData['prevStart']         = $prevStart;
+        $templateData['nextStart']         = $nextStart;
+        $templateData['start']             = $start;
+        $templateData['limit']             = $limit;
+        $templateData['paging']            = $paging;
+
 
         // Return status, any suggested view, and any data to controller
         return array(
index 160db77..d0cdea7 100644 (file)
@@ -115,17 +115,16 @@ class GlmMembersAdmin_management_leads // extends GlmDataLeadsManagement
      */
     public function modelAction($actionData = false)
     {
-
-        $gravity_forms = GFAPI::get_forms();
-
-        $groupData = new GlmDataInterestGroups( $this->wpdb, $this->config );
-        $groups    = $groupData->getList();
+        // Grab data from the Sources table
+        $sources = $this->wpdb->get_results(
+            "SELECT *
+               FROM " . GLM_MEMBERS_LEADS_PLUGIN_DB_PREFIX . "sources",
+            ARRAY_A
+        );
 
         // Compile template data
         $templateData = array(
-            'formData'     => print_r( $gravity_forms, true ),
-            'gravityForms' => $gravity_forms,
-            'groups'       => $groups,
+            'sources' => $sources
         );
 
         // Return status, suggested view, and data to controller
index 353f9a4..88ad0aa 100644 (file)
@@ -4,9 +4,16 @@
 <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}" />
+    {/if}
     <input type="hidden" name="lead_id" value="{$entry.fieldData.lead_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>
+        </tr>
         <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}
@@ -62,7 +77,9 @@
                     <strong>{$group_name}</strong>
                     {foreach $group as $interest}
                     <div>
-                        <label><input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}</label>
+                        <label>
+                            <input type="checkbox" name="interests[{$interest.id}]" {if $interest.selected} checked{/if}>{$interest.title}
+                        </label>
                     </div>
                     {/foreach}
                 </div>
             </td>
         </tr>
         <tr>
-            <td colspan="2"><input class="button" type="submit" value="Update Lead"></td>
+            <td colspan="2">
+                <input class="button" type="submit" value="{if $addingLead}Add{else}Update{/if} Lead">
+            </td>
         </tr>
+        </tbody>
     </table>
 </form>
 {/if}
index 933b566..961eb30 100644 (file)
@@ -5,9 +5,14 @@
         padding: 5px;
     }
 </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}
 <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">
         <tr>
             <td style="text-align: right;"><label for="glm-form-source_id">Source</label></td>
         </tr>
         <tr>
             <td style="text-align: right;"><label>Mail Ok</label></td>
-            <td><label><input type="checkbox" id="glm-form-mail_ok" name="mail_ok" value="1" {if $search_params.mail_ok}checked{/if}> Yes</label></td>
+            <td>
+                    <select id="glm-form-mail_ok" name="mail_ok">
+                        <option value="">Choose</option>
+                        <option value="1" {if $search_params.mail_ok == '1'}selected{/if}>Yes</option>
+                        <option value="0" {if $search_params.mail_ok === 0}selected{/if}>No</option>
+                    </select>
+            </td>
         </tr>
+        {if $user_can_edit_leads}
+            <tr>
+                <td style="text-align: right;"><label>Member Ok</label></td>
+                <td>
+                    <select id="glm-form-member_ok" name="member_ok">
+                        <option value="">Choose</option>
+                        <option value="1" {if $search_params.member_ok == '1'}selected{/if}>Yes</option>
+                        <option value="0" {if $search_params.member_ok === 0}selected{/if}>No</option>
+                    </select>
+                </td>
+            </tr>
+        {/if}
         {foreach $grouped_interests as $group_name => $group}
             <tr>
                 <td style="text-align: right;"><strong>{$group_name}</strong></td>
             <td colspan="2"><input class="button" type="submit" value="Search"></td>
         </tr>
     </table>
-</form>
 
 {if $leads}
     <h4>Search Results:</h4>
-    <form action="{$thisUrl}?page={$thisPage}">
-        <a href="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=csvExport" class="button">Download as CSV File</a>
-    </form>
-    <table class="glm-admin-table">
-        <tr>
-            <th>Contact</th>
-            <th>Source</th>
-            <th>Company</th>
-            <th>Submitted</th>
-        </tr>
-        {foreach $leads as $lead}
-        <tr>
-            <td>
-                {if $user_can_edit_leads}<a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=edit&entry={$lead.id}">{/if}
-                    {$lead.fname} {$lead.lname}
-                {if $user_can_edit_leads}</a>{/if}
-            </td>
-            <td> {$lead.source_id.name} </td>
-            <td> {$lead.org} </td>
-            <td> {$lead.date_submitted} </td>
-        </tr>
-       {/foreach}
+    <a href="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=csvExport" class="button">Download as CSV File</a>
+
+    <br clear="all">
+    <p><b>Total found:</b> {$leadCount}&nbsp;&nbsp;</p>
+        {if $paging}
+            <input type="Submit" name="pageSelect" value="Previous {$limit} Leads" class="button button-secondary glm-button"{if !$prevStart} disabled{/if}>
+            <input type="Submit" name="pageSelect" value="Next {$limit} Leads" class="button button-secondary glm-button"{if !$nextStart} disabled{/if}>
+        {/if}
+    <table class="wp-list-table striped glm-admin-table">
+        <thead>
+            <tr>
+                <th>Contact</th>
+                <th>Source</th>
+                <th>Company</th>
+                <th>Submitted</th>
+            </tr>
+        </thead>
+        <tbody>
+        {if $haveLeads}
+            {assign var="i" value="0"}
+            {foreach $leads as $lead}
+                {if $i++ is odd by 1}<tr>{else}<tr class="alternate">{/if}
+                    <td>
+                        {if $user_can_edit_leads}
+                            <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=edit&entry={$lead.id}">
+                        {/if}
+                            {$lead.fname} {$lead.lname}
+                        {if $user_can_edit_leads}</a>{/if}
+                    </td>
+                    <td> {$lead.source_id.name} </td>
+                    <td> {$lead.org} </td>
+                    <td> {$lead.date_submitted} </td>
+                </tr>
+           {/foreach}
+       {/if}
+        <tbody>
     </table>
+        {if $paging}
+            <input type="Submit" name="pageSelect" value="Previous {$limit} {$terms.term_member_plur_cap}" class="button button-secondary glm-button"{if !$prevStart} disabled{/if}>
+            <input type="Submit" name="pageSelect" value="Next {$limit} {$terms.term_member_plur_cap}" class="button button-secondary glm-button"{if !$nextStart} disabled{/if}>
+        {/if}
 {/if}
 
+</form>
+
 <script>
 jQuery(document).ready(function($){
     $('#glm-form-from_date').datepicker();
index 0915aa1..f322ffa 100644 (file)
@@ -4,27 +4,15 @@
 
 <table class="glm-admin-table glm-settings-table">
     <tr>
-        <th>
-            Form Name
-        </th>
-        <th>
-            Enabled
-        </th>
-        <th>
-            Groups
-        </th>
+        <th> Form Id </th>
+        <th> Source Name </th>
+        <th> Enabled </th>
     </tr>
-    {foreach $gravityForms as $form}
+    {foreach $sources as $source}
     <tr>
-        <td>
-            {$form.title}
-        </td>
-        <td>
-            Yes
-        </td>
-        <td>
-
-        </td>
+        <td> {$source.form_id} </td>
+        <td> {$source.title} </td>
+        <td> {$source.enabled} </td>
     </tr>
     {/foreach}
 </table>