chg: [helpers:DataFromPathHelper] Simplified usage

pull/37/head
mokaddem 2021-01-13 14:17:55 +01:00
parent dbeef75d0d
commit 35165129b9
3 changed files with 62 additions and 60 deletions

View File

@ -15,10 +15,10 @@ class DataFromPathHelper extends Helper
/** /**
* buildStringFromDataPath Inject data into a string at the correct place * buildStringFromDataPath Inject data into a string at the correct place
* *
* @param String $str The string that will have its arguements replaced by their value * @param String $str The string that will have its arguments replaced by their value
* @param mixed $data The data from which the value of datapath arguement will be taken * @param mixed $data The data from which the value of datapath arguement will be taken
* @param array $strArgs The arguments to be injected into the string. * @param array $strArgs The arguments to be injected into the string.
* - Each argument ca be of mixed type: * - Each argument can be of mixed type:
* - String: A cakephp's Hash datapath used to extract the data * - String: A cakephp's Hash datapath used to extract the data
* - array: can contain a key of either * - array: can contain a key of either
* - `datapath`: A cakephp's Hash datapath used to extract the data * - `datapath`: A cakephp's Hash datapath used to extract the data
@ -64,30 +64,32 @@ class DataFromPathHelper extends Helper
} }
/** /**
* buildStringsInArray * buildStringsInArray Apply buildStringFromDataPath for all strings in the provided array
* *
* @param array $stringArray The array containing the strings that will have their arguments replaced by their value * @param array $stringArray The array containing the strings that will have their arguments replaced by their value
* @param mixed $data The data from which the value of datapath arguement will be taken * @param mixed $data The data from which the value of datapath arguement will be taken
* @param array $instructions Instruct how $stringArray should be processed * @param array $stringArrayArgs The arguments to be injected into each strings.
* - Keys are the path to the string * - Each argument can be of mixed type:
* - Values are the path to the argument * - String: A cakephp's Hash datapath used to extract the data
* - array: can contain a key of either
* - `datapath`: A cakephp's Hash datapath used to extract the data
* - `raw`: A raw string to be injecte as-is
* - `function`: A function to be executed with its $strArgs being passed
* @param array $options Allows to configure the behavior of the function * @param array $options Allows to configure the behavior of the function
* @return array The array containing the strings with their arguments replaced by their value * @return array The array containing the strings with their arguments replaced by their value
*/ */
public function buildStringsInArray(array $stringArray, $data=[], array $instructions, array $options=[]) public function buildStringsInArray(array $stringArray, $data=[], array $stringArrayArgs, array $options=[])
{ {
foreach ($instructions as $stringPath => $argsPath) { foreach ($stringArrayArgs as $stringName => $argsPath) {
$theString = Hash::get($stringArray, $stringPath); $theString = Hash::get($stringArray, $stringName);
if (!is_null($theString)) { if (!is_null($theString)) {
$theArgs = Hash::get($stringArray, $argsPath); $argsPath = !is_array($argsPath) ? [$argsPath] : $argsPath;
$theArgs = is_null($theArgs) ? [] : $theArgs; if (!empty($argsPath['function'])) {
$theArgs = !is_array($theArgs) ? [$theArgs] : $theArgs; $newString = $argsPath['function']($data, $argsPath);
if (!empty($theArgs['function'])) {
$newString = $theArgs['function']($data, $theArgs);
} else { } else {
$newString = $this->buildStringFromDataPath($theString, $data, $theArgs, $options); $newString = $this->buildStringFromDataPath($theString, $data, $argsPath, $options);
} }
$stringArray = Hash::insert($stringArray, $stringPath, $newString); $stringArray = Hash::insert($stringArray, $stringName, $newString);
} }
} }
return $stringArray; return $stringArray;

View File

@ -55,37 +55,10 @@ echo $this->element('genericElements/IndexTable/index_table', [
'confirm' => [ 'confirm' => [
'enable' => [ 'enable' => [
'titleHtml' => __('Make {{0}} the default template?'), 'titleHtml' => __('Make {{0}} the default template?'),
'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'),
'{{0}}', '{{0}}',
]), ]),
'bodyHtml_vars' => [
[
'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data);
if (!empty($conflictingTemplate)) {
return sprintf(
"<span class=\"text-danger font-weight-bolder\">%s</span> %s.<br />
<ul><li><span class=\"font-weight-bolder\">%s</span> %s <span class=\"font-weight-bolder\">%s</span></li></ul>",
__('Conflict with:'),
$this->Html->link(
h($conflictingTemplate->name),
'/metaTemplates/view/' . h($conflictingTemplate->id),
['target' => '_blank']
),
__('By proceeding'),
h($conflictingTemplate->name),
__('will not be the default anymore')
);
}
return __('Current scope: {0}', h($row->scope));
},
'data' => [
'defaultTemplatePerScope' => $defaultTemplatePerScope
]
]
],
'type' => [ 'type' => [
'function' => function($row, $data) { 'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data); $conflictingTemplate = getConflictingTemplate($row, $data);
@ -98,13 +71,44 @@ echo $this->element('genericElements/IndexTable/index_table', [
'defaultTemplatePerScope' => $defaultTemplatePerScope 'defaultTemplatePerScope' => $defaultTemplatePerScope
] ]
], ],
'confirmText' => __('Yes, set as default') 'confirmText' => __('Yes, set as default'),
'arguments' => [
'titleHtml' => ['name'],
'bodyHtml' => [
[
'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data);
if (!empty($conflictingTemplate)) {
return sprintf(
"<span class=\"text-danger font-weight-bolder\">%s</span> %s.<br />
<ul><li><span class=\"font-weight-bolder\">%s</span> %s <span class=\"font-weight-bolder\">%s</span></li></ul>",
__('Conflict with:'),
$this->Html->link(
h($conflictingTemplate->name),
'/metaTemplates/view/' . h($conflictingTemplate->id),
['target' => '_blank']
),
__('By proceeding'),
h($conflictingTemplate->name),
__('will not be the default anymore')
);
}
return __('Current scope: {0}', h($row->scope));
},
'data' => [
'defaultTemplatePerScope' => $defaultTemplatePerScope
]
]
]
]
], ],
'disable' => [ 'disable' => [
'titleHtml' => __('Remove {{0}} as the default template?'), 'titleHtml' => __('Remove {{0}} as the default template?'),
'titleHtml_vars' => ['name'],
'type' => 'confirm-warning', 'type' => 'confirm-warning',
'confirmText' => __('Yes, do not set as default') 'confirmText' => __('Yes, do not set as default'),
'arguements' => [
'titleHtml' => ['name'],
]
] ]
] ]
] ]

View File

@ -44,20 +44,16 @@
// inject variables into the strings // inject variables into the strings
if (!empty($field['toggle_data']['confirm'])) { if (!empty($field['toggle_data']['confirm'])) {
$instructions = [ $field['toggle_data']['confirm']['enable']['arguments'] = isset($field['toggle_data']['confirm']['enable']['arguments']) ? $field['toggle_data']['confirm']['enable']['arguments'] : [];
'enable.title' => 'enable.title_vars', $field['toggle_data']['confirm']['disable']['arguments'] = isset($field['toggle_data']['confirm']['disable']['arguments']) ? $field['toggle_data']['confirm']['disable']['arguments'] : [];
'enable.titleHtml' => 'enable.titleHtml_vars', $stringArrayEnable = $field['toggle_data']['confirm']['enable'];
'enable.body' => 'enable.body_vars', unset($stringArrayEnable['arguments']);
'enable.bodyHtml' => 'enable.bodyHtml_vars', $stringArrayDisable = $field['toggle_data']['confirm']['disable'];
'enable.type' => 'enable.type', unset($stringArrayDisable['arguments']);
'disable.title' => 'disable.title_vars', $confirmOptions = array_merge(
'disable.titleHtml' => 'disable.titleHtml_vars', $this->DataFromPath->buildStringsInArray($stringArrayEnable, $row, $field['toggle_data']['confirm']['enable']['arguments'], ['highlight' => true]),
'disable.body' => 'disable.body_vars', $this->DataFromPath->buildStringsInArray($stringArrayDisable, $row, $field['toggle_data']['confirm']['disable']['arguments'], ['highlight' => true]),
'disable.bodyHtml' => 'disable.bodyHtml_vars', );
'disable.bodyHtml' => 'disable.bodyHtml_vars',
'disable.type' => 'disable.type',
];
$confirmOptions = $this->DataFromPath->buildStringsInArray($field['toggle_data']['confirm'], $row, $instructions, ['highlight' => true]);
} }
$url = $this->DataFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']); $url = $this->DataFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']);
?> ?>