Skip to content

Commit

Permalink
[4.4] Smart Search: Properly sort filters (#42835)
Browse files Browse the repository at this point in the history
* Smart Search: Properly sort filters

* Update administrator/components/com_finder/src/Service/HTML/Filter.php

Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>

---------

Co-authored-by: Martin Carl Kopp <6154099+MacJoom@users.noreply.github.com>
Co-authored-by: Allon Moritz <allon.moritz@digital-peak.com>
  • Loading branch information
3 people committed Mar 5, 2024
1 parent 0a912a8 commit ab3d32c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion administrator/components/com_finder/src/Service/HTML/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function select($idxQuery, $options)
->where('t.rgt < :rgt')
->where('t.state = 1')
->whereIn('t.access', $user->getAuthorisedViewLevels())
->order('t.title')
->order('t.level, t.parent_id, t.title')
->bind(':lft', $bv->lft, ParameterType::INTEGER)
->bind(':rgt', $bv->rgt, ParameterType::INTEGER);

Expand Down Expand Up @@ -327,6 +327,7 @@ public function select($idxQuery, $options)

// Translate branch nodes if possible.
$language = Factory::getLanguage();
$root = [];

foreach ($branches[$bk]->nodes as $node_id => $node) {
if (trim($node->parent_title, '*') === 'Language') {
Expand All @@ -340,9 +341,19 @@ public function select($idxQuery, $options)
$branches[$bk]->nodes[$node_id]->title = str_repeat('-', $node->level - 2) . $title;
} else {
$branches[$bk]->nodes[$node_id]->title = $title;
$root[] = $branches[$bk]->nodes[$node_id];
}

if ($node->parent_id && isset($branches[$bk]->nodes[$node->parent_id])) {
if (!isset($branches[$bk]->nodes[$node->parent_id]->children)) {
$branches[$bk]->nodes[$node->parent_id]->children = [];
}
$branches[$bk]->nodes[$node->parent_id]->children[] = $node;
}
}

$branches[$bk]->nodes = $this->reduce($root);

// Add the Search All option to the branch.
array_unshift($branches[$bk]->nodes, ['id' => null, 'title' => Text::_('COM_FINDER_FILTER_SELECT_ALL_LABEL')]);
}
Expand Down Expand Up @@ -487,4 +498,27 @@ public function dates($idxQuery, $options)

return $html;
}

/**
* Method to flatten a tree to a sorted array
*
* @param \stdClass[] $array
*
* @return \stdClass[] Flat array of all nodes of a tree with the children after each parent
*
* @since __DEPLOY_VERSION__
*/
private function reduce(array $array)
{
$return = [];

foreach ($array as $item) {
$return[] = $item;
if (isset($item->children)) {
$return = array_merge($return, $this->reduce($item->children));
}
}

return $return;
}
}

0 comments on commit ab3d32c

Please sign in to comment.