ENHANCEMENT: Update ContentController to manually set the current Director page in handleRequest().

MINOR: Deprecated Director::currentPage() in favour of Director::get_current_page().

From: Andrew Short <andrewjshort@gmail.com>

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88487 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Andrew Short 2009-10-11 00:07:10 +00:00 committed by Sam Minnee
parent 452b7f7246
commit bf50f560df
3 changed files with 40 additions and 28 deletions

View File

@ -168,6 +168,8 @@ class ContentController extends Controller {
* @return HTTPResponse
*/
public function handleRequest(HTTPRequest $request) {
Director::set_current_page($this->data());
$response = parent::handleRequest($request);
// If the default handler returns an error, due to the action not existing, attempt to fall over to a child
@ -184,6 +186,8 @@ class ContentController extends Controller {
}
}
Director::set_current_page(null);
return $response;
}

View File

@ -13,12 +13,15 @@
*/
class Director {
static private $urlSegment;
static private $urlParams;
static private $rules = array();
/**
* @var SiteTree
*/
private static $current_page;
/**
* @deprecated 2.4
*/
@ -274,17 +277,6 @@ class Director {
return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
} else {
/*
if(isset($arguments['Action'])) {
$arguments['Action'] = str_replace('-','',$arguments['Action']);
}
if(isset($arguments['Action']) && ClassInfo::exists($controller.'_'.$arguments['Action']))
$controller = $controller.'_'.$arguments['Action'];
*/
if(isset($arguments['URLSegment'])) self::$urlSegment = $arguments['URLSegment'] . "/";
Director::$urlParams = $arguments;
$controllerObj = new $controller();
@ -312,17 +304,33 @@ class Director {
}
/**
* Returns the dataobject of the current page.
* This will only return a value if you are looking at a SiteTree page
* Return the {@link SiteTree} object that is currently being viewed. If there is no sitetree object to return,
* then this will return the current controller.
*
* @return SiteTree
*/
public static function get_current_page() {
return self::$current_page ? self::$current_page : Controller::curr();
}
/**
* Set the currently active {@link SiteTree} object that is being used to respond to the request.
*
* @param SiteTree $page
*/
public static function set_current_page($page) {
self::$current_page = $page;
}
/**
* @deprecated 2.4 Use {@link Director::get_current_page()}.
*/
static function currentPage() {
if(isset(Director::$urlParams['URLSegment'])) {
$SQL_urlSegment = Convert::raw2sql(Director::$urlParams['URLSegment']);
user_error (
'Director::currentPage() is deprecated, please use Director::get_current_page()', E_USER_NOTICE
);
return SiteTree::get_by_link($SQL_urlSegment);
} else {
return Controller::curr();
}
return self::get_current_page();
}
/**

View File

@ -453,7 +453,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* @return boolean True if we are in the given section.
*/
public function InSection($sectionName) {
$page = Director::currentPage();
$page = Director::get_current_page();
while($page) {
if($sectionName == $page->URLSegment)
return true;
@ -558,13 +558,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
*/
protected function prepareCurrentAndSection() {
if(!self::$currentPageID || Director::urlParam('URLSegment') != self::$currentPageIDSetFromURLSegment) {
self::$currentPageID = Director::currentPage() ? Director::currentPage()->ID : null;
self::$currentPageID = Director::get_current_page() ? Director::get_current_page()->ID : null;
self::$currentPageIDSetFromURLSegment = Director::urlParam('URLSegment');
if(!isset(self::$currentPageID)) {
self::$currentPageID = -1;
$nextID = (Director::currentPage() && isset(Director::currentPage()->Parent->ID))
? Director::currentPage()->Parent->ID
$nextID = (Director::get_current_page() && isset(Director::get_current_page()->Parent->ID))
? Director::get_current_page()->Parent->ID
: null;
} else {
$nextID = SiteTree::$currentPageID;