BUGFIX: Fix #5097, Translatable uses augmentValidURLSegment to check that URLSegment is valid

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@99850 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Luke Hudson 2010-02-24 23:32:42 +00:00 committed by Sam Minnee
parent 5531e01a69
commit be26cf2579
2 changed files with 43 additions and 0 deletions

View File

@ -1152,6 +1152,14 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
$newTranslation->ID = 0; $newTranslation->ID = 0;
$newTranslation->Locale = $locale; $newTranslation->Locale = $locale;
$originalPage = $this->getTranslation(self::default_locale());
if ($originalPage) {
$urlSegment = $originalPage->URLSegment;
} else {
$urlSegment = $newTranslation->URLSegment;
}
$newTranslation->URLSegment = $urlSegment . '-' . i18n::convert_rfc1766($locale);
// hacky way to set an existing translation group in onAfterWrite() // hacky way to set an existing translation group in onAfterWrite()
$translationGroupID = $this->getTranslationGroup(); $translationGroupID = $this->getTranslationGroup();
$newTranslation->_TranslationGroupID = $translationGroupID ? $translationGroupID : $this->owner->ID; $newTranslation->_TranslationGroupID = $translationGroupID ? $translationGroupID : $this->owner->ID;
@ -1453,6 +1461,39 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
function cacheKeyComponent() { function cacheKeyComponent() {
return 'locale-'.self::get_current_locale(); return 'locale-'.self::get_current_locale();
} }
/**
* Extends the SiteTree::validURLSegment() method, to do checks appropriate
* to Translatable
*
* @return bool
*/
public function augmentValidURLSegment() {
if (self::locale_filter_enabled()) {
self::disable_locale_filter();
$reEnableFilter = true;
}
$IDFilter = ($this->owner->ID) ? "AND \"SiteTree\".\"ID\" <> {$this->owner->ID}" : null;
$parentFilter = null;
if(SiteTree::nested_urls()) {
if($this->owner->ParentID) {
$parentFilter = " AND \"SiteTree\".\"ParentID\" = {$this->owner->ParentID}";
} else {
$parentFilter = ' AND "SiteTree"."ParentID" = 0';
}
}
$existingPage = DataObject::get_one(
'SiteTree',
"\"URLSegment\" = '{$this->owner->URLSegment}' $IDFilter $parentFilter",
false // disable get_one cache, as this otherwise may pick up results from when locale_filter was on
);
if ($reEnableFilter) {
self::enable_locale_filter();
}
return !$existingPage;
}
} }

View File

@ -173,6 +173,8 @@ class TranslatableTest extends FunctionalTest {
function testTranslationCantHaveSameURLSegmentAcrossLanguages() { function testTranslationCantHaveSameURLSegmentAcrossLanguages() {
$origPage = $this->objFromFixture('Page', 'testpage_en'); $origPage = $this->objFromFixture('Page', 'testpage_en');
$translatedPage = $origPage->createTranslation('de_DE'); $translatedPage = $origPage->createTranslation('de_DE');
$this->assertEquals($translatedPage->URLSegment, 'testpage-de-DE');
$translatedPage->URLSegment = 'testpage'; $translatedPage->URLSegment = 'testpage';
$translatedPage->write(); $translatedPage->write();