diff --git a/html/ajax_dash.php b/html/ajax_dash.php index 3b4c56e46..a26e46e5a 100644 --- a/html/ajax_dash.php +++ b/html/ajax_dash.php @@ -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'; diff --git a/html/includes/forms/widget-settings.inc.php b/html/includes/forms/widget-settings.inc.php new file mode 100644 index 000000000..2ce0cde63 --- /dev/null +++ b/html/includes/forms/widget-settings.inc.php @@ -0,0 +1,52 @@ + + * 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 . */ + +/** + * 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 +))); +?> diff --git a/html/pages/front/tiles.php b/html/pages/front/tiles.php index ddaf61af4..063b4bba6 100644 --- a/html/pages/front/tiles.php +++ b/html/pages/front/tiles.php @@ -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( - '
  • '+ + '
  • '+ '\var timeout'+this.user_widget_id+' = grab_data('+this.user_widget_id+','+this.refresh+',\''+this.widget+'\');\<\/script\>'+ '
    '+this.title+'
    '+ '
    '+this.widget+'
    '+ @@ -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( - '
  • '+ + '
  • '+ '\var timeout'+widget_id+' = grab_data('+widget_id+',60,\''+widget+'\');\<\/script\>'+ '
    '+title+'
    '+ '
    '+widget+'
    '+ @@ -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('
    Problem with backend
    '); } }); - + } + + function grab_data(id,refresh,data_type) { + new_refresh = refresh * 1000; + widget_reload(id,data_type); setTimeout(function() { grab_data(id,refresh,data_type); }, diff --git a/sql-schema/068.sql b/sql-schema/068.sql new file mode 100644 index 000000000..39784b418 --- /dev/null +++ b/sql-schema/068.sql @@ -0,0 +1 @@ +alter table users_widgets add column `settings` text not null;