Skip to content

Commit

Permalink
Merge branch (pull request #247) 'template-global-fixes' of https://g…
Browse files Browse the repository at this point in the history
…ithub.com/sminnee/sapphire into sminnee-template-global-fixes
  • Loading branch information
halkyon committed May 19, 2012
2 parents aaf9115 + 8bbfa97 commit e5e8f48
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
26 changes: 0 additions & 26 deletions control/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,32 +405,6 @@ function can($perm, $member = null) {

//-----------------------------------------------------------------------------------

/**
* returns a date object for use within a template
* Usage: $Now.Year - Returns 2006
* @return Date The current date
*/
function Now() {
$d = new Date(null);
$d->setValue(date("Y-m-d h:i:s"));
return $d;
}

/**
* Returns the currently logged in user
*/
function CurrentMember() {
return Member::currentUser();
}

/**
* Return true if the visitor has signed up for a login account before
* @return boolean
*/
function PastMember() {
return Cookie::get("PastMember") ? true : false;
}

/**
* Pushes this controller onto the stack of current controllers.
* This means that any redirection, session setting, or other things that rely on Controller::curr() will now write to this
Expand Down
10 changes: 5 additions & 5 deletions docs/en/reference/built-in-page-controls.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ If the user is logged in this will print out
Welcome Back, Admin


#### <% if PastMember %>
#### <% if IsRepeatMember %>

Detect the visitor's previous experience with the site. `$PastMember` will return true if the visitor has signed up or
logged in on the site before.
Detect the visitor's previous experience with the site. `$IsRepeatMember` will return true if the visitor has signed up or logged in on the site before.

Note that as of version 2.4 `$PastVisitor` is deprecated. If you wish to check if a visitor has been to the site before,
set a cookie with `Cookie::set()` and test for it with `Cookie::get()`.
Note that as of version 2.4 `$PastVisitor` is deprecated. If you wish to check if a visitor has been to the site before, set a cookie with `Cookie::set()` and test for it with `Cookie::get()`.

Note that in 2.4 this variable was called `$PastMember`. This still works in 3.0 but is deprecated.

### Date and Time

Expand Down
8 changes: 7 additions & 1 deletion model/fieldtypes/Datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @package framework
* @subpackage model
*/
class SS_Datetime extends Date {
class SS_Datetime extends Date implements TemplateGlobalProvider {

function setValue($value, $record = null) {
if($value === false || $value === null || (is_string($value) && !strlen($value))) {
Expand Down Expand Up @@ -132,5 +132,11 @@ static function set_mock_now($datetime) {
static function clear_mock_now() {
self::$mock_now = null;
}

public static function get_template_global_variables() {
return array(
'Now' => array('method' => 'now', 'casting' => 'SS_Datetime'),
);
}
}

12 changes: 10 additions & 2 deletions security/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,13 @@ static function currentUser() {
return DataObject::get_one("Member", "\"Member\".\"ID\" = $id", true, 1);
}
}


/**
* Returns true if the current member is a repeat visitor who has logged in more than once.
*/
static function is_repeat_member() {
return Cookie::get("PastMember") ? true : false;
}

/**
* Get the ID of the current logged in user
Expand Down Expand Up @@ -1393,7 +1399,9 @@ function getHtmlEditorConfigForCMS() {
public static function get_template_global_variables() {
return array(
'CurrentMember' => 'currentUser',
'currentUser'
'currentUser',
'PastMember' => 'is_repeat_member',
'IsRepeatMember' => 'is_repeat_member',
);
}
}
Expand Down
38 changes: 26 additions & 12 deletions view/SSViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ function resetLocalScope(){
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':
Expand All @@ -66,10 +71,7 @@ function obj($name){
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;
Expand Down Expand Up @@ -402,21 +404,33 @@ function getInjectedValue($property, $params, $cast = true) {

// 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 = Config::inst()->get('ViewableData', 'default_cast', Config::FIRST_SET);
// If the handler returns an object, then we don't need to cast.
if(is_object($res['value'])) {
$res['obj'] = $res['value'];
} else {
// Get the object to cast as
$casting = isset($source['casting']) ? $source['casting'] : null;

$obj = new $casting($property);
$obj->setValue($res['value']);
// If not provided, use default
if (!$casting) $casting = Config::inst()->get('ViewableData', 'default_cast', Config::FIRST_SET);

$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
Expand Down

0 comments on commit e5e8f48

Please sign in to comment.