chg: [metaTemplate:toggle] Enforce one default template per scope

pull/37/head
mokaddem 2020-12-10 17:18:17 +01:00
parent 9a1d254b01
commit 0a377915f6
3 changed files with 20 additions and 8 deletions

View File

@ -413,7 +413,11 @@ class CRUDComponent extends Component
$data = $this->Table->get($id, $params); $data = $this->Table->get($id, $params);
if ($this->request->is(['post', 'put'])) { if ($this->request->is(['post', 'put'])) {
if (isset($params['force_state'])) {
$data->{$fieldName} = $params['force_state'];
} else {
$data->{$fieldName} = !$data->{$fieldName}; $data->{$fieldName} = !$data->{$fieldName};
}
$savedData = $this->Table->save($data); $savedData = $this->Table->save($data);
if ($savedData !== false) { if ($savedData !== false) {
$message = __('{0} field {1}. (ID: {2} {3})', $message = __('{0} field {1}. (ID: {2} {3})',

View File

@ -73,17 +73,17 @@ class MetaTemplatesController extends AppController
public function toggle($id, $fieldName = 'enabled') public function toggle($id, $fieldName = 'enabled')
{ {
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); $this->CRUD->toggle($id, $fieldName);
}
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload; return $this->restResponsePayload;
} else if($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) { } else if($this->ParamHandler->isAjax() && $this->request->is(['post', 'put'])) {
return $this->ajaxResponsePayload; return $this->ajaxResponsePayload;
} }
} }
public function getDefaultTemplatePerScope($scope = '')
{
$defaultTemplate = $this->MetaTemplates->getDefaultTemplatePerScope($scope);
return $this->RestResponse->viewData($defaultTemplate, 'json');
}
} }

View File

@ -82,6 +82,14 @@ class MetaTemplatesTable extends AppTable
return $query->all()->toArray(); return $query->all()->toArray();
} }
public function removeDefaultFlag(String $scope)
{
$this->updateAll(
['is_default' => false],
['scope' => $scope]
);
}
public function loadMetaFile(String $filePath) public function loadMetaFile(String $filePath)
{ {
if (file_exists($filePath)) { if (file_exists($filePath)) {