fix tree view hierarchy search pandora_enterprise#13421

This commit is contained in:
daniel 2024-05-20 19:52:12 +02:00
parent be8c5e1a3c
commit 01d0e23a30
2 changed files with 62 additions and 72 deletions

View File

@ -160,51 +160,19 @@ class TreeGroup extends Tree
$processed_items = $this->getProcessedGroups(); $processed_items = $this->getProcessedGroups();
if (empty($processed_items) === false) { if (empty($processed_items) === false) {
// Filter by group name. This should be done after rerieving
// the items cause we need the possible items descendants.
if (empty($this->filter['searchGroup']) === false) { if (empty($this->filter['searchGroup']) === false) {
$parentGroup = [
'name' => '',
'children' => $processed_items,
];
if (isset($this->filter['searchHirearchy']) === true if (isset($this->filter['searchHirearchy']) === true
&& (bool) $this->filter['searchHirearchy'] === true && (bool) $this->filter['searchHirearchy'] === true
) { ) {
$result = array_filter( self::getFullHierarchyGroups($parentGroup, $this->filter['searchGroup']);
$processed_items, $result = $parentGroup['children'];
function ($group) {
return self::getFullHirearchyGroups(
$group,
$this->filter['searchGroup']
);
}
);
// Reindex result.
$result = array_values($result);
} else { } else {
// Save the groups which intersect with the user groups. $result = self::pruneHierarchyGroups($parentGroup, $this->filter['searchGroup']);
$groups = db_get_all_rows_filter(
'tgrupo',
['nombre' => '%'.$this->filter['searchGroup'].'%']
);
if ($groups === false) {
$groups = [];
}
$userGroupsACL = $this->userGroupsACL;
$ids_hash = array_reduce(
$groups,
function ($userGroups, $group) use ($userGroupsACL) {
$group_id = $group['id_grupo'];
if (isset($userGroupsACL[$group_id]) === true) {
$userGroups[$group_id] = $userGroupsACL[$group_id];
}
return $userGroups;
},
[]
);
$result = self::extractGroupsWithIDs(
$processed_items,
$ids_hash
);
} }
$processed_items = ($result === false) ? [] : $result; $processed_items = ($result === false) ? [] : $result;
@ -580,41 +548,61 @@ class TreeGroup extends Tree
} }
private static function extractGroupsWithIDs($groups, $ids_hash) /**
{ * Get all hierarchy groups.
$result_groups = []; *
foreach ($groups as $group) { * @param array $group Groups.
if (isset($ids_hash[$group['id']])) { * @param string $search Serach.
$result_groups[] = $group; *
} else if (!empty($group['children'])) { * @return boolean
$result = self::extractGroupsWithIDs($group['children'], $ids_hash); */
protected static function getFullHierarchyGroups(&$group, $search): bool
// Item found on children
if (!empty($result)) {
$result_groups = array_merge($result_groups, $result);
}
}
}
return $result_groups;
}
protected function getFullHirearchyGroups($group, $search)
{ {
if (stripos($group['name'], $search) !== false) { if (stripos($group['name'], $search) !== false) {
return true; return true;
}
if (empty($group['children'] ?? []) === true) {
return false;
}
$has_occurrences = false;
$count_children = count($group['children']);
for ($i = 0; $i < $count_children; $i++) {
if (self::getFullHierarchyGroups($group['children'][$i], $search) === false) {
unset($group['children'][$i]);
} else { } else {
if (isset($group['children']) === true && empty($group['children']) === false) { $has_occurrences = true;
foreach ($group['children'] as $group_child) {
if ($this->getFullHirearchyGroups($group_child, $search) === true) {
return true;
}
}
} }
} }
return false; return $has_occurrences;
}
/**
* Get prune groups.
*
* @param array $group Groups.
* @param string $search Serach.
*
* @return array
*/
protected static function pruneHierarchyGroups($group, $search): array
{
$matches = [];
foreach (($group['children'] ?? []) as $child) {
if (stripos($child['name'], $search) !== false) {
$matches[] = $child;
} else {
$matches = [
...$matches,
...self::pruneHierarchyGroups($child, $search),
];
}
}
return $matches;
} }

View File

@ -197,7 +197,7 @@ function users_get_groups_for_select(
* *
* @return array * @return array
*/ */
function get_group_ancestors($group_id, $groups) function get_group_ancestors($group_id, $groups, $forceNotPropagate=false)
{ {
if ($group_id == 0) { if ($group_id == 0) {
return 0; return 0;
@ -209,9 +209,11 @@ function get_group_ancestors($group_id, $groups)
$parent = $groups[$group_id]['parent']; $parent = $groups[$group_id]['parent'];
if ($forceNotPropagate === false) {
if ($groups[$group_id]['propagate'] == 0) { if ($groups[$group_id]['propagate'] == 0) {
return $group_id; return $group_id;
} }
}
$r = get_group_ancestors($parent, $groups); $r = get_group_ancestors($parent, $groups);