Merged, debugged and enhanced Translatable patches from branches/translatable at r64523, r64523, 64523, thanks wakeless!

API CHANGE Changed Translatable schema from auxilliary tables (SiteTree_lang, SiteTree_lang_Live) to automatically filtered records on the original table (SiteTree, SiteTree_Live), using $Lang and $OriginalID properties. Incompatible update to old schema, migration script is in the works.
API CHANGE Removed Translatable::get_one(), Translatable::write()
ENHANCEMENT Simplified Translatable tree generation by using getSiteTreeFor() in CMSMain->createtranslation()
ENHANCEMENT Added AllChildrenIncludingDeleted(), augmentNumChildrenCountQuery(),  augmentAllChildrenIncludingDeleted(), augmentStageChildren() to Translatable class to allow for more stable tree generation.
ENHANCEMENT Moved definition of Translatable schema from augmentDatabase() to Translatable->extraStatics()
ENHANCEMENT Changes to the CMS language selection refresh the whole admin interface instead of the tree only. This way we can add a URL parameter ?lang=<lang> to /admin, which makes the specific language bookmarkable and reloadable. Changes to LangSelector.js
ENHANCEMENT Added fallback to ModelAsController->getNestedController() to fetch page with matching URLSegment but different language in case no page is found in the current language.
ENHANCEMENT Added helper methods to Translatable: getTranslation(), hasTranslation(), isTranslation(), findOriginalIDs()
ENHANCEMENT Getters and setters for Translatable->getOriginalPage() etc.
ENHANCEMENT Hooking Translatable into ModelAsController and ContentController initialization in order to call choose_site_lang()
ENHANCEMENT Simplified Translatable->augmentSQL(), augmentWrite() by not using auxilliary tables
ENHANCEMENT Showing clickable links for Translations in Translatable->updateCMSFields()
BUGFIX Modifying Hierarchy/SiteTree Children getters to accept optional "context" which can be used to set a language explicitly through the $Lang property, rather than implicitly reyling on the static Translatable::current_lang()
BUGFIX Fixed TranslatableTest to work with new datamodel
BUGFIX Temporarily disabled cookie/session selection in Translatable::choose_site_lang() until we have a good test suite for the side effects.
MINOR Added "untranslated" CSS styles to tree nodes and marking them as inactive/grey


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@69959 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-01-10 12:15:30 +00:00
parent b7d394008e
commit d3d6ae833d
4 changed files with 418 additions and 359 deletions

View File

@ -34,6 +34,12 @@ class ModelAsController extends Controller implements NestedController {
$SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']); $SQL_URLSegment = Convert::raw2sql($this->urlParams['URLSegment']);
$child = SiteTree::get_by_url($SQL_URLSegment); $child = SiteTree::get_by_url($SQL_URLSegment);
// fallback to default language
// @todo Migrate into extension point and module
if(!$child && Translatable::is_enabled()) {
$child = Translatable::get_one_by_lang('SiteTree', Translatable::default_lang(), "URLSegment = '{$SQL_URLSegment}'");
}
if(!$child) { if(!$child) {
if($child = $this->findOldPage($SQL_URLSegment)) { if($child = $this->findOldPage($SQL_URLSegment)) {
$url = Controller::join_links( $url = Controller::join_links(

View File

@ -991,6 +991,7 @@ class i18n extends Object {
* @param string $locale Locale to be set * @param string $locale Locale to be set
*/ */
static function set_locale($locale) { static function set_locale($locale) {
if(strlen($locale) == 2) Debug::backtrace();
if ($locale) self::$current_locale = $locale; if ($locale) self::$current_locale = $locale;
} }

View File

@ -169,8 +169,8 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
static $extensions = array( static $extensions = array(
"Hierarchy", "Hierarchy",
"Versioned('Stage', 'Live')",
"Translatable('Title', 'MenuTitle', 'Content', 'URLSegment', 'MetaTitle', 'MetaDescription', 'MetaKeywords', 'Status')", "Translatable('Title', 'MenuTitle', 'Content', 'URLSegment', 'MetaTitle', 'MetaDescription', 'MetaKeywords', 'Status')",
"Versioned('Stage', 'Live')"
); );
/** /**
@ -1695,6 +1695,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
if(!$this->ShowInMenus) if(!$this->ShowInMenus)
$classes .= " notinmenu"; $classes .= " notinmenu";
//TODO: Add integration
if(Translatable::is_enabled() && $controller->Lang != Translatable::default_lang() && !$this->isTranslation())
$classes .= " untranslated ";
$classes .= $this->markingClasses(); $classes .= $this->markingClasses();
return $classes; return $classes;

View File

@ -89,6 +89,15 @@ class Translatable extends DataObjectDecorator {
*/ */
protected $original_values = null; protected $original_values = null;
/**
* Overloaded getter for $Lang property.
* Not all pages in the database have their language property explicitly set,
* so we fall back to {@link Translatable::default_lang()}.
*/
function getLang() {
$record = $this->owner->toMap();
return (isset($record["Lang"])) ? $record["Lang"] : Translatable::default_lang();
}
/** /**
* Checks if a table given table exists in the db * Checks if a table given table exists in the db
@ -113,12 +122,20 @@ class Translatable extends DataObjectDecorator {
* @param $langsAvailable array A numerical array of languages which are valid choices (optional) * @param $langsAvailable array A numerical array of languages which are valid choices (optional)
* @return string Selected language (also saved in $reading_lang). * @return string Selected language (also saved in $reading_lang).
*/ */
static function choose_site_lang($langsAvailable = null) { static function choose_site_lang($langsAvailable = array()) {
$siteMode = Director::get_site_mode(); // either 'cms' or 'site' $siteMode = Director::get_site_mode(); // either 'cms' or 'site'
if(self::$reading_lang) {
self::$language_decided = true;
return self::$reading_lang;
}
if(isset($_GET['lang']) && (!isset($langsAvailable) || in_array($_GET['lang'], $langsAvailable))) { if(
(isset($_GET['lang']) && !$langsAvailable)
|| (isset($_GET['lang']) && in_array($_GET['lang'], $langsAvailable))
) {
// get from GET parameter // get from GET parameter
self::set_reading_lang($_GET['lang']); self::set_reading_lang($_GET['lang']);
/*
} elseif(isset($_COOKIE['lang.' . $siteMode]) && $siteMode && (!isset($langsAvailable) || in_array($_COOKIE['lang.' . $siteMode], $langsAvailable))) { } elseif(isset($_COOKIE['lang.' . $siteMode]) && $siteMode && (!isset($langsAvailable) || in_array($_COOKIE['lang.' . $siteMode], $langsAvailable))) {
// get from namespaced cookie // get from namespaced cookie
self::set_reading_lang($_COOKIE[$siteMode . '.lang']); self::set_reading_lang($_COOKIE[$siteMode . '.lang']);
@ -132,10 +149,11 @@ class Translatable extends DataObjectDecorator {
// get from global session // get from global session
self::set_reading_lang(Session::get('lang.global')); self::set_reading_lang(Session::get('lang.global'));
} else { } else {
// get default lang stored in class get default lang stored in class
self::set_reading_lang(self::default_lang()); self::set_reading_lang(self::default_lang());
*/
} }
self::$language_decided = true;
return self::$reading_lang; return self::$reading_lang;
} }
@ -180,8 +198,7 @@ class Translatable extends DataObjectDecorator {
* @param string $lang New reading language. * @param string $lang New reading language.
*/ */
static function set_reading_lang($lang) { static function set_reading_lang($lang) {
$key = (Director::get_site_mode()) ? 'lang.' . Director::get_site_mode() : 'lang.global'; //Session::set('currentLang',$lang);
Session::set($key, $lang);
self::$reading_lang = $lang; self::$reading_lang = $lang;
} }
@ -195,44 +212,11 @@ class Translatable extends DataObjectDecorator {
* @return DataObject * @return DataObject
*/ */
static function get_one_by_lang($class, $lang, $filter = '', $cache = false, $orderby = "") { static function get_one_by_lang($class, $lang, $filter = '', $cache = false, $orderby = "") {
$oldLang = self::current_lang(); $orig = Translatable::current_lang();
self::set_reading_lang($lang); Translatable::set_reading_lang($lang);
$result = DataObject::get_one($class, $filter, $cache, $orderby); $do = DataObject::get_one($class, $filter, $cache, $orderby);
self::set_reading_lang($oldLang); Translatable::set_reading_lang($orig);
return $result; return $do;
}
/**
* Get a singleton instance of a class in the most convenient language (@see choose_site_lang())
*
* @param string $callerClass The name of the class
* @param string $filter A filter to be inserted into the WHERE clause.
* @param boolean $cache Use caching (default: false)
* @param string $orderby A sort expression to be inserted into the ORDER BY clause.
* @return DataObject
*/
static function get_one($callerClass, $filter = "", $cache = false, $orderby = "") {
self::$language_decided = true;
self::$reading_lang = self::default_lang();
$record = DataObject::get_one($callerClass, $filter);
if (!$record) {
self::$bypass = true;
$record = DataObject::get_one($callerClass, $filter, $cache, $orderby);
self::$bypass = false;
if ($record) self::set_reading_lang($record->Lang);
} else {
$langsAvailable = (array)self::get_langs_by_id($callerClass, $record->ID);
$langsAvailable[] = self::default_lang();
$lang = self::choose_site_lang($langsAvailable);
if (isset($lang)) {
$transrecord = self::get_one_by_lang($callerClass, $lang, "\"$callerClass\".\"ID\" = $record->ID");
if ($transrecord) {
self::set_reading_lang($lang);
$record = $transrecord;
}
}
}
return $record;
} }
/** /**
@ -265,7 +249,21 @@ class Translatable extends DataObjectDecorator {
static function get_original($class, $originalLangID) { static function get_original($class, $originalLangID) {
$baseClass = $class; $baseClass = $class;
while( ($p = get_parent_class($baseClass)) != "DataObject") $baseClass = $p; while( ($p = get_parent_class($baseClass)) != "DataObject") $baseClass = $p;
return self::get_one_by_lang($class,self::default_lang(),"\"$baseClass\".ID = $originalLangID"); return self::get_one_by_lang($class,self::default_lang(),"\"$baseClass\".\"ID\" = $originalLangID");
}
function getTranslatedLangs() {
$class = ClassInfo::baseDataClass($this->owner->class); //Base Class
if($this->owner->hasExtension("Versioned") && Versioned::current_stage() == "Live") {
$class = $class."_Live";
}
$id = $this->owner->ID;
if(is_numeric($id)) {
$query = new SQLQuery('distinct Lang',"$class","(\"$class\".\"OriginalID\" =$id)");
$langs = $query->execute()->column();
}
return ($langs) ? array_values($langs) : array();
} }
/** /**
@ -276,23 +274,8 @@ class Translatable extends DataObjectDecorator {
* @return array List of languages * @return array List of languages
*/ */
static function get_langs_by_id($class, $id) { static function get_langs_by_id($class, $id) {
$query = new SQLQuery('Lang',"{$class}_lang","(\"{$class}_lang\".OriginalLangID =$id)"); $do = DataObject::get_by_id($class, $id);
$langs = $query->execute()->column(); return ($do ? $do->getTranslatedLangs() : array());
return ($langs) ? array_values($langs) : false;
}
/**
* Writes an object in a certain language. Use this instead of $object->write() if you want to write
* an instance in a determinated language independently of the currently set working language
*
* @param DataObject $object Object to be written
* @param string $lang The name of the language
*/
static function write(DataObject $object, $lang) {
$oldLang = self::current_lang();
self::set_reading_lang($lang);
$result = $object->write();
self::set_reading_lang($oldLang);
} }
/** /**
@ -340,99 +323,92 @@ class Translatable extends DataObjectDecorator {
function __construct($translatableFields) { function __construct($translatableFields) {
parent::__construct(); parent::__construct();
// @todo Disabled selection of translatable fields - we're setting all fields as translatable in setOwner()
/*
if(!is_array($translatableFields)) { if(!is_array($translatableFields)) {
$translatableFields = func_get_args(); $translatableFields = func_get_args();
} }
$this->translatableFields = $translatableFields; $this->translatableFields = $translatableFields;
*/
// workaround for extending a method on another decorator (Hierarchy):
// split the method into two calls, and overwrite the wrapper AllChildrenIncludingDeleted()
// Has to be executed even with Translatable disabled, as it overwrites the method with same name
// on Hierarchy class, and routes through to Hierarchy->doAllChildrenIncludingDeleted() instead.
$this->createMethod("AllChildrenIncludingDeleted",
"
\$context = (isset(\$args[0])) ? \$args[0] : null;
if(\$context && \$obj->getLang() == \$context->Lang && \$obj->isTranslation()) {
// if the language matches the context (e.g. CMSMain), and object is translated,
// then call method on original language instead
return \$obj->getOwner()->getOriginalPage()->doAllChildrenIncludingDeleted(\$context);
} else if(\$obj->getOwner()->hasExtension('Hierarchy') ) {
return \$obj->getOwner()->extInstance('Hierarchy')->doAllChildrenIncludingDeleted(\$context);
} else {
return null;
}"
);
}
function setOwner(Object $owner) {
parent::setOwner($owner);
// setting translatable fields by inspecting owner - this should really be done in the constructor
$this->translatableFields = array_keys($this->owner->inheritedDatabaseFields());
}
function extraDBFields() {
if(!Translatable::is_enabled()) return;
if(get_class($this->owner) == ClassInfo::baseDataClass(get_class($this->owner))) {
return array(
"db" => array(
"Lang" => "Varchar(12)",
"OriginalID" => "Int"
),
"defaults" => array(
"Lang" => Translatable::default_lang()
)
);
} else {
return array();
}
}
function findOriginalIDs() {
if(!$this->isTranslation()) {
$query = new SQLQuery("ID",
ClassInfo::baseDataClass($this->owner->class),
array("OriginalID = ".$this->owner->ID)
);
$ret = $query->execute()->column();
} else {
return array();
}
} }
function augmentSQL(SQLQuery &$query) { function augmentSQL(SQLQuery &$query) {
if (! $this->stat('enabled')) return false; if(!Translatable::is_enabled()) return;
if((($lang = self::current_lang()) && !self::is_default_lang()) || self::$bypass) {
foreach($query->from as $table => $dummy) {
if(!isset($baseTable)) {
$baseTable = $table;
}
if (self::table_exists("{$table}_lang")) { $lang = Translatable::current_lang();
$query->renameTable($table, $table . '_lang'); $baseTable = ClassInfo::baseDataClass($this->owner->class);
if (stripos($query->sql(),'.ID')) { $where = $query->where;
// Every reference to ID is now OriginalLangID if (
$query->replaceText(".ID",".OriginalLangID"); $lang
$query->where = str_replace("\"ID\"", "\"OriginalLangID\"",$query->where); && !$query->filtersOnID()
$query->select[] = "\"{$baseTable}_lang\".OriginalLangID AS ID"; && array_search($baseTable, array_keys($query->from)) !== false
&& !$this->isTranslation()
//&& !$query->filtersOnFK()
) {
$qry = "\"Lang\" = '$lang'";
if(Translatable::is_default_lang()) {
$qry .= " OR \"Lang\" = '' ";
$qry .= " OR \"Lang\" IS NULL ";
} }
if ($query->where) foreach ($query->where as $i => $wherecl) { $query->where[] = $qry;
if (substr($wherecl,0,4) == 'ID =')
// Another reference to ID to be changed
$query->where[$i] = str_replace('ID =','OriginalLangID =',$wherecl);
else {
$parts = explode(' AND ',$wherecl);
foreach ($parts as $j => $part) {
// Divide this clause between the left ($innerparts[1]) and right($innerparts[2]) part of the condition
ereg('(\"?[[:alnum:]_-]*\"?\.?\"?[[:alnum:]_-]*\"?)(.*)', $part, $innerparts);
if (strpos($innerparts[1],'.') === false)
//it may be ambiguous, so sometimes we will need to add the table
$parts[$j] = ($this->isInAugmentedTable($innerparts[1], $table) ? "\"{$table}_lang\"." : "")."$part";
else {
/* if the table has been specified we have to determine if the original (without _lang) name has to be used
* because we don't have the queried field in the augmented table (which usually means
* that is not a translatable field)
*/
$clauseparts = explode('.',$innerparts[1]);
$originalTable = str_replace('"','',str_replace('_lang','',$clauseparts[0]));
$parts[$j] = ($this->isInAugmentedTable($clauseparts[1], $originalTable) ? "\"{$originalTable}_lang\"" : "\"$originalTable\"")
. ".{$clauseparts[1]}{$innerparts[2]}";
} }
} }
$query->where[$i] = implode(' AND ',$parts);
}
}
if($table != $baseTable) {
$query->from["{$table}_lang"] = $query->from[$table];
} else {
// _lang is now the base table (the first one)
$query->from = array("{$table}_lang" => $query->from[$table]) + $query->from;
}
// unless we are bypassing this query, add the language filter
if (!self::$bypass) $query->where[] = "\"{$table}_lang\".Lang = '$lang'";
// unless this is a deletion, the query is applied to the joined table
if (!$query->delete) {
$query->from[$table] = "INNER JOIN \"$table\"".
" ON \"{$table}_lang\".OriginalLangID = \"$table\".ID";
/* if we are selecting fields (not doing counts for example) we need to select everything from
* the original table (was renamed to _lang) since some fields that we require may be there
*/
if ($query->select[0][0] == '"') $query->select = array_merge(array("\"$table\".*"),$query->select);
} else unset($query->from[$table]);
} else {
$query->from[$table] = str_replace("\"{$table}\".OriginalLangID","\"{$table}\".ID",$query->from[$table]);
}
}
}
}
/**
* Check whether a WHERE clause should be applied to the augmented table
*
* @param string $clause Where clause that need to know if can be applied to the augmented (suffixed) table
* @param string $table Name of the non-augmented table
* @return boolean True if the clause can be applied to the augmented table
*/
function isInAugmentedTable($clause, $table) {
$clause = str_replace('\"','',$clause);
$table = str_replace('_lang','',$table);
if (strpos($table,'_') !== false) return false;
$field = ereg_replace('[[:blank:]]*([[:alnum:]]*).*','\\1',$clause);
$field = trim($field);
$allFields = $this->allFieldsInTable($table);
return (array_search($field,$allFields) !== false);
}
/** /**
* Determine if the DataObject has any own translatable field (not inherited). * Determine if the DataObject has any own translatable field (not inherited).
@ -447,6 +423,50 @@ class Translatable extends DataObjectDecorator {
return false; return false;
} }
function augmentNumChildrenCountQuery(SQLQuery $query) {
if(!Translatable::is_enabled()) return;
if($this->isTranslation()) {
$query->where[0] = '\"ParentID\" = '.$this->getOriginalPage()->ID;
}
}
/**
* @var SiteTree $cache_originalPage Cached representation of the original page for this translation
* (if at all in translation mode)
*/
private $cache_originalPage = null;
function setOriginalPage($original) {
if($original instanceof DataObject) {
$this->owner->OriginalID = $original->ID;
} else {
$this->owner->OriginalID = $original;
}
}
function getOriginalPage() {
if($this->isTranslation()) {
if(!$this->cache_originalPage) {
$orig = Translatable::current_lang();
Translatable::set_reading_lang(Translatable::default_lang());
$this->cache_originalPage = DataObject::get_by_id($this->owner->class, $this->owner->OriginalID);
Translatable::set_reading_lang($orig);
}
return $this->cache_originalPage;
} else {
return $this->owner;
}
}
function isTranslation() {
if($this->getLang() && ($this->getLang() != Translatable::default_lang()) && $this->owner->exists()) {
return true;
} else {
return false;
}
}
/** /**
* Determine if a table needs Versioned support * Determine if a table needs Versioned support
* This is called at db/build time * This is called at db/build time
@ -455,162 +475,102 @@ class Translatable extends DataObjectDecorator {
* @return boolean * @return boolean
*/ */
function isVersionedTable($table) { function isVersionedTable($table) {
return false;
// Every _lang table wants Versioned support // Every _lang table wants Versioned support
return ($this->owner->databaseFields() && $this->hasOwnTranslatableFields()); return ($this->owner->databaseFields() && $this->hasOwnTranslatableFields());
} }
function augmentDatabase() { function contentcontrollerInit($controller) {
if (! $this->stat('enabled')) return false; if(!Translatable::is_enabled()) return;
self::set_reading_lang(self::default_lang()); Translatable::choose_site_lang();
$table = $this->owner->class; $controller->Lang = Translatable::current_lang();
if(($fields = $this->owner->databaseFields()) && $this->hasOwnTranslatableFields()) {
//Calculate the required fields
foreach ($fields as $field => $type) {
if (array_search($field,$this->translatableFields) === false) unset($fields[$field]);
}
$metaFields = array_diff((array)$this->owner->databaseFields(), (array)$this->owner->customDatabaseFields());
$indexes = $this->owner->databaseIndexes();
$langFields = array_merge(
array(
"Lang" => "Varchar(12)",
"OriginalLangID" => "Int"
),
$fields,
$metaFields
);
foreach ($indexes as $index => $type) {
if (true === $type && array_search($index,$langFields) === false) unset($indexes[$index]);
} }
$langIndexes = array_merge( function modelascontrollerInit($controller) {
array( if(!Translatable::is_enabled()) return;
'OriginalLangID_Lang' => '(OriginalLangID, Lang)',
'OriginalLangID' => true,
'Lang' => true,
),
(array)$indexes
);
// Create table for translated instances //$this->contentcontrollerInit($controller);
DB::requireTable("{$table}_lang", $langFields, $langIndexes);
} else {
DB::dontRequireTable("{$table}_lang");
}
} }
function initgetEditForm($controller) {
if(!Translatable::is_enabled()) return;
$this->contentcontrollerInit($controller);
}
/**
* Augment a write-record request.
* @param SQLQuery $manipulation Query to augment.
*/
function augmentWrite(&$manipulation) { function augmentWrite(&$manipulation) {
if (! $this->stat('enabled')) return false; if(!Translatable::is_enabled()) return;
if(($lang = self::current_lang()) && !self::is_default_lang()) {
$tables = array_keys($manipulation); if(!$this->isTranslation()) {
foreach($tables as $table) { $ids = $this->findOriginalIDs();
if (self::table_exists("{$table}_lang")) { if(!$ids || count($ids) == 0) return;
$manipulation["{$table}_lang"] = $manipulation[$table];
if ($manipulation[$table]['command'] == 'insert') {
$fakeID = $this->owner->ID;
// In an insert we've to populate our fields and generate a new id (since the passed one it's relative to $table)
$SessionOrigID = Session::get($this->owner->ID.'_originalLangID');
$manipulation["{$table}_lang"]['fields']['OriginalLangID'] = $this->owner->ID =
( $SessionOrigID ? $SessionOrigID : self::$creatingFromID);
$manipulation["{$table}_lang"]['RecordID'] = $manipulation["{$table}_lang"]['fields']['OriginalLangID'];
// populate lang field
$manipulation["{$table}_lang"]['fields']['Lang'] = "'$lang'" ;
// get a valid id, pre-inserting
DB::query("INSERT INTO \"{$table}_lang\" (\"Created\", \"Lang\") VALUES (NOW(), '$lang')");
$manipulation["{$table}_lang"]['id'] = $manipulation["{$table}_lang"]['fields']['ID'] = DB::getGeneratedID("{$table}_lang");
$manipulation["{$table}_lang"]['command'] = 'update';
// we don't have to insert anything in $table if we are inserting in $table_lang
unset($manipulation[$table]);
// now dataobjects may create a record before the real write in the base table, so we have to delete it - 20/08/2007
if (is_numeric($fakeID)) DB::query("DELETE FROM \"$table\" WHERE \"ID\"=$fakeID");
} }
else { $newManip = array();
if (!isset($manipulation[$table]['fields']['OriginalLangID'])) { foreach($manipulation as $table => $manip) {
// for those updates that may become inserts populate these fields if(strpos($table, "_versions") !== false) continue;
$manipulation["{$table}_lang"]['fields']['OriginalLangID'] = $this->owner->ID; /*
$manipulation["{$table}_lang"]['fields']['Lang'] = "'$lang'"; foreach($this->fieldBlackList as $blackField) {
} if(isset($manip["fields"][$blackField])) {
$id = $manipulation["{$table}_lang"]['id']; if($this->isTranslation()) {
if(!$id) user_error("Couldn't find ID in manipulation", E_USER_ERROR); unset($manip["fields"][$blackField]);
if (isset($manipulation["{$table}_lang"]['where'])) {
$manipulation["{$table}_lang"]['where'] .= "AND (Lang = '$lang') AND (OriginalLangID = $id)";
} else { } else {
$manipulation["{$table}_lang"]['where'] = "(Lang = '$lang') AND (OriginalLangID = $id)"; if(!isset($newManip[$table])) {
$newManip[$table] = array("command" =>"update",
"where" => "ID in (".implode(",", $ids).")",
"fields" => array());
} }
$realID = DB::query("SELECT \"ID\" FROM \"{$table}_lang\" WHERE (\"OriginalLangID\" = $id) AND (\"Lang\" = '$lang') LIMIT 1")->value(); $newManip[$table]["fields"][$blackField] = $manip["fields"][$blackField];
$manipulation["{$table}_lang"]['id'] = $realID;
$manipulation["{$table}_lang"]['RecordID'] = $manipulation["{$table}_lang"]['fields']['OriginalLangID'];
// we could be updating non-translatable fields at the same time, so these will remain
foreach ($manipulation[$table]['fields'] as $field => $dummy) {
if ($this->isInAugmentedTable($field, $table) ) unset($manipulation[$table]['fields'][$field]);
}
if (count($manipulation[$table]['fields']) == 0) unset($manipulation[$table]);
}
foreach ($manipulation["{$table}_lang"]['fields'] as $field => $dummy) {
if (! $this->isInAugmentedTable($field, $table) ) unset($manipulation["{$table}_lang"]['fields'][$field]);
} }
} }
} }
*/
} }
DB::manipulate($newManip);
} }
//-----------------------------------------------------------------------------------------------// //-----------------------------------------------------------------------------------------------//
/**
* Change the member dialog in the CMS
*
* This method updates the forms in the cms to allow the translations for
* the defined translatable fields.
*/
function updateCMSFields(FieldSet &$fields) { function updateCMSFields(FieldSet &$fields) {
if (! $this->stat('enabled')) return false; if(!Translatable::is_enabled()) return;
// used in CMSMain->init() to set language state when reading/writing record
$fields->push(new HiddenField("Lang", "Lang", $this->getLang()) );
$fields->push(new HiddenField("OriginalID", "OriginalID", $this->owner->OriginalID) );
// if a language other than default language is used, we're in "translation mode",
// hence have to modify the original fields
$creating = false; $creating = false;
$baseClass = $this->owner->class; $baseClass = $this->owner->class;
$allFields = $fields->toArray();
while( ($p = get_parent_class($baseClass)) != "DataObject") $baseClass = $p; while( ($p = get_parent_class($baseClass)) != "DataObject") $baseClass = $p;
$allFields = $this->owner->getAllFields(); $isTranslationMode = (Translatable::default_lang() != $this->getLang() && $this->getLang());
if(!self::is_default_lang()) {
// Get the original version record, to show the original values if($isTranslationMode) {
if (!is_numeric($allFields['ID'])) {
$originalLangID = Session::get($this->owner->ID . '_originalLangID'); $originalLangID = Session::get($this->owner->ID . '_originalLangID');
$creating = true;
$translatableFieldNames = $this->getTranslatableFields();
$allDataFields = $fields->dataFields();
$originalRecord = $this->owner->getOriginalPage();
$transformation = new Translatable_Transformation($originalRecord);
// iterate through sequential list of all datafields in fieldset
// (fields are object references, so we can replace them with the translatable CompositeField)
foreach($allDataFields as $dataField) {
if(in_array($dataField->Name(), $translatableFieldNames)) {
// if the field is translatable, perform transformation
$fields->replaceField($dataField->Name(), $transformation->transformFormField($dataField));
} else { } else {
$originalLangID = $allFields['ID']; // else field shouldn't be editable in translation-mode, make readonly
$fields->replaceField($dataField->Name(), $dataField->performReadonlyTransformation());
} }
$originalRecord = self::get_one_by_lang(
$this->owner->class,
self::$default_lang,
"\"$baseClass\".ID = ".$originalLangID
);
$this->original_values = $originalRecord->getAllFields();
$alltasks = array( 'dup' => array());
foreach($fields as $field) {
if ($field->isComposite()) {
$innertasks = $this->duplicateOrReplaceFields($field->FieldSet());
// more efficient and safe than array_merge_recursive
$alltasks['dup'] = array_merge($alltasks['dup'],$innertasks['dup']);
}
}
foreach ($alltasks['dup'] as $fieldname => $newfield) {
// Duplicate the field
$fields->replaceField($fieldname,$newfield);
} }
} else { } else {
$alreadyTranslatedLangs = null; // if we're not in "translation mode", show a dropdown to create a new translation.
if (is_numeric($allFields['ID'])) { // this action should just be possible when showing the default language,
$alreadyTranslatedLangs = self::get_langs_by_id($baseClass,$allFields['ID']); // you can't create new translations from within a "translation mode" form.
} $alreadyTranslatedLangs = $this->getTranslatedLangs();
if (!$alreadyTranslatedLangs) $alreadyTranslatedLangs = array();
foreach ($alreadyTranslatedLangs as $i => $langCode) {
$alreadyTranslatedLangs[$i] = i18n::get_language_name($langCode);
}
$fields->addFieldsToTab( $fields->addFieldsToTab(
'Root', 'Root',
new Tab(_t('Translatable.TRANSLATIONS', 'Translations'), new Tab(_t('Translatable.TRANSLATIONS', 'Translations'),
@ -619,57 +579,35 @@ class Translatable extends DataObjectDecorator {
$createButton = new InlineFormAction('createtranslation',_t('Translatable.CREATEBUTTON', 'Create')) $createButton = new InlineFormAction('createtranslation',_t('Translatable.CREATEBUTTON', 'Create'))
) )
); );
if (count($alreadyTranslatedLangs)) {
$fields->addFieldsToTab( if($alreadyTranslatedLangs) {
$fields->addFieldToTab(
'Root.Translations', 'Root.Translations',
new FieldSet( new HeaderField('ExistingTransHeader', _t('Translatable.EXISTING', 'Existing translations:'), 3)
new HeaderField('ExistingTransHeader',_t('Translatable.EXISTING', 'Existing translations:'), 3), );
new LiteralField('existingtrans',implode(', ',$alreadyTranslatedLangs)) $existingTransHTML = '<ul>';
) foreach($alreadyTranslatedLangs as $i => $langCode) {
$existingTransHTML .= sprintf('<li><a href="%s">%s</a></li>',
sprintf('admin/show/%d/?lang=%s', $this->owner->ID, $langCode),
i18n::get_language_name($langCode)
); );
} }
$existingTransHTML .= '</ul>';
$fields->addFieldToTab(
'Root.Translations',
new LiteralField('existingtrans',$existingTransHTML)
);
}
$langDropdown->addExtraClass('languageDropdown'); $langDropdown->addExtraClass('languageDropdown');
$createButton->addExtraClass('createTranslationButton'); $createButton->addExtraClass('createTranslationButton');
// disable creation of new pages via javascript
$createButton->includeDefaultJS(false); $createButton->includeDefaultJS(false);
} }
} }
protected function duplicateOrReplaceFields(&$fields) {
$tasks = array(
'dup' => array(),
);
foreach($fields as $field) {
if ($field->isComposite()) {
$innertasks = $this->duplicateOrReplaceFields($field->FieldSet());
$tasks['dup'] = array_merge($tasks['dup'],$innertasks['dup']);
}
else if(($fieldname = $field->Name()) && array_key_exists($fieldname,$this->original_values)) {
// Get a copy of the original field to show the untranslated value
if($field instanceof TextareaField) {
$nonEditableField = new ToggleField($fieldname,$field->Title(),'','+','-');
$nonEditableField->labelMore = '+';
$nonEditableField->labelLess = '-';
} else {
$nonEditableField = $field->performDisabledTransformation();
}
$nonEditableField_holder = new CompositeField($nonEditableField);
$nonEditableField_holder->setName($fieldname.'_holder');
$nonEditableField_holder->addExtraClass('originallang_holder');
$nonEditableField->setValue($this->original_values[$fieldname]);
$nonEditableField->setName($fieldname.'_original');
$nonEditableField->addExtraClass('originallang');
if (array_search($fieldname,$this->translatableFields) !== false) {
// Duplicate the field
if ($field->Title()) $nonEditableField->setTitle('Original');
$nonEditableField_holder->insertBefore($field, $fieldname.'_original');
$tasks['dup'][$fieldname] = $nonEditableField_holder;
}
}
}
return $tasks;
}
/** /**
* Get a list of fields from the tables created by this extension * Get a list of fields from the tables created by this extension
* *
@ -677,30 +615,7 @@ class Translatable extends DataObjectDecorator {
* @return array Map where the keys are db, indexes and the values are the table fields * @return array Map where the keys are db, indexes and the values are the table fields
*/ */
function fieldsInExtraTables($table){ function fieldsInExtraTables($table){
return array('db'=>null,'indexes'=>null);
if(($fields = $this->owner->databaseFields()) && $this->hasOwnTranslatableFields()) {
//Calculate the required fields
foreach ($fields as $field => $type) {
if (array_search($field,$this->translatableFields) === false) unset($fields[$field]);
}
$metaFields = array_diff((array)$this->owner->databaseFields(), (array)$this->owner->customDatabaseFields());
$indexes = $this->owner->databaseIndexes();
$langFields = array_merge(
array(
"Lang" => "Varchar(12)",
"OriginalLangID" => "Int"
),
$fields,
$metaFields
);
foreach ($indexes as $index => $type) {
if (true === $type && array_search($index,$langFields) === false) unset($indexes[$index]);
}
return array('db' => $langFields, 'indexes' => $indexes);
}
} }
/** /**
@ -736,6 +651,17 @@ class Translatable extends DataObjectDecorator {
return $langFields; return $langFields;
} }
/**
* Get the names of all translatable fields on this class
* as a numeric array.
* @todo Integrate with blacklist once branches/translatable is merged back.
*
* @return array
*/
function getTranslatableFields() {
return $this->translatableFields;
}
/** /**
* Return the base table - the class that directly extends DataObject. * Return the base table - the class that directly extends DataObject.
* @return string * @return string
@ -746,19 +672,74 @@ class Translatable extends DataObjectDecorator {
return (!$stage || $stage == $this->defaultStage) ? $baseClass : $baseClass . "_$stage"; return (!$stage || $stage == $this->defaultStage) ? $baseClass : $baseClass . "_$stage";
} }
/**
* Extends $table with a suffix if required
*
* @param string $table Name of the table
* @return string Extended table name
*/
function extendWithSuffix($table) { function extendWithSuffix($table) {
if((($lang = self::current_lang()) && !self::is_default_lang())) {
if (self::table_exists("{$table}_lang")) return $table.'_lang';
}
return $table; return $table;
} }
function getTranslation($lang, $create=true) {
if($this->owner->exists() && !$this->owner->isTranslation()) {
$orig = Translatable::current_lang();
$this->owner->flushCache();
Translatable::set_reading_lang($lang);
$filter = array("`OriginalID` = '".$this->owner->ID."'");
if($this->owner->hasExtension("Versioned") && Versioned::current_stage()) {
$translation = Versioned::get_one_by_stage($this->owner->class, Versioned::current_stage(), $filter);
} else {
$translation = DataObject::get_one($this->owner->class, $filter);
}
Translatable::set_reading_lang($orig);
if($create && !$translation) {
$class = $this->owner->class;
$translation = new $class;
$translation->update($this->owner->toMap());
$translation->ID = 0;
$translation->setOriginalPage($this->owner->ID);
$translation->Lang = $lang;
}
return $translation;
}
}
function hasTranslation($lang) {
return ($this->owner->exists()) && (array_search($lang, $this->getTranslatedLangs()) !== false);
}
function augmentStageChildren(DataObjectSet $children, $showall = false) {
if(!Translatable::is_enabled()) return;
if($this->isTranslation()) {
$children->merge($this->getOriginalPage()->stageChildren($showall));
}
}
function augmentAllChildrenIncludingDeleted(DataObjectSet $children, $context = null) {
if(!Translatable::is_enabled()) return false;
$find = array();
$replace = array();
// @todo check usage of $context
if($context && $context->Lang /*&& $this->owner->Lang != $context->Lang */&& $context->Lang != Translatable::default_lang()) {
if($children) {
foreach($children as $child) {
if($child->hasTranslation($context->Lang)) {
$trans = $child->getTranslation($context->Lang);
$find[] = $child;
$replace[] = $trans;
}
}
foreach($find as $i => $found) {
$children->replace($found, $replace[$i]);
}
}
}
}
/** /**
* Get a list of languages with at least one element translated in (including the default language) * Get a list of languages with at least one element translated in (including the default language)
* *
@ -768,7 +749,7 @@ class Translatable extends DataObjectDecorator {
static function get_existing_content_languages($className = 'SiteTree', $where = '') { static function get_existing_content_languages($className = 'SiteTree', $where = '') {
if(!Translatable::is_enabled()) return false; if(!Translatable::is_enabled()) return false;
$baseTable = ClassInfo::baseDataClass($className); $baseTable = ClassInfo::baseDataClass($className);
$query = new SQLQuery('Lang',$baseTable.'_lang',$where,"",'Lang'); $query = new SQLQuery('Distinct Lang',$baseTable,$where,"",'Lang');
$dbLangs = $query->execute()->column(); $dbLangs = $query->execute()->column();
$langlist = array_merge((array)Translatable::default_lang(), (array)$dbLangs); $langlist = array_merge((array)Translatable::default_lang(), (array)$dbLangs);
$returnMap = array(); $returnMap = array();
@ -781,4 +762,71 @@ class Translatable extends DataObjectDecorator {
} }
} }
/**
* Transform a formfield to a "translatable" representation,
* consisting of the original formfield plus a readonly-version
* of the original value, wrapped in a CompositeField.
*
* @param DataObject $original Needs the original record as we populate the readonly formfield with the original value
*
* @package sapphire
* @subpackage misc
*/
class Translatable_Transformation extends FormTransformation {
/**
* @var DataObject
*/
private $original = null;
function __construct(DataObject $original) {
$this->original = $original;
parent::__construct();
}
/**
* Returns the original DataObject attached to the Transformation
*
* @return DataObject
*/
function getOriginal() {
return $this->original;
}
/**
* @todo transformTextareaField() not used at the moment
*/
function transformTextareaField(TextareaField $field) {
$nonEditableField = new ToggleField($fieldname,$field->Title(),'','+','-');
$nonEditableField->labelMore = '+';
$nonEditableField->labelLess = '-';
return $this->baseTransform($nonEditableField, $field);
return $nonEditableField;
}
function transformFormField(FormField $field) {
$newfield = $field->performReadOnlyTransformation();
return $this->baseTransform($newfield, $field);
}
protected function baseTransform($nonEditableField, $originalField) {
$fieldname = $originalField->Name();
$nonEditableField_holder = new CompositeField($nonEditableField);
$nonEditableField_holder->setName($fieldname.'_holder');
$nonEditableField_holder->addExtraClass('originallang_holder');
$nonEditableField->setValue($this->original->$fieldname);
$nonEditableField->setName($fieldname.'_original');
$nonEditableField->addExtraClass('originallang');
$nonEditableField->setTitle('Original '.$originalField->Title());
$nonEditableField_holder->insertBefore($originalField, $fieldname.'_original');
return $nonEditableField_holder;
}
}
?> ?>