chg: [genericElement:index] Allow support of closure for variables and type

pull/37/head
mokaddem 2020-12-10 16:50:46 +01:00
parent 3d647680a0
commit fa49821731
6 changed files with 75 additions and 11 deletions

View File

@ -55,6 +55,7 @@ class MetaTemplatesController extends AppController
if ($this->ParamHandler->isRest()) { if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload; return $this->restResponsePayload;
} }
$this->set('defaultTemplatePerScope', $this->MetaTemplates->getDefaultTemplatePerScope());
$this->set('alignmentScope', 'individuals'); $this->set('alignmentScope', 'individuals');
$this->set('metaGroup', 'Administration'); $this->set('metaGroup', 'Administration');
} }
@ -79,4 +80,10 @@ class MetaTemplatesController extends AppController
return $this->ajaxResponsePayload; return $this->ajaxResponsePayload;
} }
} }
public function getDefaultTemplatePerScope($scope = '')
{
$defaultTemplate = $this->MetaTemplates->getDefaultTemplatePerScope($scope);
return $this->RestResponse->viewData($defaultTemplate, 'json');
}
} }

View File

@ -68,6 +68,20 @@ class MetaTemplatesTable extends AppTable
return $template; return $template;
} }
public function getDefaultTemplatePerScope(String $scope = '')
{
$query = $this->find('list', [
'keyField' => 'scope',
'valueField' => function ($template) {
return $template;
}
])->where(['is_default' => true]);
if (!empty($scope)) {
$query->where(['scope' => $scope]);
}
return $query->all()->toArray();
}
public function loadMetaFile(String $filePath) public function loadMetaFile(String $filePath)
{ {
if (file_exists($filePath)) { if (file_exists($filePath)) {

View File

@ -18,20 +18,26 @@ class StringFromPathHelper extends Helper
if (!empty($dataPaths)) { if (!empty($dataPaths)) {
$extractedVars = []; $extractedVars = [];
foreach ($dataPaths as $i => $dataPath) { foreach ($dataPaths as $i => $dataPath) {
$varValue = '';
if (is_array($dataPath)) { if (is_array($dataPath)) {
$varValue = ''; $varValue = '';
if (!empty($dataPath['datapath'])) { if (!empty($dataPath['datapath'])) {
$varValue = Hash::get($data, $dataPath['datapath']); $varValue = Hash::get($data, $dataPath['datapath']);
} else if (!empty($dataPath['raw'])) { } else if (!empty($dataPath['raw'])) {
$varValue = $dataPath['raw']; $varValue = $dataPath['raw'];
} else if (!empty($dataPath['function'])) {
$varValue = $dataPath['function']($data, $dataPath);
} }
$extractedVars[] = $varValue; // $extractedVars[] = $varValue;
} else { } else {
$extractedVars[] = Hash::get($data, $dataPath); $varValue = Hash::get($data, $dataPath);
} }
if (empty($dataPath['function'])) {
$varValue = $options['sanitize'] ? h($varValue) : $varValue;
}
$extractedVars[] = $varValue;
} }
foreach ($extractedVars as $i => $value) { foreach ($extractedVars as $i => $value) {
$value = $options['sanitize'] ? h($value) : $value;
$value = $options['highlight'] ? "<span class=\"font-weight-light\">${value}</span>" : $value; $value = $options['highlight'] ? "<span class=\"font-weight-light\">${value}</span>" : $value;
$str = str_replace( $str = str_replace(
"{{{$i}}}", "{{{$i}}}",

View File

@ -57,19 +57,40 @@ echo $this->element('genericElements/IndexTable/index_table', [
'titleHtml_vars' => ['name'], 'titleHtml_vars' => ['name'],
'bodyHtml' => $this->Html->nestedList([ 'bodyHtml' => $this->Html->nestedList([
__('Only one template per scope can be set as the default template'), __('Only one template per scope can be set as the default template'),
__('Current scope: {{0}}'), '{{1}}',
]), ]),
'bodyHtml_vars' => ['scope'], 'bodyHtml_vars' => [
'type' => 'confirm-warning', 'scope',
[
'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data);
if (!empty($conflictingTemplate)) {
return sprintf('<span class="text-danger font-weight-bolder">%s</span> %s', __('Conflict with:'), h($conflictingTemplate->name));
}
return __('Current scope: {0}', h($row->scope));
},
'data' => [
'defaultTemplatePerScope' => $defaultTemplatePerScope
]
]
],
'type' => [
'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data);
if (!empty($conflictingTemplate)) {
return 'confirm-danger';
}
return 'confirm-warning';
},
'data' => [
'defaultTemplatePerScope' => $defaultTemplatePerScope
]
],
'confirmText' => __('Yes, set as default') 'confirmText' => __('Yes, set as default')
], ],
'disable' => [ 'disable' => [
'titleHtml' => __('Remove {{0}} as the default template?'), 'titleHtml' => __('Remove {{0}} as the default template?'),
'titleHtml_vars' => ['name'], 'titleHtml_vars' => ['name'],
'bodyHtml' => $this->Html->nestedList([
__('Current scope: {{0}}'),
]),
'bodyHtml_vars' => ['scope'],
'type' => 'confirm-warning', 'type' => 'confirm-warning',
'confirmText' => __('Yes, do not set as default') 'confirmText' => __('Yes, do not set as default')
] ]
@ -109,4 +130,14 @@ echo $this->element('genericElements/IndexTable/index_table', [
] ]
] ]
]); ]);
function getConflictingTemplate($row, $data) {
if (!empty($data['data']['defaultTemplatePerScope'][$row->scope])) {
$conflictingTemplate = $data['data']['defaultTemplatePerScope'][$row->scope];
if (!empty($conflictingTemplate)) {
return $conflictingTemplate;
}
}
return [];
}
?> ?>

View File

@ -47,6 +47,12 @@
['highlight' => true] ['highlight' => true]
); );
} }
if (!empty($confirmOptions[$optionType]['type'])) {
if (!empty($confirmOptions[$optionType]['type']['function'])) {
$typeData = !empty($confirmOptions[$optionType]['type']['data']) ? $confirmOptions[$optionType]['type'] : [];
$confirmOptions[$optionType]['type'] = $confirmOptions[$optionType]['type']['function']($row, $typeData);
}
}
} }
} }
$url = $this->StringFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']); $url = $this->StringFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']);

View File

@ -139,7 +139,7 @@ class ModalFactory {
scrollable: false, scrollable: false,
title: '', title: '',
titleHtml: false, titleHtml: false,
body: '', body: false,
bodyHtml: false, bodyHtml: false,
variant: '', variant: '',
modalClass: [], modalClass: [],