-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1714 from Quetzacoalt91/product-filters-php-routes
Introduce PHP endpoints for product filters
- Loading branch information
Showing
28 changed files
with
2,516 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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 version 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 license@prestashop.com so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter; | ||
|
||
class AttributeMapConditionOutput | ||
{ | ||
const STRING = 'string'; | ||
const INT = 'int'; | ||
const BOOLEAN = 'boolean'; | ||
const OBJECT = 'object'; | ||
|
||
const MAP = [ | ||
AttributeType::BRAND => [ | ||
Condition::DOES_CONTAIN => [ | ||
'multiple' => true, | ||
'type' => self::STRING, | ||
], | ||
Condition::DOES_NOT_CONTAIN => [ | ||
'multiple' => true, | ||
'type' => self::STRING, | ||
], | ||
Condition::IS => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['value', 'id'], | ||
], | ||
Condition::IS_NOT => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['value', 'id'], | ||
], | ||
], | ||
AttributeType::CATEGORY => [ | ||
Condition::DOES_CONTAIN => [ | ||
'multiple' => true, | ||
'type' => self::STRING, | ||
], | ||
Condition::DOES_NOT_CONTAIN => [ | ||
'multiple' => true, | ||
'type' => self::STRING, | ||
], | ||
Condition::IS => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['value', 'id'], | ||
], | ||
Condition::IS_NOT => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['value', 'id'], | ||
], | ||
], | ||
AttributeType::PRICE => [ | ||
Condition::IS => [ | ||
'multiple' => false, | ||
'type' => self::INT, | ||
'positive' => false, | ||
], | ||
Condition::GREATER => [ | ||
'multiple' => false, | ||
'type' => self::INT, | ||
'positive' => false, | ||
], | ||
Condition::LOWER => [ | ||
'multiple' => false, | ||
'type' => self::INT, | ||
'positive' => false, | ||
], | ||
], | ||
AttributeType::PRODUCT_ID => [ | ||
Condition::IS => [ | ||
'multiple' => true, | ||
'type' => self::INT, | ||
'positive' => true, | ||
], | ||
Condition::IS_NOT => [ | ||
'multiple' => true, | ||
'type' => self::INT, | ||
'positive' => true, | ||
], | ||
], | ||
AttributeType::OUT_OF_STOCK => [ | ||
Condition::IS => [ | ||
'multiple' => false, | ||
'type' => self::BOOLEAN, | ||
], | ||
], | ||
AttributeType::FEATURE => [ | ||
Condition::IS => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['id', 'key', 'value', 'language'], | ||
], | ||
Condition::IS_NOT => [ | ||
'multiple' => true, | ||
'type' => self::OBJECT, | ||
'keys' => ['id', 'key', 'value', 'language'], | ||
], | ||
], | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?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 version 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 license@prestashop.com so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter; | ||
|
||
/** | ||
* enum only exists from PHP 8 and the module is compliant with PHP 7.2+, | ||
* thus cannot be used here. | ||
*/ | ||
class AttributeType | ||
{ | ||
const BRAND = 'brand'; | ||
const CATEGORY = 'category'; | ||
const FEATURE = 'feature'; | ||
const PRICE = 'price'; | ||
const PRODUCT_ID = 'id'; | ||
const OUT_OF_STOCK = 'out_of_stock'; | ||
|
||
public static function all() | ||
{ | ||
return [ | ||
static::BRAND, | ||
static::CATEGORY, | ||
static::FEATURE, | ||
static::PRICE, | ||
static::PRODUCT_ID, | ||
static::OUT_OF_STOCK, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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 version 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 license@prestashop.com so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter; | ||
|
||
/** | ||
* enum only exists from PHP 8 and the module is compliant with PHP 7.2+, | ||
* thus cannot be used here. | ||
*/ | ||
class Condition | ||
{ | ||
const DOES_CONTAIN = 'does_contain'; | ||
const DOES_NOT_CONTAIN = 'does_not_contain'; | ||
const GREATER = 'greater'; | ||
const LOWER = 'lower'; | ||
const IS = 'is'; | ||
const IS_NOT = 'is_not'; | ||
} |
70 changes: 70 additions & 0 deletions
70
classes/ProductFilter/FilterApplication/AttributeQueryBuilder/BrandQueryBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?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 version 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 license@prestashop.com so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder; | ||
|
||
use DbQuery; | ||
use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\Condition; | ||
|
||
class BrandQueryBuilder implements QueryBuilderInterface | ||
{ | ||
public function addWhereFromFilter(DbQuery $query, $filter): DbQuery | ||
{ | ||
switch ($filter['condition']) { | ||
case Condition::DOES_CONTAIN: | ||
$queryConditions = []; | ||
foreach ($filter['values'] as $value) { | ||
$queryConditions[] = 'm.name LIKE "%' . pSQL($value) . '%"'; | ||
} | ||
|
||
return $query->where('(' . implode(' OR ', $queryConditions) . ')'); | ||
|
||
case Condition::DOES_NOT_CONTAIN: | ||
$queryConditions = []; | ||
foreach ($filter['values'] as $value) { | ||
$queryConditions[] = 'm.name NOT LIKE "%' . pSQL($value) . '%"'; | ||
} | ||
|
||
return $query->where('(' . implode(' OR ', $queryConditions) . ')'); | ||
|
||
case Condition::IS: | ||
$filteredValues = array_map(function ($item) { | ||
return $item['value']; | ||
}, $filter['values']); | ||
|
||
return $query->where('m.name IN ["' . implode('", "', array_map('pSQL', $filteredValues)) . '"]'); | ||
|
||
case Condition::IS_NOT: | ||
$filteredValues = array_map(function ($item) { | ||
return $item['value']; | ||
}, $filter['values']); | ||
|
||
return $query->where('m.name NOT IN ["' . implode('", "', array_map('pSQL', $filteredValues)) . '"]'); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
public function addRelations(DbQuery $query): DbQuery | ||
{ | ||
return $query->innerJoin('manufacturer', 'm', 'm.id_manufacturer = p.id_manufacturer') | ||
->where('m.active = 1'); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
classes/ProductFilter/FilterApplication/AttributeQueryBuilder/CategoryQueryBuilder.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?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 version 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 license@prestashop.com so we can send you a copy immediately. | ||
* | ||
* @author PrestaShop SA and Contributors <contact@prestashop.com> | ||
* @copyright Since 2007 PrestaShop SA and Contributors | ||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 | ||
*/ | ||
|
||
namespace PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder; | ||
|
||
use Context; | ||
use DbQuery; | ||
use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\Condition; | ||
|
||
class CategoryQueryBuilder implements QueryBuilderInterface | ||
{ | ||
/** | ||
* @var Context | ||
*/ | ||
protected $context; | ||
|
||
public function __construct(Context $context) | ||
{ | ||
$this->context = $context; | ||
} | ||
|
||
public function addWhereFromFilter(DbQuery $query, $filter): DbQuery | ||
{ | ||
// At the time of implementation, CloudSync gets only the default category of the product. | ||
// We add the condition based on the default category here as well. | ||
switch ($filter['condition']) { | ||
case Condition::DOES_CONTAIN: | ||
$queryConditions = []; | ||
foreach ($filter['values'] as $value) { | ||
$queryConditions[] = 'cl.name LIKE "%' . pSQL($value) . '%"'; | ||
} | ||
|
||
return $query->where('(' . implode(' OR ', $queryConditions) . ')'); | ||
|
||
case Condition::DOES_NOT_CONTAIN: | ||
$queryConditions = []; | ||
foreach ($filter['values'] as $value) { | ||
$queryConditions[] = 'cl.name NOT LIKE "%' . pSQL($value) . '%"'; | ||
} | ||
|
||
return $query->where('(' . implode(' OR ', $queryConditions) . ')'); | ||
|
||
case Condition::IS: | ||
$filteredValues = array_map(function ($item) { | ||
return $item['id']; | ||
}, $filter['values']); | ||
|
||
return $query->where('c.id_category_default IN [' . implode(', ', array_map('intval', $filteredValues)) . ']'); | ||
|
||
case Condition::IS_NOT: | ||
$filteredValues = array_map(function ($item) { | ||
return $item['id']; | ||
}, $filter['values']); | ||
|
||
return $query->where('c.id_category_default NOT IN [' . implode(', ', array_map('intval', $filteredValues)) . ']'); | ||
} | ||
|
||
return $query; | ||
} | ||
|
||
public function addRelations(DbQuery $query): DbQuery | ||
{ | ||
return $query->innerJoin('category', 'c', 'c.id_category = p.id_category_default') | ||
->innerJoin('category_lang', 'cl', 'c.id_category = cl.id_category') | ||
->where('cl.id_lang = ' . (int) $this->context->language->id) | ||
->where('c.active = 1'); | ||
} | ||
} |
Oops, something went wrong.