2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* This controller handles what happens when you visit the root URL.
|
|
|
|
*
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class RootURLController extends Controller {
|
2009-02-22 22:30:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean $is_at_root
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
protected static $is_at_root = false;
|
2008-08-09 05:29:30 +02:00
|
|
|
|
2009-02-22 22:30:41 +01:00
|
|
|
/**
|
|
|
|
* @var string $default_homepage_urlsegment Defines which URLSegment value on a {@link SiteTree} object
|
|
|
|
* is regarded as the correct "homepage" if the requested URI doesn't contain
|
|
|
|
* an explicit segment. E.g. http://mysite.com should show http://mysite.com/home.
|
|
|
|
*/
|
|
|
|
protected static $default_homepage_urlsegment = 'home';
|
|
|
|
|
2008-11-09 15:20:19 +01:00
|
|
|
public function init() {
|
|
|
|
Director::set_site_mode('site');
|
|
|
|
parent::init();
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
public function handleRequest($request) {
|
2007-07-19 12:40:28 +02:00
|
|
|
self::$is_at_root = true;
|
2008-10-04 06:42:51 +02:00
|
|
|
$this->pushCurrent();
|
2008-11-09 15:20:19 +01:00
|
|
|
|
|
|
|
$this->init();
|
2007-08-17 05:09:46 +02:00
|
|
|
|
2009-03-10 23:08:52 +01:00
|
|
|
// If the basic database hasn't been created, then build it.
|
|
|
|
if(!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->response = new HTTPResponse();
|
|
|
|
$this->redirect("dev/build?returnURL=");
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
2008-08-09 05:29:30 +02:00
|
|
|
$controller = new ModelAsController();
|
|
|
|
$request = new HTTPRequest("GET", self::get_homepage_urlsegment().'/', $request->getVars(), $request->postVars());
|
2009-02-02 00:49:53 +01:00
|
|
|
$request->match('$URLSegment//$Action', true);
|
2008-08-09 05:29:30 +02:00
|
|
|
|
2008-09-19 07:54:07 +02:00
|
|
|
$result = $controller->handleRequest($request);
|
2008-10-04 06:42:51 +02:00
|
|
|
|
|
|
|
$this->popCurrent();
|
2008-09-19 07:54:07 +02:00
|
|
|
return $result;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-08-09 05:29:30 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return the URL segment for the current HTTP_HOST value
|
2009-02-22 22:30:41 +01:00
|
|
|
*
|
2009-04-28 01:08:39 +02:00
|
|
|
* @param string $locale
|
2009-02-22 22:30:41 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function get_homepage_urlsegment() {
|
2009-02-22 22:30:41 +01:00
|
|
|
$urlSegment = '';
|
|
|
|
|
|
|
|
// @todo Temporarily restricted to MySQL database while testing db abstraction
|
2008-11-24 00:21:29 +01:00
|
|
|
if(DB::getConn() instanceof MySQLDatabase) {
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
|
|
$host = str_replace('www.','',$host);
|
|
|
|
$SQL_host = str_replace('.','\\.',Convert::raw2sql($host));
|
|
|
|
$homePageOBJ = DataObject::get_one("SiteTree", "HomepageForDomain REGEXP '(,|^) *$SQL_host *(,|\$)'");
|
|
|
|
} else {
|
|
|
|
$homePageOBJ = null;
|
|
|
|
}
|
2009-04-01 18:35:32 +02:00
|
|
|
|
2009-04-28 01:08:39 +02:00
|
|
|
if(singleton('SiteTree')->hasExtension('Translatable') && !$homePageOBJ) {
|
|
|
|
$urlSegment = Translatable::get_homepage_urlsegment_by_locale(Translatable::current_locale());
|
2009-04-01 18:35:32 +02:00
|
|
|
} elseif($homePageOBJ) {
|
|
|
|
$urlSegment = $homePageOBJ->URLSegment;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-04-01 18:35:32 +02:00
|
|
|
|
2009-02-22 22:30:41 +01:00
|
|
|
return ($urlSegment) ? $urlSegment : self::get_default_homepage_urlsegment();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if we're currently on the root page and should be redirecting to the root
|
2009-04-28 01:08:39 +02:00
|
|
|
* Doesn't take into account actions, post vars, or get vars.
|
|
|
|
*
|
|
|
|
* @param SiteTree $currentPage
|
|
|
|
* @return boolean
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function should_be_on_root(SiteTree $currentPage) {
|
2009-04-28 01:08:39 +02:00
|
|
|
if(self::$is_at_root) return false;
|
|
|
|
|
|
|
|
$matchesHomepageSegment = (self::get_homepage_urlsegment() == $currentPage->URLSegment);
|
|
|
|
// Don't redirect translated homepage segments,
|
|
|
|
// as the redirection target '/' will show the default locale
|
|
|
|
// instead of the translation.
|
|
|
|
$isTranslatedHomepage = (
|
|
|
|
singleton('SiteTree')->hasExtension('Translatable')
|
|
|
|
&& $currentPage->Locale
|
|
|
|
&& $currentPage->Locale != Translatable::default_locale()
|
|
|
|
);
|
|
|
|
if($matchesHomepageSegment && !$isTranslatedHomepage) return true;
|
|
|
|
|
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-22 22:30:41 +01:00
|
|
|
|
|
|
|
/**
|
2009-04-28 01:08:39 +02:00
|
|
|
* Returns the (untranslated) hardcoded URL segment that will
|
|
|
|
* show when the website is accessed without a URL segment (http://mysite.com/).
|
|
|
|
* It is also the base for any redirections to '/' for the homepage,
|
|
|
|
* see {@link should_be_on_root()}.
|
|
|
|
*
|
2009-02-22 22:30:41 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
static function get_default_homepage_urlsegment() {
|
|
|
|
return self::$default_homepage_urlsegment;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|