From 34e865651494f48ffbed3561906fb89dcd074dd9 Mon Sep 17 00:00:00 2001 From: kgleizes Date: Fri, 25 Apr 2025 15:59:58 +0200 Subject: [PATCH 1/4] Improve categories count --- src/Adapter/MySQL.php | 25 +++++++++++++++++++++++-- src/Filters/Block.php | 24 +++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index 870d2f32c..43f8dfceb 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -44,6 +44,9 @@ class MySQL extends AbstractAdapter */ const INNER_JOIN = 'INNER JOIN'; + /** @var bool */ + private $categoryCount = false; + /** * {@inheritdoc} */ @@ -95,6 +98,12 @@ public function getQuery() // Prepare mapping for joined tables $filterToTableMapping = $this->getFieldMapping(); + if ($this->categoryCount === true) { + unset($filterToTableMapping['nleft']); + unset($filterToTableMapping['nright']); + unset($filterToTableMapping['id_group']); + } + // Process and generate all fields for the SQL query below $orderField = $this->computeOrderByField($filterToTableMapping); $selectFields = $this->computeSelectFields($filterToTableMapping); @@ -120,6 +129,17 @@ public function getQuery() } } + if ($this->categoryCount === true) { + foreach($whereConditions as $key => $oneCondition) { + if ($oneCondition === "p.id_group='1'") { + unset($whereConditions[$key]); + } + if (strpos($oneCondition, 'p.nleft') !== false || strpos($oneCondition, 'p.nright') !== false) { + unset($whereConditions[$key]); + } + } + } + if (!empty($whereConditions)) { $query .= ' WHERE ' . implode(' AND ', $whereConditions); } @@ -762,8 +782,9 @@ public function count() /** * {@inheritdoc} */ - public function valueCount($fieldName = null) + public function valueCount($fieldName = null, $categoryCount = false) { + $this->categoryCount = $categoryCount; $this->resetGroupBy(); if ($fieldName !== null) { $this->addGroupBy($fieldName); @@ -774,7 +795,6 @@ public function valueCount($fieldName = null) $this->setOrderField(''); $this->copyOperationsFilters(); - return $this->execute(); } @@ -838,6 +858,7 @@ protected function copyOperationsFilters() $operationsFilters = clone $initialPopulation->getOperationsFilters(); foreach ($operationsFilters as $operationName => $operations) { + $message = "Add operation ${operationName} with operation " . json_encode($operations); $this->addOperationsFilter( $operationName, $operations diff --git a/src/Filters/Block.php b/src/Filters/Block.php index d9924de81..7c3ca14dc 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -989,7 +989,25 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) $categories[$value['id_category']] = $value; } - $results = $filteredSearchAdapter->valueCount('id_category'); + $categoryCount = null !== _PS_FACETED_NEWCOUNT_ ? _PS_FACETED_NEWCOUNT_ : true; + $results = $filteredSearchAdapter->valueCount('id_category', $categoryCount); + + $categoriesId = []; + if ($categoryCount === true) { + $query = new \DbQuery(); + $query->select('id_category'); + $query->from('category'); + $depth = (int) $parent->level_depth + 1; + $nleft = $parent->nleft; + $nright = $parent->nright; + $query->where('level_depth <= ' . $depth); + $query->where('nleft > ' . $nleft); + $query->where('nright < ' . $nright); + $resultCategories = \Db::getInstance()->executeS($query); + foreach($resultCategories as $oneCategory) { + $categoriesId[] = $oneCategory['id_category']; + } + } foreach ($results as $key => $values) { $idCategory = $values['id_category']; @@ -999,6 +1017,10 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) continue; } + if ($categoryCount && empty($categoriesId) === false && in_array($idCategory, $categoriesId) === false) { + continue; + } + $count = $values['c']; $categoryArray[$idCategory] = [ 'name' => $categories[$idCategory]['name'], From 845325ade2e85a8baa7bba035bb0e320c594a626 Mon Sep 17 00:00:00 2001 From: kgleizes Date: Fri, 25 Apr 2025 18:13:54 +0200 Subject: [PATCH 2/4] Remove debug message and update id_group condition --- src/Adapter/MySQL.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Adapter/MySQL.php b/src/Adapter/MySQL.php index 43f8dfceb..947130535 100644 --- a/src/Adapter/MySQL.php +++ b/src/Adapter/MySQL.php @@ -131,7 +131,7 @@ public function getQuery() if ($this->categoryCount === true) { foreach($whereConditions as $key => $oneCondition) { - if ($oneCondition === "p.id_group='1'") { + if (strpos($oneCondition, 'p.id_group') !== false) { unset($whereConditions[$key]); } if (strpos($oneCondition, 'p.nleft') !== false || strpos($oneCondition, 'p.nright') !== false) { @@ -858,7 +858,6 @@ protected function copyOperationsFilters() $operationsFilters = clone $initialPopulation->getOperationsFilters(); foreach ($operationsFilters as $operationName => $operations) { - $message = "Add operation ${operationName} with operation " . json_encode($operations); $this->addOperationsFilter( $operationName, $operations From 4dcb5e4ad5ae93a7f5c8ca8cc840d62d9f8efc75 Mon Sep 17 00:00:00 2001 From: kgleizes Date: Fri, 23 May 2025 10:11:06 +0200 Subject: [PATCH 3/4] Fix test constant not defined case. Code review. --- src/Filters/Block.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Filters/Block.php b/src/Filters/Block.php index 7c3ca14dc..ab1a00124 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -989,7 +989,10 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) $categories[$value['id_category']] = $value; } - $categoryCount = null !== _PS_FACETED_NEWCOUNT_ ? _PS_FACETED_NEWCOUNT_ : true; + $categoryCount = true; + if (true === defined('_PS_FACETED_NEWCOUNT_') && null !== _PS_FACETED_NEWCOUNT_) { + $categoryCount = (bool) _PS_FACETED_NEWCOUNT_; + } $results = $filteredSearchAdapter->valueCount('id_category', $categoryCount); $categoriesId = []; @@ -997,12 +1000,9 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) $query = new \DbQuery(); $query->select('id_category'); $query->from('category'); - $depth = (int) $parent->level_depth + 1; - $nleft = $parent->nleft; - $nright = $parent->nright; - $query->where('level_depth <= ' . $depth); - $query->where('nleft > ' . $nleft); - $query->where('nright < ' . $nright); + $query->where('level_depth <= ' . (int) $parent->level_depth + 1); + $query->where('nleft > ' . $parent->nleft); + $query->where('nright < ' . $parent->nright); $resultCategories = \Db::getInstance()->executeS($query); foreach($resultCategories as $oneCategory) { $categoriesId[] = $oneCategory['id_category']; From 39f82873b3df8839c459ae6a453657310b9d15e7 Mon Sep 17 00:00:00 2001 From: kgleizes Date: Tue, 5 Aug 2025 17:08:15 +0200 Subject: [PATCH 4/4] Fix categories without subcategory No count will be returned --- src/Filters/Block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filters/Block.php b/src/Filters/Block.php index ab1a00124..5be2ae411 100644 --- a/src/Filters/Block.php +++ b/src/Filters/Block.php @@ -1017,7 +1017,7 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent) continue; } - if ($categoryCount && empty($categoriesId) === false && in_array($idCategory, $categoriesId) === false) { + if ($categoryCount && in_array($idCategory, $categoriesId) === false) { continue; }