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

FilterParametersUpdater manages filters in a better way #10728

Merged
merged 5 commits into from Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion src/Adapter/Product/AdminProductDataProvider.php
Expand Up @@ -117,7 +117,7 @@ public function isCategoryFiltered()
{
$filters = $this->getPersistedFilterParameters();

return isset($filters['filter_category']) && $filters['filter_category'] > 0;
return !empty($filters['filter_category']) && $filters['filter_category'] > 0;
}

/**
Expand Down Expand Up @@ -163,6 +163,12 @@ public function persistFilterParameters(array $parameters)
}

$this->entityManager->flush();

//Flush cache
$employee = Context::getContext()->employee;
$employeeId = $employee->id ?: 0;

$this->cache->deleteItem("app.product_filters_${employeeId}");
}

/**
Expand Down
103 changes: 47 additions & 56 deletions src/Adapter/Product/FilterParametersUpdater.php
Expand Up @@ -26,6 +26,8 @@

namespace PrestaShop\PrestaShop\Adapter\Product;

use PrestaShop\PrestaShop\Core\Exception\ProductException;

/**
* Can manage filter parameters from request in Product Catalogue Page.
* For internal use only.
Expand All @@ -35,13 +37,13 @@ final class FilterParametersUpdater
/**
* In case of position ordering all the filters should be reset.
*
* @param bool $hasCategoryFilter
* @param string $orderBy
* @param array $filterParameters
* @param string $orderBy
* @param bool $hasCategoryFilter
*
* @return array $filterParameters
*/
public function setPositionOrdering($filterParameters, $orderBy, $hasCategoryFilter)
public function cleanFiltersForPositionOrdering($filterParameters, $orderBy, $hasCategoryFilter)
{
if ($orderBy == 'position_ordering' && $hasCategoryFilter) {
foreach (array_keys($filterParameters) as $key) {
Expand All @@ -55,72 +57,61 @@ public function setPositionOrdering($filterParameters, $orderBy, $hasCategoryFil
}

/**
* Gets previous Product query values from persistence.
*
* @param array $filterParameters
* @param string $offset
* @param string $limit
* @param string $orderBy
* @param string $sortOrder
* @param array $queryFilterParameters
* @param array $persistedFilterParameters
* @param array $defaultFilterParameters
*
* @return array
*
* @throws ProductException
*/
public function setValues(
array $filterParameters,
$offset,
$limit,
$orderBy,
$sortOrder
public function buildFilters(
array $queryFilterParameters,
array $persistedFilterParameters,
array $defaultFilterParameters
) {
return [
'offset' => $this->getOffset($offset, $filterParameters),
'limit' => $this->getLimit($limit, $filterParameters),
'orderBy' => $this->getOrderBy($orderBy, $filterParameters),
'sortOrder' => $this->getSortOrder($sortOrder, $filterParameters),
'offset' => (int) $this->getParameter('offset', $queryFilterParameters, $persistedFilterParameters, $defaultFilterParameters),
'limit' => (int) $this->getParameter('limit', $queryFilterParameters, $persistedFilterParameters, $defaultFilterParameters),
'orderBy' => (string) $this->getParameter('orderBy', $queryFilterParameters, $persistedFilterParameters, $defaultFilterParameters),
'sortOrder' => (string) $this->getParameter('sortOrder', $queryFilterParameters, $persistedFilterParameters, $defaultFilterParameters),
];
}

/**
* @param int $offset
* @param array $filterParameters
* @param string $parameterName
* @param array $queryFilterParameters
* @param array $persistedFilterParameters
* @param array $defaultFilterParameters
*
* @return int
*/
private function getOffset($offset, array $filterParameters)
{
return ($offset === 'last' && isset($filterParameters['last_offset'])) ? $filterParameters['last_offset'] : $offset;
}

/**
* @param int $limit
* @param array $filterParameters
* @return string|int
*
* @return int
* @throws ProductException
*/
private function getLimit($limit, array $filterParameters)
{
return ($limit === 'last' && isset($filterParameters['last_limit'])) ? $filterParameters['last_limit'] : $limit;
}
private function getParameter(
$parameterName,
array $queryFilterParameters,
array $persistedFilterParameters,
array $defaultFilterParameters
) {
if (isset($queryFilterParameters[$parameterName])) {
$value = $queryFilterParameters[$parameterName];
} elseif (isset($persistedFilterParameters[$parameterName])) {
$value = $persistedFilterParameters[$parameterName];
} elseif (isset($defaultFilterParameters[$parameterName])) {
$value = $defaultFilterParameters[$parameterName];
jolelievre marked this conversation as resolved.
Show resolved Hide resolved
} else {
throw new ProductException(
'Could not find the parameter %s',
'Admin.Notifications.Error',
[$parameterName]
);
}

/**
* @param string $orderBy
* @param array $filterParameters
*
* @return string
*/
private function getOrderBy($orderBy, array $filterParameters)
{
return ($orderBy === 'last' && isset($filterParameters['last_orderBy'])) ? $filterParameters['last_orderBy'] : $orderBy;
}
if ($value === 'last' && isset($persistedFilterParameters['last_' . $parameterName])) {
$value = $persistedFilterParameters['last_' . $parameterName];
}

/**
* @param string $sortOrder
* @param array $filterParameters
*
* @return string
*/
private function getSortOrder($sortOrder, array $filterParameters)
{
return ($sortOrder === 'last' && isset($filterParameters['last_sortOrder'])) ? $filterParameters['last_sortOrder'] : $sortOrder;
return $value;
}
}
34 changes: 34 additions & 0 deletions src/Core/Exception/ProductException.php
@@ -0,0 +1,34 @@
<?php
/**
* 2007-2018 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/OSL-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-2018 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace PrestaShop\PrestaShop\Core\Exception;

/**
* Class ProductException used when an error linked to a product occurs.
*/
class ProductException extends PrestaShopCoreException
{
}
41 changes: 22 additions & 19 deletions src/PrestaShopBundle/Controller/Admin/ProductController.php
Expand Up @@ -27,6 +27,7 @@
namespace PrestaShopBundle\Controller\Admin;

use Exception;
use PrestaShop\PrestaShop\Adapter\Product\FilterParametersUpdater;
use PrestaShop\PrestaShop\Adapter\Tax\TaxRuleDataProvider;
use PrestaShop\PrestaShop\Adapter\Warehouse\WarehouseDataProvider;
use PrestaShopBundle\Component\CsvResponse;
Expand Down Expand Up @@ -126,21 +127,14 @@ public function catalogAction(

// Set values from persistence and replace in the request
$persistedFilterParameters = $productProvider->getPersistedFilterParameters();
/** @var FilterParametersUpdater $filterParametersUpdater */
$filterParametersUpdater = $this->get('prestashop.adapter.product.filter_parameters_updater');

$filters = $filterParametersUpdater->setValues(
$filters = $filterParametersUpdater->buildFilters(
$request->query->all(),
$persistedFilterParameters,
$offset,
$limit,
$orderBy,
$sortOrder
compact('offset', 'limit', 'orderBy', 'sortOrder')
);

// mimic native extract() function.
$offset = $filters['offset'];
$limit = $filters['limit'];
$orderBy = $filters['orderBy'];
$sortOrder = $filters['sortOrder'];
extract($filters);

$persistedFilterParameters = array_replace($persistedFilterParameters, $request->request->all());

Expand Down Expand Up @@ -174,14 +168,16 @@ public function catalogAction(
$categoriesForm = $this->createForm(ProductCategories::class);
if (!empty($persistedFilterParameters['filter_category'])) {
$categoriesForm->setData(
array(
'tree' => array(0 => $persistedFilterParameters['filter_category']),
)
[
'categories' => [
'tree' => [0 => $persistedFilterParameters['filter_category']],
],
]
);
}
}

$persistedFilterParameters = $filterParametersUpdater->setPositionOrdering($persistedFilterParameters, $orderBy, $hasCategoryFilter);
$cleanFilterParameters = $filterParametersUpdater->cleanFiltersForPositionOrdering($persistedFilterParameters, $orderBy, $hasCategoryFilter);

$permissionError = null;
if ($this->get('session')->getFlashBag()->has('permission_error')) {
Expand All @@ -192,13 +188,13 @@ public function catalogAction(
|| ('position' === $orderBy && 'asc' === $sortOrder && !$hasColumnFilter);
// Template vars injection
return array_merge(
$persistedFilterParameters,
$cleanFilterParameters,
[
'limit' => $limit,
'offset' => $offset,
'orderBy' => $orderBy,
'sortOrder' => $sortOrder,
'has_filter' => $hasCategoryFilter | $hasColumnFilter,
'has_filter' => $hasCategoryFilter || $hasColumnFilter,
'has_category_filter' => $hasCategoryFilter,
'has_column_filter' => $hasColumnFilter,
'products' => $products,
Expand Down Expand Up @@ -258,7 +254,14 @@ public function listAction(
if ($products === null) {
// get old values from persistence (before the current update)
$persistedFilterParameters = $productProvider->getPersistedFilterParameters();
$this->get('prestashop.adapter.filter_parameters_updater')->setValues($persistedFilterParameters, $offset, $limit, $orderBy, $sortOrder);
/** @var FilterParametersUpdater $filterParametersUpdater */
$filterParametersUpdater = $this->get('prestashop.adapter.product.filter_parameters_updater');
$filters = $filterParametersUpdater->buildFilters(
$request->query->all(),
$persistedFilterParameters,
compact('offset', 'limit', 'orderBy', 'sortOrder')
);
extract($filters);

/**
* 2 hooks are triggered here:
Expand Down