__x('dropdown action', 'Delete'),
2 => __x('dropdown action', 'Publish'),
3 => __x('dropdown action', 'Un-Publish'),
4 => __x('dropdown action', 'Un-Lock')
];
// set default action
set_default_action();
if (grv('action') != '') {
// ================= input validation and session storage =================
$filters = [
'tree_id' => [
'filter' => FILTER_VALIDATE_INT,
'default' => ''
],
'leaf_id' => [
'filter' => FILTER_VALIDATE_INT,
'default' => ''
],
'graph_tree_id' => [
'filter' => FILTER_VALIDATE_INT,
'default' => ''
],
'parent_item_id' => [
'filter' => FILTER_VALIDATE_INT,
'default' => ''
],
'parent' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '/([_\-a-z:0-9#]+)/']],
'pageset' => true,
'default' => ''
],
'position' => [
'filter' => FILTER_VALIDATE_INT,
'default' => ''
],
'nodeid' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '/([_\-a-z:0-9#]+)/']],
'pageset' => true,
'default' => ''
],
'id' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '/([_\-a-z:0-9#]+)/']],
'pageset' => true,
'default' => ''
]
];
validate_store_request_vars($filters);
// ================= input validation =================
}
if (in_array(grv('action'), [
'sortasc', 'sortdesc', 'ajax_dnd',
'lock', 'unlock', 'set_host_sort',
'set_branch_sort',
], true)) {
csrf_require_post();
}
switch (grv('action')) {
case 'save':
form_save();
break;
case 'actions':
form_actions();
break;
case 'sortasc':
tree_sort_name_asc();
header('Location: tree.php');
break;
case 'sortdesc':
tree_sort_name_desc();
header('Location: tree.php');
break;
case 'edit':
top_header();
tree_edit();
bottom_footer();
break;
case 'sites':
display_sites();
break;
case 'hosts':
display_hosts();
break;
case 'graphs':
display_graphs();
break;
case 'tree_up':
tree_up();
break;
case 'tree_down':
tree_down();
break;
case 'ajax_dnd':
tree_dnd();
break;
case 'lock':
// GHSA-73qf-hq23-c4m7: lock/unlock/get_node reached api_tree_* with no owner check; gate them like the node write actions.
if (!is_tree_allowed((int) grv('id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_lock(grv('id'), $_SESSION[SESS_USER_ID]);
tree_edit(true);
break;
case 'unlock':
if (!is_tree_allowed((int) grv('id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_unlock(grv('id'), $_SESSION[SESS_USER_ID]);
tree_edit(true);
break;
case 'copy_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_copy_node(grv('tree_id'), grv('id'), grv('parent'), grv('position'));
break;
case 'create_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_create_node(grv('tree_id'), grv('id'), grv('position'), gnrv('text'));
break;
case 'delete_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_delete_node(grv('tree_id'), grv('id'));
break;
case 'move_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_move_node(grv('tree_id'), grv('id'), grv('parent'), grv('position'));
break;
case 'rename_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to modify this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_rename_node(grv('tree_id'), grv('id'), gnrv('text'));
break;
case 'get_node':
if (!is_tree_allowed((int) grv('tree_id'))) {
raise_message('tree_idor', __('You do not have permission to view this tree.'), MESSAGE_LEVEL_ERROR);
header('Location: tree.php');
exit;
}
api_tree_get_node(grv('tree_id'), grv('id'));
break;
case 'get_host_sort':
get_host_sort_type();
break;
case 'set_host_sort':
set_host_sort_type();
break;
case 'get_branch_sort':
get_branch_sort_type();
break;
case 'set_branch_sort':
set_branch_sort_type();
break;
default:
top_header();
tree();
bottom_footer();
break;
}
function tree_check_sequences() : void {
$bad_seq = db_fetch_cell('SELECT COUNT(sequence)
FROM graph_tree
WHERE sequence <= 0');
$dup_seq = db_fetch_cell('SELECT SUM(count)
FROM (
SELECT sequence, COUNT(sequence) AS count
FROM graph_tree
GROUP BY sequence
) AS t
WHERE t.count > 1');
// report any bad or duplicate sequences to the log for reporting purposes
if ($bad_seq > 0) {
cacti_log('WARN: Found ' . $bad_seq . ' Sequences in graph_tree Table', false, 'TREE', POLLER_VERBOSITY_HIGH);
}
if ($dup_seq > 0) {
cacti_log('WARN: Found ' . $dup_seq . ' Sequences in graph_tree Table', false, 'TREE', POLLER_VERBOSITY_HIGH);
}
if ($bad_seq > 0 || $dup_seq > 0) {
// resequence the list so it has no gaps, and 0 values will appear at the top
// since that's where they would have been displayed
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY sequence, id;');
}
}
function tree_sort_name_asc() : void {
// resequence the list so it has no gaps, alphabetically ascending
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY name;');
}
function tree_sort_name_desc() : void {
// resequence the list so it has no gaps, alphabetically ascending
db_execute('SET @seq = 0; UPDATE graph_tree SET sequence = (@seq:=@seq+1) ORDER BY name DESC;');
}
function tree_down() : void {
tree_check_sequences();
$tree_id = gfrv('id');
$seq = db_fetch_cell_prepared('SELECT sequence
FROM graph_tree
WHERE id = ?',
[$tree_id]);
$new_seq = $seq + 1;
// update the old tree first
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE sequence = ?',
[$seq, $new_seq]);
// update the tree in question
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
[$new_seq, $tree_id]);
header('Location: tree.php');
exit;
}
function tree_up() : void {
tree_check_sequences();
$tree_id = gfrv('id');
$seq = db_fetch_cell_prepared('SELECT sequence
FROM graph_tree
WHERE id = ?',
[$tree_id]);
$new_seq = $seq - 1;
// update the old tree first
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE sequence = ?',
[$seq, $new_seq]);
// update the tree in question
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
[$new_seq, $tree_id]);
header('Location: tree.php');
exit;
}
function tree_dnd() : void {
if (isrv('tree_ids') && is_array(gnrv('tree_ids'))) {
$tids = gnrv('tree_ids');
$sequence = 1;
foreach ($tids as $id) {
$id = str_replace('line', '', $id);
input_validate_input_number($id, 'id');
db_execute_prepared('UPDATE graph_tree
SET sequence = ?
WHERE id = ?',
[$sequence, $id]);
$sequence++;
}
/**
* Save the last time a tree branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_branch', time());
}
header('Location: tree.php');
exit;
}
function get_host_sort_type() : void {
if (isrv('nodeid')) {
$ndata = explode('_', grv('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if ($parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
$sort_type = db_fetch_cell_prepared('SELECT host_grouping_type
FROM graph_tree_items
WHERE id = ?',
[$branch]);
if ($sort_type == HOST_GROUPING_GRAPH_TEMPLATE) {
print 'hsgt';
} else {
print 'hsdq';
}
}
}
}
}
}
function set_host_sort_type() : void {
$type = '';
$branch = '';
// clean up type string
if (isrv('type')) {
srv('type', sanitize_search_string(grv('type')));
}
if (isrv('nodeid')) {
$ndata = explode('_', grv('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if ($parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
if (grv('type') == 'hsgt') {
$type = HOST_GROUPING_GRAPH_TEMPLATE;
} else {
$type = HOST_GROUPING_DATA_QUERY_INDEX;
}
db_execute_prepared('UPDATE graph_tree_items
SET host_grouping_type = ?
WHERE id = ?',
[$type, $branch]);
break;
}
}
}
}
}
function get_branch_sort_type() : void {
if (isrv('nodeid')) {
$ndata = explode('_', grv('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if ($parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
$sort_type = db_fetch_cell_prepared('SELECT sort_children_type
FROM graph_tree_items
WHERE id = ?',
[$branch]);
switch($sort_type) {
case TREE_ORDERING_INHERIT:
print __x('ordering of tree items', 'inherit');
break;
case TREE_ORDERING_NONE:
print __x('ordering of tree items', 'manual');
break;
case TREE_ORDERING_ALPHABETIC:
print __x('ordering of tree items', 'alpha');
break;
case TREE_ORDERING_NATURAL:
print __x('ordering of tree items', 'natural');
break;
case TREE_ORDERING_NUMERIC:
print __x('ordering of tree items', 'numeric');
break;
default:
print '';
break;
}
break;
}
}
}
} else {
print '';
}
}
function set_branch_sort_type() : void {
$type = '';
$branch = '';
// clean up type string
if (isrv('type')) {
srv('type', sanitize_search_string(grv('type')));
}
if (isrv('nodeid')) {
$ndata = explode('_', grv('nodeid'));
if (cacti_sizeof($ndata)) {
foreach ($ndata as $n) {
$parts = explode(':', $n);
if ($parts[0] == 'tbranch') {
$branch = $parts[1];
input_validate_input_number($branch, 'branch');
switch(grv('type')) {
case 'inherit':
$type = TREE_ORDERING_INHERIT;
break;
case 'manual':
$type = TREE_ORDERING_NONE;
break;
case 'alpha':
$type = TREE_ORDERING_ALPHABETIC;
break;
case 'natural':
$type = TREE_ORDERING_NATURAL;
break;
case 'numeric':
$type = TREE_ORDERING_NUMERIC;
break;
default:
break;
}
if (is_numeric($type) && is_numeric($branch)) {
db_execute_prepared('UPDATE graph_tree_items
SET sort_children_type = ?
WHERE id = ?',
[$type, $branch]);
}
$first_child = db_fetch_row_prepared('SELECT id, graph_tree_id
FROM graph_tree_items
WHERE parent = ?
ORDER BY position
LIMIT 1',
[$branch]);
if (!empty($first_child)) {
api_tree_sort_branch($first_child['id'], $first_child['graph_tree_id']);
}
break;
}
}
}
}
}
function form_save() : void {
// clear graph tree cache on save - affects current user only, other users should see changes in <5 minutes
if (isset($_SESSION['dhtml_tree'])) {
unset($_SESSION['dhtml_tree']);
}
if (isrv('save_component_tree')) {
// ================= input validation =================
gfrv('id');
gfrv('sequence');
// ====================================================
if (gfrv('id') > 0) {
$prev_order = db_fetch_cell_prepared('SELECT sort_type
FROM graph_tree
WHERE id = ?',
[grv('id')]);
} else {
$prev_order = 1;
}
$save['id'] = grv('id');
$save['name'] = form_input_validate(gnrv('name'), 'name', '', false, 3);
$save['sort_type'] = form_input_validate(gnrv('sort_type'), 'sort_type', '', true, 3);
$save['last_modified'] = date('Y-m-d H:i:s', time());
$save['enabled'] = grv('enabled') == 'true' || grv('enabled') == 'on' ? 'on' : 'off';
$save['modified_by'] = $_SESSION[SESS_USER_ID];
if (ierv('sequence')) {
$save['sequence'] = api_tree_get_max_sequence() + 1;
} else {
$save['sequence'] = grv('sequence');
}
if (empty($save['id'])) {
$save['user_id'] = $_SESSION[SESS_USER_ID];
}
if (!is_error_message()) {
$tree_id = sql_save($save, 'graph_tree');
if ($tree_id) {
raise_message(1);
// sort the tree using the algorithm chosen by the user
if ($save['sort_type'] != $prev_order) {
if ($save['sort_type'] != TREE_ORDERING_NONE) {
sort_recursive(0, $tree_id);
}
}
if (empty($save['id'])) {
/**
* Save the last time a tree was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
}
} else {
raise_message(2);
}
}
header('Location: tree.php?action=edit&id=' . (isset($tree_id) ? $tree_id : ''));
exit;
}
}
function sort_recursive(int $branch, int $tree_id) : void {
$leaves = db_fetch_assoc_prepared('SELECT *
FROM graph_tree_items
WHERE graph_tree_id = ?
AND parent = ?
AND local_graph_id = 0
AND host_id = 0',
[$tree_id, $branch]);
if (cacti_sizeof($leaves)) {
foreach ($leaves as $leaf) {
if ($leaf['sort_children_type'] == TREE_ORDERING_INHERIT) {
$first_child = db_fetch_cell_prepared('SELECT id
FROM graph_tree_items
WHERE parent = ?',
[$leaf['id']]);
if (!empty($first_child)) {
api_tree_sort_branch($first_child, $tree_id);
if (leaves_exist($leaf['id'], $tree_id)) {
sort_recursive($first_child, $tree_id);
}
}
}
}
}
}
function leaves_exist(int $parent, int $tree_id) : int {
return (int) db_fetch_cell_prepared('SELECT COUNT(*)
FROM graph_tree_items
WHERE graph_tree_id = ?
AND parent = ?
AND local_graph_id = 0
AND host_id = 0',
[$tree_id, $parent]);
}
function form_actions() : void {
global $actions;
// ================= input validation =================
gfrv('drp_action', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^([a-zA-Z0-9_]+)$/']]);
// ====================================================
// if we are to save this form, instead of display it
if (isrv('selected_items')) {
$selected_items = sanitize_unserialize_selected_items(gnrv('selected_items'));
if ($selected_items != false) {
if (gnrv('drp_action') == '1') { // delete
db_execute('DELETE FROM graph_tree WHERE ' . array_to_sql_or($selected_items, 'id'));
db_execute('DELETE FROM graph_tree_items WHERE ' . array_to_sql_or($selected_items, 'graph_tree_id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (gnrv('drp_action') == '2') { // publish
db_execute("UPDATE graph_tree
SET enabled='on',
last_modified=NOW(),
modified_by=" . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (gnrv('drp_action') == '3') { // un-publish
db_execute("UPDATE graph_tree
SET enabled='off',
last_modified=NOW(),
modified_by=" . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
/**
* Save the last time a tree or branch was created/updated
* for Caching.
*/
set_config_option('time_last_change_tree', time());
set_config_option('time_last_change_branch', time());
} elseif (gnrv('drp_action') == '4') { // un-lock
db_execute('UPDATE graph_tree
SET locked=0,
last_modified=NOW(),
modified_by=' . $_SESSION[SESS_USER_ID] . '
WHERE ' . array_to_sql_or($selected_items, 'id'));
}
}
header('Location: tree.php');
exit;
} else {
$ilist = '';
$iarray = [];
// loop through each of the selected items
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
// ================= input validation =================
input_validate_input_number($matches[1], 'chk[1]');
// ====================================================
$ilist .= '
' . htmle(db_fetch_cell_prepared('SELECT name FROM graph_tree WHERE id = ?', [$matches[1]])) . '
';
$iarray[] = $matches[1];
}
}
$form_data = [
'general' => [
'page' => 'tree.php',
'actions' => $actions,
'optvar' => 'drp_action',
'item_array' => $iarray,
'item_list' => $ilist
],
'options' => [
1 => [
'smessage' => __('Click \'Continue\' to Delete the following Tree.'),
'pmessage' => __('Click \'Continue\' to Delete following Trees.'),
'scont' => __('Delete Tree'),
'pcont' => __('Delete Trees')
],
2 => [
'smessage' => __('Click \'Continue\' to Publish the following Tree.'),
'pmessage' => __('Click \'Continue\' to Publish following Trees.'),
'scont' => __('Publish Tree'),
'pcont' => __('Publish Trees')
],
3 => [
'smessage' => __('Click \'Continue\' to Un-Publish the following Tree.'),
'pmessage' => __('Click \'Continue\' to Un-Publish following Trees.'),
'scont' => __('Un-Publish Tree'),
'pcont' => __('Un-Publish Trees')
],
4 => [
'smessage' => __('Click \'Continue\' to Un-Lock the following Tree.'),
'pmessage' => __('Click \'Continue\' to Un-Lock following Trees.'),
'scont' => __('Un-Lock Tree'),
'pcont' => __('Un-Lock Trees')
],
]
];
form_continue_confirmation($form_data);
}
}
function tree_edit(bool $partial = false) : void {
global $fields_tree_edit;
// ================= input validation =================
gfrv('id');
gfrv('type');
// ====================================================
load_current_session_value('type', 'sess_tree_edit_type', '0');
if (!ierv('id')) {
$tree = db_fetch_row_prepared('SELECT *
FROM graph_tree
WHERE id = ?',
[grv('id')]);
$header_label = __esc('Trees [edit: %s]', $tree['name']);
// Reset the cookie state if tree id has changed
if (isset($_SESSION['sess_tree_id']) && $_SESSION['sess_tree_id'] != grv('id')) {
$select_first = true;
} else {
$select_first = false;
}
$_SESSION['sess_tree_id'] = grv('id');
} else {
$tree = [];
$header_label = __('Trees [new]');
}
print '
';
form_start('tree.php', 'tree_edit');
// Remove inherit from the main tree option
unset($fields_tree_edit['sort_type']['array'][0]);
html_start_box($header_label, '100%', true, 3, 'center', '');
if (!cacti_sizeof($tree)) {
unset($fields_tree_edit['enabled']);
}
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => inject_form_variables($fields_tree_edit, $tree)
]
);
html_end_box(true, true);
$lockdiv = '';
$editable = true;
if (isset($tree['locked']) && $tree['locked'] == 0) {
$lockdiv = "
" . __('To Edit this tree, you must first lock it by pressing the Edit Tree button.') . '
" . __('This tree has been locked for Editing on %s by %s.', $tree['locked_date'], get_username($tree['modified_by']));
if ($tree['modified_by'] == $_SESSION[SESS_USER_ID]) {
$lockdiv .= '
';
} else {
$editable = false;
$lockdiv .= __('To edit the tree, you must first unlock it and then lock it as yourself') . '
';
}
} else {
$tree['id'] = 0;
}
if ($editable) {
form_save_button('tree.php', 'return');
}
if (!ierv('id')) {
print $lockdiv;
}
print '';
if ($partial) {
return;
}
if (!ierv('id')) {
print "
\n";
}
}
}
function display_graphs() : void {
$sql_where = '';
$sql_params = [];
$site_ids = gfrv('site_id', FILTER_VALIDATE_IS_NUMERIC_LIST);
$host_ids = gfrv('host_id', FILTER_VALIDATE_IS_NUMERIC_LIST);
if (gnrv('filter') != '') {
$sql_where .= 'WHERE (title_cache LIKE ? OR gt.name LIKE ? OR hsc.field_value LIKE ?) AND local_graph_id > 0';
$sql_params[] = '%' . gnrv('filter') . '%';
$sql_params[] = '%' . gnrv('filter') . '%';
$sql_params[] = '%' . gnrv('filter') . '%';
} else {
$sql_where .= 'WHERE local_graph_id > 0';
}
if ($site_ids != '') {
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . 'h.site_id IN( ' . $site_ids . ')';
}
if ($host_ids != '') {
$sql_where .= ($sql_where != '' ? ' AND ' : 'WHERE ') . 'gl.host_id IN( ' . $host_ids . ')';
}
$graphs = db_fetch_assoc_prepared("SELECT gtg.local_graph_id AS id,
gtg.title_cache AS title, gt.name AS template_name,
GROUP_CONCAT(DISTINCT hsc.field_name) AS field_name,
GROUP_CONCAT(DISTINCT hsc.field_value) AS field_value
FROM graph_templates_graph AS gtg
LEFT JOIN graph_templates AS gt
ON gt.id = gtg.graph_template_id
LEFT JOIN graph_local AS gl
ON gtg.local_graph_id = gl.id
LEFT JOIN host as h
ON gl.host_id = h.id
LEFT JOIN host_snmp_cache AS hsc
ON hsc.host_id = h.id
AND hsc.snmp_query_id = gl.snmp_query_id
AND hsc.snmp_index = gl.snmp_index
$sql_where
GROUP BY gtg.local_graph_id
ORDER BY title_cache
LIMIT " . read_config_option('autocomplete_rows'),
$sql_params);
if (cacti_sizeof($graphs)) {
foreach ($graphs as $g) {
if (gnrv('filter') != '') {
$title = $g['title'];
if ($g['field_value'] != '' && stripos($g['field_value'], gnrv('filter')) !== false) {
$parts = explode(',', $g['field_value']);
$nparts = explode(',', $g['field_name']);
foreach ($parts as $index => $p) {
if ($p != '' && stripos($p, gnrv('filter')) !== false) {
$g['title'] .= " [ {$nparts[$index]}:$p ]";
}
}
}
}
if (is_graph_allowed($g['id'])) {
print "
" . htmle($g['title']) . '
';
}
}
}
}
function tree() : void {
global $actions, $item_rows;
$button1 = [
'method' => 'button',
'href' => 'tree.php?action=sortasc',
'callback' => true,
'title' => __esc('Sort Trees Ascending'),
'class' => 'ti ti-sort-ascending-letters'
];
$button2 = [
'method' => 'button',
'href' => 'tree.php?action=sortdesc',
'callback' => true,
'title' => __esc('Sort Trees Descending'),
'class' => 'ti ti-sort-descending-letters'
];
// create the page filter
$pageFilter = new CactiTableFilter(__('Trees'), 'tree.php', 'form_tree', 'sess_tree', 'tree.php?action=edit');
$pageFilter->rows_label = __('Trees');
$pageFilter->set_sort_array('sequence', 'ASC');
$pageFilter->add_button('sortasc', $button1);
$pageFilter->add_button('sortdesc', $button2);
$pageFilter->render();
// if the number of rows is -1, set it to the default
if (grv('rows') == -1) {
$rows = read_config_option('num_rows_table');
} else {
$rows = grv('rows');
}
$sql_where = '';
$sql_params = [];
// form the 'where' clause for our main sql query
if (grv('filter') != '') {
$sql_where .= 'WHERE (t.name LIKE ? OR ti.title LIKE ?)';
$sql_params[] = '%' . grv('filter') . '%';
$sql_params[] = '%' . grv('filter') . '%';
}
$sql_order = get_order_string();
$sql_limit = ' LIMIT ' . ($rows * (grv('page') - 1)) . ',' . $rows;
$trees = db_fetch_assoc_prepared("SELECT t.*,
SUM(CASE WHEN ti.host_id > 0 THEN 1 ELSE 0 END) AS hosts,
SUM(CASE WHEN ti.local_graph_id > 0 THEN 1 ELSE 0 END) AS graphs,
SUM(CASE WHEN ti.local_graph_id = 0 AND host_id = 0 AND site_id = 0 THEN 1 ELSE 0 END) AS branches,
SUM(CASE WHEN ti.site_id > 0 THEN 1 ELSE 0 END) AS sites
FROM graph_tree AS t
LEFT JOIN graph_tree_items AS ti
ON t.id=ti.graph_tree_id
$sql_where
GROUP BY t.id
$sql_order
$sql_limit",
$sql_params);
$sql = "SELECT COUNT(DISTINCT(t.id))
FROM graph_tree AS t
LEFT JOIN graph_tree_items AS ti
ON t.id=ti.graph_tree_id
$sql_where";
$total_rows = get_total_row_data($_SESSION[SESS_USER_ID], $sql, $sql_params, 'tree');
$nav = html_nav_bar('tree.php?filter=' . grv('filter'), MAX_DISPLAY_PAGES, grv('page'), $rows, $total_rows, 11, __('Trees'), 'page', 'main');
form_start('tree.php', 'chk');
print $nav;
html_start_box('', '100%', false, 3, 'center', '');
$display_text = [
'name' => [
'display' => __('Tree Name'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The name by which this Tree will be referred to as.')
],
'id' => [
'display' => __('ID'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The internal database ID for this Tree. Useful when performing automation or debugging.')
],
'enabled' => [
'display' => __('Published'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('Unpublished Trees cannot be viewed from the Graph tab')
],
'locked' => [
'display' => __('Locked'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('A Tree must be locked in order to be edited.')
],
'user_id' => [
'display' => __('Owner'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The original author of this Tree.')
],
'sequence' => [
'display' => __('Order'),
'align' => 'center',
'sort' => 'ASC',
'tip' => __('To change the order of the trees, first sort by this column, press the up or down arrows once they appear.')
],
'last_modified' => [
'display' => __('Last Edited'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The date that this Tree was last edited.')
],
'modified_by' => [
'display' => __('Edited By'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The last user to have modified this Tree.')
],
'sites' => [
'display' => __('Sites'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of Site Branches in this Tree.')
],
'branches' => [
'display' => __('Branches'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of Branches in this Tree.')
],
'hosts' => [
'display' => __('Devices'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of individual Devices in this Tree.')
],
'graphs' => [
'display' => __('Graphs'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('The total number of individual Graphs in this Tree.')
]
];
html_header_sort_checkbox($display_text, grv('sort_column'), grv('sort_direction'), false);
$i = 1;
if (cacti_sizeof($trees)) {
foreach ($trees as $tree) {
$sequence = '';
if (grv('sort_column') == 'sequence' && grv('sort_direction') == 'ASC') {
if ($i == 1 && cacti_sizeof($trees) == 1) {
$sequence .= '';
$sequence .= '';
} elseif ($i == 1) {
$sequence .= '';
$sequence .= '';
} elseif ($i == cacti_sizeof($trees)) {
$sequence .= '';
$sequence .= '';
} else {
$sequence .= '';
$sequence .= '';
}
}
form_alternate_row('line' . $tree['id'], true);
form_selectable_cell(filter_value($tree['name'], grv('filter'), 'tree.php?action=edit&id=' . $tree['id']), $tree['id']);
form_selectable_cell($tree['id'], $tree['id'], '', 'right');
form_selectable_cell($tree['enabled'] == 'on' ? __('Yes') : __('No'), $tree['id']);
form_selectable_cell($tree['locked'] == '1' ? __('Yes') : __('No'), $tree['id']);
form_selectable_cell(get_username($tree['user_id']), $tree['id']);
form_selectable_cell($sequence, $tree['id'], '', 'nowrap center');
form_selectable_cell(substr($tree['last_modified'],0,16), $tree['id'], '', 'right');
form_selectable_cell(get_username($tree['modified_by']), $tree['id'], '', 'right');
form_selectable_cell($tree['sites'] > 0 ? number_format_i18n($tree['sites'], -1) : '-', $tree['id'], '', 'right');
form_selectable_cell($tree['branches'] > 0 ? number_format_i18n($tree['branches'], -1) : '-', $tree['id'], '', 'right');
form_selectable_cell($tree['hosts'] > 0 ? number_format_i18n($tree['hosts'], -1) : '-', $tree['id'], '', 'right');
form_selectable_cell($tree['graphs'] > 0 ? number_format_i18n($tree['graphs'], -1) : '-', $tree['id'], '', 'right');
form_checkbox_cell($tree['name'], $tree['id']);
form_end_row();
$i++;
}
} else {
print "
" . __('No Trees Found') . '
';
}
html_end_box(false);
if (cacti_sizeof($trees)) {
print $nav;
}
// draw the dropdown containing a list of available actions for this form
draw_actions_dropdown($actions);
form_end();
if (grv('sort_column') == 'sequence' && grv('sort_direction') == 'ASC') {
?>