From 1f32072d692acaaa7abfffe6fac9cba8cd2ef4d6 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Mon, 7 Dec 2020 14:17:10 +0100 Subject: [PATCH] chg: [CRUDComponent] Added toggle - Applied to meta-templates --- src/Controller/Component/CRUDComponent.php | 57 ++++++++++++++++++- .../Component/ParamHandlerComponent.php | 9 +++ .../Component/RestResponseComponent.php | 23 ++++++++ src/Controller/MetaTemplatesController.php | 20 ++----- templates/MetaTemplates/index.php | 4 +- .../IndexTable/Fields/toggle.php | 26 ++++++--- templates/genericTemplates/toggle.php | 1 + 7 files changed, 115 insertions(+), 25 deletions(-) create mode 100644 templates/genericTemplates/toggle.php diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index 5df6396..8578dfe 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -16,7 +16,7 @@ class CRUDComponent extends Component $this->Table = $config['table']; $this->request = $config['request']; $this->TableAlias = $this->Table->getAlias(); - $this->ObjectAlias = \Cake\Utility\Inflector::singularize($this->TableAlias); + $this->ObjectAlias = Inflector::singularize($this->TableAlias); $this->MetaFields = $config['MetaFields']; $this->MetaTemplates = $config['MetaTemplates']; } @@ -349,6 +349,61 @@ class CRUDComponent extends Component return $query; } + public function toggle(int $id, string $fieldName = 'enabled', array $params = []): void + { + if (empty($id)) { + throw new NotFoundException(__('Invalid {0}.', $this->ObjectAlias)); + } + + $data = $this->Table->get($id, $params); + if ($this->request->is(['post', 'put'])) { + $data->{$fieldName} = !$data->{$fieldName}; + $data = $this->Table->save($data); + if ($data !== false) { + $message = __('{0}\'s `{1}` field: {2}. (ID: {3})', + $this->ObjectAlias, + $fieldName, + $data->{$fieldName} ? __('enabled') : __('disabled'), + $data->id, + ); + if ($this->Controller->ParamHandler->isRest()) { + $this->Controller->restResponsePayload = $this->RestResponse->viewData($data, 'json'); + } else if ($this->Controller->ParamHandler->isAjax()) { + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxSuccessResponse($this->ObjectAlias, 'toggle', $data, $message); + } else { + $this->Controller->Flash->success($message); + if (empty($params['redirect'])) { + $this->Controller->redirect(['action' => 'view', $id]); + } else { + $this->Controller->redirect($params['redirect']); + } + } + } else { + $validationMessage = $this->prepareValidationError($data); + $message = __( + '{0} could not be modified.{1}', + $this->ObjectAlias, + empty($validationMessage) ? '' : ' ' . __('Reason:{0}', $validationMessage) + ); + if ($this->Controller->ParamHandler->isRest()) { + } else if ($this->Controller->ParamHandler->isAjax()) { + $this->Controller->ajaxResponsePayload = $this->Controller->RestResponse->ajaxFailResponse($this->ObjectAlias, 'toggle', $data, $message); + } else { + $this->Controller->Flash->error($message); + if (empty($params['redirect'])) { + $this->Controller->redirect(['action' => 'view', $id]); + } else { + $this->Controller->redirect($params['redirect']); + } + } + } + } + $this->Controller->set('entity', $data); + $this->Controller->set('fieldName', $fieldName); + $this->Controller->viewBuilder()->setLayout('ajax'); + $this->Controller->render('/genericTemplates/toggle'); + } + public function toggleEnabled(int $id, array $path, string $fieldName = 'enabled'): bool { if (empty($id)) { diff --git a/src/Controller/Component/ParamHandlerComponent.php b/src/Controller/Component/ParamHandlerComponent.php index 77d1177..0ef678f 100644 --- a/src/Controller/Component/ParamHandlerComponent.php +++ b/src/Controller/Component/ParamHandlerComponent.php @@ -61,4 +61,13 @@ class ParamHandlerComponent extends Component { return (json_decode($data) != null) ? true : false; } + + public function isAjax() + { + if ($this->isAjax !== null) { + return $this->isAjax; + } + $this->isAjax = $this->request->is('ajax'); + return $this->isAjax; + } } diff --git a/src/Controller/Component/RestResponseComponent.php b/src/Controller/Component/RestResponseComponent.php index d433358..4fa5f21 100644 --- a/src/Controller/Component/RestResponseComponent.php +++ b/src/Controller/Component/RestResponseComponent.php @@ -4,6 +4,7 @@ namespace App\Controller\Component; use Cake\Controller\Component; use Cake\Core\Configure; +use Cake\Utility\Inflector; class RestResponseComponent extends Component { @@ -419,6 +420,28 @@ class RestResponseComponent extends Component return $this->__sendResponse($response, 200, $format); } + public function ajaxSuccessResponse($ObjectAlias, $action, $entity, $message) + { + $action = $this->__dissectAdminRouting($action); + $response = [ + 'success' => true, + 'message' => $message, + 'url' => $this->__generateURL($action, $ObjectAlias, $entity->id) + ]; + return $this->viewData($response); + } + + public function ajaxFailResponse($ObjectAlias, $action, $entity, $message) + { + $action = $this->__dissectAdminRouting($action); + $response = [ + 'success' => false, + 'message' => $message, + 'url' => $this->__generateURL($action, $ObjectAlias, $entity->id) + ]; + return $this->viewData($response); + } + private function __sendResponse($response, $code, $format = false, $raw = false, $download = false, $headers = array()) { if (strtolower($format) === 'application/xml' || strtolower($format) === 'xml') { diff --git a/src/Controller/MetaTemplatesController.php b/src/Controller/MetaTemplatesController.php index ac3df97..a1abd66 100644 --- a/src/Controller/MetaTemplatesController.php +++ b/src/Controller/MetaTemplatesController.php @@ -57,23 +57,11 @@ class MetaTemplatesController extends AppController 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'); - } + $this->CRUD->toggle($id); 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()); + return $this->restResponsePayload; + } else if($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) { + return $this->ajaxResponsePayload; } } } diff --git a/templates/MetaTemplates/index.php b/templates/MetaTemplates/index.php index c6987d7..86f8603 100644 --- a/templates/MetaTemplates/index.php +++ b/templates/MetaTemplates/index.php @@ -24,7 +24,9 @@ echo $this->element('genericElements/IndexTable/index_table', [ 'name' => 'Enabled', 'sort' => 'enabled', 'data_path' => 'enabled', - 'element' => 'boolean' + 'element' => 'toggle', + 'url' => '/metaTemplates/toggle', + 'url_params_data_paths' => ['id'], ], [ 'name' => __('Scope'), diff --git a/templates/element/genericElements/IndexTable/Fields/toggle.php b/templates/element/genericElements/IndexTable/Fields/toggle.php index cd17d9d..9461bb6 100644 --- a/templates/element/genericElements/IndexTable/Fields/toggle.php +++ b/templates/element/genericElements/IndexTable/Fields/toggle.php @@ -11,7 +11,7 @@ $checkboxId = 'GenericToggle-' . $seed; $tempboxId = 'TempBox-' . $seed; echo sprintf( - '