BUGFIX Allowing translated homepages in RootUrlController

BUGFIX Limiting RootURLController to just trigger Translatable if no DomainForHomepage value is found
BUGFIX Using $locale parameter in Translatable::get_homepage_urlsegment_by_locale()
BUGFIX Updated enabling mechanism in Translatable->alternateGetByUrl()
MINOR Removed HTTP_HOST setting in TranslatableTest, wasn't necessary
API CHANGE Deprecated Translatable::get_homepage_urlsegment_by_language(), use get_homepage_urlsegment_by_locale()

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75328 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-04-27 23:08:39 +00:00
parent eb86cc1b07
commit 99c1fe9f93
4 changed files with 53 additions and 20 deletions

View File

@ -50,6 +50,7 @@ class RootURLController extends Controller {
/**
* Return the URL segment for the current HTTP_HOST value
*
* @param string $locale
* @return string
*/
static function get_homepage_urlsegment() {
@ -65,8 +66,8 @@ class RootURLController extends Controller {
$homePageOBJ = null;
}
if(singleton('SiteTree')->hasExtension('Translatable')) {
$urlSegment = Translatable::get_homepage_urlsegment_by_language(Translatable::current_locale());
if(singleton('SiteTree')->hasExtension('Translatable') && !$homePageOBJ) {
$urlSegment = Translatable::get_homepage_urlsegment_by_locale(Translatable::current_locale());
} elseif($homePageOBJ) {
$urlSegment = $homePageOBJ->URLSegment;
}
@ -76,14 +77,34 @@ class RootURLController extends Controller {
/**
* Returns true if we're currently on the root page and should be redirecting to the root
* Doesn't take into account actions, post vars, or get vars
* Doesn't take into account actions, post vars, or get vars.
*
* @param SiteTree $currentPage
* @return boolean
*/
static function should_be_on_root(SiteTree $currentPage) {
if(!self::$is_at_root) return self::get_homepage_urlsegment() == $currentPage->URLSegment;
else return false;
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;
}
/**
* 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()}.
*
* @return string
*/
static function get_default_homepage_urlsegment() {

View File

@ -10,20 +10,23 @@
* <h2>Configuration</h2>
*
* Enabling Translatable in the $extension array of a DataObject
* <code>
* <example>
* class MyClass extends DataObject {
* static $extensions = array(
* "Translatable"
* );
* }
* </code>
* </example>
*
* Enabling Translatable through {@link Object::add_extension()} in your _config.php:
* <example>
* Translatable::set_default_locale('en_US');
* Object::add_extension('MyClass', 'Translatable');
* </example>
*
* Make sure to rebuild the database through /dev/build after enabling translatable.
* Use the correct {@link set_default_locale()} before building the database
* for the first time, as this locale will be written on all new records.
*
* <h2>Usage</h2>
*
@ -210,7 +213,9 @@ class Translatable extends DataObjectDecorator {
}
/**
* Set default language.
* Set default language. Please set this value *before* creating
* any database records (like pages), as this locale will be attached
* to all new records.
*
* @param $locale String
*/
@ -663,9 +668,9 @@ class Translatable extends DataObjectDecorator {
*/
function alternateGetByUrl($urlSegment, $extraFilter, $cache = null, $orderby = null) {
$SQL_URLSegment = Convert::raw2sql($urlSegment);
Translatable::disable();
$record = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$SQL_URLSegment}'");
Translatable::enable();
self::$enable_lang_filter = false;
$record = DataObject::get_one('SiteTree', "\"URLSegment\" = '{$SQL_URLSegment}'", false);
self::$enable_lang_filter = true;
return $record;
}
@ -1023,14 +1028,14 @@ class Translatable extends DataObjectDecorator {
* @param string $locale
* @return string|boolean URLSegment (e.g. "home")
*/
static function get_homepage_urlsegment_by_language($locale) {
static function get_homepage_urlsegment_by_locale($locale) {
$origHomepageObj = Translatable::get_one_by_locale(
'SiteTree',
Translatable::default_locale(),
sprintf('"URLSegment" = \'%s\'', RootUrlController::get_default_homepage_urlsegment())
);
if($origHomepageObj) {
$translatedHomepageObj = $origHomepageObj->getTranslation(Translatable::current_locale());
$translatedHomepageObj = $origHomepageObj->getTranslation($locale);
if($translatedHomepageObj) {
return $translatedHomepageObj->URLSegment;
}
@ -1039,6 +1044,13 @@ class Translatable extends DataObjectDecorator {
return null;
}
/**
* @deprecated 2.4 Use get_homepage_urlsegment_by_locale()
*/
static function get_homepage_urlsegment_by_language($locale) {
return self::get_homepage_urlsegment_by_locale($locale);
}
/**
* @deprecated 2.4 Use is_default_locale()
*/

View File

@ -37,7 +37,11 @@ class RootURLControllerTest extends SapphireTest {
foreach($tests as $domain => $urlSegment) {
$_SERVER['HTTP_HOST'] = $domain;
$this->assertEquals($urlSegment, RootURLController::get_homepage_urlsegment(), "Testing $domain matches $urlSegment");
$this->assertEquals(
$urlSegment,
RootURLController::get_homepage_urlsegment(Translatable::default_locale()),
"Testing $domain matches $urlSegment"
);
}
$_SERVER['HTTP_HOST'] = $originalHost;

View File

@ -622,8 +622,6 @@ class TranslatableTest extends FunctionalTest {
}
function testRootUrlDefaultsToTranslatedUrlSegment() {
$_originalHost = $_SERVER['HTTP_HOST'];
$origPage = $this->objFromFixture('Page', 'homepage_en');
$origPage->publish('Stage', 'Live');
$translationDe = $origPage->createTranslation('de_DE');
@ -631,8 +629,7 @@ class TranslatableTest extends FunctionalTest {
$translationDe->write();
$translationDe->publish('Stage', 'Live');
// test with translatable enabled
$_SERVER['HTTP_HOST'] = '/?locale=de';
// test with translatable
Translatable::set_reading_locale('de_DE');
$this->assertEquals(
RootURLController::get_homepage_urlsegment(),
@ -653,7 +650,6 @@ class TranslatableTest extends FunctionalTest {
// setting back to default
Translatable::set_reading_locale('en_US');
$_SERVER['HTTP_HOST'] = $_originalHost;
}
function testSiteTreeChangePageTypeInMaster() {