From 92ec7f44ef3d2eb1352c353ac8c1c7dfb029b132 Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Fri, 28 Oct 2022 12:40:57 +0200 Subject: [PATCH] Update Dashboard Endpoints for Admin GUI --- application/controllers/dashboard/Config.php | 55 +++++++++++- .../controllers/dashboard/Dashboard.php | 87 +++++++++++++++++++ application/controllers/dashboard/Widget.php | 79 +++++++++++------ .../dashboard/Dashboard_Widget_model.php | 14 +-- application/models/dashboard/Widget_model.php | 33 +++++++ 5 files changed, 228 insertions(+), 40 deletions(-) create mode 100644 application/controllers/dashboard/Dashboard.php create mode 100644 application/models/dashboard/Widget_model.php diff --git a/application/controllers/dashboard/Config.php b/application/controllers/dashboard/Config.php index f7736f329..c5b912e1f 100644 --- a/application/controllers/dashboard/Config.php +++ b/application/controllers/dashboard/Config.php @@ -17,12 +17,16 @@ class Config extends Auth_Controller 'addWidgetsToPreset' => 'dashboard/admin:rw', 'removeWidgetFromPreset' => 'dashboard/admin:rw', 'addWidgetsToUserOverride' => 'dashboard/benutzer:rw', - 'removeWidgetFromUserOverride' => 'dashboard/benutzer:rw' + 'removeWidgetFromUserOverride' => 'dashboard/benutzer:rw', + 'Funktionen' => 'dashboard/admin:r', + 'Preset' => 'dashboard/admin:r', + 'PresetBatch' => 'dashboard/admin:r' ) ); $this->load->library('dashboard/DashboardLib', null, 'DashboardLib'); $this->load->library('AuthLib', null, 'AuthLib'); + $this->load->model('ressource/Funktion_model', 'FunktionModel'); } public function index() @@ -91,9 +95,8 @@ class Config extends Auth_Controller } $preset_decoded = json_decode($preset->preset, true); - - if( $this->DashboardLib->removeWidgetFromWidgets($preset_decoded['widgets'], - $funktion_kurzbz, $widgetid) ) + if (!$this->DashboardLib->removeWidgetFromWidgets($preset_decoded['widgets'], + $funktion_kurzbz, $widgetid)) { http_response_code(404); $this->terminateWithJsonError('widgetid ' . $widgetid . ' not found'); @@ -168,4 +171,48 @@ class Config extends Auth_Controller $this->outputJsonSuccess(array('msg' => 'override successfully updated.')); } + public function Funktionen() + { + $funktionen = $this->FunktionModel->load(); + + if (isError($funktionen)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($funktionen) + ]); + } + + return $this->outputJsonSuccess(getData($funktionen) ?: []); + } + + public function Preset() + { + $db = $this->input->get('db'); + $funktion = $this->input->get('funktion'); + + $conf = $this->DashboardLib->getPreset($db, $funktion); + + if (!$conf) + return $this->outputJsonSuccess(['widgets' => [$funktion => []]]); + + return $this->outputJsonSuccess(json_decode($conf->preset, true)); + } + + public function PresetBatch() + { + $db = $this->input->get('db'); + $funktionen = $this->input->get('funktionen'); + $result = []; + + foreach ($funktionen as $funktion) { + $conf = $this->DashboardLib->getPreset($db, $funktion); + if ($conf) + $result[$funktion] = json_decode($conf->preset, true)['widgets'][$funktion]; + else + $result[$funktion] = []; + } + + return $this->outputJsonSuccess($result); + } + } diff --git a/application/controllers/dashboard/Dashboard.php b/application/controllers/dashboard/Dashboard.php new file mode 100644 index 000000000..8cf37d579 --- /dev/null +++ b/application/controllers/dashboard/Dashboard.php @@ -0,0 +1,87 @@ + 'dashboard/admin:r', + 'Create' => 'dashboard/admin:rw', + 'Update' => 'dashboard/admin:rw', + 'Delete' => 'dashboard/admin:rw' + ) + ); + + $this->load->library('dashboard/DashboardLib', null, 'DashboardLib'); + $this->load->model('dashboard/Dashboard_model', 'DashboardModel'); + } + + public function index() + { + $result = $this->DashboardModel->load(); + + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + + return $this->outputJsonSuccess(getData($result) ?: []); + } + + public function Create() + { + $input = $this->getPostJSON(); + + $result = $this->DashboardModel->insert($input); + + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + + return $this->outputJsonSuccess(getData($result) ?: []); + } + + public function Update() + { + $input = $this->getPostJSON(); + + $result = $this->DashboardModel->update($input->dashboard_id, $input); + + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + + return $this->outputJsonSuccess(getData($result) ?: []); + } + + public function Delete() + { + $input = $this->getPostJSON(); + + $result = $this->DashboardModel->delete($input->dashboard_id); + + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + + return $this->outputJsonSuccess(getData($result) ?: []); + } + +} diff --git a/application/controllers/dashboard/Widget.php b/application/controllers/dashboard/Widget.php index 6f900fcc9..9a878fa36 100644 --- a/application/controllers/dashboard/Widget.php +++ b/application/controllers/dashboard/Widget.php @@ -7,36 +7,19 @@ defined('BASEPATH') || exit('No direct script access allowed'); */ class Widget extends Auth_Controller { - private $demoData = [ - [ - "id" => 1, - "name" => 'Widget 1', - "description" => 'Das ist ein Test Widget', - "icon" => 'https://upload.wikimedia.org/wikipedia/commons/8/8a/Farben-Testbild.svg', - "file" => 'DashboardWidget/Widget1.js', - "arguments" => [ - "test" => 2 - ], - "size" => [ - "width" => [ "max" => 3 ], - "height" => [ "max" => 3 ] - ] - ] - ]; - public function __construct() { parent::__construct( array( 'index' => 'dashboard/benutzer:r', + 'getAll' => 'dashboard/admin:r', 'getWidgetsForDashboard' => 'dashboard/benutzer:rw', - 'addWidgetToUserOverride' => 'dashboard/benutzer:rw', - 'updateWidgetsToUserOverride' => 'dashboard/benutzer:rw', - 'removeWidgetFromUserOverride' => 'dashboard/benutzer:rw' + 'setAllowed' => 'dashboard/admin:rw' ) ); $this->load->library('dashboard/DashboardLib', null, 'DashboardLib'); + $this->load->model('dashboard/Widget_model', 'WidgetModel'); $this->load->model('dashboard/Dashboard_Widget_model', 'DashboardWidgetModel'); } @@ -44,7 +27,7 @@ class Widget extends Auth_Controller { $widget_id = $this->input->get('id'); - $widget = $this->DashboardWidgetModel->load($widget_id); + $widget = $this->WidgetModel->load($widget_id); if (isError($widget) || !getData($widget)) return $this->outputJsonSuccess([ @@ -65,15 +48,61 @@ class Widget extends Auth_Controller return $this->outputJsonSuccess(current(getData($widget))); } - public function getWidgetsForDashboard() + public function getAll() { - $db = $this->input->get('db'); - $result = $this->DashboardWidgetModel->getAllForDashboard($db); + $dashboard_id = $this->input->get('dashboard_id'); + $result = $this->WidgetModel->getWithAllowedForDashboard($dashboard_id); if (isError($result)) return $this->outputJsonError(getError($result)); - $this->outputJsonSuccess(getData($result)); + $this->outputJsonSuccess(getData($result) ?: []); } + public function getWidgetsForDashboard() + { + $db = $this->input->get('db'); + $result = $this->WidgetModel->getForDashboard($db); + + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + + $this->outputJsonSuccess(getData($result) ?: []); + } + + public function setAllowed() { + $input = $this->getPostJSON(); + + $dashboard_id = $input->dashboard_id; + $widget_id = $input->widget_id; + $action = $input->action; + + if ($action == 'add') { + $result = $this->DashboardWidgetModel->insert([ + 'dashboard_id' => $dashboard_id, + 'widget_id' => $widget_id + ]); + } elseif ($action == 'delete') { + $result = $this->DashboardWidgetModel->delete([ + 'dashboard_id' => $dashboard_id, + 'widget_id' => $widget_id + ]); + } else { + http_response_code(404); // TODO(chris): 400? + $this->terminateWithJsonError([ + 'error' => 'action value invalid' + ]); + } + if (isError($result)) { + http_response_code(404); + $this->terminateWithJsonError([ + 'error' => getError($result) + ]); + } + return $this->outputJsonSuccess(getData($result)); + } } diff --git a/application/models/dashboard/Dashboard_Widget_model.php b/application/models/dashboard/Dashboard_Widget_model.php index 59aadf8f8..eb4540cd9 100644 --- a/application/models/dashboard/Dashboard_Widget_model.php +++ b/application/models/dashboard/Dashboard_Widget_model.php @@ -8,17 +8,9 @@ class Dashboard_Widget_model extends DB_Model public function __construct() { parent::__construct(); - $this->dbTable = 'dashboard.tbl_widget'; - $this->pk = 'widget_id'; - } - - public function getAllForDashboard($db) - { - $this->addSelect($this->dbTable . '.*'); - $this->addJoin('dashboard.tbl_dashboard_widget', 'widget_id'); - $this->addJoin('dashboard.tbl_dashboard', 'dashboard_id'); - - return $this->loadWhere(['dashboard_kurzbz' => $db]); + $this->dbTable = 'dashboard.tbl_dashboard_widget'; + $this->pk = ['dashboard_id', 'widget_id']; + $this->hasSequence = false; } } diff --git a/application/models/dashboard/Widget_model.php b/application/models/dashboard/Widget_model.php new file mode 100644 index 000000000..4e5a31502 --- /dev/null +++ b/application/models/dashboard/Widget_model.php @@ -0,0 +1,33 @@ +dbTable = 'dashboard.tbl_widget'; + $this->pk = 'widget_id'; + } + + public function getWithAllowedForDashboard($dashboard_id) + { + $this->addSelect($this->dbTable . '.*'); + $this->addSelect('CASE WHEN dashboard_id = ? THEN 1 ELSE 0 END AS allowed', false); + $this->addJoin('dashboard.tbl_dashboard_widget', 'widget_id', 'LEFT'); + + return $this->execQuery($this->db->get_compiled_select($this->dbTable), [$dashboard_id]); + } + + public function getForDashboard($db) + { + $this->addSelect($this->dbTable . '.*'); + $this->addJoin('dashboard.tbl_dashboard_widget', 'widget_id'); + $this->addJoin('dashboard.tbl_dashboard', 'dashboard_id'); + + return $this->loadWhere(['dashboard_kurzbz' => $db]); + } + +}