From 02153e469f9239696520ec0079a9d1085d0ed7c5 Mon Sep 17 00:00:00 2001 From: ma0068 Date: Fri, 19 Dec 2025 11:39:31 +0100 Subject: [PATCH 01/31] Dashboard Admin Cleanup - refactoring Api: FHC-API controller for Edit/Update, widgets and presets - delete dashboard with Prompt - phrases --- .../frontend/v1/dashboard/DashboardAdmin.php | 294 ++++++++++++++++++ .../views/dashboard/dashboard_demo_admin.php | 6 +- .../api/factory/dashboard/dashboardAdmin.js | 85 +++++ ...shboardAdmin.js => DashboardAdmin_DEPR.js} | 0 public/js/components/Dashboard/Admin.js | 83 +++-- public/js/components/Dashboard/Admin/Edit.js | 4 +- .../js/components/Dashboard/Admin/Presets.js | 157 +++++----- .../js/components/Dashboard/Admin/Widgets.js | 34 +- public/js/components/Dashboard/Section.js | 2 +- system/phrasesupdate.php | 121 +++++++ 10 files changed, 664 insertions(+), 122 deletions(-) create mode 100644 application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php create mode 100644 public/js/api/factory/dashboard/dashboardAdmin.js rename public/js/apps/{DashboardAdmin.js => DashboardAdmin_DEPR.js} (100%) diff --git a/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php b/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php new file mode 100644 index 000000000..5e5ba4d40 --- /dev/null +++ b/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php @@ -0,0 +1,294 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +/** + * This controller operates between (interface) the JS (GUI) and the back-end + * Provides data to the ajax get calls about addresses + * This controller works with JSON calls on the HTTP GET or POST and the output is always JSON + */ +class DashboardAdmin extends FHCAPI_Controller +{ + public function __construct() + { + parent::__construct([ + 'getAllDashboards' => 'dashboard/admin:r', + 'createDashboard' => 'dashboard/admin:rw', + 'updateDashboard' => 'dashboard/admin:rw', + 'deleteDashboard' => 'dashboard/admin:rw', + 'loadWidget' => ['dashboard/benutzer:r', 'dashboard/admin:r'], + 'getAllWidgets' => 'dashboard/admin:r', + 'getWidgetsForDashboard' => ['dashboard/benutzer:rw', 'dashboard/admin:r'], + 'setWidgetAllowed' => 'dashboard/admin:rw', + + 'index' => 'dashboard/benutzer:r', + 'dummy' => 'dashboard/benutzer:r', + 'genWidgetId' => 'dashboard/benutzer:rw', + 'addWidgetsToPreset' => 'dashboard/admin:rw', + 'removeWidgetFromPreset' => 'dashboard/admin:rw', + 'addWidgetsToUserOverride' => 'dashboard/benutzer:rw', + 'removeWidgetFromUserOverride' => 'dashboard/benutzer:rw', + 'funktionen' => 'dashboard/admin:r', + 'preset' => 'dashboard/admin:r', + 'presetBatch' => 'dashboard/admin:r' + ]); + + // Load language phrases + $this->loadPhrases([ + 'ui' + ]); + + $this->load->library('dashboard/DashboardLib', null, 'DashboardLib'); + + $this->load->model('dashboard/Dashboard_model', 'DashboardModel'); + $this->load->model('dashboard/Widget_model', 'WidgetModel'); + $this->load->model('dashboard/Dashboard_Widget_model', 'DashboardWidgetModel'); + $this->load->model('ressource/Funktion_model', 'FunktionModel'); + } + + public function getAllDashboards() + { + $result = $this->DashboardModel->load(); + + if (isError($result)) + { + return $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + } + $this->terminateWithSuccess($result); + } + + public function createDashboard() + { + $dashboard_kurzbz = $this->input->post('dashboard_kurzbz'); + $result = $this->DashboardModel->insert( + [ + 'dashboard_kurzbz' => $dashboard_kurzbz + ] + ); + + if (isError($result)) + { + $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + } + $this->terminateWithSuccess((getData($result) ?: [])); + } + + public function updateDashboard() + { + $dashboard_id = $this->input->post('dashboard_id'); + $dashboard_kurzbz = $this->input->post('dashboard_kurzbz'); + $beschreibung = $this->input->post('beschreibung'); + if(!$dashboard_id) + { + $this->terminateWithError($this->p->t('ui', 'error_missingId', ['id'=> 'Dashboard ID']), self::ERROR_TYPE_GENERAL); + } + + $result = $this->DashboardModel->update( + [ + 'dashboard_id' => $dashboard_id, + ], + [ + 'dashboard_kurzbz' => $dashboard_kurzbz, + 'beschreibung' => $beschreibung, + ] + ); + + if (isError($result)) + { + $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + } + $this->terminateWithSuccess($result); + } + + public function deleteDashboard() + { + $dashboard_id = $this->input->post('dashboard_id'); + if(!$dashboard_id) + { + return $this->terminateWithError($this->p->t('ui', 'error_missingId', ['id'=> 'Dashboard ID']), self::ERROR_TYPE_GENERAL); + } + + //delete all presets + $this->load->model('dashboard/Dashboard_Preset_model', 'DashboardPresetModel'); + $resultPresets = $this->DashboardPresetModel->delete( + array('dashboard_id' => $dashboard_id) + ); + if ($resultPresets === false) + { + return $this->terminateWithError($resultPresets, self::ERROR_TYPE_GENERAL); + } + + //delete all widgets + $this->load->model('dashboard/Dashboard_Widget_model', 'DashboardWidgetModel'); + + $resultWidgets = $this->DashboardWidgetModel->delete( + array('dashboard_id' => $dashboard_id) + ); + if ($resultWidgets === false) + { + return $this->terminateWithError($resultWidgets, self::ERROR_TYPE_GENERAL); + } + + $result = $this->DashboardModel->delete( + array('dashboard_id' => $dashboard_id) + ); + + if (isError($result)) + { + return $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + } + $this->terminateWithSuccess($result); + } + + public function getAllWidgets() + { + $dashboard_id = $this->input->get('dashboard_id'); + if(!$dashboard_id) + { + return $this->terminateWithError($this->p->t('ui', 'error_missingId', ['id'=> 'Dashboard ID']), self::ERROR_TYPE_GENERAL); + } + //$this->terminateWithError($dashboard_id); + $result = $this->WidgetModel->getWithAllowedForDashboard($dashboard_id); + + if (isError($result)) + $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + + $this->terminateWithSuccess(getData($result) ?: []); + } + + public function setWidgetAllowed() + { + $dashboard_id = $this->input->post('dashboard_id'); + $widget_id = $this->input->post('widget_id'); + $action = $this->input->post('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 + { + $this->terminateWithError("action value invalid", self::ERROR_TYPE_GENERAL); + } + if (isError($result)) + $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + + $this->terminateWithSuccess(getData($result) ?: []); + } + + //Presets + public function funktionen() + { + $result = $this->FunktionModel->load(); + + if (isError($result)) + $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); + + $this->terminateWithSuccess(getData($result) ?: []); + } + + public function addWidgetsToPreset() + { + $raw = $this->input->raw_input_stream; + $json = json_decode($raw); + + $dashboard_kurzbz = $json->db; + $funktion_kurzbz = $json->funktion_kurzbz; + $widgets = $json->widgets; + + $preset = $this->DashboardLib->getPresetOrCreateEmptyPreset($dashboard_kurzbz, $funktion_kurzbz); + + $preset_decoded = json_decode($preset->preset, true); + $this->DashboardLib->addWidgetsToWidgets($preset_decoded, $dashboard_kurzbz, $funktion_kurzbz, $widgets); + + $preset->preset = json_encode($preset_decoded); + $result = $this->DashboardLib->insertOrUpdatePreset($preset); + + if (isError($result)) + $this->terminateWithError($this->p->t('dashboard', 'error_savePreset'), self::ERROR_TYPE_GENERAL); + $this->terminateWithSuccess($preset_decoded); + } + + public function removeWidgetFromPreset() + { + $raw = $this->input->raw_input_stream; + $json = json_decode($raw); + + $dashboard_kurzbz = $json->db; + $funktion_kurzbz = $json->funktion_kurzbz; + $widgetid = $json->widgetid; + + $preset = $this->DashboardLib->getPreset($dashboard_kurzbz, $funktion_kurzbz); + if ($preset === null) + { + $this->terminateWithError($this->p->t('ui', 'error_presetAndFunctionNotFound', ['dashboard'=> $dashboard_kurzbz, 'funktion'=> $funktion_kurzbz]), self::ERROR_TYPE_GENERAL); + } + + $preset_decoded = json_decode($preset->preset, true); + if (!$this->DashboardLib->removeWidgetFromWidgets($preset_decoded, $funktion_kurzbz, $widgetid)) + { + $this->terminateWithError($this->p->t('ui', 'error_missingId', ['id'=> 'Widget ID']), self::ERROR_TYPE_GENERAL); + } + + $preset->preset = json_encode($preset_decoded); + $result = $this->DashboardLib->insertOrUpdatePreset($preset); + if (isError($result)) + { + $this->terminateWithError($this->p->t('dashboard', 'error_deleteWidget'), self::ERROR_TYPE_GENERAL); + } + $this->terminateWithSuccess(array('msg' => $this->p->t('dashboard', 'success_savePreset'))); + } + + public function presetBatch() + { + $db = $this->input->get('db'); + $funktionen = $this->input->get('funktionen'); + $result = []; + + if($funktionen) + { + foreach ($funktionen as $funktion) { + $conf = $this->DashboardLib->getPreset($db, $funktion); + if ($conf) { + $preset = json_decode($conf->preset, true); + if (!isset($preset[$funktion]) || !isset($preset[$funktion]['widgets'])) + $result[$funktion] = []; + else { + $result[$funktion] = $preset[$funktion]['widgets']; + } + } else + $result[$funktion] = []; + } + } + else + $result = []; + + return $this->terminateWithSuccess($result); + } +} diff --git a/application/views/dashboard/dashboard_demo_admin.php b/application/views/dashboard/dashboard_demo_admin.php index 0d92146a8..b57a2bfa0 100644 --- a/application/views/dashboard/dashboard_demo_admin.php +++ b/application/views/dashboard/dashboard_demo_admin.php @@ -8,9 +8,11 @@ $this->load->view( 'axios027' => true, 'restclient' => true, 'vue3' => true, - 'customJSModules' => ['public/js/apps/DashboardAdmin.js'], + 'primevue3' => true, + 'customJSModules' => ['public/js/apps/Dashboard/Admin.js'], 'customCSSs' => [ - 'public/css/components/dashboard.css' + 'public/css/components/dashboard.css', + 'public/css/components/primevue.css', ], 'navigationcomponent' => true ) diff --git a/public/js/api/factory/dashboard/dashboardAdmin.js b/public/js/api/factory/dashboard/dashboardAdmin.js new file mode 100644 index 000000000..a672460e4 --- /dev/null +++ b/public/js/api/factory/dashboard/dashboardAdmin.js @@ -0,0 +1,85 @@ +/* +* 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 . +*/ + +export default { + addDashboard(params) { + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/createDashboard', + params + }; + }, + updateDashboard(params) { + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/updateDashboard', + params + }; + }, + deleteDashboard(dashboard_id) { + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/deleteDashboard', + params: { dashboard_id } + }; + }, + getAllDashboards(){ + return { + method: 'get', + url: 'api/frontend/v1/dashboard/DashboardAdmin/getAllDashboards' + }; + }, + getAllWidgets(dashboard_id){ + return { + method: 'get', + url: 'api/frontend/v1/dashboard/DashboardAdmin/getAllWidgets', + params: {dashboard_id} + }; + }, + setWidgetAllowed(params){ + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/setWidgetAllowed', + params + }; + }, + loadFunktionen(){ + return { + method: 'get', + url: 'api/frontend/v1/dashboard/DashboardAdmin/funktionen' + }; + }, + addWidgetsToPreset(params){ + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/addWidgetsToPreset', + params + }; + }, + removeWidgetFromPreset(params){ + return { + method: 'post', + url: 'api/frontend/v1/dashboard/DashboardAdmin/removeWidgetFromPreset', + params + }; + }, + presetBatch(params){ + return { + method: 'get', + url: 'api/frontend/v1/dashboard/DashboardAdmin/presetBatch', + params + }; + }, +} \ No newline at end of file diff --git a/public/js/apps/DashboardAdmin.js b/public/js/apps/DashboardAdmin_DEPR.js similarity index 100% rename from public/js/apps/DashboardAdmin.js rename to public/js/apps/DashboardAdmin_DEPR.js diff --git a/public/js/components/Dashboard/Admin.js b/public/js/components/Dashboard/Admin.js index f1a837880..b35346fb5 100644 --- a/public/js/components/Dashboard/Admin.js +++ b/public/js/components/Dashboard/Admin.js @@ -3,11 +3,14 @@ import DashboardAdminEdit from "./Admin/Edit.js"; import DashboardAdminWidgets from "./Admin/Widgets.js"; import DashboardAdminPresets from "./Admin/Presets.js"; +import ApiDashboardAdmin from "../../api/factory/dashboard/dashboardAdmin.js"; + export default { + name: 'DashboardAdmin', components: { DashboardAdminEdit, DashboardAdminWidgets, - DashboardAdminPresets + DashboardAdminPresets, }, provide() { return { @@ -22,9 +25,6 @@ export default { }; }, computed: { - apiurl() { - return FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router + '/dashboard'; - }, dashboard() { return this.dashboards.find(el => el.dashboard_id == this.current); } @@ -35,33 +35,49 @@ export default { BsPrompt.popup('New Dashboard name').then( name => { _name = name; - return axios.post(this.apiurl + '/Dashboard/create', { + const params = { dashboard_kurzbz: name - }) - } - ).then(res => { - let newDashboard = { - dashboard_id: res.data.retval, - dashboard_kurzbz: _name, - beschreibung: '' - }; - this.dashboards.push(newDashboard); - this.current = newDashboard.dashboard_id; - }).catch(err => err !== undefined ? console.error('ERROR:', err) : 0); + }; + return this.$api + .call(ApiDashboardAdmin.addDashboard(params)) + .then(response =>{ + this.$fhcAlert.alertSuccess(this.$p.t('ui', 'successSave')); //TODO(Manu) phrase here working + + let newDashboard = { + dashboard_id: response.data, + dashboard_kurzbz: _name, + beschreibung: '' + }; + this.dashboards.push(newDashboard); + this.current = newDashboard.dashboard_id; + }) + .catch(this.$fhcAlert.handleSystemError); + }); }, dashboardUpdate(dashboard) { - // TODO(chris): Loading or message - axios.post(this.apiurl + '/Dashboard/update', dashboard).then(() => { - let old = this.dashboards.find(el => el.dashboard_id == dashboard.dashboard_id); - old.dashboard_kurzbz = dashboard.dashboard_kurzbz; - old.beschreibung = dashboard.beschreibung; - }).catch(err => console.error('ERROR:', err)); + return this.$api + .call(ApiDashboardAdmin.updateDashboard(dashboard)) + .then(response =>{ + this.$fhcAlert.alertSuccess(this.$p.t('ui', 'successSave')); //TODO(Manu) phrase here working + + let old = this.dashboards.find(el => el.dashboard_id == dashboard.dashboard_id); + old.dashboard_kurzbz = dashboard.dashboard_kurzbz; + old.beschreibung = dashboard.beschreibung; + }) + .catch(this.$fhcAlert.handleSystemError); }, dashboardDelete(dashboard_id) { - axios.post(this.apiurl + '/Dashboard/delete', {dashboard_id}).then(() => { - this.current = -1; - this.dashboards = this.dashboards.filter(el => el.dashboard_id != dashboard_id); - }).catch(err => console.error('ERROR:', err)); + return this.$api + .call(ApiDashboardAdmin.deleteDashboard(dashboard_id)) + .then(response => { + this.$fhcAlert.alertSuccess(this.$p.t('ui', 'successDelete')); //TODO(Manu) phrase here NOT always working + + }) + .catch(this.$fhcAlert.handleSystemError) + .finally(() => { + this.current = -1; + this.dashboards = this.dashboards.filter(el => el.dashboard_id != dashboard_id); + }); }, assignWidgets(widgets) { this.widgets = widgets; @@ -72,18 +88,23 @@ export default { } }, created() { - axios.get(this.apiurl + '/Dashboard').then(res => { - this.dashboards = res.data.retval; - }).catch(err => console.error('ERROR:', err)); + this.$api + .call(ApiDashboardAdmin.getAllDashboards()) + .then(result => { + this.dashboards = result.data.retval; + }) + .catch(this.$fhcAlert.handleSystemError); }, template: `
+
- -
+