From 6df0ce51d3b375a3098289532f100deda7f399bb Mon Sep 17 00:00:00 2001 From: iglocska Date: Fri, 20 Nov 2020 11:09:24 +0100 Subject: [PATCH] new: [meta templates] reworked --- src/Controller/Component/ACLComponent.php | 7 ++++ src/Controller/Component/CRUDComponent.php | 24 ++++++++++- src/Controller/MetaTemplatesController.php | 41 ++++++++++++++++++- src/Model/Table/MetaTemplatesTable.php | 21 ++++++++-- templates/MetaTemplates/index.php | 18 ++++---- .../genericElements/Form/genericForm.php | 5 +-- templates/genericTemplates/confirm.php | 29 +++++++++++++ 7 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 templates/genericTemplates/confirm.php diff --git a/src/Controller/Component/ACLComponent.php b/src/Controller/Component/ACLComponent.php index 802b015..1cc9897 100644 --- a/src/Controller/Component/ACLComponent.php +++ b/src/Controller/Component/ACLComponent.php @@ -625,6 +625,13 @@ class ACLComponent extends Component 'actions' => ['delete', 'edit', 'view'], 'skipTopMenu' => 1, 'popup' => 1 + ], + 'update' => [ + 'url' => '/metaTemplates/update', + 'label' => __('Update Meta Templates'), + 'actions' => ['index', 'view'], + 'skipTopMenu' => 1, + 'popup' => 1 ] ] ] diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index c4ecb63..3378ad2 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -53,7 +53,7 @@ class CRUDComponent extends Component $metaQuery = $this->MetaTemplates->find(); $metaQuery->where([ 'scope' => $this->Table->metaFields, - //'enabled' => 1 + 'enabled' => 1 ]); $metaQuery->contain(['MetaTemplateFields']); $metaTemplates = $metaQuery->all(); @@ -341,4 +341,26 @@ class CRUDComponent extends Component } return $query; } + + public function toggleEnabled(int $id, array $path, string $fieldName = 'enabled'): bool + { + if (empty($id)) { + throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); + } + $data = $this->Table->get($id); + if ($this->request->is('post')) { + $data[$fieldName] = $data[$fieldName] ? true : false; + $this->Table->save($data); + $this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData(['value' => $data[$fieldName]], 'json'); + } else { + if ($this->Controller->ParamHandler->isRest()) { + $this->Controller->restResponsePayload = $this->Controller->RestResponse->viewData(['value' => $data[$fieldName]], 'json'); + } else { + $this->Controller->set('fieldName', $fieldName); + $this->Controller->set('currentValue', $data[$fieldName]); + $this->Controller->set('path', $path); + $this->Controller->render('/genericTemplates/ajaxForm'); + } + } + } } diff --git a/src/Controller/MetaTemplatesController.php b/src/Controller/MetaTemplatesController.php index ff5a041..ac3df97 100644 --- a/src/Controller/MetaTemplatesController.php +++ b/src/Controller/MetaTemplatesController.php @@ -11,8 +11,23 @@ class MetaTemplatesController extends AppController { public function update() { - $result = $this->MetaTemplates->update(); - return $this->RestResponse->viewData($result, 'json'); + if ($this->request->is('post')) { + $result = $this->MetaTemplates->update(); + if ($this->ParamHandler->isRest()) { + return $this->RestResponse->viewData($result, 'json'); + } else { + $this->Flash->success(__('{0} templates updated.', count($result))); + $this->redirect($this->referer()); + } + } else { + if (!$this->ParamHandler->isRest()) { + $this->set('title', __('Update Meta Templates')); + $this->set('question', __('Are you sure you wish to update the Meta Template definitions?')); + $this->set('actionName', __('Update')); + $this->set('path', ['controller' => 'metaTemplates', 'action' => 'update']); + $this->render('/genericTemplates/confirm'); + } + } } public function index() @@ -39,4 +54,26 @@ class MetaTemplatesController extends AppController } $this->set('metaGroup', 'Administration'); } + + public function toggle($id) + { + $template = $this->MetaTemplates->getTemplate($id); + $template['enabled'] = $template['enabled'] ? 0 : 1; + $result = $this->MetaTemplates->save($template); + if ($template['enabled']) { + $message = $result ? __('Template enabled.') : __('Could not enable template'); + } else { + $message = $result ? __('Template disabled.') : __('Could not disable template'); + } + if ($this->ParamHandler->isRest()) { + if ($result) { + return $this->RestResponse->saveSuccessResponse('MetaTemplates', 'toggle', $id, 'json', $message); + } else { + return $this->RestResponse->saveFailResponse('MetaTemplates', 'toggle', $id, 'json', $message); + } + } else { + if ($this->Flash->{$result ? 'success' : 'error'}($message)); + $this->redirect($this->referer()); + } + } } diff --git a/src/Model/Table/MetaTemplatesTable.php b/src/Model/Table/MetaTemplatesTable.php index 74c6eb0..9fce152 100644 --- a/src/Model/Table/MetaTemplatesTable.php +++ b/src/Model/Table/MetaTemplatesTable.php @@ -47,8 +47,9 @@ class MetaTemplatesTable extends AppTable $files = scandir($path); foreach ($files as $k => $file) { if (substr($file, -5) === '.json') { - $files_processed[] = $file; - $this->loadMetaFile($path . $file); + if ($this->loadMetaFile($path . $file) === true) { + $files_processed[] = $file; + } } } } @@ -56,6 +57,17 @@ class MetaTemplatesTable extends AppTable return $files_processed; } + public function getTemplate($id) + { + $query = $this->find(); + $query->where(['id' => $id]); + $template = $query->first(); + if (empty($template)) { + throw new NotFoundException(__('Invalid template ID specified.')); + } + return $template; + } + public function loadMetaFile(String $filePath) { if (file_exists($filePath)) { @@ -69,17 +81,18 @@ class MetaTemplatesTable extends AppTable $template = $this->newEntity($metaTemplate); $result = $this->save($template); if (!$result) { - return false; + return __('Something went wrong, could not create the template.'); } } else { if ($template->version >= $metaTemplate['version']) { - return true; + return false; } foreach (['version', 'source', 'name', 'namespace', 'scope', 'description'] as $field) { $template->{$field} = $metaTemplate[$field]; } $result = $this->save($template); if (!$result) { + return __('Something went wrong, could not update the template.'); return false; } } diff --git a/templates/MetaTemplates/index.php b/templates/MetaTemplates/index.php index d8547ee..c6987d7 100644 --- a/templates/MetaTemplates/index.php +++ b/templates/MetaTemplates/index.php @@ -20,6 +20,12 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'sort' => 'id', 'data_path' => 'id', ], + [ + 'name' => 'Enabled', + 'sort' => 'enabled', + 'data_path' => 'enabled', + 'element' => 'boolean' + ], [ 'name' => __('Scope'), 'sort' => 'scope', @@ -39,13 +45,7 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'name' => __('UUID'), 'sort' => 'uuid', 'data_path' => 'uuid' - ], - [ - 'name' => 'Enabled', - 'sort' => 'enabled', - 'data_path' => 'enabled', - 'element' => 'boolean' - ], + ] ], 'title' => __('Meta Field Templates'), 'description' => __('The various templates used to enrich certain objects by a set of standardised fields.'), @@ -57,7 +57,7 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'icon' => 'eye' ], [ - 'url' => '/metaTemplates/enable', + 'url' => '/metaTemplates/toggle', 'url_params_data_paths' => ['id'], 'title' => __('Enable template'), 'icon' => 'plus', @@ -68,7 +68,7 @@ echo $this->element('genericElements/IndexTable/index_table', [ ] ], [ - 'url' => '/metaTemplates/disable', + 'url' => '/metaTemplates/toggle', 'url_params_data_paths' => ['id'], 'title' => __('DIsable template'), 'icon' => 'minus', diff --git a/templates/element/genericElements/Form/genericForm.php b/templates/element/genericElements/Form/genericForm.php index fd424fa..a0167a6 100644 --- a/templates/element/genericElements/Form/genericForm.php +++ b/templates/element/genericElements/Form/genericForm.php @@ -103,7 +103,7 @@ $ajaxFlashMessage, $formCreate, $fieldsString, - empty($metaFieldStrings) ? '' : $this->element( + empty($metaFieldString) ? '' : $this->element( 'genericElements/accordion_scaffold', [ 'body' => $metaFieldString, 'title' => 'Meta fields' @@ -126,14 +126,13 @@ $data['description'] ), $fieldsString, - empty($metaFieldStrings) ? '' : $this->element( + empty($metaFieldString) ? '' : $this->element( 'genericElements/accordion_scaffold', [ 'body' => $metaFieldString, 'title' => 'Meta fields' ] ), $this->element('genericElements/Form/submitButton', $submitButtonData), - //$metaFieldString, $formEnd, '

', empty($ajax) ? '' : '' diff --git a/templates/genericTemplates/confirm.php b/templates/genericTemplates/confirm.php new file mode 100644 index 0000000..ea4c3b2 --- /dev/null +++ b/templates/genericTemplates/confirm.php @@ -0,0 +1,29 @@ + +