diff --git a/src/Controller/Component/CRUDComponent.php b/src/Controller/Component/CRUDComponent.php index d847a10..b851934 100644 --- a/src/Controller/Component/CRUDComponent.php +++ b/src/Controller/Component/CRUDComponent.php @@ -413,7 +413,11 @@ class CRUDComponent extends Component $data = $this->Table->get($id, $params); if ($this->request->is(['post', 'put'])) { - $data->{$fieldName} = !$data->{$fieldName}; + if (isset($params['force_state'])) { + $data->{$fieldName} = $params['force_state']; + } else { + $data->{$fieldName} = !$data->{$fieldName}; + } $savedData = $this->Table->save($data); if ($savedData !== false) { $message = __('{0} field {1}. (ID: {2} {3})', diff --git a/src/Controller/MetaTemplatesController.php b/src/Controller/MetaTemplatesController.php index 16dbc5b..a66cc59 100644 --- a/src/Controller/MetaTemplatesController.php +++ b/src/Controller/MetaTemplatesController.php @@ -73,17 +73,17 @@ class MetaTemplatesController extends AppController public function toggle($id, $fieldName = 'enabled') { - $this->CRUD->toggle($id, $fieldName); + if ($this->request->is('POST') && $fieldName == 'is_default') { + $template = $this->MetaTemplates->get($id); + $this->MetaTemplates->removeDefaultFlag($template->scope); + $this->CRUD->toggle($id, $fieldName, ['force_state' => !$template->is_default]); + } else { + $this->CRUD->toggle($id, $fieldName); + } if ($this->ParamHandler->isRest()) { return $this->restResponsePayload; } else if($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) { return $this->ajaxResponsePayload; } } - - public function getDefaultTemplatePerScope($scope = '') - { - $defaultTemplate = $this->MetaTemplates->getDefaultTemplatePerScope($scope); - return $this->RestResponse->viewData($defaultTemplate, 'json'); - } } diff --git a/src/Model/Table/MetaTemplatesTable.php b/src/Model/Table/MetaTemplatesTable.php index 7910729..1097950 100644 --- a/src/Model/Table/MetaTemplatesTable.php +++ b/src/Model/Table/MetaTemplatesTable.php @@ -82,6 +82,14 @@ class MetaTemplatesTable extends AppTable return $query->all()->toArray(); } + public function removeDefaultFlag(String $scope) + { + $this->updateAll( + ['is_default' => false], + ['scope' => $scope] + ); + } + public function loadMetaFile(String $filePath) { if (file_exists($filePath)) {