From bf50f560dff8eef5480a2db292a5f3e2bb3ae4cd Mon Sep 17 00:00:00 2001 From: Andrew Short Date: Sun, 11 Oct 2009 00:07:10 +0000 Subject: [PATCH] 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 git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88487 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/ContentController.php | 4 +++ core/control/Director.php | 56 +++++++++++++++++------------- core/model/SiteTree.php | 8 ++--- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/core/control/ContentController.php b/core/control/ContentController.php index 56715cf19..6e0d9ed0a 100755 --- a/core/control/ContentController.php +++ b/core/control/ContentController.php @@ -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; } diff --git a/core/control/Director.php b/core/control/Director.php index 76eafcbd5..1a0ad23dc 100755 --- a/core/control/Director.php +++ b/core/control/Director.php @@ -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(); @@ -296,7 +288,7 @@ class Director { } } } - + /** * Returns the urlParam with the given name */ @@ -310,19 +302,35 @@ class Director { static function urlParams() { return Director::$urlParams; } - + /** - * 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']); - - return SiteTree::get_by_link($SQL_urlSegment); - } else { - return Controller::curr(); - } + user_error ( + 'Director::currentPage() is deprecated, please use Director::get_current_page()', E_USER_NOTICE + ); + + return self::get_current_page(); } /** diff --git a/core/model/SiteTree.php b/core/model/SiteTree.php index e5e827994..0a4aa973c 100755 --- a/core/model/SiteTree.php +++ b/core/model/SiteTree.php @@ -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;