diff options
author | Urban Müller | 2017-10-16 18:22:27 +0200 |
---|---|---|
committer | Urban Müller | 2017-10-16 18:22:27 +0200 |
commit | 8d90e01b7ffa90519ce7393175ddd446c69fe84c (patch) | |
tree | e40cf9ac706c54c5e295d20bd172f3540d75b471 /it_text.class | |
parent | c63ef15a33d671786ca7e3012b50ba1ad684cf92 (diff) | |
download | itools-8d90e01b7ffa90519ce7393175ddd446c69fe84c.tar.gz itools-8d90e01b7ffa90519ce7393175ddd446c69fe84c.tar.bz2 itools-8d90e01b7ffa90519ce7393175ddd446c69fe84c.zip |
allow simple functions calls in ET() and T() labels
Diffstat (limited to 'it_text.class')
-rw-r--r-- | it_text.class | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/it_text.class b/it_text.class index 29bc2d1..0045370 100644 --- a/it_text.class +++ b/it_text.class @@ -36,6 +36,7 @@ class it_text * @param $p['global'] store text object in global it_text for global functions (default: true) * @param $p['phpfile'] optional texts file(s), default: all texts.php in include path * @param $p['phpfiles'] text files to load in addition to $p['phpfile'] (Note: $p['phpfile'] defaults to all texts.php files in include path) + * @param $p['transmogrifiers'] comma separated functions that may be called by using {foo:bar} (foo will be called with bar as argument) in T() */ function it_text($p = array()) { @@ -47,10 +48,11 @@ function it_text($p = array()) $p['phpfiles'][] = $phpfile; } - $this->p = ($p += array( + $this->p = ($p += (array)$GLOBALS['it_text_defaultconfig'] + array( 'global' => true, 'phpfiles' => array_unique(array_merge((array)$p['phpfiles'], (array)$p['phpfile'])), )); + $this->allowedfuncs = array_flip(explode(",", $p['transmogrifiers'])); # Read and merge texts from php files if none defined yet foreach ($p['phpfiles'] as $phpfile) @@ -170,7 +172,7 @@ function text($label, $language = null) */ function etext($label, $values = null, $language = null) { - return $this->transmogrify($this->text($label, $language), $values, $label); + return $this->transmogrify($this->text($label, $language), $values, $label, $this->allowedfuncs); } @@ -226,22 +228,26 @@ function set($label, $text = null, $language = null) /** - * Replaces variables of the form {obj.var} with value, e.g. {user.name} + * Replaces variables of the form {obj.var} with value from $values, e.g. {user.name}, or result of a func, e.g. {LU:foo.html} * NOTE: Invalid object names or non-existing variables are simply deleted. */ -function transmogrify($text, $values = null, $label = null) +function transmogrify($text, $values = null, $label = null, $allowedfuncs = null) { - foreach (preg_split('/{([\w.]+)}/', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $i => $part) + foreach (preg_split('#{([^}]*)}#', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $i => $part) { if ($i % 2) # odd offsets are delimiters, i.e. braces to be replaced { - $value = $values ? $values : $GLOBALS; - foreach (explode(".", $part) as $key) + if (it::match('^[\w.]+$', $part)) { - $value = is_object($value) ? $value->$key : $value[$key]; - if ($value === null && $values && $label) # do not test in $GLOBALS mode - it::error(array('title' => "No value given for text variable {" . $key ."} in label $label", 'backtraceskip' => 3)); - } + $value = $values ? $values : $GLOBALS; + foreach (explode(".", $part) as $key) + { + $value = is_object($value) ? $value->$key : $value[$key]; + if ($value === null && $values && $label) # do not test in $GLOBALS mode + it::error(array('title' => "No value given for text variable {" . $key ."} in label $label", 'backtraceskip' => 3)); + } + } else + $value = (list($func, $arg) = it::match('^([\w:]+)\((.*)\)$', $part)) && isset($allowedfuncs[$func]) ? call_user_func($func, $arg) : "{" . $part . "}"; $result .= $GLOBALS['debug_texts'] ? "</span>$value<span style='background:#8F8'>" : $value; } |