From 1670dab5e102d81d261c9e444fd57f06653831ff Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Fri, 3 Dec 2010 00:50:29 +0000 Subject: [PATCH] API CHANGE Using i18n::validate_locale() in various Translatable methods to ensure the locale exists (as defined through i18n::$allowed_locales) (from r114470) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@114474 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Translatable.php | 18 ++++++++++++++++++ tests/model/TranslatableTest.php | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/core/model/Translatable.php b/core/model/Translatable.php index 55737b427..15d1226ed 100755 --- a/core/model/Translatable.php +++ b/core/model/Translatable.php @@ -268,6 +268,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @param $locale String */ static function set_default_locale($locale) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + $localeList = i18n::get_locale_list(); if(isset($localeList[$locale])) { self::$default_locale = $locale; @@ -295,6 +297,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @param string $lang New reading language. */ static function set_current_locale($locale) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + self::$current_locale = $locale; } @@ -308,6 +312,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return DataObject */ static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + $orig = Translatable::get_current_locale(); Translatable::set_current_locale($locale); $do = DataObject::get_one($class, $filter, $cache, $orderby); @@ -329,6 +335,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return mixed The objects matching the conditions. */ static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + $oldLang = self::get_current_locale(); self::set_current_locale($locale); $result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having); @@ -1053,6 +1061,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return DataObjectSet */ function getTranslations($locale = null, $stage = null) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + if($this->owner->exists()) { // HACK need to disable language filtering in augmentSQL(), // as we purposely want to get different language @@ -1103,6 +1113,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return DataObject Translated object */ function getTranslation($locale, $stage = null) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + $translations = $this->getTranslations($locale, $stage); return ($translations) ? $translations->First() : null; } @@ -1120,6 +1132,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return DataObject The translated object */ function createTranslation($locale) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + if(!$this->owner->exists()) { user_error('Translatable::createTranslation(): Please save your record before creating a translation', E_USER_ERROR); } @@ -1179,6 +1193,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return boolean */ function canTranslate($member = null, $locale) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser(); // check for locale @@ -1218,6 +1234,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider { * @return boolean */ function hasTranslation($locale) { + if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale)); + return ( $this->owner->Locale == $locale || array_search($locale, $this->getTranslatedLocales()) !== false diff --git a/tests/model/TranslatableTest.php b/tests/model/TranslatableTest.php index 462e45b82..157ee35fc 100755 --- a/tests/model/TranslatableTest.php +++ b/tests/model/TranslatableTest.php @@ -57,7 +57,7 @@ class TranslatableTest extends FunctionalTest { $this->assertEquals(301, $response->getStatusCode(), 'Locale GET param causes redirect if it exists'); $this->assertContains($translatedPage->URLSegment, $response->getHeader('Location')); - $response = $this->get(Controller::join_links($origPage->URLSegment, '?locale=xx_XX')); + $response = $this->get(Controller::join_links($origPage->URLSegment, '?locale=fr_FR')); $this->assertEquals(200, $response->getStatusCode(), 'Locale GET param without existing translation shows original page'); }