chg: [helper:stringFromPath] Added same feature but for array of strings

pull/37/head
mokaddem 2020-12-11 10:11:59 +01:00
parent f279939c8b
commit f4725207fc
3 changed files with 49 additions and 27 deletions

View File

@ -5,7 +5,7 @@ namespace App\View\Helper;
use Cake\View\Helper;
use Cake\Utility\Hash;
class StringFromPathHelper extends Helper
class DataFromPathHelper extends Helper
{
private $defaultOptions = [
'sanitize' => true, // Should the variables to be injected into the string be sanitized. (ignored with the function)
@ -25,7 +25,7 @@ class StringFromPathHelper extends Helper
* - `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 String
* @return String The string with its arguments replaced by their value
*/
public function buildStringFromDataPath(String $str, $data=[], array $strArgs=[], array $options=[])
{
@ -62,4 +62,34 @@ class StringFromPathHelper extends Helper
}
return $str;
}
/**
* buildStringsInArray
*
* @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 $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=[])
{
foreach ($instructions as $stringPath => $argsPath) {
$theString = Hash::get($stringArray, $stringPath);
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);
} else {
$newString = $this->buildStringFromDataPath($theString, $data, $theArgs, $options);
}
$stringArray = Hash::insert($stringArray, $stringPath, $newString);
}
}
return $stringArray;
}
}

View File

@ -66,7 +66,7 @@ echo $this->element('genericElements/IndexTable/index_table', [
if (!empty($conflictingTemplate)) {
return sprintf(
"<span class=\"text-danger font-weight-bolder\">%s</span> %s.<br />
<span class=\"font-weight-bolder\">%s</span> %s <span class=\"font-weight-bolder\">%s</span>",
<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),

View File

@ -30,32 +30,24 @@
$tempboxId
);
// inject title and body vars into their placeholder
// inject variables into the strings
if (!empty($field['toggle_data']['confirm'])) {
$availableConfirmOptions = ['enable', 'disable'];
$confirmOptions = $field['toggle_data']['confirm'];
foreach ($availableConfirmOptions as $optionType) {
$availableType = ['title', 'titleHtml', 'body', 'bodyHtml'];
foreach ($availableType as $varType) {
if (!isset($confirmOptions[$optionType][$varType])) {
continue;
}
$confirmOptions[$optionType][$varType] = $this->StringFromPath->buildStringFromDataPath(
$confirmOptions[$optionType][$varType],
$row,
$confirmOptions[$optionType][$varType . '_vars'],
['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);
}
}
}
$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]);
}
$url = $this->StringFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']);
$url = $this->DataFromPath->buildStringFromDataPath($field['url'], $row, $field['url_params_vars']);
?>
<?php if ($requirementMet): ?>