Skip to content

Commit

Permalink
#2679: Auto-redirect renamed pages
Browse files Browse the repository at this point in the history
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65781 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sam Minnee committed Nov 13, 2008
1 parent 2f25dda commit f11d7cc
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions core/control/ModelAsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ public function handleRequest($request) {
$this->urlParams = $request->allParams();

$this->init();
$nested = $this->getNestedController();
if(is_object($nested)) {
$result = $nested->handleRequest($request);
} else {
$result = $nested;
$result = $this->getNestedController();

if(is_object($result) && $result instanceOf RequestHandler) {
$result = $result->handleRequest($request);
}

$this->popCurrent();
Expand All @@ -39,6 +38,20 @@ public function getNestedController() {
$child = DataObject::get_one("SiteTree", "URLSegment = '$SQL_URLSegment'", false);
}
if(!$child) {
if($child = $this->findOldPage($SQL_URLSegment)) {
$url = Controller::join_links(
Director::baseURL(),
$child->URLSegment,
$this->urlParams['Action'],
$this->urlParams['ID'],
$this->urlParams['OtherID']
);

$response = new HTTPResponse();
$response->redirect($url, 301);
return $response;
}

$child = $this->get404Page();
}

Expand Down Expand Up @@ -67,6 +80,23 @@ public function getNestedController() {
}
}

protected function findOldPage($urlSegment) {
$versionedQuery = new SQLQuery (
'RecordID', 'SiteTree_versions',
"`WasPublished` = 1 AND `URLSegment` = '$urlSegment'",
'`LastEdited` DESC, `WasPublished`',
null, null, 1
);

$result = $versionedQuery->execute();

if($result->numRecords() == 1 && $redirectPage = $result->nextRecord()) {
if($redirectObj = DataObject::get_by_id('SiteTree', $redirectPage['RecordID'])) return $redirectObj;
}

return false;
}

protected function get404Page() {
if($page = DataObject::get_one("ErrorPage", "ErrorCode = '404'")) return $page;
else return DataObject::get_one("SiteTree", "URLSegment = '404'");
Expand Down

0 comments on commit f11d7cc

Please sign in to comment.