From 8794d778169a6aa0813fd97502fc4c2a0c912e9d Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Mon, 19 Oct 2015 10:26:23 -0400 Subject: [PATCH] Completed Category selection for search picklist based on shortcode categories, added text category selection to shortcode. --- classes/data/dataCategories.php | 22 +++++- controllers/front.php | 3 +- models/front/members/list.php | 130 ++++++++++++++++++++++++++------ views/front/members/list.html | 2 +- 4 files changed, 130 insertions(+), 27 deletions(-) diff --git a/classes/data/dataCategories.php b/classes/data/dataCategories.php index 599f72ff..2fe54225 100644 --- a/classes/data/dataCategories.php +++ b/classes/data/dataCategories.php @@ -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; } } diff --git a/controllers/front.php b/controllers/front.php index ff8a2558..68cc9a7f 100644 --- a/controllers/front.php +++ b/controllers/front.php @@ -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("Template File:
$view", 'Process'); + glmMembersFront::addNotice("Template File: $view", 'Process'); $x = $smarty->template->getTemplateVars(); $templateVars = '
' . print_r($x, 1) . '
'; diff --git a/models/front/members/list.php b/models/front/members/list.php index 92ec8e1c..277b847b 100644 --- a/models/front/members/list.php +++ b/models/front/members/list.php @@ -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("Categories Requested: $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, diff --git a/views/front/members/list.html b/views/front/members/list.html index 7ac353e1..709f3f1d 100644 --- a/views/front/members/list.html +++ b/views/front/members/list.html @@ -24,7 +24,7 @@
{$terms.term_member_cap} Category: