silverstripe-cms/code/Controllers/RootURLController.php

171 lines
5.1 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:32:32 +02:00
namespace SilverStripe\CMS\Controllers;
2016-08-10 06:08:39 +02:00
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\SS_HTTPRequest;
use SilverStripe\Control\SS_HTTPResponse;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\DB;
2016-07-22 01:32:32 +02:00
use Translatable;
class RootURLController extends Controller {
/**
* @var bool
*/
protected static $is_at_root = false;
/**
* @config
* @var string
*/
private static $default_homepage_link = 'home';
/**
* @var string
*/
protected static $cached_homepage_link;
/**
* Get the full form (e.g. /home/) relative link to the home page for the current HTTP_HOST value. Note that the
* link is trimmed of leading and trailing slashes before returning to ensure consistency.
*
* @return string
*/
static public function get_homepage_link() {
if(!self::$cached_homepage_link) {
// @todo Move to 'homepagefordomain' module
if(class_exists('HomepageForDomainExtension')) {
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
$candidates = SiteTree::get()->where(array(
'"SiteTree"."HomepageForDomain" LIKE ?' => "%$host%"
));
if($candidates) foreach($candidates as $candidate) {
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
}
}
}
if(!self::$cached_homepage_link) {
// TODO Move to 'translatable' module
if (
class_exists('Translatable')
&& SiteTree::has_extension('Translatable')
&& $link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())
) {
self::$cached_homepage_link = $link;
} else {
2016-07-22 01:32:32 +02:00
self::$cached_homepage_link = Config::inst()->get('SilverStripe\\CMS\\Controllers\\RootURLController', 'default_homepage_link');
}
}
}
return self::$cached_homepage_link;
}
/**
* Set the URL Segment used for your homepage when it is created by dev/build.
* This allows you to use home page URLs other than the default "home".
*
* @deprecated 4.0 Use the "RootURLController.default_homepage_link" config setting instead
* @param string $urlsegment the URL segment for your home page
*/
static public function set_default_homepage_link($urlsegment = "home") {
Deprecation::notice('4.0', 'Use the "RootURLController.default_homepage_link" config setting instead');
2016-07-22 01:32:32 +02:00
Config::inst()->update('SilverStripe\\CMS\\Controllers\\RootURLController', 'default_homepage_link', $urlsegment);
}
/**
* Gets the link that denotes the homepage if there is not one explicitly defined for this HTTP_HOST value.
*
* @deprecated 4.0 Use the "RootURLController.default_homepage_link" config setting instead
* @return string
*/
static public function get_default_homepage_link() {
Deprecation::notice('4.0', 'Use the "RootURLController.default_homepage_link" config setting instead');
2016-07-22 01:32:32 +02:00
return Config::inst()->get('SilverStripe\\CMS\\Controllers\\RootURLController', 'default_homepage_link');
}
/**
* Returns TRUE if a request to a certain page should be redirected to the site root (i.e. if the page acts as the
* home page).
*
* @param SiteTree $page
* @return bool
*/
static public function should_be_on_root(SiteTree $page) {
if(!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')) {
return !(
2016-08-10 06:08:39 +02:00
class_exists('Translatable')
&& $page->hasExtension('Translatable')
&& $page->Locale
&& $page->Locale != Translatable::default_locale()
);
}
return false;
}
/**
* Resets the cached homepage link value - useful for testing.
*/
static public function reset() {
self::$cached_homepage_link = null;
}
2015-02-23 16:51:09 +01:00
protected function beforeHandleRequest(SS_HTTPRequest $request, DataModel $model) {
parent::beforeHandleRequest($request, $model);
self::$is_at_root = true;
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
));
}
}
/**
* @param SS_HTTPRequest $request
2014-02-10 21:35:13 +01:00
* @param DataModel|null $model
* @return SS_HTTPResponse
*/
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) {
self::$is_at_root = true;
2015-02-23 16:51:09 +01:00
$this->beforeHandleRequest($request, $model);
2015-02-23 16:51:09 +01:00
if (!$this->getResponse()->isFinished()) {
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(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
return $this->getResponse();
}
2015-02-23 16:51:09 +01:00
$request->setUrl(self::get_homepage_link() . '/');
$request->match('$URLSegment//$Action', true);
$controller = new ModelAsController();
$response = $controller->handleRequest($request, $model);
2015-02-23 16:51:09 +01:00
$this->prepareResponse($response);
}
2015-02-23 16:51:09 +01:00
$this->afterHandleRequest();
2015-02-23 16:51:09 +01:00
return $this->getResponse();
}
}