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

Minor refactoring #892

Merged
merged 1 commit into from
Aug 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Definition/Availability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* 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.md.
* 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 [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\FacetedSearch\Definition;

class Availability
{
const IN_STOCK = 2;
const AVAILABLE = 1;
const NOT_AVAILABLE = 0;
}
17 changes: 9 additions & 8 deletions src/Filters/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Group;
use Manufacturer;
use PrestaShop\Module\FacetedSearch\Adapter\InterfaceAdapter;
use PrestaShop\Module\FacetedSearch\Definition\Availability;
use PrestaShop\Module\FacetedSearch\Product\Search;
use PrestaShop\PrestaShop\Core\Localization\Locale;
use PrestaShop\PrestaShop\Core\Localization\Specification\NumberSymbolList;
Expand Down Expand Up @@ -435,23 +436,23 @@ private function getAvailabilitiesBlock($filter, $selectedFilters)
$availabilityOptions = [];
if ($this->psStockManagement) {
$availabilityOptions = [
2 => [
Availability::IN_STOCK => [
'name' => $this->context->getTranslator()->trans(
'In stock',
[],
'Modules.Facetedsearch.Shop'
),
'nbr' => 0,
],
1 => [
Availability::AVAILABLE => [
'name' => $this->context->getTranslator()->trans(
'Available',
[],
'Modules.Facetedsearch.Shop'
),
'nbr' => 0,
],
0 => [
Availability::NOT_AVAILABLE => [
'name' => $this->context->getTranslator()->trans(
'Not available',
[],
Expand All @@ -473,7 +474,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters)
],
]
);
$availabilityOptions[0]['nbr'] = $filteredSearchAdapter->count();
$availabilityOptions[Availability::NOT_AVAILABLE]['nbr'] = $filteredSearchAdapter->count();

// Products in stock, or with out-of-stock ordering enabled
$filteredSearchAdapter->addOperationsFilter(
Expand All @@ -487,7 +488,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters)
],
]
);
$availabilityOptions[1]['nbr'] = $filteredSearchAdapter->count();
$availabilityOptions[Availability::AVAILABLE]['nbr'] = $filteredSearchAdapter->count();

// Products in stock
$filteredSearchAdapter->addOperationsFilter(
Expand All @@ -498,7 +499,7 @@ private function getAvailabilitiesBlock($filter, $selectedFilters)
],
]
);
$availabilityOptions[2]['nbr'] = $filteredSearchAdapter->count();
$availabilityOptions[Availability::IN_STOCK]['nbr'] = $filteredSearchAdapter->count();

// If some filter was selected, we want to show only this single filter, it does not make sense to show others
if (isset($selectedFilters['availability'])) {
Expand All @@ -513,8 +514,8 @@ private function getAvailabilitiesBlock($filter, $selectedFilters)
// Hide Available option if the count is the same as In stock, it doesn't make no sense
// Product count is a reliable indicator here, because there can never be product IN STOCK that is not AVAILABLE
// So if the counts match, it MUST BE the same products
if ($availabilityOptions[1]['nbr'] == $availabilityOptions[2]['nbr']) {
unset($availabilityOptions[1]);
if ($availabilityOptions[Availability::AVAILABLE]['nbr'] == $availabilityOptions[Availability::IN_STOCK]['nbr']) {
unset($availabilityOptions[Availability::AVAILABLE]);
}
}

Expand Down
53 changes: 27 additions & 26 deletions src/Filters/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Context;
use Db;
use Manufacturer;
use PrestaShop\Module\FacetedSearch\Definition\Availability;
use PrestaShop\Module\FacetedSearch\Filters;
use PrestaShop\Module\FacetedSearch\URLSerializer;
use PrestaShop\PrestaShop\Core\Product\Search\Facet;
Expand Down Expand Up @@ -257,7 +258,7 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
$searchFilters = [];

// Get filters configured in module settings for the current query
$filters = $this->provider->getFiltersForQuery($query, $idShop);
$configuredFilters = $this->provider->getFiltersForQuery($query, $idShop);

/*
* Parses submitted encoded facets from (URL) string into a nice array.
Expand All @@ -266,30 +267,30 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
* work very well, because there could be duplicate values for both facet and filter.
* For example, if there are two features, feature values or categories with the same name.
*/
$facetAndFiltersLabels = $this->urlSerializer->unserialize($query->getEncodedFacets());
$receivedFilters = $this->urlSerializer->unserialize($query->getEncodedFacets());

// Go through filters that are configured and find out which should be activated,
// depending on what was provided in the encodedFacets.
foreach ($filters as $filter) {
foreach ($configuredFilters as $filter) {
$filterLabel = $this->convertFilterTypeToLabel($filter['type']);

switch ($filter['type']) {
case self::TYPE_MANUFACTURER:
if (!isset($facetAndFiltersLabels[$filterLabel])) {
if (!isset($receivedFilters[$filterLabel])) {
// No need to filter if no information
continue 2;
}

$manufacturers = Manufacturer::getManufacturers(false, $idLang);
$searchFilters[$filter['type']] = [];
foreach ($manufacturers as $manufacturer) {
if (in_array($manufacturer['name'], $facetAndFiltersLabels[$filterLabel])) {
if (in_array($manufacturer['name'], $receivedFilters[$filterLabel])) {
$searchFilters[$filter['type']][$manufacturer['name']] = $manufacturer['id_manufacturer'];
}
}
break;
case self::TYPE_AVAILABILITY:
if (!isset($facetAndFiltersLabels[$filterLabel])) {
if (!isset($receivedFilters[$filterLabel])) {
// No need to filter if no information
continue 2;
}
Expand All @@ -299,28 +300,28 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
'Not available',
[],
'Modules.Facetedsearch.Shop'
) => 0,
) => Availability::NOT_AVAILABLE,
$this->context->getTranslator()->trans(
'Available',
[],
'Modules.Facetedsearch.Shop'
) => 1,
) => Availability::AVAILABLE,
$this->context->getTranslator()->trans(
'In stock',
[],
'Modules.Facetedsearch.Shop'
) => 2,
) => Availability::IN_STOCK,
];

$searchFilters[$filter['type']] = [];
foreach ($quantityArray as $quantityName => $quantityId) {
if (isset($facetAndFiltersLabels[$filterLabel]) && in_array($quantityName, $facetAndFiltersLabels[$filterLabel])) {
if (isset($receivedFilters[$filterLabel]) && in_array($quantityName, $receivedFilters[$filterLabel])) {
$searchFilters[$filter['type']][] = $quantityId;
}
}
break;
case self::TYPE_CONDITION:
if (!isset($facetAndFiltersLabels[$filterLabel])) {
if (!isset($receivedFilters[$filterLabel])) {
// No need to filter if no information
continue 2;
}
Expand All @@ -345,7 +346,7 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)

$searchFilters[$filter['type']] = [];
foreach ($conditionArray as $conditionName => $conditionId) {
if (isset($facetAndFiltersLabels[$filterLabel]) && in_array($conditionName, $facetAndFiltersLabels[$filterLabel])) {
if (isset($receivedFilters[$filterLabel]) && in_array($conditionName, $receivedFilters[$filterLabel])) {
$searchFilters[$filter['type']][] = $conditionId;
}
}
Expand All @@ -357,10 +358,10 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
continue;
}

if (isset($facetAndFiltersLabels[$feature['url_name']])) {
$featureValueLabels = $facetAndFiltersLabels[$feature['url_name']];
} elseif (isset($facetAndFiltersLabels[$feature['name']])) {
$featureValueLabels = $facetAndFiltersLabels[$feature['name']];
if (isset($receivedFilters[$feature['url_name']])) {
$featureValueLabels = $receivedFilters[$feature['url_name']];
} elseif (isset($receivedFilters[$feature['name']])) {
$featureValueLabels = $receivedFilters[$feature['name']];
} else {
continue;
}
Expand All @@ -382,10 +383,10 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
continue;
}

if (isset($facetAndFiltersLabels[$attributeGroup['url_name']])) {
$attributeLabels = $facetAndFiltersLabels[$attributeGroup['url_name']];
} elseif (isset($facetAndFiltersLabels[$attributeGroup['attribute_group_name']])) {
$attributeLabels = $facetAndFiltersLabels[$attributeGroup['attribute_group_name']];
if (isset($receivedFilters[$attributeGroup['url_name']])) {
$attributeLabels = $receivedFilters[$attributeGroup['url_name']];
} elseif (isset($receivedFilters[$attributeGroup['attribute_group_name']])) {
$attributeLabels = $receivedFilters[$attributeGroup['attribute_group_name']];
} else {
continue;
}
Expand All @@ -402,8 +403,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
break;
case self::TYPE_PRICE:
case self::TYPE_WEIGHT:
if (isset($facetAndFiltersLabels[$filterLabel])) {
$filters = $facetAndFiltersLabels[$filterLabel];
if (isset($receivedFilters[$filterLabel])) {
$filters = $receivedFilters[$filterLabel];
if (isset($filters[1]) && isset($filters[2])) {
$from = $filters[1];
$to = $filters[2];
Expand All @@ -413,8 +414,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
}
break;
case self::TYPE_CATEGORY:
if (isset($facetAndFiltersLabels[$filterLabel])) {
foreach ($facetAndFiltersLabels[$filterLabel] as $queryFilter) {
if (isset($receivedFilters[$filterLabel])) {
foreach ($receivedFilters[$filterLabel] as $queryFilter) {
/*
* This works only for categories that are child of the category we are browsing (or home category).
* Categories deeper in the tree will never be found. This could be fixed by providing a unique ID
Expand All @@ -428,8 +429,8 @@ public function createFacetedSearchFiltersFromQuery(ProductSearchQuery $query)
}
break;
default:
if (isset($facetAndFiltersLabels[$filterLabel])) {
foreach ($facetAndFiltersLabels[$filterLabel] as $queryFilter) {
if (isset($receivedFilters[$filterLabel])) {
foreach ($receivedFilters[$filterLabel] as $queryFilter) {
$searchFilters[$filter['type']][] = $queryFilter;
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/Product/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Group;
use PrestaShop\Module\FacetedSearch\Adapter\AbstractAdapter;
use PrestaShop\Module\FacetedSearch\Adapter\MySQL as MySQLAdapter;
use PrestaShop\Module\FacetedSearch\Definition\Availability;
use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery;

class Search
Expand Down Expand Up @@ -192,32 +193,32 @@ private function addSearchFilters($selectedFilters)
// Simple cases with 1 option selected
if (count($filterValues) == 1) {
// Not available
if ($filterValues[0] == 0) {
if ($filterValues[0] == Availability::NOT_AVAILABLE) {
$operationsFilter[] = [
['quantity', [0], '<='],
['out_of_stock', $this->psOrderOutOfStock ? [0] : [0, 2], '='],
];
// Available
} elseif ($filterValues[0] == 1) {
} elseif ($filterValues[0] == Availability::AVAILABLE) {
$operationsFilter[] = [
['out_of_stock', $this->psOrderOutOfStock ? [1, 2] : [1], '='],
];
$operationsFilter[] = [
['quantity', [0], '>'],
];
// In stock
} elseif ($filterValues[0] == 2) {
} elseif ($filterValues[0] == Availability::IN_STOCK) {
$operationsFilter[] = [
['quantity', [0], '>'],
];
}
// Cases with 2 options selected
} elseif (count($filterValues) == 2) {
// Not available and available, we show everything
if (in_array(0, $filterValues) && in_array(1, $filterValues)) {
if (in_array(Availability::NOT_AVAILABLE, $filterValues) && in_array(Availability::AVAILABLE, $filterValues)) {
break;
// Not available or in stock
} elseif (in_array(0, $filterValues) && in_array(2, $filterValues)) {
} elseif (in_array(Availability::NOT_AVAILABLE, $filterValues) && in_array(Availability::IN_STOCK, $filterValues)) {
$operationsFilter[] = [
['quantity', [0], '<='],
['out_of_stock', $this->psOrderOutOfStock ? [0] : [0, 2], '='],
Expand All @@ -226,7 +227,7 @@ private function addSearchFilters($selectedFilters)
['quantity', [0], '>'],
];
// Available or in stock
} elseif (in_array(1, $filterValues) && in_array(2, $filterValues)) {
} elseif (in_array(Availability::AVAILABLE, $filterValues) && in_array(Availability::IN_STOCK, $filterValues)) {
$operationsFilter[] = [
['out_of_stock', $this->psOrderOutOfStock ? [1, 2] : [1], '='],
];
Expand Down
9 changes: 5 additions & 4 deletions tests/php/FacetedSearch/Filters/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use PrestaShop\Module\FacetedSearch\Adapter\MySQL;
use PrestaShop\Module\FacetedSearch\Definition\Availability;
use PrestaShop\Module\FacetedSearch\Filters\Block;
use PrestaShop\Module\FacetedSearch\Filters\DataAccessor;
use PrestaShop\Module\FacetedSearch\Filters\Provider;
Expand Down Expand Up @@ -380,16 +381,16 @@ public function testGetFiltersBlockWithQuantities()
'id_key' => 0,
'name' => 'Availability',
'values' => [
2 => [
Availability::IN_STOCK => [
'name' => 'In stock',
'nbr' => 50,
],
1 => [
Availability::AVAILABLE => [
'name' => 'Available',
'nbr' => 100,
'checked' => true,
],
0 => [
Availability::NOT_AVAILABLE => [
'name' => 'Not available',
'nbr' => 1000,
],
Expand All @@ -403,7 +404,7 @@ public function testGetFiltersBlockWithQuantities()
10,
[
'availability' => [
1,
Availability::AVAILABLE,
],
]
)
Expand Down
Loading
Loading