new: [meta templates] reworked

pull/34/head
iglocska 2020-11-20 11:09:24 +01:00
parent ac79db5699
commit 6df0ce51d3
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
7 changed files with 126 additions and 19 deletions

View File

@ -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
]
]
]

View File

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

View File

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

View File

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

View File

@ -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',

View File

@ -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,
'<br /><br />',
empty($ajax) ? '</div>' : ''

View File

@ -0,0 +1,29 @@
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><?= h($title) ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p><?= h($question) ?></p>
</div>
<div class="modal-footer">
<?= $this->Form->postLink(
h($actionName),
$path,
['class' => 'btn btn-primary button-execute']
)
?>
<button type="button" class="btn btn-secondary cancel-button" data-dismiss="modal"><?= __('Cancel') ?></button>
</div>
</div>
</div>
<script type="text/javascript">
$(document).keydown(function(e) {
if(e.which === 13 && e.ctrlKey) {
$('.button-execute').click();
}
});
</script>