[Console] Now the group filter extract the searched groups and show their descendants

This commit is contained in:
Alejandro Gallardo Escobar 2017-12-05 09:43:37 +01:00
parent f572603882
commit 33ab429182
2 changed files with 51 additions and 36 deletions

View File

@ -57,31 +57,6 @@ class Tree {
}
public function setFilter($filter) {
// Filter the user groups
if (!empty($filter['groupID'])) {
$group_id = $filter['groupID'];
$this->userGroups = isset($this->userGroupsACL[$group_id])
? array($group_id => $this->userGroupsACL[$group_id])
: array();
}
else if (!empty($filter['searchGroup'])) {
$groups = db_get_all_rows_filter('tgrupo', array('nombre' => '%' . $filter['searchGroup'] . '%'));
// Save the groups which intersect
$userGroupsACL = $this->userGroupsACL;
$this->userGroups = array_reduce($groups, function ($userGroups, $group) use ($userGroupsACL) {
$group_id = $group['id_grupo'];
if (isset($userGroupsACL[$group_id])) {
$userGroups[$group_id] = $userGroupsACL[$group_id];
}
return $userGroups;
}, array());
}
else {
$this->userGroups = $this->userGroupsACL;
}
$this->filter = $filter;
}
@ -1877,6 +1852,25 @@ class Tree {
}
}
private static function extractGroupsWithIDs ($groups, $ids_hash) {
$result_groups = array();
foreach ($groups as $group) {
if (isset($ids_hash[$group['id']])) {
$result_groups[] = $group;
}
else if (!empty($group['children'])) {
$result = self::extractGroupsWithIDs($group['children'], $ids_hash);
// Item found on children
if (!empty($result)) {
$result_groups = array_merge($result_groups, $result);
}
}
}
return $result_groups;
}
private static function extractItemWithID ($items, $item_id, $item_type = "group", $strictACL = false) {
foreach ($items as $item) {
if ($item["type"] != $item_type)
@ -2166,14 +2160,33 @@ class Tree {
$processed_items = $this->getProcessedGroups($items, true);
// groupID filter. To access the view from tactical views f.e.
if (!empty($processed_items) && !empty($this->filter['groupID'])) {
$result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group", $this->strictACL);
if (!empty($processed_items)) {
// Filter by group name. This should be done after rerieving the items cause we need the possible items descendants
if (!empty($this->filter['searchGroup'])) {
// Save the groups which intersect with the user groups
$groups = db_get_all_rows_filter('tgrupo', array('nombre' => '%' . $this->filter['searchGroup'] . '%'));
if ($groups == false) $groups = array();
$userGroupsACL = $this->userGroupsACL;
$ids_hash = array_reduce($groups, function ($userGroups, $group) use ($userGroupsACL) {
$group_id = $group['id_grupo'];
if (isset($userGroupsACL[$group_id])) {
$userGroups[$group_id] = $userGroupsACL[$group_id];
}
if ($result === false)
$processed_items = array();
else
$processed_items = array($result);
return $userGroups;
}, array());
$result = self::extractGroupsWithIDs($processed_items, $ids_hash);
$processed_items = ($result === false) ? array() : $result;
}
// groupID filter. To access the view from tactical views f.e.
if (!empty($this->filter['groupID'])) {
$result = self::extractItemWithID($processed_items, $this->filter['groupID'], "group", $this->strictACL);
$processed_items = ($result === false) ? array() : array($result);
}
}
}
// Agents

View File

@ -135,11 +135,13 @@ $agent_status_arr[AGENT_STATUS_CRITICAL] = __('Critical');
$agent_status_arr[AGENT_STATUS_UNKNOWN] = __('Unknown');
$agent_status_arr[AGENT_STATUS_NOT_INIT] = __('Not init');
$row = array();
$row[] = __('Search group');
$row[] = html_print_input_text("search_group", $search_group, '', is_metaconsole() ? 70 : 40, 30, true);
if ($tab == 'group' || is_metaconsole()) {
$row = array();
$row[] = __('Search group');
$row[] = html_print_input_text("search_group", $search_group, '', is_metaconsole() ? 70 : 40, 30, true);
$table->data[] = $row;
$table->data[] = $row;
}
$row = array();
$row[] = __('Search agent');