Skip to content
Open
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
25 changes: 23 additions & 2 deletions src/Adapter/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class MySQL extends AbstractAdapter
*/
const INNER_JOIN = 'INNER JOIN';

/** @var bool */
private $categoryCount = false;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -122,6 +131,18 @@ public function getQuery()
}
}


if ($this->categoryCount === true) {
foreach($whereConditions as $key => $oneCondition) {
if (strpos($oneCondition, 'p.id_group') !== false) {
unset($whereConditions[$key]);
}
if (strpos($oneCondition, 'p.nleft') !== false || strpos($oneCondition, 'p.nright') !== false) {
unset($whereConditions[$key]);
}
}
}

// Add where conditions if any
if (!empty($whereConditions)) {
$query .= ' WHERE ' . implode(' AND ', $whereConditions);
Expand Down Expand Up @@ -792,8 +813,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);
Expand All @@ -804,7 +826,6 @@ public function valueCount($fieldName = null)
$this->setOrderField('');

$this->copyOperationsFilters();

return $this->execute();
}

Expand Down
24 changes: 23 additions & 1 deletion src/Filters/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,25 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent)
$categories[$value['id_category']] = $value;
}

$results = $filteredSearchAdapter->valueCount('id_category');
$categoryCount = true;
if (true === defined('_PS_FACETED_NEWCOUNT_') && null !== _PS_FACETED_NEWCOUNT_) {
$categoryCount = (bool) _PS_FACETED_NEWCOUNT_;
}
$results = $filteredSearchAdapter->valueCount('id_category', $categoryCount);

$categoriesId = [];
if ($categoryCount === true) {
$query = new \DbQuery();
$query->select('id_category');
$query->from('category');
$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'];
}
}

foreach ($results as $key => $values) {
$idCategory = $values['id_category'];
Expand All @@ -999,6 +1017,10 @@ private function getCategoriesBlock($filter, $selectedFilters, $idLang, $parent)
continue;
}

if ($categoryCount && in_array($idCategory, $categoriesId) === false) {
continue;
}

$count = $values['c'];
$categoryArray[$idCategory] = [
'name' => $categories[$idCategory]['name'],
Expand Down