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\View\Helper;
use Cake\Utility\Hash; use Cake\Utility\Hash;
class StringFromPathHelper extends Helper class DataFromPathHelper extends Helper
{ {
private $defaultOptions = [ private $defaultOptions = [
'sanitize' => true, // Should the variables to be injected into the string be sanitized. (ignored with the function) '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 * - `raw`: A raw string to be injecte as-is
* - `function`: A function to be executed with its $strArgs being passed * - `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 String * @return String The string with its arguments replaced by their value
*/ */
public function buildStringFromDataPath(String $str, $data=[], array $strArgs=[], array $options=[]) public function buildStringFromDataPath(String $str, $data=[], array $strArgs=[], array $options=[])
{ {
@ -62,4 +62,34 @@ class StringFromPathHelper extends Helper
} }
return $str; 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)) { if (!empty($conflictingTemplate)) {
return sprintf( return sprintf(
"<span class=\"text-danger font-weight-bolder\">%s</span> %s.<br /> "<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:'), __('Conflict with:'),
$this->Html->link( $this->Html->link(
h($conflictingTemplate->name), h($conflictingTemplate->name),

View File

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