2011-03-22 10:26:53 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage control
|
|
|
|
*/
|
|
|
|
class RootURLController extends Controller {
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected static $is_at_root = false;
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
2013-03-18 11:47:15 +01:00
|
|
|
* @config
|
2011-03-22 10:26:53 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $default_homepage_link = 'home';
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $cached_homepage_link;
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function get_homepage_link() {
|
2011-03-22 10:26:53 +01:00
|
|
|
if(!self::$cached_homepage_link) {
|
2013-06-21 00:45:33 +02:00
|
|
|
// @todo Move to 'homepagefordomain' module
|
2012-03-14 22:43:47 +01:00
|
|
|
if(class_exists('HomepageForDomainExtension')) {
|
|
|
|
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
2013-06-21 00:45:33 +02:00
|
|
|
$candidates = SiteTree::get()->where(array(
|
|
|
|
'"SiteTree"."HomepageForDomain" LIKE ?' => "%$host%"
|
|
|
|
));
|
2012-03-14 22:43:47 +01:00
|
|
|
if($candidates) foreach($candidates as $candidate) {
|
|
|
|
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
|
|
|
|
self::$cached_homepage_link = trim($candidate->RelativeLink(true), '/');
|
|
|
|
}
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
if(!self::$cached_homepage_link) {
|
2012-03-14 22:43:47 +01:00
|
|
|
// TODO Move to 'translatable' module
|
2011-03-22 10:26:53 +01:00
|
|
|
if (
|
2011-03-22 09:30:10 +01:00
|
|
|
class_exists('Translatable')
|
2012-10-09 23:59:52 +02:00
|
|
|
&& SiteTree::has_extension('Translatable')
|
2011-03-22 10:26:53 +01:00
|
|
|
&& $link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())
|
|
|
|
) {
|
|
|
|
self::$cached_homepage_link = $link;
|
|
|
|
} else {
|
2013-03-18 11:47:15 +01:00
|
|
|
self::$cached_homepage_link = Config::inst()->get('RootURLController', 'default_homepage_link');
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
return self::$cached_homepage_link;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2012-02-03 04:24:00 +01:00
|
|
|
/**
|
|
|
|
* 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".
|
|
|
|
*
|
2015-06-19 02:00:42 +02:00
|
|
|
* @deprecated 4.0 Use the "RootURLController.default_homepage_link" config setting instead
|
2012-02-03 04:24:00 +01:00
|
|
|
* @param string $urlsegment the URL segment for your home page
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function set_default_homepage_link($urlsegment = "home") {
|
2015-06-19 02:00:42 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "RootURLController.default_homepage_link" config setting instead');
|
2013-03-18 11:47:15 +01:00
|
|
|
Config::inst()->update('RootURLController', 'default_homepage_link', $urlsegment);
|
2012-02-03 04:24:00 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the link that denotes the homepage if there is not one explicitly defined for this HTTP_HOST value.
|
|
|
|
*
|
2015-06-19 02:00:42 +02:00
|
|
|
* @deprecated 4.0 Use the "RootURLController.default_homepage_link" config setting instead
|
2011-03-22 10:26:53 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function get_default_homepage_link() {
|
2015-06-19 02:00:42 +02:00
|
|
|
Deprecation::notice('4.0', 'Use the "RootURLController.default_homepage_link" config setting instead');
|
2013-03-18 11:47:15 +01:00
|
|
|
return Config::inst()->get('RootURLController', 'default_homepage_link');
|
2011-03-22 10:26:53 +01:00
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function should_be_on_root(SiteTree $page) {
|
2011-03-22 10:26:53 +01:00
|
|
|
if(!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')) {
|
|
|
|
return !(
|
2011-03-22 09:30:10 +01:00
|
|
|
class_exists('Translatable') && $page->hasExtension('Translatable') && $page->Locale && $page->Locale != Translatable::default_locale()
|
2011-03-22 10:26:53 +01:00
|
|
|
);
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2011-03-22 10:26:53 +01:00
|
|
|
/**
|
|
|
|
* Resets the cached homepage link value - useful for testing.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
static public function reset() {
|
2011-03-22 10:26:53 +01:00
|
|
|
self::$cached_homepage_link = null;
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
2014-02-10 21:35:13 +01:00
|
|
|
* @param DataModel|null $model
|
2011-03-22 10:26:53 +01:00
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
2012-03-27 06:05:11 +02:00
|
|
|
public function handleRequest(SS_HTTPRequest $request, DataModel $model = null) {
|
2011-03-22 10:26:53 +01:00
|
|
|
self::$is_at_root = true;
|
2015-02-23 16:51:09 +01:00
|
|
|
$this->beforeHandleRequest($request, $model);
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2015-02-23 16:51:09 +01:00
|
|
|
if (!$this->getResponse()->isFinished()) {
|
|
|
|
if (!DB::is_active() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
|
|
|
return $this->getResponse();
|
|
|
|
}
|
2015-08-14 02:39:45 +02:00
|
|
|
|
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-08-14 02:39:45 +02:00
|
|
|
|
2015-02-23 16:51:09 +01:00
|
|
|
$this->prepareResponse($response);
|
|
|
|
}
|
2013-02-27 18:21:48 +01:00
|
|
|
|
2015-02-23 16:51:09 +01:00
|
|
|
$this->afterHandleRequest();
|
2015-08-14 02:39:45 +02:00
|
|
|
|
2015-02-23 16:51:09 +01: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
|
|
|
}
|