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);
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})',

View File

@ -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');
}
}

View File

@ -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)) {