diff --git a/src/View/Helper/StringFromPathHelper.php b/src/View/Helper/DataFromPathHelper.php similarity index 61% rename from src/View/Helper/StringFromPathHelper.php rename to src/View/Helper/DataFromPathHelper.php index 6603f21..5c8b363 100644 --- a/src/View/Helper/StringFromPathHelper.php +++ b/src/View/Helper/DataFromPathHelper.php @@ -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; + } } diff --git a/templates/MetaTemplates/index.php b/templates/MetaTemplates/index.php index 11864f2..6a534c2 100644 --- a/templates/MetaTemplates/index.php +++ b/templates/MetaTemplates/index.php @@ -66,7 +66,7 @@ echo $this->element('genericElements/IndexTable/index_table', [ if (!empty($conflictingTemplate)) { return sprintf( "%s %s.
- %s %s %s", + ", __('Conflict with:'), $this->Html->link( h($conflictingTemplate->name), diff --git a/templates/element/genericElements/IndexTable/Fields/toggle.php b/templates/element/genericElements/IndexTable/Fields/toggle.php index e111f75..c8f8a3e 100644 --- a/templates/element/genericElements/IndexTable/Fields/toggle.php +++ b/templates/element/genericElements/IndexTable/Fields/toggle.php @@ -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']); ?>