From f64c5d74832651f4c51e6a0f27d5b1100f757754 Mon Sep 17 00:00:00 2001 From: Thomas Nabord Date: Fri, 5 Apr 2024 15:45:48 +0000 Subject: [PATCH] Start implementation of filters of features --- .../FeatureQueryBuilder.php | 106 ++++++++++++++++++ .../FilterApplication/QueryBuilder.php | 3 + config/admin/product_filter.yml | 8 ++ 3 files changed, 117 insertions(+) create mode 100644 classes/ProductFilter/FilterApplication/AttributeQueryBuilder/FeatureQueryBuilder.php diff --git a/classes/ProductFilter/FilterApplication/AttributeQueryBuilder/FeatureQueryBuilder.php b/classes/ProductFilter/FilterApplication/AttributeQueryBuilder/FeatureQueryBuilder.php new file mode 100644 index 000000000..c7f1d92e6 --- /dev/null +++ b/classes/ProductFilter/FilterApplication/AttributeQueryBuilder/FeatureQueryBuilder.php @@ -0,0 +1,106 @@ + + * @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; +use PrestaShop\Module\PsxMarketingWithGoogle\Repository\LanguageRepository; + +class FeatureQueryBuilder implements QueryBuilderInterface +{ + /** + * @var Context + */ + protected $context; + + /** + * @var LanguageRepository + */ + protected $languageRepository; + + /** + * @var string + */ + protected $currentLanguageIsoCode; + + public function __construct( + Context $context, + LanguageRepository $languageRepository + ) { + $this->context = $context; + $this->languageRepository = $languageRepository; + $this->currentLanguageIsoCode = $this->languageRepository->getIsoById( + (int) $this->context->language->id + ); + } + + public function addWhereFromFilter(DbQuery $query, $filter): DbQuery + { + /* + Example of payload: + + { + "attribute": "feature", + "condition": "in", + "values": [ + { + "id": "6", + "key": "Brasserie", + "value": "BRASSERIE DES LEGENDES", + "language": "fr" + }, + { + "id": "6", + "key": "Brasserie", + "value": "BRASSERY DES LEGENDES", + "language": "gb" + } + ] + } + */ + + + switch ($filter['condition']) { + case Condition::IS: + // TODO + return $query; + case Condition::IS_NOT: + // TODO + return $query; + } + + return $query; + } + + public function addRelations(DbQuery $query): DbQuery + { + return $query + ->leftJoin('feature_product', 'fp', 'fp.id_product = p.id_product') + ->innerJoin('feature', 'f', 'fp.id_feature = f.id_feature') + ->innerJoin('feature_shop', 'fs', 'fs.id_feature = f.id_feature') + ->innerJoin('feature_lang', 'fl', 'fl.id_feature = f.id_feature') + ->innerJoin('feature_value', 'fv', 'fv.id_feature = fp.id_feature') + ->innerJoin('feature_value_lang', 'fvl', 'fvl.id_feature_value = fv.id_feature_value') + ->where('fs.id_shop = ' . (int) $this->context->shop->id) + ->where('fl.id_lang = ' . (int) $this->context->language->id); + } +} diff --git a/classes/ProductFilter/FilterApplication/QueryBuilder.php b/classes/ProductFilter/FilterApplication/QueryBuilder.php index 76abb91e5..a3f2401e7 100644 --- a/classes/ProductFilter/FilterApplication/QueryBuilder.php +++ b/classes/ProductFilter/FilterApplication/QueryBuilder.php @@ -25,6 +25,7 @@ use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\BrandQueryBuilder; use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\CategoryQueryBuilder; use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\CustomAttributeQueryBuilder; +use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\FeatureQueryBuilder; use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\OutOfStockQueryBuilder; use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\PriceQueryBuilder; use PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\ProductIdQueryBuilder; @@ -47,6 +48,7 @@ public function __construct( BrandQueryBuilder $brandQueryBuilder, CategoryQueryBuilder $categoryQueryBuilder, CustomAttributeQueryBuilder $customAttributeQueryBuilder, + FeatureQueryBuilder $featureQueryBuilder, OutOfStockQueryBuilder $outOfStockQueryBuilder, PriceQueryBuilder $priceQueryBuilder, ProductIdQueryBuilder $productIdQueryBuilder @@ -56,6 +58,7 @@ public function __construct( AttributeType::BRAND => $brandQueryBuilder, AttributeType::CATEGORY => $categoryQueryBuilder, AttributeType::CUSTOM_ATTRIBUTE => $customAttributeQueryBuilder, + AttributeType::FEATURE =>$featureQueryBuilder, AttributeType::OUT_OF_STOCK => $outOfStockQueryBuilder, AttributeType::PRICE => $priceQueryBuilder, AttributeType::PRODUCT_ID => $productIdQueryBuilder, diff --git a/config/admin/product_filter.yml b/config/admin/product_filter.yml index a94bdf5d0..e1502d98d 100644 --- a/config/admin/product_filter.yml +++ b/config/admin/product_filter.yml @@ -47,6 +47,13 @@ services: class: PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\CustomAttributeQueryBuilder public: true + PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\FeatureQueryBuilder: + class: PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\FeatureQueryBuilder + public: true + arguments: + - '@psxmarketingwithgoogle.context' + - '@PrestaShop\Module\PsxMarketingWithGoogle\Repository\LanguageRepository' + PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\OutOfStockQueryBuilder: class: PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\OutOfStockQueryBuilder public: true @@ -67,6 +74,7 @@ services: - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\BrandQueryBuilder' - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\CategoryQueryBuilder' - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\CustomAttributeQueryBuilder' + - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\FeatureQueryBuilder' - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\OutOfStockQueryBuilder' - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\PriceQueryBuilder' - '@PrestaShop\Module\PsxMarketingWithGoogle\ProductFilter\FilterApplication\AttributeQueryBuilder\ProductIdQueryBuilder'