Skip to content

Commit

Permalink
Finalizing #4540 - Add Devices to Reports
Browse files Browse the repository at this point in the history
This finalizes the adding of Devices to a Report from the Device Management page.
  • Loading branch information
TheWitness committed Jan 25, 2022
1 parent bcc3b5e commit 2f926d4
Show file tree
Hide file tree
Showing 3 changed files with 344 additions and 180 deletions.
46 changes: 45 additions & 1 deletion host.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
include_once('./lib/html_tree.php');
include_once('./lib/ping.php');
include_once('./lib/poller.php');
include_once('./lib/reports.php');
include_once('./lib/snmp.php');
include_once('./lib/template.php');
include_once('./lib/utility.php');
Expand All @@ -43,7 +44,8 @@
4 => __('Change Device Settings'),
5 => __('Clear Statistics'),
6 => __('Apply Automation Rules'),
7 => __('Sync to Device Template')
7 => __('Sync to Device Template'),
8 => __('Place Device on Report')
);

$device_actions = api_plugin_hook_function('device_action_array', $device_actions);
Expand Down Expand Up @@ -304,6 +306,15 @@ function form_actions() {
api_device_clear_statistics($selected_items);
} elseif (get_request_var('drp_action') == '7') { // sync to device template
api_device_sync_device_templates($selected_items);
} elseif (get_request_var('drp_action') == '8') { // place device on report
if (!reports_add_devices(get_filter_request_var('report_id'), $selected_items, get_filter_request_var('timespan'), get_filter_request_var('align'))) {
$name = db_fetch_cell_prepared('SELECT name
FROM reports
WHERE id = ?',
array(get_request_var('report_id')));

raise_message('reports_add_error', __('Unable to add some Devices to Report \'%s\'', $name), MESSAGE_LEVEL_WARN);
}
} elseif (get_request_var('drp_action') == '1') { // delete
if (!isset_request_var('delete_type')) {
set_request_var('delete_type', 2);
Expand Down Expand Up @@ -494,6 +505,39 @@ function form_actions() {
</tr>";

$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel'). "' onClick='cactiReturnTo()'>&nbsp;<input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Run Automation on Device(s)') . "'>";
} elseif (get_request_var('drp_action') == '8') {
global $alignment, $graph_timespans;

$reports = db_fetch_assoc_prepared('SELECT id, name
FROM reports
WHERE user_id = ?
ORDER BY name',
array($_SESSION['sess_user_id']));

if (cacti_sizeof($reports)) {
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to add the selected Graphs to the Report below.') . "</p>
<div class='itemlist'><ul>$host_list</ul></div>
</td>
</tr>
<tr><td>" . __('Report Name') . '<br>';
form_dropdown('report_id', $reports, 'name', 'id', '', '', '0');
print '</td></tr>';

print '<tr><td>' . __('Timespan') . '<br>';
form_dropdown('timespan', $graph_timespans, '', '', '', '', read_user_setting('default_timespan'));
print '</td></tr>';

print '<tr><td>' . __('Align') . '<br>';
form_dropdown('align', $alignment, '', '', '', '', REPORTS_ALIGN_CENTER);
print "</td></tr>\n";

$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'>&nbsp;<input type='submit' class='ui-button ui-corner-all ui-widget' value='" . __esc('Continue') . "' title='" . __esc('Add Devices to Report') . "'>";
} else {
print "<tr><td class='even'><span class='textError'>" . __('You currently have no Reports defined.') . "</span></td></tr>\n";
$save_html = "<input type='button' class='ui-button ui-corner-all ui-widget' value='" . __esc('Return') . "' onClick='cactiReturnTo()'>";
}
} else {
$save['drp_action'] = get_request_var('drp_action');
$save['host_list'] = $host_list;
Expand Down
96 changes: 92 additions & 4 deletions lib/reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,94 @@ function duplicate_reports($_id, $_title) {
}
}

function reports_add_devices($report_id, $device_ids, $timespan, $align) {
$report_user = db_fetch_cell_prepared('SELECT user_id
FROM reports
WHERE id = ?',
array($report_id));

if ($report_user != $_SESSION['sess_user_id']) {
raise_message('reports_not_owner');

return false;
} else {
$errors = 0;

foreach($device_ids as $device_id) {
$sequence = db_fetch_cell_prepared('SELECT MAX(sequence)
FROM reports_items
WHERE report_id = ?',
array($report_id)) + 1;

$exists = db_fetch_cell_prepared('SELECT id
FROM reports_items
WHERE host_id = ?
AND item_type = 5
AND report_id = ?
AND timespan = ?',
array($device_id, $report_id, $timespan));

$device_exists = db_fetch_cell_prepared('SELECT id
FROM host
WHERE id = ?',
array($device_id));

$description = db_fetch_cell_prepared('SELECT description
FROM host
WHERE id = ?',
array($device_id));

if (!$exists) {
if ($device_exists > 0) {
$host_template_id = db_fetch_cell_prepared('SELECT host_template_id
FROM host
WHERE id = ?',
array($device_id));

$site_id = db_fetch_cell_prepared('SELECT site_id
FROM host
WHERE id = ?',
array($device_id));

// Setup defaults
$graph_template_id = -1;
$local_graph_id = 0;

db_execute_prepared('INSERT INTO reports_items
(report_id, item_type, host_template_id, site_id, host_id, graph_template_id, local_graph_id, timespan, align, sequence)
VALUES (?, 5, ?, ?, ?, ?, ?, ?, ?, ?)',
array(
$report_id,
$host_template_id,
$site_id,
$device_id,
$graph_template_id,
$local_graph_id,
$timespan,
$align,
$sequence
)
);

raise_message('reports_add_device_' . $device_id, __('Device \'%s\' sucessfully added to Report.', $description), MESSAGE_LEVEL_INFO);
} else {
$errors++;
raise_message('reports_device_not_found', __('Device not found! Unable to add to Report'), MESSAGE_LEVEL_ERROR);
}
} else {
$errors++;
raise_message('reports_no_add_device_' . $device_id, __('Device \'%s\' not added to Report as it already exists on report.', $description), MESSAGE_LEVEL_WARN);
}
}

if ($errors) {
return false;
} else {
return true;
}
}
}

function reports_add_graphs($report_id, $local_graph_id, $timespan, $align) {
$report_user = db_fetch_cell_prepared('SELECT user_id
FROM reports
Expand All @@ -87,14 +175,14 @@ function reports_add_graphs($report_id, $local_graph_id, $timespan, $align) {
WHERE report_id = ?',
array($report_id)) + 1;

$existing = db_fetch_cell_prepared('SELECT id
$exists = db_fetch_cell_prepared('SELECT id
FROM reports_items
WHERE local_graph_id = ?
AND report_id = ?
AND timespan = ?',
array($local_graph_id, $report_id, $timespan));

if (!$existing) {
if (!$exists) {
$gd = db_fetch_row_prepared('SELECT *
FROM graph_local
WHERE id = ?',
Expand Down Expand Up @@ -1808,14 +1896,14 @@ function reports_graphs_action_execute($action) {

foreach($selected_items as $local_graph_id) {
/* see if the graph is already added */
$existing = db_fetch_cell_prepared('SELECT id
$exists = db_fetch_cell_prepared('SELECT id
FROM reports_items
WHERE local_graph_id = ?
AND report_id = ?
AND timespan = ?',
array($local_graph_id, $reports_id, get_nfilter_request_var('timespan')));

if (!$existing) {
if (!$exists) {
$sequence = db_fetch_cell_prepared('SELECT MAX(sequence)
FROM reports_items
WHERE report_id = ?',
Expand Down

0 comments on commit 2f926d4

Please sign in to comment.