diff --git a/ps_facetedsearch.php b/ps_facetedsearch.php index 669a9c43c..df84917b1 100644 --- a/ps_facetedsearch.php +++ b/ps_facetedsearch.php @@ -543,15 +543,9 @@ public function getContent() } elseif (!Tools::getValue('categoryBox')) { $message = $this->displayError($this->trans('You must select at least one category.', [], 'Modules.Facetedsearch.Admin')); } else { - if (Tools::getValue('id_layered_filter')) { - $this->getDatabase()->execute( - 'DELETE FROM ' . _DB_PREFIX_ . 'layered_filter - WHERE id_layered_filter = ' . (int) Tools::getValue('id_layered_filter') - ); - $this->buildLayeredCategories(); - } - - if (Tools::getValue('scope') == 1) { + // Get or generate id + $idLayeredFilter = (int) Tools::getValue('id_layered_filter'); + if (Tools::getValue('scope')) { $this->getDatabase()->execute('TRUNCATE TABLE ' . _DB_PREFIX_ . 'layered_filter'); $categories = $this->getDatabase()->executeS( 'SELECT id_category FROM ' . _DB_PREFIX_ . 'category' @@ -562,81 +556,69 @@ public function getContent() } } - $idLayeredFilter = (int) Tools::getValue('id_layered_filter'); - - if (!$idLayeredFilter) { - $idLayeredFilter = (int) $this->getDatabase()->Insert_ID(); - } - - $shopList = []; - + // Associate Shops if (isset($_POST['checkBoxShopAsso_layered_filter'])) { + $shopList = []; foreach ($_POST['checkBoxShopAsso_layered_filter'] as $idShop => $row) { - $assos[] = ['id_object' => (int) $idLayeredFilter, 'id_shop' => (int) $idShop]; + $assos[] = ['id_shop' => (int) $idShop]; $shopList[] = (int) $idShop; } } else { - $shopList = [$this->getContext()->shop->id]; + $shopList = [(int) $this->getContext()->shop->id]; } - $this->getDatabase()->execute( - 'DELETE FROM ' . _DB_PREFIX_ . 'layered_filter_shop WHERE `id_layered_filter` = ' . (int) $idLayeredFilter - ); - - if (count($_POST['categoryBox'])) { + if (!empty($_POST['categoryBox']) && is_array($_POST['categoryBox'])) { /* Clean categoryBox before use */ - if (isset($_POST['categoryBox']) && is_array($_POST['categoryBox'])) { - foreach ($_POST['categoryBox'] as &$categoryBoxTmp) { - $categoryBoxTmp = (int) $categoryBoxTmp; - } - } + $_POST['categoryBox'] = array_map('intval', $_POST['categoryBox']); + $filterValues = [ + 'shop_list' => $shopList, + ]; - $filterValues = []; - - foreach ($_POST['categoryBox'] as $idc) { - $filterValues['categories'][] = (int) $idc; + foreach ($_POST['categoryBox'] as $idCategoryLayered) { + $filterValues['categories'][] = $idCategoryLayered; } - $filterValues['shop_list'] = $shopList; - $values = false; - - foreach ($_POST['categoryBox'] as $idCategoryLayered) { - foreach ($_POST as $key => $value) { - if (substr($key, 0, 17) == 'layered_selection' && $value == 'on') { - $values = true; - $type = 0; - $limit = 0; - - if (Tools::getValue($key . '_filter_type')) { - $type = Tools::getValue($key . '_filter_type'); - } - if (Tools::getValue($key . '_filter_show_limit')) { - $limit = Tools::getValue($key . '_filter_show_limit'); - } - - $filterValues[$key] = [ - 'filter_type' => (int) $type, - 'filter_show_limit' => (int) $limit, - ]; - } + foreach ($_POST as $key => $value) { + if (!preg_match('~^(?Playered_selection_.*)(? (int) Tools::getValue($matches['key'] . '_filter_type', 0), + 'filter_show_limit' => (int) Tools::getValue($matches['key'] . '_filter_show_limit', 0), + ]; } - $valuesToInsert = [ + $values = [ 'name' => pSQL(Tools::getValue('layered_tpl_name')), 'filters' => pSQL(serialize($filterValues)), 'n_categories' => (int) count($filterValues['categories']), - 'date_add' => date('Y-m-d H:i:s'), ]; - - if (isset($_POST['id_layered_filter']) && $_POST['id_layered_filter']) { - $valuesToInsert['id_layered_filter'] = (int) Tools::getValue('id_layered_filter'); + ]; + + if (!$idLayeredFilter) { + $values['date_add'] = date('Y-m-d H:i:s'); + $sql = 'INSERT INTO ' . _DB_PREFIX_ . 'layered_filter ' . + '(name, filters, n_categories, date_add, id_layered_filter) ' . + 'VALUES (' . + '"' . pSQL($values['name']) . '", ' . + '"' . $values['filters'] . '", ' . + '' . (int) $values['n_categories'] . ', ' . + '"' . pSQL($values['date_add']) . '", ' . + '' . $idLayeredFilter . ')'; + $this->getDatabase()->execute($sql); + $idLayeredFilter = (int) $this->getDatabase()->Insert_ID(); + } else { + $this->getDatabase()->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'layered_filter_shop WHERE `id_layered_filter` = ' . (int) $idLayeredFilter + ); + $sql = 'UPDATE ' . _DB_PREFIX_ . 'layered_filter ' . + 'SET name = "' . pSQL($values['name']) . '", ' . + 'filters = "' . $values['filters'] . '", ' . + 'n_categories = ' . (int) $values['n_categories'] . ' ' . + 'WHERE id_layered_filter = ' . $idLayeredFilter; + $this->getDatabase()->execute($sql); } - $idLayeredFilter = isset($valuesToInsert['id_layered_filter']) ? (int) $valuesToInsert['id_layered_filter'] : 'NULL'; - $sql = 'INSERT INTO ' . _DB_PREFIX_ . 'layered_filter (name, filters, n_categories, date_add, id_layered_filter) VALUES ("' . pSQL($valuesToInsert['name']) . '", "' . $valuesToInsert['filters'] . '",' . (int) $valuesToInsert['n_categories'] . ',"' . pSQL($valuesToInsert['date_add']) . '",' . $idLayeredFilter . ')'; - $this->getDatabase()->execute($sql); - $idLayeredFilter = (int) $this->getDatabase()->Insert_ID(); - if (isset($assos)) { foreach ($assos as $asso) { $this->getDatabase()->execute( @@ -647,8 +629,15 @@ public function getContent() } $this->buildLayeredCategories(); - $message = $this->displayConfirmation($this->trans('Your filter', [], 'Modules.Facetedsearch.Admin') . ' "' . Tools::safeOutput(Tools::getValue('layered_tpl_name')) . '" ' . - ((isset($_POST['id_layered_filter']) && $_POST['id_layered_filter']) ? $this->trans('was updated successfully.', [], 'Modules.Facetedsearch.Admin') : $this->trans('was added successfully.', [], 'Modules.Facetedsearch.Admin'))); + $message = $this->displayConfirmation( + $this->trans('Your filter', [], 'Modules.Facetedsearch.Admin') . ' "' . + Tools::safeOutput(Tools::getValue('layered_tpl_name')) . '" ' . + ( + !empty($_POST['id_layered_filter']) ? + $this->trans('was updated successfully.', [], 'Modules.Facetedsearch.Admin') : + $this->trans('was added successfully.', [], 'Modules.Facetedsearch.Admin') + ) + ); } } } elseif (Tools::isSubmit('submitLayeredSettings')) { @@ -737,33 +726,36 @@ public function getContent() } if (Tools::getValue('edit_filters_template')) { + $idLayeredFilter = (int) Tools::getValue('id_layered_filter'); $template = $this->getDatabase()->getRow( 'SELECT * FROM `' . _DB_PREFIX_ . 'layered_filter` - WHERE id_layered_filter = ' . (int) Tools::getValue('id_layered_filter') + WHERE id_layered_filter = ' . $idLayeredFilter ); - $filters = Tools::unSerialize($template['filters']); - $treeCategoriesHelper->setSelectedCategories($filters['categories']); - $this->context->smarty->assign('categories_tree', $treeCategoriesHelper->render()); - - $selectShops = $filters['shop_list']; - unset($filters['categories']); - unset($filters['shop_list']); - - $this->context->smarty->assign([ - 'current_url' => $this->context->link->getAdminLink('AdminModules') . '&configure=ps_facetedsearch&tab_module=front_office_features&module_name=ps_facetedsearch', - 'uri' => $this->getPathUri(), - 'id_layered_filter' => (int) Tools::getValue('id_layered_filter'), - 'template_name' => $template['name'], - 'attribute_groups' => $attributeGroups, - 'features' => $features, - 'filters' => $filters, - 'total_filters' => 6 + count($attributeGroups) + count($features), - 'default_filters' => $this->getDefaultFilters(), - ]); - - return $this->display(__FILE__, 'views/templates/admin/view.tpl'); + if (!empty($template)) { + $filters = Tools::unSerialize($template['filters']); + $treeCategoriesHelper->setSelectedCategories($filters['categories']); + $this->context->smarty->assign('categories_tree', $treeCategoriesHelper->render()); + + $selectShops = $filters['shop_list']; + unset($filters['categories']); + unset($filters['shop_list']); + + $this->context->smarty->assign([ + 'current_url' => $this->context->link->getAdminLink('AdminModules') . '&configure=ps_facetedsearch&tab_module=front_office_features&module_name=ps_facetedsearch', + 'uri' => $this->getPathUri(), + 'id_layered_filter' => $idLayeredFilter, + 'template_name' => $template['name'], + 'attribute_groups' => $attributeGroups, + 'features' => $features, + 'filters' => $filters, + 'total_filters' => 6 + count($attributeGroups) + count($features), + 'default_filters' => $this->getDefaultFilters(), + ]); + + return $this->display(__FILE__, 'views/templates/admin/view.tpl'); + } } $this->context->smarty->assign([ diff --git a/src/Filters/Converter.php b/src/Filters/Converter.php index 77c8b6488..b3445dfea 100644 --- a/src/Filters/Converter.php +++ b/src/Filters/Converter.php @@ -85,6 +85,7 @@ public function getFacetsFromFilterBlocks(array $filterBlocks) $facet = new Facet(); $facet ->setLabel($filterBlock['name']) + ->setProperty('filter_show_limit', $filterBlock['filter_show_limit']) ->setMultipleSelectionAllowed(true); switch ($filterBlock['type']) { diff --git a/src/Product/SearchProvider.php b/src/Product/SearchProvider.php index b98c9ebd7..199cd61f9 100644 --- a/src/Product/SearchProvider.php +++ b/src/Product/SearchProvider.php @@ -175,8 +175,8 @@ public function runQuery( $this->labelRangeFilters($facets); $this->addEncodedFacetsToFilters($facets); - $this->hideZeroValues($facets); $this->hideUselessFacets($facets, (int) $result->getTotalProductsCount()); + $this->hideZeroValuesAndShowLimit($facets); $facetCollection = new FacetCollection(); $nextMenu = $facetCollection->setFacets($facets); @@ -422,16 +422,24 @@ private function addEncodedFacetsToFilters(array $facets) /** * Hide entries with 0 results + * Hide depending of show limit parameter * * @param array $facets */ - private function hideZeroValues(array $facets) + private function hideZeroValuesAndShowLimit(array $facets) { foreach ($facets as $facet) { + $count = 0; + $filterShowLimit = (int) $facet->getProperty('filter_show_limit'); foreach ($facet->getFilters() as $filter) { - if ($filter->getMagnitude() === 0) { + if ($filter->getMagnitude() === 0 + || ($filterShowLimit > 0 && $count >= $filterShowLimit) + ) { $filter->setDisplayed(false); + continue; } + + ++$count; } } } diff --git a/views/templates/admin/_functions/show_limit.tpl b/views/templates/admin/_functions/show_limit.tpl new file mode 100644 index 000000000..c2f617728 --- /dev/null +++ b/views/templates/admin/_functions/show_limit.tpl @@ -0,0 +1,34 @@ +{** + * 2007-2019 PrestaShop. + * + * NOTICE OF LICENSE + * + * This source file is subject to the Academic Free License 3.0 (AFL-3.0) + * that is bundled with this package in the file LICENSE.txt. + * It is also available through the world-wide-web at this URL: + * https://opensource.org/licenses/AFL-3.0 + * If you did not receive a copy of the license and are unable to + * obtain it through the world-wide-web, please send an email + * to license@prestashop.com so we can send you a copy immediately. + * + * DISCLAIMER + * + * Do not edit or add to this file if you wish to upgrade PrestaShop to newer + * versions in the future. If you wish to customize PrestaShop for your + * needs please refer to http://www.prestashop.com for more information. + * + * @author PrestaShop SA + * @copyright 2007-2019 PrestaShop SA + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + * International Registered Trademark & Property of PrestaShop SA + *} + +{function get_limit_select element=''} + {assign var="name" value="{$element}_filter_show_limit"} + +{/function} diff --git a/views/templates/admin/add.tpl b/views/templates/admin/add.tpl index cf4eeef7b..832721241 100644 --- a/views/templates/admin/add.tpl +++ b/views/templates/admin/add.tpl @@ -23,6 +23,7 @@ * International Registered Trademark & Property of PrestaShop SA *} {include file='./_partials/messages.tpl'} +{include file='./_functions/show_limit.tpl'}

{l s='New filters template' d='Modules.Facetedsearch.Admin'}

@@ -69,13 +70,7 @@
- + {call get_limit_select element="layered_selection_subcategories"}
@@ -107,13 +102,7 @@
- + {call get_limit_select element="layered_selection_stock"}
@@ -144,13 +133,7 @@
- + {call get_limit_select element="layered_selection_condition"}
@@ -181,13 +164,7 @@
- + {call get_limit_select element="layered_selection_manufacturer"}
@@ -216,16 +193,6 @@ {l s='Product weight filter (slider)' d='Modules.Facetedsearch.Admin'}
- -
- -
@@ -250,16 +217,6 @@ {l s='Product price filter (slider)' d='Modules.Facetedsearch.Admin'}
- -
- -
@@ -311,13 +268,7 @@
- + {call get_limit_select element="layered_selection_ag_{(int)$attribute_group['id_attribute_group']}"}
@@ -373,13 +324,7 @@
- + {call get_limit_select element="layered_selection_feat_{(int)$feature['id_feature']}"}
diff --git a/views/templates/admin/view.tpl b/views/templates/admin/view.tpl index 1b0e6ef0a..3c7951c3e 100644 --- a/views/templates/admin/view.tpl +++ b/views/templates/admin/view.tpl @@ -23,6 +23,7 @@ * International Registered Trademark & Property of PrestaShop SA *} {include file='./_partials/messages.tpl'} +{include file='./_functions/show_limit.tpl'}

{l s='New filters template' d='Modules.Facetedsearch.Admin'}

@@ -85,16 +86,12 @@ {/if}
- -
- -
+ {if empty($filter['slider'])} + +
+ {call get_limit_select element=$filterId} +
+ {/if}
{/foreach} @@ -141,13 +138,7 @@
- + {call get_limit_select element="layered_selection_ag_{(int)$attribute_group['id_attribute_group']}"}
@@ -203,13 +194,7 @@
- + {call get_limit_select element="layered_selection_feat_{(int)$feature['id_feature']}"}