Fixed alpha list for contact list display
authorLaury GvR <laury@gaslightmedia.com>
Fri, 13 Jan 2017 22:07:27 +0000 (17:07 -0500)
committerLaury GvR <laury@gaslightmedia.com>
Fri, 13 Jan 2017 22:07:27 +0000 (17:07 -0500)
Alpha list was producing errors on the contact list due to wrong
code in the model and shortcodes.php, thinking alpha was undefined.

classes/data/dataContacts.php
models/front/contacts/list.php
setup/shortcodes.php
views/front/contacts/list.html

index 6d3b63d..fcac99f 100644 (file)
@@ -479,6 +479,43 @@ class GlmDataContacts extends GlmDataAbstract
         return $r;
     }
 
+    
+
+    /*
+     * Get Alpha list of first characters in member name
+     * for those members that have active info.
+     *
+     * @param string $where Where clause
+     *     Note the table refernces M and I.
+     * @param string $selected Optional selected alpha character
+     *
+     * @return object Class object
+     *
+     */
+    public function getAlphaList($where = '', $selected = '')
+    {
+
+        
+        $sql = "
+           SELECT DISTINCT LEFT(T.lname, 1) AS alpha
+             FROM ".GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX. "contacts T
+            WHERE T.active = true
+                  $where
+            ORDER BY alpha
+        ;";
+        
+        $alphaData = $this->wpdb->get_results($sql, ARRAY_A);
+        
+        // Set selected
+        foreach ($alphaData as $k=>$v) {
+            $alphaData[$k]['default'] = ($v['alpha'] == $selected);
+        }
+
+        return $alphaData;
+
+    }
+    
+    
     /*
      * Get the contact's "Permissions" (contact_role)
      *
index 7fc8898..85d6a16 100644 (file)
@@ -14,9 +14,6 @@
  */
 
 // Translation table for [glm-contacts-list] "show" options to configuration parameters
-$GLOBALS['showOpts'] = array(
-
-);
 
 // Load Contacts data abstract
 require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH.'/data/dataContacts.php';
@@ -104,43 +101,34 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
     public function modelAction ($actionData = false)
     {
 
-        $view = 'index.html';
-        $option = 'list';
-        $fromMemberMenu = false;
-        $loggedInMember = false;
-        $refType = false;
-        $refTypeName = false;
-        $haveMember = false;
+        $view           = 'index.html';
+        $option         = 'list';
+        $haveMember     = false;
         $this->memberID = false;
-        $memberData = false;
-        $memberName = false;
-        $membersList = false;
-        $contactsList = false;
-        $haveContacts = false;
-        $contactID = false;
-        $contactInfo = false;
-        $newContactEmailExists = false;
-        $newContactUsernameExists = false;
-        $misMatchedWpUsers = false;
-        $newContactCreated = false;
-        $usingExistingWPContact = false;
-        $usernameChangedToWP = false;
-        $contactUpdated = false;
+        $memberData     = false;
+        $memberName     = false;
+        $membersList    = false;
+        $contactsList   = false;
+        $haveContacts   = false;
+        $contactID      = false;
+        $contactInfo    = false;
         $filterArchived = false;
-        $filterText = false;
-        $countySearch = false;
-        $countyList = array();
-        $haveCounties = false;
-        $haveFilter = false;
-        $userDeleted = false;
-        $wpUserDeleted = false;
+        $filterText     = false;
+        $countySearch   = false;
+        $countyList     = array();
+        $haveCounties  = false;
+        $haveFilter     = false;
         $contactMembers = array();
-        $order = false;
+        $order          = false;
+        
+        $textSearch     = '';
+        $blankStart     = false;
+        $membersFound   = false;
 
-        $numbContacts = false;
+        // Paging Parameters
+        $paging = true;
         $numbDisplayed = false;
         $lastDisplayed = false;
-        $paging = true;
         $prevStart = false;
         $nextStart = false;
         $start = 1;
@@ -157,6 +145,18 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
             'delete',
             'list'
         );
+        
+        
+        if (isset($_REQUEST['textSearch'])) {
+            $actionData['request']['text-search'] = $_REQUEST['textSearch'];
+        }
+        if (isset($_REQUEST['alpha'])) {
+            $actionData['request']['alpha'] = $_REQUEST['alpha'];
+        }
+        // If user clicked a page request then we need to check the savedAlpha value
+        if (isset($_REQUEST['savedAlpha']) && isset($_REQUEST['pageSelect'])) {
+            $actionData['request']['alpha'] = $_REQUEST['savedAlpha'];
+        }
 
         // If this is a logged in member user, then show their contacts only
         if (isset($this->config['loggedInUser']['contactUser'])) {
@@ -177,26 +177,7 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
             }
 
         }
-
-        // If we're coming from the Member Contacts Menu/Tab
-        if (defined('GLM_MEMBERS_CONTACTS_MEMBER_MENU')) {
-
-            $fromMemberMenu = true;
-
-            // Check if we've received member ID
-            if (isset($_REQUEST['member'])) {
-
-                // Clean up the member ID and store it in wordpress option
-                $this->memberID = $_REQUEST['member']-0;
-                update_option('glmMembersDatabaseMemberID', $this->memberID);
-
-            // Otherwise check if a member is stored in wordpress option
-            } else {
-                $this->memberID = get_option('glmMembersDatabaseMemberID', false);
-            }
-
-        }
-
+       
         // Load members data class
         require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMembers.php';
         $this->Members = new GlmDataMembers($this->wpdb, $this->config);
@@ -216,7 +197,7 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
         $option = 'list';
 
         $where = ' TRUE ';
-
+        
         // Only list member contacts for the selected member
         if ($haveMember) {
             $where .= " AND ref_type = ".$this->config['ref_type_numb']['Member'].' AND ref_dest = '.$this->memberID;
@@ -247,22 +228,47 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
         
         // Get the total number of contacts listed
         $numbContacts = $this->getStats($where);
+        
         // If the number of events is less than a page, don't do paging
         if ($numbContacts <= $limit) {
             $paging = false;
         }
         
         // Setting $paging to false until we figure it out for contacts
-        $paging = false;
+        //$paging = false;
 
         // Get full list of names matching this where clause for search box
         $namesList = $this->getIdName($where);
 
+
+        $contactsList = $this->getList($where, $order, true, 'id',$start, $limit);
+        
+        // If doing alpha list
+        $alphaList = false;
+        $alphaWhere = '';
+        if ($this->config['settings']['list_show_search_alpha']) {
+
+            $alphaSelected = false;
+
+            // Check for alpha selected
+            if ($actionData['request']['alpha'] && strlen($actionData['request']['alpha']) == 1) {
+                $alphaSelected = strtoupper($actionData['request']['alpha']);
+                $alphaWhere .= " AND T.lname LIKE '$alphaSelected%'";
+            }
+
+            // Get full list for all other filters, but not filtered by alpha (that would be silly)
+            $alphaList = $this->getAlphaList(' AND '.$where, $alphaSelected);
+
+        }        
+        
+        $order = "lname, fname";
+        // Get list of contacts
+                
         // Check if we're doing paging
         if (isset($_REQUEST['pageSelect'])) {
 
             // If request is for Next
-            if ($_REQUEST['pageSelect'][0] == 'N') {
+            if ($_REQUEST['pageSelect'] == 'Next') {
                 $newStart = $_REQUEST['nextStart'] - 0;
 
             // Otherwise it must be Previous
@@ -274,10 +280,43 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
                 $start = $newStart;
             }
         }
-        $order = "lname, fname";
-        // Get list of contacts
-        $contactsList = $this->getList($where, $order);
+        
+        $list = $contactsList['list'];
+
+        // If we're not paging, then force $start and $limit to false to data abstract returns everything.
+        $resultParam = 'listResult';
+        if (!$paging) {
+            $start = false;
+            $limit = false;
+            $resultParam = 'list';
+        }
+        
+        // Get stats for the current selection
+        $contactsFound = $this->getStats(str_replace('T.', '', $where));
+
+        // Get stats for number of members found matching current selection criteria (includes alpha selection)
+        $filteredContactsFound = $this->getStats(str_replace('T.', '', $where.$alphaWhere));
+
+        // Get member list sorted as specified
+        switch ($this->config['settings']['list_order_list']) {
+
+            // Pseudo-Random list order
+            case $this->config['sort_order_numb']['Pseudo-Random']:
+                ${$resultParam} = $this->getList($where.$alphaWhere, 'pseudo-random', true, 'id', $start, $limit);
+                break;
+
+            // Default is alpha-numeric list order
+            default:
+            case $this->config['sort_order_numb']['Alpha-Numeric']:
+                ${$resultParam} = $this->getList($where.$alphaWhere, $order, true, 'id', $start, $limit);
+                break;
+            
+        }
 
+        if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
+            glmMembersFront::addNotice($list, 'DataBlock', 'Member Data');
+        }
+        
         if ($contactsList != false) {
             if ($paging) {
                 //Get paging results
@@ -306,16 +345,6 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
         if ($countyList[0]) {
             $haveCounties = true;
         }
-//        
-//        
-//
-//        $r = array(
-//            'contactRole' => $contactRole,
-//            'wpRole' => $this->config['contact_role_wordpress'][$contactRole]
-//        );
-//
-//        return $r;
-        
         
         // If the option is "edit" don't let lower-level users assign privileges above the user's pay grade
         if (($option == 'edit' || $option == 'create') && $this->config['loggedInUser']['contactUser']) {
@@ -326,45 +355,34 @@ class GlmMembersFront_contacts_list extends GlmDataContacts
             }
 
         }
-        
-        
 
         // Compile template data
         $templateData = array(
             'option' => $option,
-            'loggedInMember' => $loggedInMember,
-            'fromMemberMenu' => $fromMemberMenu,
             'haveMember' => $haveMember,
             'memberID' => $this->memberID,
             'memberData' => $memberData,
             'memberName' => $memberName,
             'membersList' => $membersList,
-            'refType' => $refType,
-            'refTypeName' => $refTypeName,
             'haveContacts' => $haveContacts,
-            'contactsList' => $contactsList,
+            'contactsList' => $list,
             'numbContacts' => $numbContacts,
             'contactID' => $contactID,
             'contactInfo' => $contactInfo,
-            'newContactEmailExists' => $newContactEmailExists,
-            'newContactUsernameExists' => $newContactUsernameExists,
-            'misMatchedWpUsers' => $misMatchedWpUsers,
-            'usernameChangedToWP' => $usernameChangedToWP,
-            'newContactCreated' => $newContactCreated,
-            'usingExistingWPContact' => $usingExistingWPContact,
-            'contactUpdated' => $contactUpdated,
             'filterArchived' => $filterArchived,
             'filterText' => $filterText,
             'countyList' => $countyList,
             'haveCounties' => $haveCounties,
             'countySearch' => $countySearch,
             'haveFilter' => $haveFilter,
-            'userDeleted' => $userDeleted,
-            'wpUserDeleted' => $wpUserDeleted,
             'contactMembers' => $contactMembers,
-
+            'alphaList' => $alphaList,
+            'alphaSelected' => $alphaSelected,
+            'contactsFound' => $contactsFound,
+            'filteredContactsFound' => $filteredContactsFound,
             'numbDisplayed' => $numbDisplayed,
             'lastDisplayed' => $lastDisplayed,
+            'textSearch' => $textSearch,
             'paging' => $paging,
             'prevStart' => $prevStart,
             'nextStart' => $nextStart,
index 7fbfc24..dbb41dd 100644 (file)
@@ -104,6 +104,7 @@ $glmMembersContactsShortcodes = array(
             'member'   => false,
             'template' => false,
             'limit'    => null,
+            'alpha'    => false,
         )
     ),
 );
index cd38cda..029c5b1 100644 (file)
@@ -46,8 +46,27 @@ and open the template in the editor.
         {if $paging}
             <input type="Submit" name="pageSelect" value="Previous {$limit} Contacts" class="button button-secondary glm-button"{if !$prevStart} disabled{/if}>
             <input type="Submit" name="pageSelect" value="Next {$limit} Contacts" class="button button-secondary glm-button"{if !$nextStart} disabled{/if}>
-        {/if}    
+        {/if}  
+        
+                    {if $settings.list_show_search_alpha}
+                        <div class="glm-alpha-links">
+                            <a href="{$thisUrl}?glm_action=list&textSearch={$textSearch}&alpha=" class="glm-alpha-link{if !$alphaSelected} glm-alpha-link-selected{/if}">All</a>
+                      {foreach $alphaList as $a}
+                            <a href="{$thisUrl}?glm_action=list&textSearch={$textSearch}&alpha={$a.alpha}" class="glm-alpha-link{if $a.default} glm-alpha-link-selected{/if}">{$a.alpha}</a>
+                      {/foreach}
+                       </div>
+                    {/if}
+                    
 
+                    {if $paging}
+                    
+                        <br>
+                        <a href="{$thisUrl}?glm_action=list&textSearch={$textSearch}&pageSelect=Previous&prevStart={$prevStart}&nextStart={$nextStart}&limit={$limit}&alpha={$alphaSelected}" class="glm-alpha-link" {if !$prevStart} style="pointer-events: none; opacity: 0.5;"{/if}>Previous page</a>
+                        <a href="{$thisUrl}?glm_action=list&textSearch={$textSearch}&pageSelect=Next&prevStart={$prevStart}&nextStart={$nextStart}&limit={$limit}&alpha={$alphaSelected}" class="glm-alpha-link" {if !$nextStart} style="pointer-events: none; opacity: 0.5;"{/if}>Next page</a>
+                        <br>
+                        showing {$start} through {$lastDisplayed} of {$filteredContactsFound}
+                    {/if}
+        
         {if $haveContacts}
             {foreach $contactsList as $contact}
             <div class="glm-member-list-container glm-member-container small-12 columns">
@@ -94,70 +113,7 @@ and open the template in the editor.
             <input type="Submit" name="pageSelect" value="Next {$limit} Contacts" class="button button-secondary glm-button"{if !$nextStart} disabled{/if}>
         {/if}
     </form>
-     
-    <script type="text/javascript">
-        jQuery(document).ready(function($) {
-            
-/*            
-            // Filter triggers
-            $(".listFilter" ).change( function() {
-                
-                var filter = '';
-                
-                // Check for archived filter
-                if ($("#filterArchived").attr('checked')) {
-                    filter += '&filterArchived=true';
-                }
-                
-                // Check for text filter
-                var filterText = $("#filterText").val();
-                if (filterText != '') {
-                    filter += '&filterText=' + encodeURIComponent(filterText).replace(/%20/g,'+');
-                }
-                
-                window.location.href = "{$thisUrl}?page={$thisPage}&glm_action=index" + filter;
-                
-                return false;
-            });
-*/
-
-            /*
-             * Do autocomplete search for member
-             * label: What will be searched
-             * value: What will be displayed when selected
-             * id: Member id added so we can go to the member while showing what was selected
-             * Also note that autocomplete does not properly render HTML codes, so we
-             * "unescape" them for HTML in Smarty.
-             */
-
-             var availableTags = [
-    {foreach $namesList as $e}
-                { label: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"} {$e.ref_dest_name|replace:'"':"'"} ( username: {$e.username})", value: "{$e.fname|replace:'"':"'"} {$e.lname|replace:'"':"'"}, {$e.ref_dest_name|replace:'"':"'"}, {$e.username}", id: '{$e.id}', member: '{$e.ref_dest}' },
-    {/foreach}
-             ];
             
-             $( "#glmContactsSearch" ).autocomplete({
-                 source: availableTags,
-                 html: true,
-                 position: { my : "right top", at: "right bottom" },
-                 select: function( event, ui ) {
-                     var contactID = ui.item.id;
-                     var memberID = ui.item.member;
-                     window.location.replace("{$adminUrl}?page=glm-members-admin-menu-member&glm_action=contacts&option=edit&member=" + memberID + "&contact=" + contactID );
-                 },
-                 response: function(event, ui) {
-                     if (!ui.content.length) {
-                         var noResult = { value:"",label:"No results found" };
-                         ui.content.push(noResult);
-                     }
-                 }
-             });
-
-        });
-    </script>
-            
-
-
 {include file='front/footer.html'}
 
     </body>