diff --git a/src/Adapter/Product/AdminProductDataProvider.php b/src/Adapter/Product/AdminProductDataProvider.php index 398a99c3cb290..745c0f4468cf5 100644 --- a/src/Adapter/Product/AdminProductDataProvider.php +++ b/src/Adapter/Product/AdminProductDataProvider.php @@ -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; } /** @@ -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}"); } /** diff --git a/src/Adapter/Product/FilterParametersUpdater.php b/src/Adapter/Product/FilterParametersUpdater.php index 455799166edfd..d687d1d460e99 100644 --- a/src/Adapter/Product/FilterParametersUpdater.php +++ b/src/Adapter/Product/FilterParametersUpdater.php @@ -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. @@ -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) { @@ -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]; + } 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; } } diff --git a/src/Core/Exception/ProductException.php b/src/Core/Exception/ProductException.php new file mode 100644 index 0000000000000..57e0db1c28887 --- /dev/null +++ b/src/Core/Exception/ProductException.php @@ -0,0 +1,34 @@ + + * @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 +{ +} diff --git a/src/PrestaShopBundle/Controller/Admin/ProductController.php b/src/PrestaShopBundle/Controller/Admin/ProductController.php index cc154f41d4fef..7d2f845fbe7bd 100644 --- a/src/PrestaShopBundle/Controller/Admin/ProductController.php +++ b/src/PrestaShopBundle/Controller/Admin/ProductController.php @@ -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; @@ -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()); @@ -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')) { @@ -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, @@ -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: