2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage control
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class RootURLController extends Controller {
|
2009-02-22 22:30:41 +01:00
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:17 +02:00
|
|
|
* @var bool
|
2009-02-22 22:30:41 +01:00
|
|
|
*/
|
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
|
|
|
/**
|
2009-10-11 02:07:17 +02:00
|
|
|
* @var string
|
2009-02-22 22:30:41 +01:00
|
|
|
*/
|
2009-10-11 02:07:17 +02:00
|
|
|
protected static $default_homepage_link = 'home';
|
2009-02-22 22:30:41 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:07:17 +02: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.
|
|
|
|
*
|
2009-02-22 22:30:41 +01:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:07:17 +02:00
|
|
|
public static function get_homepage_link() {
|
|
|
|
$host = str_replace('www.', null, $_SERVER['HTTP_HOST']);
|
2009-06-16 05:15:01 +02:00
|
|
|
$SQL_host = Convert::raw2sql($host);
|
|
|
|
|
2009-10-11 02:07:17 +02:00
|
|
|
$candidates = DataObject::get('SiteTree', "\"HomepageForDomain\" LIKE '%$SQL_host%'");
|
|
|
|
|
|
|
|
if($candidates) foreach($candidates as $candidate) {
|
|
|
|
if(preg_match('/(,|^) *' . preg_quote($host) . ' *(,|$)/', $candidate->HomepageForDomain)) {
|
|
|
|
return trim($candidate->RelativeLink(true), '/');
|
2009-06-16 05:15:01 +02:00
|
|
|
}
|
2008-11-24 00:21:29 +01:00
|
|
|
}
|
2009-04-01 18:35:32 +02:00
|
|
|
|
2009-10-11 02:07:17 +02:00
|
|
|
if(Object::has_extension('SiteTree', 'Translatable')) {
|
|
|
|
if($link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale())) return $link;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-11 02:07:17 +02:00
|
|
|
|
|
|
|
return self::get_default_homepage_link();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:17 +02:00
|
|
|
* Gets the link that denotes the homepage if there is not one explicitly defined for this HTTP_HOST value.
|
2009-04-28 01:08:39 +02:00
|
|
|
*
|
2009-10-11 02:07:17 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:07:17 +02:00
|
|
|
public static function get_default_homepage_link() {
|
|
|
|
return self::$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
|
|
|
|
*/
|
|
|
|
public static function should_be_on_root(SiteTree $page) {
|
|
|
|
if(!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')) {
|
|
|
|
return !(
|
|
|
|
$page->hasExtension('Translatable') && $page->Locale && $page->Locale != Translatable::default_locale()
|
|
|
|
);
|
|
|
|
}
|
2009-08-05 07:30:04 +02:00
|
|
|
|
2009-10-11 02:07:17 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated 2.4 Use {@link RootURLController::get_homepage_link()}
|
|
|
|
*/
|
|
|
|
public static function get_homepage_urlsegment() {
|
|
|
|
user_error (
|
|
|
|
'RootURLController::get_homepage_urlsegment() is deprecated, please use get_homepage_link()', E_USER_NOTICE
|
2009-04-28 01:08:39 +02:00
|
|
|
);
|
|
|
|
|
2009-10-11 02:07:17 +02:00
|
|
|
return self::get_homepage_link();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-22 22:30:41 +01:00
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:17 +02:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
2009-02-22 22:30:41 +01:00
|
|
|
*/
|
2009-10-11 02:07:17 +02:00
|
|
|
public function handleRequest(HTTPRequest $request) {
|
|
|
|
self::$is_at_root = true;
|
|
|
|
|
|
|
|
$this->pushCurrent();
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
if(!DB::isActive() || !ClassInfo::hasTable('SiteTree')) {
|
|
|
|
$this->response = new HTTPResponse();
|
|
|
|
$this->response->redirect('dev/build/?returnURL=');
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new HTTPRequest (
|
|
|
|
$request->httpMethod(), self::get_homepage_link() . '/', $request->getVars(), $request->postVars()
|
|
|
|
);
|
|
|
|
$request->match('$URLSegment//$Action', true);
|
|
|
|
|
|
|
|
$controller = new ModelAsController();
|
|
|
|
$result = $controller->handleRequest($request);
|
|
|
|
|
|
|
|
$this->popCurrent();
|
|
|
|
return $result;
|
2009-02-22 22:30:41 +01:00
|
|
|
}
|
2009-10-11 02:07:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|