Update Dashboard Endpoints for Admin GUI

This commit is contained in:
cgfhtw
2022-10-28 12:40:57 +02:00
parent be6ad8ce2a
commit 92ec7f44ef
5 changed files with 228 additions and 40 deletions
+51 -4
View File
@@ -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);
}
}
@@ -0,0 +1,87 @@
<?php
defined('BASEPATH') || exit('No direct script access allowed');
/**
* Description of Widget
*
* @author chris
*/
class Dashboard extends Auth_Controller
{
public function __construct()
{
parent::__construct(
array(
'index' => '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) ?: []);
}
}
+54 -25
View File
@@ -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));
}
}
@@ -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;
}
}
@@ -0,0 +1,33 @@
<?php
class Widget_model extends DB_Model
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->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]);
}
}