Skip to content

Commit

Permalink
Merge pull request #1714 from Quetzacoalt91/product-filters-php-routes
Browse files Browse the repository at this point in the history
Introduce PHP endpoints for product filters
  • Loading branch information
ga-devfront committed Apr 30, 2024
2 parents 4b43271 + cf9b362 commit 26d1de9
Show file tree
Hide file tree
Showing 28 changed files with 2,516 additions and 8 deletions.
3 changes: 1 addition & 2 deletions classes/Adapter/ConfigurationAdapter.php
Expand Up @@ -21,12 +21,11 @@
namespace PrestaShop\Module\PsxMarketingWithGoogle\Adapter;

use Configuration;
use Shop;

class ConfigurationAdapter
{
/**
* @var Shop
* @var int
*/
private $shopId;

Expand Down
119 changes: 119 additions & 0 deletions classes/ProductFilter/AttributeMapConditionOutput.php
@@ -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'],
],
],
];
}
47 changes: 47 additions & 0 deletions classes/ProductFilter/AttributeType.php
@@ -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,
];
}
}
35 changes: 35 additions & 0 deletions classes/ProductFilter/Condition.php
@@ -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';
}
@@ -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');
}
}
@@ -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');
}
}

0 comments on commit 26d1de9

Please sign in to comment.