From 908ef4c1b75ed58c32ca48d9ff1cced3943cd5a6 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 28 Apr 2009 03:12:57 +0000 Subject: [PATCH] ENHANCEMENT Auto-update locale values in Translatable->requireDefaultRecords() with default language when Translatable is first enabled git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@75421 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/model/Translatable.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/core/model/Translatable.php b/core/model/Translatable.php index 4cca283f0..492c90e97 100755 --- a/core/model/Translatable.php +++ b/core/model/Translatable.php @@ -479,6 +479,36 @@ class Translatable extends DataObjectDecorator { DB::requireTable("{$baseDataClass}_translationgroups", $fields, $indexes); } + /** + * @todo Find more appropriate place to hook into database building + */ + function requireDefaultRecords() { + // @todo This relies on the Locale attribute being on the base data class, and not any subclasses + if($this->owner->class != ClassInfo::baseDataClass($this->owner->class)) return false; + + // If the Translatable extension was added after the first records were already + // created in the database, make sure to update the Locale property if + // if wasn't set before + $idsWithoutLocale = DB::query(sprintf( + 'SELECT "ID" FROM "%s" WHERE "Locale" IS NULL OR "Locale" = \'\'', + ClassInfo::baseDataClass($this->owner->class) + ))->column(); + if($idsWithoutLocale) { + foreach($idsWithoutLocale as $id) { + $obj = DataObject::get_by_id($this->owner->class, $id); + $obj->Locale = Translatable::default_locale(); + $obj->write(); + $obj->destroy(); + unset($obj); + } + Database::alteration_message(sprintf( + "Added default locale '%s' to table %s","changed", + Translatable::default_locale(), + $this->owner->class + )); + } + } + /** * Add a record to a "translation group", * so its relationship to other translations