2011-03-22 10:26:53 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage control
|
|
|
|
*/
|
|
|
|
class RootURLController extends Controller {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected static $is_at_root = false;
|
|
|
|
|
|
|
|
/**
|
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';
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$cached_homepage_link;
|
|
|
|
}
|
|
|
|
|
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".
|
|
|
|
*
|
2013-03-18 11:47:15 +01:00
|
|
|
* @deprecated 3.2 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") {
|
2013-03-18 11:47:15 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "RootURLController.default_homepage_link" config setting instead');
|
|
|
|
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.
|
|
|
|
*
|
2013-03-18 11:47:15 +01:00
|
|
|
* @deprecated 3.2 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() {
|
2013-03-18 11:47:15 +01:00
|
|
|
Deprecation::notice('3.2', 'Use the "RootURLController.default_homepage_link" config setting instead');
|
|
|
|
return Config::inst()->get('RootURLController', 'default_homepage_link');
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2012-05-01 04:46:16 +02:00
|
|
|
$this->setDataModel($model);
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
$this->pushCurrent();
|
|
|
|
$this->init();
|
|
|
|
|
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 = new SS_HTTPResponse();
|
|
|
|
$this->response->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null));
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2013-02-27 18:21:48 +01:00
|
|
|
$request->setUrl(self::get_homepage_link() . '/');
|
2011-03-22 10:26:53 +01:00
|
|
|
$request->match('$URLSegment//$Action', true);
|
|
|
|
$controller = new ModelAsController();
|
2013-02-27 18:21:48 +01:00
|
|
|
|
2011-05-01 07:42:27 +02:00
|
|
|
$result = $controller->handleRequest($request, $model);
|
2011-03-22 10:26:53 +01:00
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|