Added per-widget settings

This commit is contained in:
Daniel Preussker 2015-09-05 16:15:11 +01:00
parent 41cff31941
commit 7acc1d266a
No known key found for this signature in database
GPG Key ID: F5CFDD99E67F8B99
4 changed files with 97 additions and 9 deletions

View File

@ -33,8 +33,11 @@ if ($type == 'placeholder') {
}
elseif (is_file('includes/common/'.$type.'.inc.php')) {
$results_limit = 10;
$no_form = true;
$results_limit = 10;
$no_form = true;
$widget_id = mres($_POST['id']);
$widget_settings = json_decode(dbFetchCell('select settings from users_widgets where user_widget_id = ?',array($widget_id)),true);
$widget_dimensions = dbfetchRow('select size_x,size_y from users_widgets where user_widget_id = ?',array($widget_id));
include 'includes/common/'.$type.'.inc.php';
$output = implode('', $common_output);
$status = 'ok';

View File

@ -0,0 +1,52 @@
<?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
)));
?>

View File

@ -18,7 +18,7 @@
$no_refresh = true;
foreach (dbFetchRows('SELECT * FROM `users_widgets` LEFT JOIN `widgets` ON `widgets`.`widget_id`=`users_widgets`.`widget_id` WHERE `user_id`=?',array($_SESSION['user_id'])) as $items) {
foreach (dbFetchRows('SELECT user_widget_id,users_widgets.widget_id,title,widget,col,row,size_x,size_y,refresh FROM `users_widgets` LEFT JOIN `widgets` ON `widgets`.`widget_id`=`users_widgets`.`widget_id` WHERE `user_id`=?',array($_SESSION['user_id'])) as $items) {
$data[] = $items;
}
@ -121,7 +121,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg
gridster.remove_all_widgets();
$.each(serialization, function() {
gridster.add_widget(
'<li id="'+this.user_widget_id+'">'+
'<li id="'+this.user_widget_id+'" data-type="'+this.widget+'">'+
'\<script\>var timeout'+this.user_widget_id+' = grab_data('+this.user_widget_id+','+this.refresh+',\''+this.widget+'\');\<\/script\>'+
'<header class="widget_header">'+this.title+'<button style="color: #ffffff" type="button" class="close close-widget" data-widget-id="'+this.user_widget_id+'" aria-label="Close"><span aria-hidden="true">&times;</span></button></header>'+
'<div class="widget_body" id="widget_body_'+this.user_widget_id+'">'+this.widget+'</div>'+
@ -166,7 +166,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg
var size_x = data.extra.size_x;
var size_y = data.extra.size_y;
gridster.add_widget(
'<li id="'+widget_id+'">'+
'<li id="'+widget_id+'" data-type="'+widget+'">'+
'\<script\>var timeout'+widget_id+' = grab_data('+widget_id+',60,\''+widget+'\');\<\/script\>'+
'<header class="widget_header">'+title+'<button type="button" class="close close-widget" data-widget-id="'+widget_id+'" aria-label="Close"><span aria-hidden="true">&times;</span></button></header>'+
'<div class="widget_body" id="widget_body_'+widget_id+'">'+widget+'</div>'+
@ -209,12 +209,40 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg
});
function grab_data(id,refresh,data_type) {
new_refresh = refresh * 1000;
function widget_settings(data) {
var widget_settings = {};
var widget_id = 0;
datas = $(data).serializeArray();
for( var field in datas ) {
widget_settings[datas[field].name] = datas[field].value;
}
console.log(widget_settings);
$('.gridster').find('div[id^=widget_body_]').each(function() {
if(this.contains(data)) {
widget_id = $(this).parent().attr('id');
widget_type = $(this).parent().data('type');
}
});
if( widget_id > 0 && widget_settings != {} ) {
$.ajax({
type: 'POST',
url: 'ajax_form.php',
data: {type: 'widget-settings', id: widget_id, settings: widget_settings},
dataType: "json",
success: function (data) {
if( data.status == "ok" ) {
widget_reload(widget_id,widget_type);
}
}
});
}
}
function widget_reload(id,data_type) {
$.ajax({
type: 'POST',
url: 'ajax_dash.php',
data: {type: data_type},
data: {type: data_type, id: id},
dataType: "json",
success: function (data) {
if (data.status == 'ok') {
@ -228,7 +256,11 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg
$("#widget_body_"+id).html('<div class="alert alert-info">Problem with backend</div>');
}
});
}
function grab_data(id,refresh,data_type) {
new_refresh = refresh * 1000;
widget_reload(id,data_type);
setTimeout(function() {
grab_data(id,refresh,data_type);
},

1
sql-schema/068.sql Normal file
View File

@ -0,0 +1 @@
alter table users_widgets add column `settings` text not null;