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
*
* @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 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
* - array: can contain a key of either
* - `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 mixed $data The data from which the value of datapath arguement will be taken
* @param array $instructions Instruct how $stringArray should be processed
* - Keys are the path to the string
* - Values are the path to the argument
* @param array $stringArrayArgs The arguments to be injected into each strings.
* - Each argument can be of mixed type:
* - 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
* @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) {
$theString = Hash::get($stringArray, $stringPath);
foreach ($stringArrayArgs as $stringName => $argsPath) {
$theString = Hash::get($stringArray, $stringName);
if (!is_null($theString)) {
$theArgs = Hash::get($stringArray, $argsPath);
$theArgs = is_null($theArgs) ? [] : $theArgs;
$theArgs = !is_array($theArgs) ? [$theArgs] : $theArgs;
if (!empty($theArgs['function'])) {
$newString = $theArgs['function']($data, $theArgs);
$argsPath = !is_array($argsPath) ? [$argsPath] : $argsPath;
if (!empty($argsPath['function'])) {
$newString = $argsPath['function']($data, $argsPath);
} 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;

View File

@ -55,37 +55,10 @@ echo $this->element('genericElements/IndexTable/index_table', [
'confirm' => [
'enable' => [
'titleHtml' => __('Make {{0}} the default template?'),
'titleHtml_vars' => ['name'],
'bodyHtml' => $this->Html->nestedList([
__('Only one template per scope can be set as the default template'),
'{{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' => [
'function' => function($row, $data) {
$conflictingTemplate = getConflictingTemplate($row, $data);
@ -98,13 +71,44 @@ echo $this->element('genericElements/IndexTable/index_table', [
'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' => [
'titleHtml' => __('Remove {{0}} as the default template?'),
'titleHtml_vars' => ['name'],
'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
if (!empty($field['toggle_data']['confirm'])) {
$instructions = [
'enable.title' => 'enable.title_vars',
'enable.titleHtml' => 'enable.titleHtml_vars',
'enable.body' => 'enable.body_vars',
'enable.bodyHtml' => 'enable.bodyHtml_vars',
'enable.type' => 'enable.type',
'disable.title' => 'disable.title_vars',
'disable.titleHtml' => 'disable.titleHtml_vars',
'disable.body' => 'disable.body_vars',
'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]);
$field['toggle_data']['confirm']['enable']['arguments'] = isset($field['toggle_data']['confirm']['enable']['arguments']) ? $field['toggle_data']['confirm']['enable']['arguments'] : [];
$field['toggle_data']['confirm']['disable']['arguments'] = isset($field['toggle_data']['confirm']['disable']['arguments']) ? $field['toggle_data']['confirm']['disable']['arguments'] : [];
$stringArrayEnable = $field['toggle_data']['confirm']['enable'];
unset($stringArrayEnable['arguments']);
$stringArrayDisable = $field['toggle_data']['confirm']['disable'];
unset($stringArrayDisable['arguments']);
$confirmOptions = array_merge(
$this->DataFromPath->buildStringsInArray($stringArrayEnable, $row, $field['toggle_data']['confirm']['enable']['arguments'], ['highlight' => true]),
$this->DataFromPath->buildStringsInArray($stringArrayDisable, $row, $field['toggle_data']['confirm']['disable']['arguments'], ['highlight' => true]),
);
}
$url = $this->DataFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']);
?>