2011-03-22 10:26:53 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Controllers;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Control\NestedController;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2016-07-22 01:32:32 +02:00
|
|
|
use Exception;
|
|
|
|
use Translatable;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* ModelAsController deals with mapping the initial request to the first {@link SiteTree}/{@link ContentController}
|
|
|
|
* pair, which are then used to handle the request.
|
|
|
|
*/
|
|
|
|
class ModelAsController extends Controller implements NestedController {
|
2016-08-10 06:08:39 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
private static $extensions = array('SilverStripe\\CMS\\Controllers\\OldPageRedirector');
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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) {
|
2016-09-08 16:46:48 +02:00
|
|
|
$controller = $sitetree->getControllerName();
|
2011-03-22 10:26:53 +01:00
|
|
|
|
2016-09-08 16:46:48 +02:00
|
|
|
if ($action && class_exists($controller . '_' . ucfirst($action))) {
|
2011-03-22 10:26:53 +01:00
|
|
|
$controller = $controller . '_' . ucfirst($action);
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2016-09-08 16:46:48 +02:00
|
|
|
return Injector::inst()->create($controller, $sitetree);
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
public function init() {
|
2016-07-22 01:32:32 +02:00
|
|
|
singleton('SilverStripe\\CMS\\Model\\SiteTree')->extend('modelascontrollerInit', $this);
|
2011-03-22 10:26:53 +01:00
|
|
|
parent::init();
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2016-09-09 01:26:24 +02:00
|
|
|
protected function beforeHandleRequest(HTTPRequest $request, DataModel $model) {
|
2015-02-23 16:51:09 +01:00
|
|
|
parent::beforeHandleRequest($request, $model);
|
|
|
|
// If the database has not yet been created, redirect to the build page.
|
2016-08-10 06:08:39 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
2015-02-23 16:51:09 +01:00
|
|
|
$this->getResponse()->redirect(Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
'dev/build',
|
2016-05-23 20:04:28 +02:00
|
|
|
'?' . http_build_query(array(
|
2015-02-23 16:51:09 +01:00
|
|
|
'returnURL' => isset($_GET['url']) ? $_GET['url'] : null,
|
2016-05-23 20:04:28 +02:00
|
|
|
))
|
2015-02-23 16:51:09 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* @uses ModelAsController::getNestedController()
|
2016-09-09 01:26:24 +02:00
|
|
|
* @param HTTPRequest $request
|
2014-02-10 21:35:13 +01:00
|
|
|
* @param DataModel $model
|
2016-09-09 01:26:24 +02:00
|
|
|
* @return HTTPResponse
|
2011-03-22 10:26:53 +01:00
|
|
|
*/
|
2016-09-09 01:26:24 +02:00
|
|
|
public function handleRequest(HTTPRequest $request, DataModel $model) {
|
2015-02-23 16:51:09 +01:00
|
|
|
$this->beforeHandleRequest($request, $model);
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
// If we had a redirection or something, halt processing.
|
2015-08-14 02:39:45 +02:00
|
|
|
if($this->getResponse()->isFinished()) {
|
2011-03-22 10:26:53 +01:00
|
|
|
$this->popCurrent();
|
2015-08-14 02:39:45 +02:00
|
|
|
return $this->getResponse();
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the database has not yet been created, redirect to the build page.
|
2016-08-10 06:08:39 +02:00
|
|
|
/** @skipUpgrade */
|
|
|
|
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
2015-08-14 02:39:45 +02:00
|
|
|
$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
2011-03-22 10:26:53 +01:00
|
|
|
$this->popCurrent();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
|
|
|
return $this->getResponse();
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
try {
|
|
|
|
$result = $this->getNestedController();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
if($result instanceof RequestHandler) {
|
2015-04-30 01:04:02 +02:00
|
|
|
$result = $result->handleRequest($this->getRequest(), $model);
|
2016-09-09 01:26:24 +02:00
|
|
|
} else if(!($result instanceof HTTPResponse)) {
|
2015-08-14 02:39:45 +02:00
|
|
|
user_error("ModelAsController::getNestedController() returned bad object type '" .
|
2011-03-22 10:26:53 +01:00
|
|
|
get_class($result)."'", E_USER_WARNING);
|
|
|
|
}
|
2016-09-09 01:26:24 +02:00
|
|
|
} catch(HTTPResponse_Exception $responseException) {
|
2011-03-22 10:26:53 +01:00
|
|
|
$result = $responseException->getResponse();
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* @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();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
if(!$URLSegment = $request->param('URLSegment')) {
|
|
|
|
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
|
|
|
|
}
|
2015-08-14 02:39:45 +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();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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);
|
|
|
|
}
|
2016-08-10 06:08:39 +02:00
|
|
|
/** @var SiteTree $sitetree */
|
2016-07-22 01:32:32 +02:00
|
|
|
$sitetree = DataObject::get_one('SilverStripe\\CMS\\Model\\SiteTree', $conditions);
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2013-06-21 00:45:33 +02:00
|
|
|
// 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();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
if(!$sitetree) {
|
2015-12-28 21:17:31 +01:00
|
|
|
$this->httpError(404, 'The requested page could not be found.');
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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);
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
if(isset($_REQUEST['debug'])) {
|
2016-09-27 07:02:25 +02:00
|
|
|
Debug::message("Using record #$sitetree->ID of type " . get_class($sitetree) . " with link {$sitetree->Link()}");
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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
|
|
|
/**
|
2015-06-19 02:00:42 +02:00
|
|
|
* @deprecated 4.0 Use OldPageRedirector::find_old_page instead
|
2013-07-22 16:31:07 +02:00
|
|
|
*
|
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) {
|
2015-06-19 02:00:42 +02:00
|
|
|
Deprecation::notice('4.0', 'Use OldPageRedirector::find_old_page instead');
|
2013-07-22 16:31:07 +02:00
|
|
|
if ($parent) {
|
2016-08-10 06:08:39 +02:00
|
|
|
$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
|
|
|
}
|