From 39bbcac217037fecdb555ac731c29ef3008e8236 Mon Sep 17 00:00:00 2001 From: cgfhtw Date: Wed, 12 Oct 2022 08:58:48 +0200 Subject: [PATCH] Widget controller & model & table --- application/controllers/dashboard/Widget.php | 44 +++++++++++-------- .../dashboard/Dashboard_Widget_model.php | 24 ++++++++++ public/js/components/Dashboard/Dashboard.js | 14 +++--- public/js/components/Dashboard/Item.js | 12 ++--- public/js/components/Dashboard/Section.js | 4 +- .../Dashboard/CachedWidgetLoader.js | 2 + 6 files changed, 68 insertions(+), 32 deletions(-) create mode 100644 application/models/dashboard/Dashboard_Widget_model.php diff --git a/application/controllers/dashboard/Widget.php b/application/controllers/dashboard/Widget.php index f1f0614d7..6f5bd6a6c 100644 --- a/application/controllers/dashboard/Widget.php +++ b/application/controllers/dashboard/Widget.php @@ -37,37 +37,43 @@ class Widget extends Auth_Controller ); $this->load->library('dashboard/DashboardLib', null, 'DashboardLib'); + $this->load->model('dashboard/Dashboard_Widget_model', 'DashboardWidgetModel'); } public function index() { $widget_id = $this->input->get('id'); - foreach ($this->demoData as $widget) { - if ($widget["id"] == $widget_id) - return $this->outputJsonSuccess($widget); - } - return $this->outputJsonSuccess([ - "id" => 0, - "name" => 'Widget Not Found', - "file" => 'DashboardWidget/Default.js', - "arguments" => [ - "className" => 'alert-danger', - "title" => 'Widget Not Found', - "msg" => 'The widget with the id ' . $widget_id . ' could not be found' - ], - "size" => [ - "width" => 1, - "height" => 1 - ] - ]); + $widget = $this->DashboardWidgetModel->load($widget_id); + + if (isError($widget) || !getData($widget)) + return $this->outputJsonSuccess([ + "widget_id" => 0, + "widget_kurzbz" => "notfound", + "arguments" => json_encode([ + "className" => 'alert-danger', + "title" => 'Widget Not Found', + "msg" => 'The widget with the id ' . $widget_id . ' could not be found' + ]), + "setup" => json_encode([ + "name" => 'Widget Not Found', + "file" => 'DashboardWidget/Default.js', + "width" => 1, + "height" => 1 + ]) + ]); + return $this->outputJsonSuccess(current(getData($widget))); } public function getWidgetsForDashboard() { $db = $this->input->get('db'); + $result = $this->DashboardWidgetModel->getAllForDashboard($db); - $this->outputJsonSuccess($this->demoData); + if (isError($result)) + return $this->outputJsonError(getError($result)); + + $this->outputJsonSuccess(getData($result)); } public function addWidgetToUserOverride() diff --git a/application/models/dashboard/Dashboard_Widget_model.php b/application/models/dashboard/Dashboard_Widget_model.php new file mode 100644 index 000000000..59aadf8f8 --- /dev/null +++ b/application/models/dashboard/Dashboard_Widget_model.php @@ -0,0 +1,24 @@ +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]); + } + +} diff --git a/public/js/components/Dashboard/Dashboard.js b/public/js/components/Dashboard/Dashboard.js index 04976da9c..345ebd9b1 100644 --- a/public/js/components/Dashboard/Dashboard.js +++ b/public/js/components/Dashboard/Dashboard.js @@ -27,6 +27,10 @@ export default { db: this.dashboard }}).then(res => { //console.log(res.data.retval); + res.data.retval.forEach(widget => { + widget.arguments = JSON.parse(widget.arguments); + widget.setup = JSON.parse(widget.setup); + }); this.widgets = res.data.retval; }).catch(err => console.error('ERROR:', err)); } @@ -151,12 +155,12 @@ export default {