Added code to members list to support multiple category selection by the user in...
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 10 Apr 2017 18:21:59 +0000 (14:21 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 10 Apr 2017 18:28:04 +0000 (14:28 -0400)
See front/members/list.html for a commented out example of how to enable that.

models/front/members/list.php
views/front/members/list.html

index 778d075..56d30d8 100644 (file)
@@ -162,6 +162,7 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
         $catSearchSelected    = false;
         $regionSearchSelected = false;
         $mapItems             = false;
+        $multiSelectCats      = false;
 
         // Paging Parameters
         $paging        = $this->config['settings']['list_pagination']; // Now this is in management
@@ -171,6 +172,13 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
         $nextStart     = false;
         $start         = 1;
         $limit         = $this->config['settings']['list_pagination_count']; // Now this is in management
+
+        // Check if a multi-select for categories is being submitted. If so, assign that to the category selection
+        if (isset($_REQUEST['categorySearchMultiple']) && is_array($_REQUEST['categorySearchMultiple'])) {
+            $actionData['request']['category'] = implode(',', $_REQUEST['categorySearchMultiple']);
+            $multiSelectCats = true;
+        }
+
         // Check for URL parameter override of shortcode parameters
         if (isset($_REQUEST['categorySearch'])) {
             $actionData['request']['category-search'] = $_REQUEST['categorySearch'];
@@ -441,36 +449,53 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
 
         }
 
-        // If there's categories specified above ($cats array)
-        if (count($cats) > 0) {
+        // If there's categories specified above ($cats array) - and we have category data
+        if (count($cats) > 0 && isset( $categoryData ) && is_array( $categoryData )) {
 
-            /*  * Restrict available categories for selection to those in shortcode or children
-             * Note that the array is already ordered by parent then children then next parent
-             * so $parentSelected
-             */
-            $parentSelected = false;
-            if ( isset( $categoryData ) && is_array( $categoryData ) ) {
-            reset($categoryData);
-            while (list($k, $v) = each($categoryData)) {
+            // Check if this is a submitted multi-select for categories, set selected rather than remove other categories
+            if ($multiSelectCats) {
 
-                // If this is a parent, then clear parent selected
-                if ($v['parent_id'] != $parentSelected) {
-                    $parentSelected = false;
+                // Add ['default'] value to all categories
+                reset($categoryData);
+                while (list($k, $v) = each($categoryData)) {
+                    $categoryData[$k]['default'] = false;
                 }
 
-                // If category or parent is selected
-                if ($parentSelected == false && !in_array($k, $cats)) {
+                // Now set that for the categories in $cats.
+                reset($cats);
+                foreach ($cats as $c) {
+                    $categoryData[$c]['default'] = true;
+                }
 
-                    // Drop this category from the list
-                    unset($categoryData[$k]);
+            } else {
+
+                /*  * Restrict available categories for selection to those in shortcode or children
+                 * Note that the array is already ordered by parent then children then next parent
+                 * so $parentSelected
+                 */
+                $parentSelected = false;
+                reset($categoryData);
+                while (list($k, $v) = each($categoryData)) {
+
+                    // If this is a parent, then clear parent selected
+                    if ($v['parent_id'] != $parentSelected) {
+                        $parentSelected = false;
+                    }
+
+                    // If category or parent is selected
+                    if ($parentSelected == false && !in_array($k, $cats)) {
+
+                        // Drop this category from the list
+                        unset($categoryData[$k]);
+
+                    // Otherwise, if this is a top-level category that's not dropped, save as currently selected parent
+                    } elseif (!$v['parent_id']) {
+                        $parentSelected = $k;
+                    }
 
-                // Otherwise, if this is a top-level category that's not dropped, save as currently selected parent
-                } elseif (!$v['parent_id']) {
-                    $parentSelected = $k;
                 }
 
-            }
-            }
+            } // Not multiselect cats
 
         }
 
@@ -478,7 +503,7 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
         $catSelectedForQuery = $catSelected;
 
         // If we're doing category search selection Get category filter data
-        if ($this->config['settings']['list_show_search_category']) {
+        if ($this->config['settings']['list_show_search_category'] && !$multiSelectCats) {
 
             // Add default flag as false to all entries
             if ( isset( $categoryData ) && is_array( $categoryData ) ) {
index 7a5a6be..c493ee8 100644 (file)
                         {if $settings.list_show_search_category}
                             <div>
                                 By Category:
+<!-- To enable multi-select, use this code for the category search instead of the code below                                
+                                <select name="categorySearchMultiple[]" id="categorySelect" multiple>
+                                {if !$singleCategory}
+                                    <option value="">(all)</option>
+                                {/if}
+                                {foreach from=$categories item=v}
+                                    <option value="{$v.id}"{if $v.default} selected="selected"{/if}>
+                                        {if $v.parent}&nbsp;&nbsp;&nbsp;&nbsp;{/if}{$v.name}
+                                    </option>
+                                {/foreach}
+                                </select>
+-->                                 
                                 <select name="categorySearch" id="categorySelect">
                                 {if !$singleCategory}
                                     <option value="">(all)</option>
@@ -34,6 +46,7 @@
                                     </option>
                                 {/foreach}
                                 </select>
+                                
                             </div>
                         {/if}
                         {if $settings.list_show_search_amenities}