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;
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
use Exception;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2017-06-08 08:02:18 +02:00
|
|
|
use SilverStripe\Control\NestedController;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Dev\Debug;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2019-01-13 21:17:06 +01:00
|
|
|
use SilverStripe\View\Parsers\URLSegmentFilter;
|
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.
|
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class ModelAsController extends Controller implements NestedController
|
|
|
|
{
|
2017-06-08 08:02:18 +02:00
|
|
|
private static $extensions = [
|
|
|
|
OldPageRedirector::class,
|
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and
|
|
|
|
* return it.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
*/
|
2022-10-18 07:21:09 +02:00
|
|
|
public static function controller_for(SiteTree $sitetree, $action = null): ContentController
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
$controller = $sitetree->getControllerName();
|
|
|
|
|
2022-04-13 07:07:59 +02:00
|
|
|
if ($action && class_exists($controller . '_' . ucfirst($action ?? ''))) {
|
|
|
|
$controller = $controller . '_' . ucfirst($action ?? '');
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Injector::inst()->create($controller, $sitetree);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function init()
|
|
|
|
{
|
2017-09-20 03:51:07 +02:00
|
|
|
singleton(SiteTree::class)->extend('modelascontrollerInit', $this);
|
2017-01-25 21:59:25 +01:00
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
protected function beforeHandleRequest(HTTPRequest $request)
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2017-06-08 08:02:18 +02:00
|
|
|
parent::beforeHandleRequest($request);
|
2017-01-25 21:59:25 +01:00
|
|
|
// If the database has not yet been created, redirect to the build page.
|
|
|
|
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->getResponse()->redirect(Controller::join_links(
|
|
|
|
Director::absoluteBaseURL(),
|
|
|
|
'dev/build',
|
2020-04-19 06:18:01 +02:00
|
|
|
'?' . http_build_query([
|
2017-01-25 21:59:25 +01:00
|
|
|
'returnURL' => isset($_GET['url']) ? $_GET['url'] : null,
|
2020-04-19 06:18:01 +02:00
|
|
|
])
|
2017-01-25 21:59:25 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @uses ModelAsController::getNestedController()
|
|
|
|
*/
|
2022-10-18 07:21:09 +02:00
|
|
|
public function handleRequest(HTTPRequest $request): HTTPResponse
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
2017-06-08 08:02:18 +02:00
|
|
|
$this->beforeHandleRequest($request);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
// If we had a redirection or something, halt processing.
|
|
|
|
if ($this->getResponse()->isFinished()) {
|
|
|
|
$this->popCurrent();
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the database has not yet been created, redirect to the build page.
|
|
|
|
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
2022-08-26 08:34:14 +02:00
|
|
|
$this->getResponse()->redirect(Controller::join_links(Director::absoluteBaseURL(), 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)));
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->popCurrent();
|
|
|
|
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-10-18 07:21:09 +02:00
|
|
|
$result = $this->getNestedController()->handleRequest($this->getRequest());
|
|
|
|
$result = $result;
|
2017-01-25 21:59:25 +01:00
|
|
|
} catch (HTTPResponse_Exception $responseException) {
|
|
|
|
$result = $responseException->getResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception If URLSegment not passed in as a request parameter.
|
|
|
|
*/
|
2022-10-18 07:21:09 +02:00
|
|
|
public function getNestedController(): ContentController
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
if (!$URLSegment = $request->param('URLSegment')) {
|
|
|
|
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
|
|
|
|
}
|
|
|
|
|
2019-01-13 21:17:06 +01:00
|
|
|
// url encode unless it's multibyte (already pre-encoded in the database)
|
|
|
|
$filter = URLSegmentFilter::create();
|
|
|
|
if (!$filter->getAllowMultibyte()) {
|
2022-04-13 07:07:59 +02:00
|
|
|
$URLSegment = rawurlencode($URLSegment ?? '');
|
2019-01-13 21:17:06 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
// Select child page
|
2019-05-17 03:40:15 +02:00
|
|
|
$tableName = DataObject::singleton(SiteTree::class)->baseTable();
|
|
|
|
$conditions = [sprintf('"%s"."URLSegment"', $tableName) => $URLSegment];
|
2017-06-08 08:02:18 +02:00
|
|
|
if (SiteTree::config()->get('nested_urls')) {
|
2019-05-17 03:40:15 +02:00
|
|
|
$conditions[] = [sprintf('"%s"."ParentID"', $tableName) => 0];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
/** @var SiteTree $sitetree */
|
2017-06-08 08:02:18 +02:00
|
|
|
$sitetree = DataObject::get_one(SiteTree::class, $conditions);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
if (!$sitetree) {
|
|
|
|
$this->httpError(404, 'The requested page could not be found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_REQUEST['debug'])) {
|
|
|
|
Debug::message("Using record #$sitetree->ID of type " . get_class($sitetree) . " with link {$sitetree->Link()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::controller_for($sitetree, $this->getRequest()->param('Action'));
|
|
|
|
}
|
2012-04-11 04:51:33 +02:00
|
|
|
}
|