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
This commit is contained in:
Ingo Schommer 2010-12-03 00:50:29 +00:00 committed by Sam Minnee
parent 924f0feb5d
commit 1670dab5e1
2 changed files with 19 additions and 1 deletions

View File

@ -268,6 +268,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @param $locale String * @param $locale String
*/ */
static function set_default_locale($locale) { 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(); $localeList = i18n::get_locale_list();
if(isset($localeList[$locale])) { if(isset($localeList[$locale])) {
self::$default_locale = $locale; self::$default_locale = $locale;
@ -295,6 +297,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @param string $lang New reading language. * @param string $lang New reading language.
*/ */
static function set_current_locale($locale) { static function set_current_locale($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
self::$current_locale = $locale; self::$current_locale = $locale;
} }
@ -308,6 +312,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return DataObject * @return DataObject
*/ */
static function get_one_by_locale($class, $locale, $filter = '', $cache = false, $orderby = "") { 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(); $orig = Translatable::get_current_locale();
Translatable::set_current_locale($locale); Translatable::set_current_locale($locale);
$do = DataObject::get_one($class, $filter, $cache, $orderby); $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. * @return mixed The objects matching the conditions.
*/ */
static function get_by_locale($class, $locale, $filter = '', $sort = '', $join = "", $limit = "", $containerClass = "DataObjectSet", $having = "") { 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(); $oldLang = self::get_current_locale();
self::set_current_locale($locale); self::set_current_locale($locale);
$result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having); $result = DataObject::get($class, $filter, $sort, $join, $limit, $containerClass, $having);
@ -1053,6 +1061,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return DataObjectSet * @return DataObjectSet
*/ */
function getTranslations($locale = null, $stage = null) { function getTranslations($locale = null, $stage = null) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
if($this->owner->exists()) { if($this->owner->exists()) {
// HACK need to disable language filtering in augmentSQL(), // HACK need to disable language filtering in augmentSQL(),
// as we purposely want to get different language // as we purposely want to get different language
@ -1103,6 +1113,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return DataObject Translated object * @return DataObject Translated object
*/ */
function getTranslation($locale, $stage = null) { function getTranslation($locale, $stage = null) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
$translations = $this->getTranslations($locale, $stage); $translations = $this->getTranslations($locale, $stage);
return ($translations) ? $translations->First() : null; return ($translations) ? $translations->First() : null;
} }
@ -1120,6 +1132,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return DataObject The translated object * @return DataObject The translated object
*/ */
function createTranslation($locale) { function createTranslation($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
if(!$this->owner->exists()) { if(!$this->owner->exists()) {
user_error('Translatable::createTranslation(): Please save your record before creating a translation', E_USER_ERROR); 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 * @return boolean
*/ */
function canTranslate($member = null, $locale) { 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(); if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
// check for locale // check for locale
@ -1218,6 +1234,8 @@ class Translatable extends DataObjectDecorator implements PermissionProvider {
* @return boolean * @return boolean
*/ */
function hasTranslation($locale) { function hasTranslation($locale) {
if($locale && !i18n::validate_locale($locale)) throw new InvalidArgumentException(sprintf('Invalid locale "%s"', $locale));
return ( return (
$this->owner->Locale == $locale $this->owner->Locale == $locale
|| array_search($locale, $this->getTranslatedLocales()) !== false || array_search($locale, $this->getTranslatedLocales()) !== false

View File

@ -57,7 +57,7 @@ class TranslatableTest extends FunctionalTest {
$this->assertEquals(301, $response->getStatusCode(), 'Locale GET param causes redirect if it exists'); $this->assertEquals(301, $response->getStatusCode(), 'Locale GET param causes redirect if it exists');
$this->assertContains($translatedPage->URLSegment, $response->getHeader('Location')); $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'); $this->assertEquals(200, $response->getStatusCode(), 'Locale GET param without existing translation shows original page');
} }