Completed Category selection for search picklist based on shortcode categories, added...
authorChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Oct 2015 14:26:23 +0000 (10:26 -0400)
committerChuck Scott <cscott@gaslightmedia.com>
Mon, 19 Oct 2015 14:26:23 +0000 (10:26 -0400)
classes/data/dataCategories.php
controllers/front.php
models/front/members/list.php
views/front/members/list.html

index 599f72f..2fe5422 100644 (file)
@@ -124,7 +124,7 @@ class GlmDataCategories extends GlmDataAbstract
                         'use' => 'a'
                 ),
 
-                // Parent
+                // Parent - for adding, deleting and editing, has selection tabels
                 'parent' => array(
                         'field' => 'parent',
                         'type' => 'pointer',
@@ -137,6 +137,14 @@ class GlmDataCategories extends GlmDataAbstract
                         'use' => 'a'
                 ),
 
+                // Parent ID
+                'parent_id' => array(
+                        'field' => 'parent',
+                        'as' => 'parent_id',
+                        'type' => 'integer',
+                        'use' => 'a'
+                ),
+
                 // Description
                 'descr' => array(
                         'field' => 'descr',
@@ -295,15 +303,23 @@ class GlmDataCategories extends GlmDataAbstract
      * Get categories list sorted by parent/child then alpha
      *
      * @return array Array of categories
+     * @param boolean $forEdit If true include list of options for each parent field
      *
      * @access public
      */
 
-    public function getListSortedParentChild() {
+    public function getListSortedParentChild($forEdit = true) {
 
         $categories = $this->getList();
         $categoriesSorted = $this->sortParentChild($categories);
 
+        if (!$forEdit) {
+            while (list($k, $v) = each($categoriesSorted)) {
+                $categoriesSorted[$k]['parent_id'] = $v['parent']['value'];
+                $categoriesSorted[$k]['parent'] = $v['parent']['name'];
+            }
+        }
+
         return $categoriesSorted;
 
     }
@@ -383,7 +399,7 @@ class GlmDataCategories extends GlmDataAbstract
 
         });
 
-            return $array;
+        return $array;
     }
 
 }
index ff8a255..68cc9a7 100644 (file)
@@ -240,6 +240,7 @@ class glmMembersFront extends GlmPluginSupport
                     array(
                         'map' => true,
                         'category' => false,
+                        'category-name' => false,
                         'alpha' => false,
                         'search' => false,
                         'amenities' => false,
@@ -478,7 +479,7 @@ class glmMembersFront extends GlmPluginSupport
         // If view debug has been requested
         if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
 
-            glmMembersFront::addNotice("<b>Template File:</b><br> $view", 'Process');
+            glmMembersFront::addNotice("<b>Template File:</b> $view", 'Process');
 
             $x = $smarty->template->getTemplateVars();
             $templateVars = '<pre>' . print_r($x, 1) . '</pre>';
index 92ec8e1..277b847 100644 (file)
@@ -152,10 +152,9 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
         $haveFilter = false;
         $textSearch = '';
 
-        // Check for "show" shortcode parameter
+        // Check for "show" shortcode parameter (what elements to show on the page)
         $settings = array();
         $showList = $actionData['request']['show'];
-
         if ($showList) {
 
             // Separate options and see if we have any
@@ -232,30 +231,122 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             $whereSep = ' AND ';
         }
 
-        // Get category filter data
-        $categoryData = false;
+        // Get category data for search pick list and shortcode selection
+        require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataCategories.php');
+        $Categories = new GlmDataCategories($this->wpdb, $this->config);
+        $categoryData = $Categories->getListSortedParentChild(false);
+
+        // Get any numeric categories selected in the submitted shortcode
         $catSelected = '';
-        if ($this->config['settings']['list_show_search_category']) {
+        $cats = array();
+        if (isset($actionData['request']['category'])) {
+
+            $catsRequested = explode(',', $actionData['request']['category']);
+
+            // Check for numeric IDs only
+            while (list($k, $v) = each($catsRequested)) {
+
+                // If it's just numeric
+                if (preg_match('/^[0-9]*$/', trim($v))) {
+
+                    // Clean up the category number and add to cats array
+                    $cats[] = ($v-0);
+
+                }
+
+            }
+
+        }
 
-            // Get category data for search pick list
-            require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataCategories.php');
-            $Categories = new GlmDataCategories($this->wpdb, $this->config);
-            $categoryData = $Categories->getListSortedParentChild();
+        // Get any text categories selected in the submitted shortcode
+        if (isset($actionData['request']['category-name'])) {
+
+            // Break up shortcode categories into an array to make searching easy
+            $catsRequested = explode('|', $actionData['request']['category-name']);
+
+            // Check for numeric or text selection of categories
+            while (list($k, $v) = each($catsRequested)) {
+
+                $vStripped = html_entity_decode(stripslashes(trim($v)));
+
+                reset($categoryData);
+                foreach ($categoryData as $c) {
+
+                    $x = html_entity_decode(stripslashes(trim($c['name'])));
+
+                    // If this entry matches - apply html entity decode in case string is encoded
+                    if ($x == $vStripped) {
+
+                        // Update the $cats array with the ID rather than the name and mark as found
+                        $cats[] = $c['id'];
+                    }
+                }
+
+            }
+
+        }
+
+        // If there's categories specified with the shortcode
+        if (count($cats) > 0) {
+
+            /*
+             * 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;
+                }
+
+            }
+
+        }
+
+        // If we're doing category search selection Get category filter data
+        if ($this->config['settings']['list_show_search_category']) {
 
             // Add default flag as false to all entries
             foreach ($categoryData as $k=>$v) {
                 $categoryData[$k]['default'] = false;
             }
+
+            // Check if a category has been submitted
+            if (isset($_REQUEST['categorySearch'])) {
+                $c = $_REQUEST['categorySearch'] - 0;
+
+                // Since we have one category selected by the user, make that default
+                if ($c && isset($categoryData[$c])) {
+                    $categoryData[$c]['default'] = true;
+                    $catSelected = $c;
+                }
+
+            }
+
         }
-        // Check if a category has been submitted
-        if (isset($_REQUEST['categorySearch'])) {
-            $catSelected = $_REQUEST['categorySearch'] - 0;
-        } elseif ($actionData['request']['category']) {
-            $catSelected = $actionData['request']['category'];
+
+        if (GLM_MEMBERS_PLUGIN_FRONT_DEBUG) {
+            glmMembersFront::addNotice("<b>Categories Requested:</b> $catSelected", 'Process');
+
         }
 
-        // If we have a category ID
-        if ($catSelected > 0) {
+        // If we have a category ID or IDs
+        if ($catSelected != '') {
 
             $where .= $whereSep." T.id in (
                     SELECT DISTINCT(member_info)
@@ -266,13 +357,8 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             )";
             $whereSep = ' AND ';
 
-            // Set default to true for the selected category
-            $c = explode(',', $catSelected);
-            $categoryData[$c[0]]['default'] = true;
-
         }
 
-
         // Get amenity filter data
         $amenityData = false;
         $amenSelected = '';
@@ -374,7 +460,7 @@ class GlmMembersFront_members_list extends GlmDataMemberInfo
             'alphaSelected' => $alphaSelected
         );
 
-        // Return status, suggested view, and data to controller
+        // Return status, suggested view, and data to controller - also return any modified settings
         return array(
             'status' => $success,
             'menuItemRedirect' => false,
index 7ac353e..709f3f1 100644 (file)
@@ -24,7 +24,7 @@
                         <div class="small-12 medium-5 columns">
                             {$terms.term_member_cap} Category: 
                             <select name="categorySearch" id="categorySelect">
-                                <option value=""></option>
+                                <option value="">(all)</option>
                     {foreach from=$categories item=v}
                                 <option value="{$v.id}" data-parent="{$v.parent.name}"{if $v.default} selected="selected"{/if}>
                                     {if $v.parent.value}&nbsp;&nbsp;&nbsp;&nbsp;{/if}{$v.name}