From 9cff50fa3b5475642a11c6fca148568d11fb6fb5 Mon Sep 17 00:00:00 2001 From: chfhtw Date: Mon, 23 Mar 2026 15:44:42 +0100 Subject: [PATCH] extract preset logic from dashboard admin api --- .../frontend/v1/dashboard/DashboardAdmin.php | 148 +------------ .../frontend/v1/dashboard/admin/Preset.php | 196 ++++++++++++++++++ .../api/factory/dashboard/dashboardAdmin.js | 20 +- system/phrasesupdate.php | 60 ------ 4 files changed, 207 insertions(+), 217 deletions(-) create mode 100644 application/controllers/api/frontend/v1/dashboard/admin/Preset.php diff --git a/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php b/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php index af844b29e..414475aba 100644 --- a/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php +++ b/application/controllers/api/frontend/v1/dashboard/DashboardAdmin.php @@ -31,11 +31,7 @@ class DashboardAdmin extends FHCAPI_Controller 'getAllDashboards' => 'dashboard/admin:r', 'createDashboard' => 'dashboard/admin:rw', 'updateDashboard' => 'dashboard/admin:rw', - 'deleteDashboard' => 'dashboard/admin:rw', - 'addWidgetsToPreset' => 'dashboard/admin:rw', - 'removeWidgetFromPreset' => 'dashboard/admin:rw', - 'funktionen' => 'dashboard/admin:r', - 'presetBatch' => 'dashboard/admin:r' + 'deleteDashboard' => 'dashboard/admin:rw' ]); // Load language phrases @@ -144,146 +140,4 @@ class DashboardAdmin extends FHCAPI_Controller } $this->terminateWithSuccess($result); } - - //Presets - public function funktionen() - { - $dashboard_kurzbz = $this->input->get('db'); - if(!$dashboard_kurzbz) - { - $this->terminateWithError('missing query parameter db'); - } - $sql = <<db->escape($dashboard_kurzbz)} - ), - general as ( - select - 'general' as funktion_kurzbz, - 'Allgemein' as beschreibung - ) - - ( - select - f.funktion_kurzbz, - f.beschreibung, - count(p.preset_id) as has_preset - from - general f - left join - dashboard_presets p on p.funktion_kurzbz is null - group by - f.funktion_kurzbz, f.beschreibung - ) - union ALL - ( - select - f.funktion_kurzbz, - f.beschreibung, - count(p.preset_id) as has_preset - from - public.tbl_funktion f - left join - dashboard_presets p on p.funktion_kurzbz = f.funktion_kurzbz - group by - f.funktion_kurzbz, f.beschreibung - order by - f.beschreibung asc - ) -EOSQL; - $result = $this->FunktionModel->execReadOnlyQuery($sql); - - if (isError($result)) - $this->terminateWithError($result, self::ERROR_TYPE_GENERAL); - - $funktionen = getData($result) ?: []; - $this->terminateWithSuccess($funktionen); - } - - 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/controllers/api/frontend/v1/dashboard/admin/Preset.php b/application/controllers/api/frontend/v1/dashboard/admin/Preset.php new file mode 100644 index 000000000..7dc810181 --- /dev/null +++ b/application/controllers/api/frontend/v1/dashboard/admin/Preset.php @@ -0,0 +1,196 @@ +. + */ + +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 Preset extends FHCAPI_Controller +{ + public function __construct() + { + parent::__construct([ + 'list' => 'dashboard/admin:r', + 'getBatch' => 'dashboard/admin:r', + 'addWidgets' => 'dashboard/admin:rw', + 'removeWidget' => 'dashboard/admin:rw' + ]); + + // Load language phrases + $this->loadPhrases([ + 'ui' + ]); + + // Libraries + $this->load->library('dashboard/DashboardLib'); + + // Models + $this->load->model('ressource/Funktion_model', 'FunktionModel'); + } + + public function list($dashboard_kurzbz) + { + $sql = " + WITH + dashboard_presets AS ( + SELECT + * + FROM + dashboard.tbl_dashboard_preset dp + JOIN + dashboard.tbl_dashboard d ON d.dashboard_id = dp.dashboard_id + WHERE + d.dashboard_kurzbz = {$this->db->escape($dashboard_kurzbz)} + ), + general AS ( + SELECT + 'general' AS funktion_kurzbz, + 'Allgemein' AS beschreibung + ) + + ( + SELECT + f.funktion_kurzbz, + f.beschreibung, + COUNT(p.preset_id) AS has_preset + FROM + general f + LEFT JOIN + dashboard_presets p ON p.funktion_kurzbz IS NULL + GROUP BY + f.funktion_kurzbz, f.beschreibung + ) + UNION ALL + ( + SELECT + f.funktion_kurzbz, + f.beschreibung, + COUNT(p.preset_id) AS has_preset + FROM + public.tbl_funktion f + LEFT JOIN + dashboard_presets p ON p.funktion_kurzbz = f.funktion_kurzbz + GROUP BY + f.funktion_kurzbz, f.beschreibung + ORDER BY + f.beschreibung ASC + ) + "; + + $result = $this->FunktionModel->execReadOnlyQuery($sql); + + $funktionen = $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess($funktionen); + } + + public function getBatch() + { + $this->load->library('form_validation'); + + $this->form_validation->set_rules('db', 'Dashboard', 'required'); + + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); + + $db = $this->input->post('db'); + $funktionen = $this->input->post('funktionen') ?: []; + + $result = []; + + 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] = []; + } + } + + return $this->terminateWithSuccess($result); + } + + public function addWidgets() + { + $this->load->library('form_validation'); + + $this->form_validation->set_rules('db', 'Dashboard', 'required'); + $this->form_validation->set_rules('funktion_kurzbz', 'Funktion', 'required'); + + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); + + $dashboard_kurzbz = $this->input->post('db'); + $funktion_kurzbz = $this->input->post('funktion_kurzbz'); + $widgets = $this->input->post('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); + + $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess($preset_decoded); + } + + public function removeWidget() + { + $this->load->library('form_validation'); + + $this->form_validation->set_rules('db', 'Dashboard', 'required'); + $this->form_validation->set_rules('funktion_kurzbz', 'Funktion', 'required'); + $this->form_validation->set_rules('widgetid', 'Widget', 'required'); + + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); + + $dashboard_kurzbz = $this->input->post('db'); + $funktion_kurzbz = $this->input->post('funktion_kurzbz'); + $widgetid = $this->input->post('widgetid'); + + $preset = $this->dashboardlib->getPreset($dashboard_kurzbz, $funktion_kurzbz); + if (!$preset) + show_404(); + + $preset_decoded = json_decode($preset->preset, true); + + if (!$this->dashboardlib->removeWidgetFromWidgets($preset_decoded, $funktion_kurzbz, $widgetid)) + show_404(); + + $preset->preset = json_encode($preset_decoded); + + $result = $this->dashboardlib->insertOrUpdatePreset($preset); + + $this->getDataOrTerminateWithError($result); + + $this->terminateWithSuccess(array('msg' => $this->p->t('dashboard', 'success_savePreset'))); + } +} diff --git a/public/js/api/factory/dashboard/dashboardAdmin.js b/public/js/api/factory/dashboard/dashboardAdmin.js index 4a6745866..f734dae64 100644 --- a/public/js/api/factory/dashboard/dashboardAdmin.js +++ b/public/js/api/factory/dashboard/dashboardAdmin.js @@ -41,32 +41,32 @@ export default { url: 'api/frontend/v1/dashboard/DashboardAdmin/getAllDashboards' }; }, - loadFunktionen(dashboard_kurzbz){ + loadFunktionen(dashboard_kurzbz) { return { method: 'get', - url: 'api/frontend/v1/dashboard/DashboardAdmin/funktionen?db=' + url: 'api/frontend/v1/dashboard/admin/preset/list/' + encodeURIComponent(dashboard_kurzbz) }; }, - addWidgetsToPreset(params){ + presetBatch(params) { return { method: 'post', - url: 'api/frontend/v1/dashboard/DashboardAdmin/addWidgetsToPreset', + url: 'api/frontend/v1/dashboard/admin/preset/getBatch', params }; }, - removeWidgetFromPreset(params){ + addWidgetsToPreset(params) { return { method: 'post', - url: 'api/frontend/v1/dashboard/DashboardAdmin/removeWidgetFromPreset', + url: 'api/frontend/v1/dashboard/admin/preset/addWidgets', params }; }, - presetBatch(params){ + removeWidgetFromPreset(params) { return { - method: 'get', - url: 'api/frontend/v1/dashboard/DashboardAdmin/presetBatch', + method: 'post', + url: 'api/frontend/v1/dashboard/admin/preset/removeWidget', params }; - }, + } } \ No newline at end of file diff --git a/system/phrasesupdate.php b/system/phrasesupdate.php index 9c458fd47..f7628f8ef 100644 --- a/system/phrasesupdate.php +++ b/system/phrasesupdate.php @@ -57217,66 +57217,6 @@ I have been informed that I am under no obligation to consent to the transmissio ) ) ), - array( - 'app' => 'core', - 'category' => 'dashboard', - 'phrase' => 'error_savePreset', - 'insertvon' => 'system', - 'phrases' => array( - array( - 'sprache' => 'German', - 'text' => 'Voreinstellung konnten nicht gespeichert werden.', - 'description' => '', - 'insertvon' => 'system' - ), - array( - 'sprache' => 'English', - 'text' => 'Preset could not be saved.', - 'description' => '', - 'insertvon' => 'system' - ) - ) - ), - array( - 'app' => 'core', - 'category' => 'dashboard', - 'phrase' => 'error_presetAndFunctionNotFound', - 'insertvon' => 'system', - 'phrases' => array( - array( - 'sprache' => 'German', - 'text' => 'Voreinstellung für Dashboard {dashboard} und Funktion {funktion} nicht gefunden.', - 'description' => '', - 'insertvon' => 'system' - ), - array( - 'sprache' => 'English', - 'text' => 'Preset for dashboard {dashboard} and function {funktion} not found.', - 'description' => '', - 'insertvon' => 'system' - ) - ) - ), - array( - 'app' => 'core', - 'category' => 'dashboard', - 'phrase' => 'error_deleteWidget', - 'insertvon' => 'system', - 'phrases' => array( - array( - 'sprache' => 'German', - 'text' => 'Widget konnte nicht entfernt werden', - 'description' => '', - 'insertvon' => 'system' - ), - array( - 'sprache' => 'English', - 'text' => 'failed to remove widget', - 'description' => '', - 'insertvon' => 'system' - ) - ) - ), array( 'app' => 'core', 'category' => 'dashboard',