Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make filter show limit working again #102

Merged
merged 6 commits into from Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
170 changes: 81 additions & 89 deletions ps_facetedsearch.php
Expand Up @@ -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'
Expand All @@ -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('~^(?P<key>layered_selection_.*)(?<!_filter_)(?<!type)(?<!show_limit)$~', $key, $matches)) {
continue;
}

$filterValues[$matches['key']] = [
'filter_type' => (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(
Expand All @@ -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')
)
);
PierreRambaud marked this conversation as resolved.
Show resolved Hide resolved
}
}
} elseif (Tools::isSubmit('submitLayeredSettings')) {
Expand Down Expand Up @@ -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([
Expand Down
1 change: 1 addition & 0 deletions src/Filters/Converter.php
Expand Up @@ -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']) {
Expand Down
13 changes: 10 additions & 3 deletions src/Product/SearchProvider.php
Expand Up @@ -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);
Expand Down Expand Up @@ -422,16 +422,23 @@ 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;
foreach ($facet->getFilters() as $filter) {
if ($filter->getMagnitude() === 0) {
if ($filter->getMagnitude() === 0
|| $count >= $facet->getProperty('filter_show_limit')
) {
$filter->setDisplayed(false);
continue;
}

++$count;
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions 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 <contact@prestashop.com>
* @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"}
<select name="{$name}">
<option value="0">{l s='No limit' d='Modules.Facetedsearch.Admin'}</option>
{for $index=2 to 20}
<option value="{$index}">{$index}</option>
{/for}
</select>
{/function}