Merge pull request #1853 from QuxLabs/issue-1847

This commit is contained in:
Søren Rosiak
2015-09-06 15:45:39 +02:00
9 changed files with 304 additions and 80 deletions
+122
View File
@@ -0,0 +1,122 @@
<?php
/* Copyright (C) 2015 Daniel Preussker, QuxLabs UG <preussker@quxlabs.com>
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/**
* Generic Graph Widget
* @author Daniel Preussker
* @copyright 2015 Daniel Preussker, QuxLabs UG
* @license GPL
* @package LibreNMS
* @subpackage Widgets
*/
if( defined('show_settings') || empty($widget_settings) ) {
$common_output[] = '
<form class="form" onsubmit="widget_settings(this); return false;">
<div class="form-group">
<label for="device_id" class="col-sm-2 control-label">Device: </label>
<div class="col-sm-10">
<input type="text" class="form-control input-sm widget-device-input-'.$unique_id.'" name="graph_device" placeholder="Device Name" value="'.$widget_settings['graph_device'].'">
</div>
</div>
<div class="form-group">
<label for="graph_type" class="col-sm-2 control-label">Graph: </label>
<div class="col-sm-8">
<select class="form-control input-sm" name="graph_type">';
foreach (get_graph_subtypes('device') as $avail_type) {
$common_output[] = '<option value="device_'.$avail_type.'"';
if ('device_'.$avail_type == $widget_settings['graph_type']) {
$common_output[] = " selected";
}
$display_type = is_mib_graph('device', $avail_type) ? $avail_type : nicecase($avail_type);
$common_output[] = '>'.$display_type.'</option>';
}
$common_output[] = '
</select>
</div>
<div class="col-sm-offset-10 col-sm-2">
<div class="checkbox input-sm">
<label>
<input type="checkbox" name="graph_legend" class="widget_setting" value="1" '.($widget_settings['graph_legend'] ? 'checked' : '').'> Legend
</label>
</div>
</div>
</div>
<button type="submit" class="btn btn-default">Set</button>
</form>
<style>
.twitter-typeahead {
width: 100%;
}
</style>
<script>
function '.$unique_id.'() {
var '.$unique_id.' = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace("name"),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "ajax_search.php?search=%QUERY&type=device&map=1",
filter: function (output) {
return $.map(output, function (item) {
return {
name: item.name,
};
});
},
wildcard: "%QUERY"
}
});
'.$unique_id.'.initialize();
$(".widget-device-input-'.$unique_id.'").typeahead({
hint: true,
highlight: true,
minLength: 1,
classNames: {
menu: "typeahead-left"
}
},
{
source: '.$unique_id.'.ttAdapter(),
async: true,
displayKey: "name",
valueKey: "name",
templates: {
header: "<h5><strong>&nbsp;Devices</strong></h5>",
suggestion: Handlebars.compile("<p>&nbsp;{{name}}</p>")
}
});
}
</script>
<script>
$(function() { '.$unique_id.'(); });
</script>';
}
else {
$widget_settings['device_id'] = dbFetchCell('select device_id from devices where hostname = ?',array($widget_settings['graph_device']));
$widget_settings['title'] = $widget_settings['graph_device']." / ".$widget_settings['graph_type'];
$common_output[] = generate_minigraph_image(
array('device_id'=>(int) $widget_settings['device_id']),
$config['time']['day'],
$config['time']['now'],
$widget_settings['graph_type'],
$widget_settings['graph_legend'] == 1 ? 'yes' : 'no',
$widget_dimensions['x'],
$widget_dimensions['y'],
'&',
'minigraph-image',
1
);
}
@@ -20,22 +20,14 @@ elseif ($sub_type == 'remove-all') {
}
}
elseif ($sub_type == 'add' && is_numeric($widget_id)) {
$dupe_check = dbFetchCEll('SELECT `user_widget_id` FROM `users_widgets` WHERE `user_id`=? AND `widget_id`=?',array($_SESSION['user_id'],$widget_id));
if (is_numeric($dupe_check)) {
$message = 'This widget has already been added';
}
else {
$widget = dbFetchRow('SELECT * FROM `widgets` WHERE `widget_id`=?', array($widget_id));
if (is_array($widget)) {
list($x,$y) = explode(',',$widget['base_dimensions']);
$item_id = dbInsert(array('user_id'=>$_SESSION['user_id'],'widget_id'=>$widget_id, 'col'=>1,'row'=>1,'refresh'=>60,'title'=>$widget['widget_title'],'size_x'=>$x,'size_y'=>$y),'users_widgets');
if (is_numeric($item_id)) {
$extra = array('widget_id'=>$item_id,'title'=>$widget['widget_title'],'widget'=>$widget['widget'],'size_x'=>$x,'size_y'=>$y);
$status = 'ok';
$message = '';
}
$widget = dbFetchRow('SELECT * FROM `widgets` WHERE `widget_id`=?', array($widget_id));
if (is_array($widget)) {
list($x,$y) = explode(',',$widget['base_dimensions']);
$item_id = dbInsert(array('user_id'=>$_SESSION['user_id'],'widget_id'=>$widget_id, 'col'=>1,'row'=>1,'refresh'=>60,'title'=>$widget['widget_title'],'size_x'=>$x,'size_y'=>$y),'users_widgets');
if (is_numeric($item_id)) {
$extra = array('user_widget_id'=>$item_id,'widget_id'=>$item_id,'title'=>$widget['widget_title'],'widget'=>$widget['widget'],'refresh'=>60,'size_x'=>$x,'size_y'=>$y);
$status = 'ok';
$message = '';
}
}
}
@@ -0,0 +1,51 @@
<?php
/* Copyright (C) 2015 Daniel Preussker, QuxLabs UG <preussker@quxlabs.com>
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/**
* Store per-widget settings
* @author Daniel Preussker
* @copyright 2015 Daniel Preussker, QuxLabs UG
* @license GPL
* @package LibreNMS
* @subpackage Widgets
*/
$status = 'error';
$message = 'unknown error';
$widget_id = (int) $_REQUEST['id'];
if ($widget_id < 1) {
$status = 'error';
$message = 'ERROR: malformed widget ID.';
}
else {
$widget_settings = $_REQUEST['settings'];
if (!is_array($widget_settings)) {
$widget_settings = array();
}
if (dbUpdate(array('settings'=>json_encode($widget_settings)),'users_widgets','user_widget_id=?',array($widget_id))) {
$status = 'ok';
$message = 'Updated';
}
else {
$status = 'error';
$message = 'ERROR: Could not update';
}
}
die(json_encode(array(
'status' => $status,
'message' => $message
)));
+2 -2
View File
@@ -181,8 +181,8 @@ function get_percentage_colours($percentage) {
}//end get_percentage_colours()
function generate_minigraph_image($device, $start, $end, $type, $legend='no', $width=275, $height=100, $sep='&amp;', $class='minigraph-image') {
return '<img class="'.$class.'" src="graph.php?'.implode($sep, array('device='.$device['device_id'], "from=$start", "to=$end", "width=$width", "height=$height", "type=$type", "legend=$legend")).'">';
function generate_minigraph_image($device, $start, $end, $type, $legend='no', $width=275, $height=100, $sep='&amp;', $class='minigraph-image',$absolute_size=0) {
return '<img class="'.$class.'" src="graph.php?'.implode($sep, array('device='.$device['device_id'], "from=$start", "to=$end", "width=$width", "height=$height", "type=$type", "legend=$legend", "absolute=$absolute_size")).'">';
}//end generate_minigraph_image()
+4
View File
@@ -103,3 +103,7 @@ else {
}
$rrd_options .= ' --font-render-mode normal';
if (isset($_GET['absolute']) && $_GET['absolute'] == "1") {
$rrd_options .= ' --full-size-mode';
}