BUGFIX Fixed finding a translated homepage without an explicit URLSegment (e.g. http://mysite.com/?lang=de) - see #3540

ENHANCEMENT Added Translatable::get_homepage_urlsegment_by_language()
ENHANCEMENT Added RootURLController::get_default_homepage_urlsegment()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@72054 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-02-22 21:30:41 +00:00
parent 338bb01f1a
commit 4b4b7cca9a
5 changed files with 95 additions and 9 deletions

View File

@ -38,9 +38,9 @@ class ModelAsController extends Controller implements NestedController {
$url = Controller::join_links(
Director::baseURL(),
$child->URLSegment,
$this->urlParams['Action'],
$this->urlParams['ID'],
$this->urlParams['OtherID']
(isset($this->urlParams['Action'])) ? $this->urlParams['Action'] : null,
(isset($this->urlParams['ID'])) ? $this->urlParams['ID'] : null,
(isset($this->urlParams['OtherID'])) ? $this->urlParams['OtherID'] : null
);
$response = new HTTPResponse();

View File

@ -6,8 +6,19 @@
* @subpackage control
*/
class RootURLController extends Controller {
/**
* @var boolean $is_at_root
*/
protected static $is_at_root = false;
/**
* @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';
public function init() {
Director::set_site_mode('site');
parent::init();
@ -20,7 +31,6 @@ class RootURLController extends Controller {
$this->init();
$controller = new ModelAsController();
$request = new HTTPRequest("GET", self::get_homepage_urlsegment().'/', $request->getVars(), $request->postVars());
$request->match('$URLSegment//$Action', true);
@ -32,9 +42,13 @@ class RootURLController extends Controller {
/**
* Return the URL segment for the current HTTP_HOST value
*
* @return string
*/
static function get_homepage_urlsegment() {
// Temporarily restricted to MySQL database while testing db abstraction
$urlSegment = '';
// @todo Temporarily restricted to MySQL database while testing db abstraction
if(DB::getConn() instanceof MySQLDatabase) {
$host = $_SERVER['HTTP_HOST'];
$host = str_replace('www.','',$host);
@ -45,10 +59,12 @@ class RootURLController extends Controller {
}
if($homePageOBJ) {
return $homePageOBJ->URLSegment;
} else {
return 'home';
$urlSegment = $homePageOBJ->URLSegment;
} elseif(Translatable::is_enabled()) {
$urlSegment = Translatable::get_homepage_urlsegment_by_language(Translatable::current_lang());
}
return ($urlSegment) ? $urlSegment : self::get_default_homepage_urlsegment();
}
/**
@ -59,6 +75,13 @@ class RootURLController extends Controller {
if(!self::$is_at_root) return self::get_homepage_urlsegment() == $currentPage->URLSegment;
else return false;
}
/**
* @return string
*/
static function get_default_homepage_urlsegment() {
return self::$default_homepage_urlsegment;
}
}
?>

View File

@ -815,6 +815,31 @@ class Translatable extends DataObjectDecorator {
}
return $returnMap;
}
/**
* Gets a URLSegment value for a homepage in another language.
* The value is inferred by finding the homepage in default language
* (as identified by RootURLController::$default_homepage_urlsegment).
* Returns NULL if no translated page can be found.
*
* @param string $Lang
* @return string|boolean URLSegment (e.g. "home")
*/
static function get_homepage_urlsegment_by_language($lang) {
$origHomepageObj = Translatable::get_one_by_lang(
'SiteTree',
Translatable::default_lang(),
sprintf('"URLSegment" = \'%s\'', RootUrlController::get_default_homepage_urlsegment())
);
if($origHomepageObj) {
$translatedHomepageObj = $origHomepageObj->getTranslation(Translatable::current_lang());
if($translatedHomepageObj) {
return $translatedHomepageObj->URLSegment;
}
}
return null;
}
}

View File

@ -41,7 +41,7 @@ class TranslatableTest extends FunctionalTest {
parent::tearDown();
}
function testGetOriginalPage() {
$origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de');
@ -478,6 +478,40 @@ class TranslatableTest extends FunctionalTest {
// reset language
Translatable::set_reading_lang('en');
}
function testRootUrlDefaultsToTranslatedUrlSegment() {
$_originalHost = $_SERVER['HTTP_HOST'];
$origPage = $this->objFromFixture('Page', 'homepage_en');
$origPage->publish('Stage', 'Live');
$translationDe = $origPage->createTranslation('de');
$translationDe->URLSegment = 'heim';
$translationDe->write();
$translationDe->publish('Stage', 'Live');
// test with translatable enabled
$_SERVER['HTTP_HOST'] = '/?lang=de';
Translatable::set_reading_lang('de');
$this->assertEquals(
RootURLController::get_homepage_urlsegment(),
'heim',
'Homepage with different URLSegment in non-default language is found'
);
// test with translatable disabled
Translatable::disable();
$_SERVER['HTTP_HOST'] = '/';
$this->assertEquals(
RootURLController::get_homepage_urlsegment(),
'home',
'Homepage is showing in default language if ?lang GET variable is left out'
);
Translatable::enable();
// setting back to default
Translatable::set_reading_lang('en');
$_SERVER['HTTP_HOST'] = $_originalHost;
}
}
class TranslatableTest_DataObject extends DataObject implements TestOnly {

View File

@ -1,4 +1,8 @@
Page:
homepage_en:
Title: Home
URLSegment: home
Lang: en
testpage_en:
Title: Home
MenuTitle: A Testpage