2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-10-11 02:07:19 +02:00
|
|
|
* ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController}
|
|
|
|
* pair, which are then used to handle the request.
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
|
|
|
* @package sapphire
|
2009-03-22 23:59:14 +01:00
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class ModelAsController extends Controller implements NestedController {
|
|
|
|
|
2009-10-11 02:07:02 +02:00
|
|
|
/**
|
|
|
|
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
|
|
|
|
* return it.
|
|
|
|
*
|
2009-10-11 02:07:19 +02:00
|
|
|
* @param SiteTree $sitetree
|
|
|
|
* @param string $action
|
2009-10-11 02:07:02 +02:00
|
|
|
* @return ContentController
|
|
|
|
*/
|
2009-10-11 02:07:19 +02:00
|
|
|
public static function controller_for(SiteTree $sitetree, $action = null) {
|
2010-01-13 00:06:59 +01:00
|
|
|
if($sitetree->class == 'SiteTree') $controller = "ContentController";
|
|
|
|
else $controller = "{$sitetree->class}_Controller";
|
2010-02-04 06:16:44 +01:00
|
|
|
|
2009-10-11 02:07:02 +02:00
|
|
|
if($action && class_exists($controller . '_' . ucfirst($action))) {
|
|
|
|
$controller = $controller . '_' . ucfirst($action);
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
return class_exists($controller) ? new $controller($sitetree) : $sitetree;
|
2009-10-11 02:07:02 +02:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
public function init() {
|
|
|
|
singleton('SiteTree')->extend('modelascontrollerInit', $this);
|
2010-02-04 06:16:44 +01:00
|
|
|
parent::init();
|
2009-10-11 02:07:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @uses ModelAsController::getNestedController()
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @return SS_HTTPResponse
|
2009-10-11 02:07:19 +02:00
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
public function handleRequest(SS_HTTPRequest $request) {
|
2009-10-11 02:07:19 +02:00
|
|
|
$this->request = $request;
|
2007-08-18 01:11:43 +02:00
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
$this->pushCurrent();
|
2010-02-04 06:16:44 +01:00
|
|
|
|
|
|
|
// Create a response just in case init() decides to redirect
|
|
|
|
$this->response = new SS_HTTPResponse();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->init();
|
2010-02-16 02:20:19 +01:00
|
|
|
|
|
|
|
// If we had a redirection or something, halt processing.
|
|
|
|
if($this->response->isFinished()) {
|
|
|
|
$this->popCurrent();
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
// If the database has not yet been created, redirect to the build page.
|
2009-03-10 23:08:52 +01:00
|
|
|
if(!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
|
2009-11-24 11:52:17 +01:00
|
|
|
$this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
2009-04-29 01:40:35 +02:00
|
|
|
$this->popCurrent();
|
2009-10-11 02:07:19 +02:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
return $this->response;
|
|
|
|
}
|
2009-10-11 02:07:19 +02:00
|
|
|
|
2009-11-10 22:47:25 +01:00
|
|
|
try {
|
|
|
|
$result = $this->getNestedController();
|
2010-04-26 02:23:42 +02:00
|
|
|
|
2009-11-10 22:47:25 +01:00
|
|
|
if($result instanceof RequestHandler) {
|
|
|
|
$result = $result->handleRequest($this->request);
|
2010-01-13 00:28:12 +01:00
|
|
|
} else if(!($result instanceof SS_HTTPResponse)) {
|
2010-01-13 00:06:59 +01:00
|
|
|
user_error("ModelAsController::getNestedController() returned bad object type '" .
|
|
|
|
get_class($result)."'", E_USER_WARNING);
|
2009-11-10 22:47:25 +01:00
|
|
|
}
|
|
|
|
} catch(SS_HTTPResponse_Exception $responseException) {
|
|
|
|
$result = $responseException->getResponse();
|
2008-05-15 10:44:37 +02:00
|
|
|
}
|
2007-08-18 01:11:43 +02:00
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
/**
|
|
|
|
* @return ContentController
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getNestedController() {
|
2009-10-11 02:07:19 +02:00
|
|
|
$request = $this->request;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
if(!$URLSegment = $request->param('URLSegment')) {
|
|
|
|
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
|
|
|
|
}
|
2010-04-26 02:23:42 +02:00
|
|
|
|
2010-03-11 23:27:03 +01:00
|
|
|
// Find page by link, regardless of current locale settings
|
2009-10-11 02:07:22 +02:00
|
|
|
Translatable::disable_locale_filter();
|
2010-03-11 23:27:03 +01:00
|
|
|
$sitetree = DataObject::get_one(
|
|
|
|
'SiteTree',
|
|
|
|
sprintf(
|
|
|
|
'"URLSegment" = \'%s\' %s',
|
|
|
|
Convert::raw2sql($URLSegment),
|
|
|
|
(SiteTree::nested_urls() ? 'AND "ParentID" = 0' : null)
|
|
|
|
)
|
|
|
|
);
|
2009-10-11 02:07:22 +02:00
|
|
|
Translatable::enable_locale_filter();
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
if(!$sitetree) {
|
|
|
|
// If a root page has been renamed, redirect to the new location.
|
2010-04-26 02:23:42 +02:00
|
|
|
// See ContentController->handleRequest() for similiar logic.
|
|
|
|
$redirect = self::find_old_page($URLSegment);
|
|
|
|
if($redirect = self::find_old_page($URLSegment)) {
|
|
|
|
$params = $request->getVars();
|
|
|
|
if(isset($params['url'])) unset($params['url']);
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
$this->response = new SS_HTTPResponse();
|
2010-04-26 02:23:42 +02:00
|
|
|
$this->response->redirect(
|
|
|
|
Controller::join_links(
|
|
|
|
$redirect->Link(
|
|
|
|
Controller::join_links(
|
|
|
|
$request->param('Action'),
|
|
|
|
$request->param('ID'),
|
|
|
|
$request->param('OtherID')
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// Needs to be in separate join links to avoid urlencoding
|
|
|
|
($params) ? '?' . http_build_query($params) : null
|
|
|
|
),
|
|
|
|
301
|
|
|
|
);
|
2009-01-16 05:07:17 +01:00
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
return $this->response;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-11 02:07:25 +02:00
|
|
|
if($response = ErrorPage::response_for(404)) {
|
2009-10-11 02:07:19 +02:00
|
|
|
return $response;
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-10-11 02:07:19 +02:00
|
|
|
$this->httpError(404, 'The requested page could not be found.');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-11 02:07:19 +02:00
|
|
|
|
2010-03-11 23:27:03 +01:00
|
|
|
// Enforce current locale setting to the loaded SiteTree object
|
2009-10-11 02:07:19 +02:00
|
|
|
if($sitetree->Locale) Translatable::set_current_locale($sitetree->Locale);
|
|
|
|
|
|
|
|
if(isset($_REQUEST['debug'])) {
|
|
|
|
Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::controller_for($sitetree, $this->request->param('Action'));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
/**
|
2010-04-26 02:23:42 +02:00
|
|
|
* @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment.
|
|
|
|
* @param int $parentID The ID of the parent of the page the URLSegment belongs to.
|
2009-10-11 02:07:19 +02:00
|
|
|
* @return SiteTree
|
|
|
|
*/
|
2010-05-10 08:24:50 +02:00
|
|
|
static function find_old_page($URLSegment,$parentID = 0, $ignoreNestedURLs = false) {
|
2009-10-11 02:07:19 +02:00
|
|
|
$URLSegment = Convert::raw2sql($URLSegment);
|
|
|
|
|
2010-05-10 08:24:50 +02:00
|
|
|
$useParentIDFilter = SiteTree::nested_urls() && !$ignoreNestedURLs;
|
|
|
|
|
2009-10-11 02:07:19 +02:00
|
|
|
// First look for a non-nested page that has a unique URLSegment and can be redirected to.
|
2010-04-26 02:23:42 +02:00
|
|
|
if(SiteTree::nested_urls()) {
|
|
|
|
$pages = DataObject::get(
|
|
|
|
'SiteTree',
|
2010-05-10 08:24:50 +02:00
|
|
|
"\"URLSegment\" = '$URLSegment'" . ($useParentIDFilter ? ' AND "ParentID" = ' . (int)$parentID : '')
|
2010-04-26 02:23:42 +02:00
|
|
|
);
|
|
|
|
if($pages && $pages->Count() == 1) return $pages->First();
|
2009-03-04 04:44:11 +01:00
|
|
|
}
|
2009-10-11 02:07:19 +02:00
|
|
|
|
|
|
|
// Get an old version of a page that has been renamed.
|
|
|
|
$query = new SQLQuery (
|
|
|
|
'"RecordID"',
|
|
|
|
'"SiteTree_versions"',
|
2010-05-10 08:24:50 +02:00
|
|
|
"\"URLSegment\" = '$URLSegment' AND \"WasPublished\" = 1" . ($useParentIDFilter ? ' AND "ParentID" = ' . (int)$parentID : ''),
|
2009-10-11 02:07:19 +02:00
|
|
|
'"LastEdited" DESC',
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
1
|
|
|
|
);
|
2009-12-10 09:50:12 +01:00
|
|
|
$record = $query->execute()->first();
|
|
|
|
|
2010-05-10 08:24:50 +02:00
|
|
|
if($record && ($oldPage = DataObject::get_by_id('SiteTree', $record['RecordID']))) {
|
2009-12-10 09:50:12 +01:00
|
|
|
// Run the page through an extra filter to ensure that all decorators are applied.
|
|
|
|
if(SiteTree::get_by_link($oldPage->RelativeLink())) return $oldPage;
|
2008-11-13 03:10:15 +01:00
|
|
|
}
|
2010-05-10 08:24:50 +02:00
|
|
|
|
|
|
|
// Otherwise, if nested URLs is enabled, look for a page anywhere in the site that may be
|
|
|
|
// left from pre-nested-URLs days
|
|
|
|
if(SiteTree::nested_urls() && !$ignoreNestedURLs && $parentID == 0) {
|
|
|
|
return self::find_old_page($URLSegment, $parentID, true);
|
|
|
|
}
|
2008-11-13 03:10:15 +01:00
|
|
|
}
|
|
|
|
|
2009-12-10 09:50:12 +01:00
|
|
|
}
|