From acdd07ac6f38872d4ac9b96d3bcd7adce55f3bff Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Fri, 16 Mar 2012 14:14:04 +1300 Subject: [PATCH] BUGFIX: Allow template globals to be used in both object and value contexts (e.g. $Now.Year) BUGFIX: Allow template globals to return objects as well as values. --- view/SSViewer.php | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/view/SSViewer.php b/view/SSViewer.php index c07d8b83e..0f5a5d6a0 100644 --- a/view/SSViewer.php +++ b/view/SSViewer.php @@ -52,7 +52,12 @@ class SSViewer_Scope { array_splice($this->itemStack, $this->localIndex+1); } - function obj($name){ + function getObj($name, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) { + $on = $this->itemIterator ? $this->itemIterator->current() : $this->item; + return $on->obj($name, $arguments, $forceReturnedObject, $cache, $cacheName); + } + + function obj($name, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null){ switch ($name) { case 'Up': @@ -64,10 +69,7 @@ class SSViewer_Scope { break; default: - $on = $this->itemIterator ? $this->itemIterator->current() : $this->item; - - $arguments = func_get_args(); - $this->item = call_user_func_array(array($on, 'obj'), $arguments); + $this->item = $this->getObj($name, $arguments, $forceReturnedObject, $cache, $cacheName); $this->itemIterator = null; $this->upIndex = $this->currentIndex ? $this->currentIndex : count($this->itemStack)-1; @@ -389,21 +391,33 @@ class SSViewer_DataPresenter extends SSViewer_Scope { // If we want to provide a casted object, look up what type object to use if ($cast) { - // Get the object to cast as - $casting = isset($source['casting']) ? $source['casting'] : null; - // If not provided, use default - if (!$casting) $casting = Object::get_static('ViewableData', 'default_cast'); + // If the handler returns an object, then we don't need to cast. + if(is_object($res['value'])) { + $res['obj'] = $res['value']; - $obj = new $casting($property); - $obj->setValue($res['value']); + } else { + // Get the object to cast as + $casting = isset($source['casting']) ? $source['casting'] : null; + // If not provided, use default + if (!$casting) $casting = Object::get_static('ViewableData', 'default_cast'); - $res['obj'] = $obj; + $obj = new $casting($property); + $obj->setValue($res['value']); + + $res['obj'] = $obj; + } } return $res; } } + function getObj($name, $arguments = null, $forceReturnedObject = true, $cache = false, $cacheName = null) { + $result = $this->getInjectedValue($name, (array)$arguments); + if($result) return $result['obj']; + else return parent::getObj($name, $arguments, $forceReturnedObject, $cache, $cacheName); + } + function __call($name, $arguments) { //extract the method name and parameters $property = $arguments[0]; //the name of the function being called