Newer
Older
Peter Rotich
committed
<?php
/*********************************************************************
Peter Rotich
committed
Peter Rotich
committed
Peter Rotich <peter@osticket.com>
Peter Rotich
committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
class VariableReplacer {
var $start_delim;
var $end_delim;
var $objects;
var $variables;
var $errors;
function VariableReplacer($start_delim='%{', $end_delim='}') {
$this->start_delim = $start_delim;
$this->end_delim = $end_delim;
$this->objects = array();
$this->variables = array();
}
function setError($error) {
$this->errors[] = $error;
}
function getErrors() {
return $this->errors;
}
Peter Rotich
committed
function getObj($tag) {
return @$this->objects[$tag];
}
if($val && is_object($val)) {
$this->objects[$var] = $val;
} elseif($var && is_array($var)) {
$this->assign($k, $v);
} elseif($var) {
$this->variables[$var] = $val;
}
Peter Rotich
committed
}
function getVar($obj, $var) {
if (!$var) {
if (method_exists($obj, 'asVar'))
return call_user_func(array($obj, 'asVar'), $this);
elseif (method_exists($obj, '__toString'))
return (string) $obj;
}
Peter Rotich
committed
list($v, $part) = explode('.', $var, 2);
if ($v && is_callable(array($obj, 'get'.ucfirst($v)))) {
$rv = call_user_func(array($obj, 'get'.ucfirst($v)), $this);
if(!$rv || !is_object($rv))
return $rv;
Peter Rotich
committed
return $this->getVar($rv, $part);
}
if (is_array($obj) && isset($obj[$v]))
return $obj[$v];
if (!$var || !method_exists($obj, 'getVar'))
Peter Rotich
committed
list($tag, $remainder) = explode('.', $var, 2);
if(($rv = call_user_func(array($obj, 'getVar'), $tag, $this))===false)
Peter Rotich
committed
if(!is_object($rv))
return $rv;
return $this->getVar($rv, $remainder);
Peter Rotich
committed
}
function replaceVars($input) {
Peter Rotich
committed
if($input && is_array($input))
return array_map(array($this, 'replaceVars'), $input);
Peter Rotich
committed
if(!($vars=$this->_parse($input)))
return $input;
Peter Rotich
committed
return str_replace(array_keys($vars), array_values($vars), $input);
Peter Rotich
committed
}
function _resolveVar($var) {
//Variable already memoized?
if($var && @isset($this->variables[$var]))
Peter Rotich
committed
return $this->variables[$var];
$parts = explode('.', $var, 2);
if($parts && ($obj=$this->getObj($parts[0])))
return $this->getVar($obj, $parts[1]);
elseif($parts[0] && @isset($this->variables[$parts[0]])) { //root override
if (is_array($this->variables[$parts[0]])
&& isset($this->variables[$parts[0]][$parts[1]]))
return $this->variables[$parts[0]][$parts[1]];
return $this->variables[$parts[0]];
Peter Rotich
committed
//Unknown object or variable - leavig it alone.
$this->setError(sprintf(__('Unknown object for "%s" tag'), $var));
Peter Rotich
committed
}
function _parse($text) {
$input = $text;
$result = array();
if(!preg_match_all('/'.$this->start_delim.'([A-Za-z_][\w._]+)'.$this->end_delim.'/',
$input, $result))
Peter Rotich
committed
return null;
$vars = array();
foreach($result[0] as $k => $v) {
if(isset($vars[$v])) continue;
$val=$this->_resolveVar($result[1][$k]);
if($val!==false)
Peter Rotich
committed
$vars[$v] = $val;
}
return $vars;
}
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
static function compileScope($scope, $recurse=5, $exclude=false) {
$items = array();
foreach ($scope as $name => $info) {
if ($exclude === $name)
continue;
if (isset($info['class']) && $recurse) {
$items[$name] = $info['desc'];
foreach (static::compileScope($info['class']::getVarScope(), $recurse-1,
@$info['exclude'] ?: $name)
as $name2=>$desc) {
$items["{$name}.{$name2}"] = $desc;
}
}
if (!is_array($info)) {
$items[$name] = $info;
}
}
return $items;
}
static function compileFormScope($form) {
$items = array();
foreach ($form->getFields() as $f) {
if (!($name = $f->get('name')))
continue;
if (!$f->isStorable() || !$f->hasData())
continue;
$desc = $f->getLocal('label');
if (($class = $f->asVarType()) && class_exists($class)) {
$desc = array('desc' => $desc, 'class' => $class);
}
$items[$name] = $desc;
foreach (VariableReplacer::compileFieldScope($f) as $name2=>$desc) {
$items["$name.$name2"] = $desc;
}
}
return $items;
}
static function compileFieldScope($field, $recurse=2, $exclude=false) {
$items = array();
if (!$field->hasSubFields())
return $items;
foreach ($field->getSubFields() as $f) {
if (!($name = $f->get('name')))
continue;
if ($exclude === $name)
continue;
$items[$name] = $f->getLabel();
if ($recurse) {
foreach (static::compileFieldScope($f, $recurse-1, $name)
as $name2=>$desc) {
if (($class = $f->asVarType()) && class_exists($class)) {
$desc = array('desc' => $desc, 'class' => $class);
}
$items["$name.$name2"] = $desc;
}
}
}
return $items;
}
}
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
class PlaceholderList
/* implements TemplateVariable */ {
var $items;
function __construct($items) {
$this->items = $items;
}
function asVar() {
$items = array();
foreach ($this->items as $I) {
if (method_exists($I, 'asVar')) {
$items[] = $I->asVar();
}
else {
$items[] = (string) $I;
}
}
return implode(',', $items);
}
function getVar($tag) {
$items = array();
foreach ($this->items as $I) {
if (is_object($I) && method_exists($I, 'get'.ucfirst($tag))) {
$items[] = call_user_func(array($I, 'get'.ucfirst($tag)));
}
elseif (method_exists($I, 'getVar')) {
$items[] = $I->getVar($tag);
}
}
if (count($items) == 1) {
return $items[0];
}
return new static($items);
}
}
interface TemplateVariable {
// function asVar(); — not absolutely required
// function getVar($name); — not absolutely required
static function getVarScope();