2018-09-18 09:28:22 +02:00
|
|
|
<?php
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Tree view.
|
|
|
|
*
|
|
|
|
* @category Tree
|
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage Community
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
|
|
|
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
|
|
|
|
* Please see http://pandorafms.org for full contribution list
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation for version 2.
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
|
|
|
|
2018-09-18 09:28:22 +02:00
|
|
|
global $config;
|
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
require_once $config['homedir'].'/include/class/Tree.class.php';
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Tree group edition.
|
|
|
|
*/
|
2019-01-30 16:18:44 +01:00
|
|
|
class TreeGroupEdition extends TreeGroup
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Construct.
|
|
|
|
*
|
|
|
|
* @param string $type Type.
|
|
|
|
* @param string $rootType Root.
|
|
|
|
* @param integer $id Id.
|
|
|
|
* @param integer $rootID Root Id.
|
|
|
|
* @param boolean $serverID Server.
|
|
|
|
* @param string $childrenMethod Method children.
|
|
|
|
* @param string $access Access ACL.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$type,
|
|
|
|
$rootType='',
|
|
|
|
$id=-1,
|
|
|
|
$rootID=-1,
|
|
|
|
$serverID=false,
|
|
|
|
$childrenMethod='on_demand',
|
|
|
|
$access='AR'
|
|
|
|
) {
|
2019-01-30 16:18:44 +01:00
|
|
|
global $config;
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
parent::__construct(
|
|
|
|
$type,
|
|
|
|
$rootType,
|
|
|
|
$id,
|
|
|
|
$rootID,
|
|
|
|
$serverID,
|
|
|
|
$childrenMethod,
|
|
|
|
$access
|
|
|
|
);
|
2019-01-30 16:18:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Get data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-01-30 16:18:44 +01:00
|
|
|
protected function getData()
|
|
|
|
{
|
|
|
|
if ($this->id == -1) {
|
|
|
|
$this->getFirstLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Get process group.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-01-30 16:18:44 +01:00
|
|
|
protected function getProcessedGroups()
|
|
|
|
{
|
|
|
|
$processed_groups = [];
|
2021-03-15 13:31:24 +01:00
|
|
|
// Index and process the groups.
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups = $this->getGroupCounters();
|
|
|
|
|
|
|
|
// If user have not permissions in parent, set parent node to 0 (all)
|
2021-03-15 13:31:24 +01:00
|
|
|
// Avoid to do foreach for admins.
|
|
|
|
if (users_can_manage_group_all('AR') === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
foreach ($groups as $id => $group) {
|
2021-03-15 13:31:24 +01:00
|
|
|
if (isset($this->userGroups[$groups[$id]['parent']]) === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups[$id]['parent'] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
// Build the group hierarchy.
|
2019-01-30 16:18:44 +01:00
|
|
|
foreach ($groups as $id => $group) {
|
2021-03-15 13:31:24 +01:00
|
|
|
if (isset($groups[$id]['parent']) === true
|
|
|
|
&& ($groups[$id]['parent'] != 0)
|
|
|
|
) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$parent = $groups[$id]['parent'];
|
2021-03-15 13:31:24 +01:00
|
|
|
// Parent exists.
|
|
|
|
if (isset($groups[$parent]['children']) === false) {
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups[$parent]['children'] = [];
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
// Store a reference to the group into the parent.
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups[$parent]['children'][] = &$groups[$id];
|
2021-03-15 13:31:24 +01:00
|
|
|
// This group was introduced into a parent.
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups[$id]['have_parent'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
// Sort the children groups.
|
2019-01-30 16:18:44 +01:00
|
|
|
foreach ($groups as $id => $group) {
|
2021-03-15 13:31:24 +01:00
|
|
|
if (isset($groups[$id]['children']) === true) {
|
2019-01-30 16:18:44 +01:00
|
|
|
usort($groups[$id]['children'], ['Tree', 'cmpSortNames']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
// Filter groups and eliminates the reference
|
|
|
|
// to children groups out of her parent.
|
2019-01-30 16:18:44 +01:00
|
|
|
$groups = array_filter(
|
|
|
|
$groups,
|
|
|
|
function ($group) {
|
|
|
|
return !$group['have_parent'];
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
usort($groups, ['Tree', 'cmpSortNames']);
|
|
|
|
return $groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
/**
|
|
|
|
* Get group counters.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-01-30 16:18:44 +01:00
|
|
|
protected function getGroupCounters()
|
|
|
|
{
|
|
|
|
$messages = [
|
|
|
|
'confirm' => __('Confirm'),
|
|
|
|
'cancel' => __('Cancel'),
|
|
|
|
'messg' => __('Are you sure?'),
|
|
|
|
];
|
2021-03-15 13:31:24 +01:00
|
|
|
|
|
|
|
$group_acl = '';
|
|
|
|
if (users_can_manage_group_all('AR') === false) {
|
|
|
|
$user_groups_str = implode(',', $this->userGroupsArray);
|
|
|
|
$group_acl = sprintf(
|
|
|
|
'AND id_grupo IN (%s)',
|
|
|
|
$user_groups_str
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = sprintf(
|
|
|
|
'SELECT id_grupo AS gid,
|
|
|
|
nombre as name,
|
|
|
|
parent,
|
|
|
|
icon
|
2018-09-18 09:28:22 +02:00
|
|
|
FROM tgrupo
|
2021-03-15 13:31:24 +01:00
|
|
|
WHERE 1=1 %s',
|
|
|
|
$group_acl
|
|
|
|
);
|
2018-09-18 09:28:22 +02:00
|
|
|
|
2019-01-30 16:18:44 +01:00
|
|
|
$stats = db_get_all_rows_sql($sql);
|
|
|
|
$group_stats = [];
|
|
|
|
foreach ($stats as $group) {
|
|
|
|
$group_stats[$group['gid']]['name'] = $group['name'];
|
|
|
|
$group_stats[$group['gid']]['parent'] = $group['parent'];
|
|
|
|
$group_stats[$group['gid']]['icon'] = $group['icon'];
|
|
|
|
$group_stats[$group['gid']]['id'] = $group['gid'];
|
|
|
|
$group_stats[$group['gid']]['type'] = 'group';
|
2018-09-18 09:28:22 +02:00
|
|
|
|
2021-03-15 13:31:24 +01:00
|
|
|
$group_stats[$group['gid']] = $this->getProcessedItem(
|
|
|
|
$group_stats[$group['gid']]
|
|
|
|
);
|
2019-01-30 16:18:44 +01:00
|
|
|
$group_stats[$group['gid']]['delete']['messages'] = $messages;
|
|
|
|
$group_stats[$group['gid']]['edit'] = 1;
|
|
|
|
$group_stats[$group['gid']]['alerts'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $group_stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|