2011-03-22 10:26:53 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController}
|
|
|
|
* pair, which are then used to handle the request.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage control
|
|
|
|
*/
|
|
|
|
class ModelAsController extends Controller implements NestedController {
|
2013-07-22 16:31:07 +02:00
|
|
|
private static $extensions = array('OldPageRedirector');
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
|
|
|
|
* return it.
|
|
|
|
*
|
|
|
|
* @param SiteTree $sitetree
|
|
|
|
* @param string $action
|
|
|
|
* @return ContentController
|
|
|
|
*/
|
2013-10-23 15:54:07 +02:00
|
|
|
public static function controller_for(SiteTree $sitetree, $action = null) {
|
|
|
|
if ($sitetree->class == 'SiteTree') {
|
|
|
|
$controller = "ContentController";
|
|
|
|
} else {
|
|
|
|
$ancestry = ClassInfo::ancestry($sitetree->class);
|
|
|
|
while ($class = array_pop($ancestry)) {
|
|
|
|
if (class_exists($class . "_Controller")) break;
|
|
|
|
}
|
|
|
|
$controller = ($class !== null) ? "{$class}_Controller" : "ContentController";
|
|
|
|
}
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
if($action && class_exists($controller . '_' . ucfirst($action))) {
|
|
|
|
$controller = $controller . '_' . ucfirst($action);
|
|
|
|
}
|
|
|
|
|
2012-05-30 07:09:25 +02:00
|
|
|
return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree;
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function init() {
|
|
|
|
singleton('SiteTree')->extend('modelascontrollerInit', $this);
|
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @uses ModelAsController::getNestedController()
|
2014-02-10 21:35:13 +01:00
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @param DataModel $model
|
2011-03-22 10:26:53 +01:00
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2011-05-01 07:42:27 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model) {
|
2015-04-30 01:04:02 +02:00
|
|
|
$this->setRequest($request);
|
2012-05-01 04:46:16 +02:00
|
|
|
$this->setDataModel($model);
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
$this->pushCurrent();
|
|
|
|
|
|
|
|
// Create a response just in case init() decides to redirect
|
|
|
|
$this->response = new SS_HTTPResponse();
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
// If we had a redirection or something, halt processing.
|
|
|
|
if($this->response->isFinished()) {
|
|
|
|
$this->popCurrent();
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the database has not yet been created, redirect to the build page.
|
2013-06-21 00:45:33 +02:00
|
|
|
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
2011-03-22 10:26:53 +01:00
|
|
|
$this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
|
|
|
$this->popCurrent();
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = $this->getNestedController();
|
|
|
|
|
|
|
|
if($result instanceof RequestHandler) {
|
2015-04-30 01:04:02 +02:00
|
|
|
$result = $result->handleRequest($this->getRequest(), $model);
|
2011-03-22 10:26:53 +01:00
|
|
|
} else if(!($result instanceof SS_HTTPResponse)) {
|
|
|
|
user_error("ModelAsController::getNestedController() returned bad object type '" .
|
|
|
|
get_class($result)."'", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
} catch(SS_HTTPResponse_Exception $responseException) {
|
|
|
|
$result = $responseException->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ContentController
|
2014-02-10 21:35:13 +01:00
|
|
|
* @throws Exception If URLSegment not passed in as a request parameter.
|
2011-03-22 10:26:53 +01:00
|
|
|
*/
|
|
|
|
public function getNestedController() {
|
2015-04-30 01:04:02 +02:00
|
|
|
$request = $this->getRequest();
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
if(!$URLSegment = $request->param('URLSegment')) {
|
|
|
|
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
|
|
|
|
}
|
2012-05-08 22:10:44 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
// Find page by link, regardless of current locale settings
|
2011-03-22 09:30:10 +01:00
|
|
|
if(class_exists('Translatable')) Translatable::disable_locale_filter();
|
2013-06-21 00:45:33 +02:00
|
|
|
|
|
|
|
// Select child page
|
|
|
|
$conditions = array('"SiteTree"."URLSegment"' => rawurlencode($URLSegment));
|
|
|
|
if(SiteTree::config()->nested_urls) {
|
|
|
|
$conditions[] = array('"SiteTree"."ParentID"' => 0);
|
|
|
|
}
|
|
|
|
$sitetree = DataObject::get_one('SiteTree', $conditions);
|
|
|
|
|
|
|
|
// Check translation module
|
|
|
|
// @todo Refactor out module specific code
|
2011-03-22 09:30:10 +01:00
|
|
|
if(class_exists('Translatable')) Translatable::enable_locale_filter();
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
if(!$sitetree) {
|
2012-09-27 02:30:44 +02:00
|
|
|
$response = ErrorPage::response_for(404);
|
|
|
|
$this->httpError(404, $response ? $response : 'The requested page could not be found.');
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enforce current locale setting to the loaded SiteTree object
|
2011-03-22 09:30:10 +01:00
|
|
|
if(class_exists('Translatable') && $sitetree->Locale) Translatable::set_current_locale($sitetree->Locale);
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
if(isset($_REQUEST['debug'])) {
|
|
|
|
Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}");
|
|
|
|
}
|
|
|
|
|
2015-04-30 01:04:02 +02:00
|
|
|
return self::controller_for($sitetree, $this->getRequest()->param('Action'));
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2013-07-22 16:31:07 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
2013-07-22 16:31:07 +02:00
|
|
|
* @deprecated 3.2 Use OldPageRedirector::find_old_page instead
|
|
|
|
*
|
2011-03-22 10:26:53 +01:00
|
|
|
* @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment.
|
2014-02-10 21:35:13 +01:00
|
|
|
* @param int $parent The ID of the parent of the page the URLSegment belongs to.
|
|
|
|
* @param bool $ignoreNestedURLs
|
2011-03-22 10:26:53 +01:00
|
|
|
* @return SiteTree
|
|
|
|
*/
|
2013-07-22 16:31:07 +02:00
|
|
|
static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) {
|
|
|
|
Deprecation::notice('3.2', 'Use OldPageRedirector::find_old_page instead');
|
|
|
|
if ($parent) {
|
|
|
|
$parent = SiteTree::get()->byId($parent);
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2013-07-22 16:31:07 +02:00
|
|
|
$url = OldPageRedirector::find_old_page(array($URLSegment), $parent);
|
|
|
|
return SiteTree::get_by_link($url);
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2012-04-11 04:51:33 +02:00
|
|
|
}
|