diff --git a/html/includes/forms/add-dashboard.inc.php b/html/includes/forms/add-dashboard.inc.php new file mode 100644 index 0000000000..e34993a6eb --- /dev/null +++ b/html/includes/forms/add-dashboard.inc.php @@ -0,0 +1,41 @@ + + * 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 . */ + +/** + * Create Dashboards + * @author Daniel Preussker + * @copyright 2015 Daniel Preussker, QuxLabs UG + * @license GPL + * @package LibreNMS + * @subpackage Dashboards + */ + +$status = 'error'; +$message = 'unknown error'; +if (isset($_REQUEST['dashboard_name']) && ($dash_id = dbInsert(array('dashboard_name'=>$_REQUEST['dashboard_name'],'user_id'=>$_SESSION['user_id']),'dashboards'))) { + $status = 'ok'; + $message = 'Created'; +} +else { + $status = 'error'; + $message = 'ERROR: Could not create'; +} + +die(json_encode(array( + 'status' => $status, + 'message' => $message, + 'dashboard_id' => $dash_id +))); + diff --git a/html/includes/forms/delete-dashboard.inc.php b/html/includes/forms/delete-dashboard.inc.php new file mode 100644 index 0000000000..2874dec6e3 --- /dev/null +++ b/html/includes/forms/delete-dashboard.inc.php @@ -0,0 +1,45 @@ + + * 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 . */ + +/** + * Delete Dashboards + * @author Daniel Preussker + * @copyright 2015 Daniel Preussker, QuxLabs UG + * @license GPL + * @package LibreNMS + * @subpackage Dashboards + */ + +$status = 'error'; +$message = 'unknown error'; +if (isset($_REQUEST['dashboard_id'])) { + dbDelete('users_widgets','user_id = ? && dashboard_id = ?',array($_SESSION['user_id'],$_REQUEST['dashboard_id'])); + if (dbDelete('dashboards','user_id = ? && dashboard_id = ?',array($_SESSION['user_id'],$_REQUEST['dashboard_id']))) { + $status = 'ok'; + $message = 'Deleted dashboard'; + } + else { + $message = 'ERROR: Could not delete dashboard '.$_REQUEST['dashboard_id']; + } +} +else { + $message = 'ERROR: Not enough params'; +} + +die(json_encode(array( + 'status' => $status, + 'message' => $message, +))); + diff --git a/html/includes/forms/edit-dashboard.inc.php b/html/includes/forms/edit-dashboard.inc.php new file mode 100644 index 0000000000..fe9d9c527d --- /dev/null +++ b/html/includes/forms/edit-dashboard.inc.php @@ -0,0 +1,44 @@ + + * 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 . */ + +/** + * Edit Dashboards + * @author Daniel Preussker + * @copyright 2015 Daniel Preussker, QuxLabs UG + * @license GPL + * @package LibreNMS + * @subpackage Dashboards + */ + +$status = 'error'; +$message = 'unknown error'; +if (isset($_REQUEST['dashboard_id']) && isset($_REQUEST['dashboard_name']) && isset($_REQUEST['access'])) { + if(dbUpdate(array('dashboard_name'=>$_REQUEST['dashboard_name'],'access'=>$_REQUEST['access']),'dashboards','(user_id = ? || access = 2) && dashboard_id = ?',array($_SESSION['user_id'],$_REQUEST['dashboard_id']))) { + $status = 'ok'; + $message = 'Updated dashboard'; + } + else { + $message = 'ERROR: Could not update dashboard '.$_REQUEST['dashboard_id']; + } +} +else { + $message = 'ERROR: Not enough params'; +} + +die(json_encode(array( + 'status' => $status, + 'message' => $message, +))); + diff --git a/html/includes/forms/update-dashboard-config.inc.php b/html/includes/forms/update-dashboard-config.inc.php index de278fd918..e9bdb23dbc 100644 --- a/html/includes/forms/update-dashboard-config.inc.php +++ b/html/includes/forms/update-dashboard-config.inc.php @@ -6,41 +6,65 @@ $message = 'Error updating user dashboard config'; $data = json_decode($_POST['data'],true); $sub_type = mres($_POST['sub_type']); $widget_id = mres($_POST['widget_id']); +$dasboard_id = mres($_POST['dashboard_id']); if ($sub_type == 'remove' && is_numeric($widget_id)) { - if ($widget_id == 0 || dbDelete('users_widgets','`user_id`=? AND `user_widget_id`=?', array($_SESSION['user_id'],$widget_id))) { - $status = 'ok'; - $message = ''; - } -} -elseif ($sub_type == 'remove-all') { - if (dbDelete('users_widgets','`user_id`=?', array($_SESSION['user_id']))) { - $status = 'ok'; - $message = ''; - } -} -elseif ($sub_type == 'add' && is_numeric($widget_id)) { - $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); + if (dbFetchCell('select 1 from dashboards where (user_id = ? || access = 2) && dashboard_id = ?',array($_SESSION['user_id'],$dasboard_id)) == 1) { + if ($widget_id == 0 || dbDelete('users_widgets','`user_widget_id`=? AND `dashboard_id`=?', array($widget_id,$dasboard_id))) { $status = 'ok'; $message = ''; } } + else { + $status = 'error'; + $message = 'ERROR: You have no write access.'; + } +} +elseif ($sub_type == 'remove-all') { + if (dbFetchCell('select 1 from dashboards where (user_id = ? || access = 2) && dashboard_id = ?',array($_SESSION['user_id'],$dasboard_id)) == 1) { + if (dbDelete('users_widgets','`dashboard_id`=?', array($dasboard_id))) { + $status = 'ok'; + $message = ''; + } + } + else { + $status = 'error'; + $message = 'ERROR: You have no write access.'; + } +} +elseif ($sub_type == 'add' && is_numeric($widget_id)) { + if (dbFetchCell('select 1 from dashboards where (user_id = ? || access = 2) && dashboard_id = ?',array($_SESSION['user_id'],$dasboard_id)) == 1) { + $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,'settings'=>'','dashboard_id'=>$dasboard_id),'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 = ''; + } + } + } + else { + $status = 'error'; + $message = 'ERROR: You have no write access.'; + } } else { - $status = 'ok'; - $message = ''; - - foreach ($data as $line) { - if (is_array($line)) { - $update = array('col'=>$line['col'],'row'=>$line['row'],'size_x'=>$line['size_x'],'size_y'=>$line['size_y']); - dbUpdate($update, 'users_widgets', '`user_widget_id`=?', array($line['id'])); + if (dbFetchCell('select 1 from dashboards where (user_id = ? || access = 2) && dashboard_id = ?',array($_SESSION['user_id'],$dasboard_id)) == 1) { + $status = 'ok'; + $message = ''; + foreach ($data as $line) { + if (is_array($line)) { + $update = array('col'=>$line['col'],'row'=>$line['row'],'size_x'=>$line['size_x'],'size_y'=>$line['size_y']); + dbUpdate($update, 'users_widgets', '`user_widget_id`=? AND `dashboard_id`=?', array($line['id'],$dasboard_id)); + } } } + else { + $status = 'error'; + $message = 'ERROR: You have no write access.'; + } } $response = array( diff --git a/html/includes/forms/widget-settings.inc.php b/html/includes/forms/widget-settings.inc.php index 1926d34913..fabe6c4aab 100644 --- a/html/includes/forms/widget-settings.inc.php +++ b/html/includes/forms/widget-settings.inc.php @@ -35,13 +35,19 @@ else { 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'; + if (dbFetchCell('select 1 from users_widgets inner join dashboards on users_widgets.dashboard_id = dashboards.dashboard_id where user_widget_id = ? && (users_widgets.user_id = ? || dashboards.access = 2)',array($widget_id,$_SESSION['user_id'])) == 1) { + 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'; + } } else { $status = 'error'; - $message = 'ERROR: Could not update'; + $message = 'ERROR: You have no write-access to this dashboard'; } } diff --git a/html/pages/front/tiles.php b/html/pages/front/tiles.php index 9c7c5c933e..a3669f5598 100644 --- a/html/pages/front/tiles.php +++ b/html/pages/front/tiles.php @@ -17,37 +17,176 @@ */ $no_refresh = true; - -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) { +if (dbFetchCell('SELECT dashboard_id FROM dashboards WHERE user_id=?',array($_SESSION['user_id'])) == 0) { + $vars['dashboard'] = dbInsert(array('dashboard_name'=>'Default','user_id'=>$_SESSION['user_id']),'dashboards'); + if (dbFetchCell('select 1 from users_widgets where user_id = ? && dashboard_id = ?',array($_SESSION['user_id'],0)) == 1) { + dbUpdate(array('dashboard_id'=>$vars['dashboard']),'users_widgets','user_id = ? && dashboard_id = ?',array($_SESSION['user_id'],0)); + } +} +if (!empty($vars['dashboard'])) { + $orig = $vars['dashboard']; + $vars['dashboard'] = dbFetchRow('select * from dashboards where user_id = ? && dashboard_id = ? order by dashboard_id limit 1',array($_SESSION['user_id'],$vars['dashboard'])); + if (empty($vars['dashboard'])) { + $vars['dashboard'] = dbFetchRow('select dashboards.*,users.username from dashboards inner join users on dashboards.user_id = users.user_id where dashboards.dashboard_id = ? && dashboards.access > 0',array($orig)); + } +} +if (empty($vars['dashboard'])) { + $vars['dashboard'] = dbFetchRow('select * from dashboards where user_id = ? order by dashboard_id limit 1',array($_SESSION['user_id'])); + if (isset($orig)) { + $msg_box[] = array('type' => 'error', 'message' => 'Dashboard #'.$orig.' does not exist! Loaded '.$vars['dashboard']['dashboard_name'].' instead.','title' => 'Requested Dashboard Not Found!'); + } +} +$data = array(); +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 `dashboard_id`=?',array($vars['dashboard']['dashboard_id'])) as $items) { $data[] = $items; } - -if (!is_array($data)) { +if (empty($data)) { $data[] = array('user_widget_id'=>'0','widget_id'=>1,'title'=>'Add a widget','widget'=>'placeholder','col'=>1,'row'=>1,'size_x'=>2,'size_y'=>2,'refresh'=>60); } - -$data = serialize(json_encode($data)); +$data = serialize(json_encode($data)); $dash_config = unserialize(stripslashes($data)); - +$dashboards = dbFetchRows("SELECT * FROM `dashboards` WHERE `user_id` = ? && `dashboard_id` != ?",array($_SESSION['user_id'],$vars['dashboard']['dashboard_id'])); ?> -
- Widgets - - -
-
-
+
+
+
+ +
+ + +
+ + + +
+
+
+
+
+
+
+ + New Dashboard + + + + + +
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+ + Dashboard Name + + + + + + +
+
+
+
+
+
+ + +
+
+
+
+ Add Widgets +
+ + +
+
+
+
+
+ +
+
+
+
+
+
+ +
+
+
+
+
+
+ +
+
+
+
+
@@ -75,7 +214,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg $.ajax({ type: 'POST', url: 'ajax_form.php', - data: {type: "update-dashboard-config", data: s}, + data: {type: "update-dashboard-config", data: s, dashboard_id: }, dataType: "json", success: function (data) { if (data.status == 'ok') { @@ -91,6 +230,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg } $(function(){ + dashboard_collapse(); gridster = $(".gridster ul").gridster({ widget_base_dimensions: [100, 100], widget_margins: [5, 5], @@ -129,7 +269,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg $.ajax({ type: 'POST', url: 'ajax_form.php', - data: {type: "update-dashboard-config", sub_type: 'remove-all'}, + data: {type: "update-dashboard-config", sub_type: 'remove-all', dashboard_id: }, dataType: "json", success: function (data) { if (data.status == 'ok') { @@ -150,7 +290,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg $.ajax({ type: 'POST', url: 'ajax_form.php', - data: {type: "update-dashboard-config", sub_type: 'add', widget_id: widget_id}, + data: {type: "update-dashboard-config", sub_type: 'add', widget_id: widget_id, dashboard_id: }, dataType: "json", success: function (data) { if (data.status == 'ok') { @@ -172,7 +312,7 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg $.ajax({ type: 'POST', url: 'ajax_form.php', - data: {type: "update-dashboard-config", sub_type: 'remove', widget_id: widget_id}, + data: {type: "update-dashboard-config", sub_type: 'remove', widget_id: widget_id, dashboard_id: }, dataType: "json", success: function (data) { if (data.status == 'ok') { @@ -201,6 +341,81 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg }); + function dashboard_collapse(target) { + if (target !== undefined) { + $('.dash-collapse:not('+target+')').each(function() { + $(this).fadeOut(0); + }); + $(target).fadeToggle(300); + } else { + $('.dash-collapse').fadeOut(0); + } + } + + function dashboard_delete(data) { + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: 'delete-dashboard', dashboard_id: $(data).data('dashboard')}, + dataType: "json", + success: function (data) { + if( data.status == "ok" ) { + $("#message").html('
' + data.message + '
'); + window.location.href="/overview"; + } + else { + $("#message").html('
' + data.message + '
'); + } + } + }); + } + + function dashboard_edit(data) { + datas = $(data).serializeArray(); + data = []; + for( var field in datas ) { + data[datas[field].name] = datas[field].value; + } + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: 'edit-dashboard', dashboard_name: data['dashboard_name'], dashboard_id: , access: data['access']}, + dataType: "json", + success: function (data) { + if( data.status == "ok" ) { + $("#message").html('
' + data.message + '
'); + window.location.href="/overview/dashboard="; + } + else { + $("#message").html('
' + data.message + '
'); + } + } + }); + } + + function dashboard_add(data) { + datas = $(data).serializeArray(); + data = []; + for( var field in datas ) { + data[datas[field].name] = datas[field].value; + } + $.ajax({ + type: 'POST', + url: 'ajax_form.php', + data: {type: 'add-dashboard', dashboard_name: data['dashboard_name']}, + dataType: "json", + success: function (data) { + if( data.status == "ok" ) { + $("#message").html('
' + data.message + '
'); + window.location.href="/overview/dashboard="+data.dashboard_id; + } + else { + $("#message").html('
' + data.message + '
'); + } + } + }); + } + function widget_dom(data) { dom = '
  • '+ '
    '+data.title+''+ @@ -241,6 +456,9 @@ foreach (dbFetchRows("SELECT * FROM `widgets` ORDER BY `widget_title`") as $widg if( data.status == "ok" ) { widget_reload(widget_id,widget_type); } + else { + $("#message").html('
    ' + data.message + '
    '); + } } }); } diff --git a/sql-schema/069.sql b/sql-schema/069.sql new file mode 100644 index 0000000000..a629aae3d4 --- /dev/null +++ b/sql-schema/069.sql @@ -0,0 +1,2 @@ +CREATE TABLE `dashboards` ( `dashboard_id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL DEFAULT 0, `dashboard_name` varchar(255) NOT NULL, `access` int(1) NOT NULL DEFAULT 0, PRIMARY KEY (`dashboard_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; +ALTER TABLE `users_widgets` ADD COLUMN `dashboard_id` int(11) NOT NULL;