Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.4] Smart Search: Properly sort filters #42835

Merged
merged 4 commits into from
Mar 5, 2024
Merged
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
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;
}
}