ENHANCEMENT Showing all available languages in language selector above sitetree in CMS - you can now create new pages in completely new languages without any relation to an "original"

ENHANCEMENT Added CMSMain->IsTranslatableEnabled
API CHANGE Removed CMSMain->switchlanguage() - createTranslation() is sufficient for new, ajax refreshing of CMS state got way too complicated for switching languages, we now just reload the entire CMS with a different ?lang GET parameter

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@73345 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-03-18 17:18:52 +00:00
parent a9053b4b67
commit 15454352bb
2 changed files with 55 additions and 106 deletions

View File

@ -51,7 +51,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'rollback',
'sidereport',
'submit',
'switchlanguage',
'unpublish',
'versions',
'EditForm',
@ -360,9 +359,9 @@ JS;
}
/**
* Get a databsae record to be managed by the CMS
* Get a database record to be managed by the CMS
*/
public function getRecord($id) {
public function getRecord($id) {
$treeClass = $this->stat('tree_class');
@ -486,6 +485,7 @@ JS;
if(!singleton($className)->canCreate()) return Security::permissionFailure($this);
$p = $this->getNewItem("new-$className-$parent".$suffix, false);
$p->Lang = $_REQUEST['Lang'];
$p->write();
return $this->returnItemToUser($p);
@ -497,18 +497,6 @@ JS;
public function getNewItem($id, $setID = true) {
list($dummy, $className, $parentID, $suffix) = array_pad(explode('-',$id),4,null);
if(Translatable::is_enabled()) {
if (Translatable::default_lang() != $this->Lang) {
Translatable::set_reading_lang(Translatable::default_lang());
$originalItem = DataObject::get_by_id($className,$suffix);
Translatable::set_reading_lang($this->Lang);
$translation = $originalItem->getTranslation($this->Lang);
if(!$translation) $translation = $originalItem->createTranslation($this->Lang);
if($setID) $translation->ID = $id;
return $translation;
}
}
$newItem = new $className();
if( !$suffix ) {
@ -1172,14 +1160,18 @@ JS;
foreach( $this->PageTypes() as $arrayData ) {
$pageTypes[$arrayData->getField('ClassName')] = $arrayData->getField('AddAction');
}
return new Form($this, "AddPageOptionsForm", new FieldSet(
$fields = new FieldSet(
new HiddenField("ParentID"),
new HiddenField("Lang", 'Lang', Translatable::current_lang()),
new DropdownField("PageType", "", $pageTypes, 'Page')
),
new FieldSet(
);
$actions = new FieldSet(
new FormAction("addpage", _t('CMSMain.GO',"Go"))
));
);
return new Form($this, "AddPageOptionsForm", $fields, $actions);
}
/**
@ -1311,94 +1303,25 @@ JS;
}
}
/**
* Switch the cms language and reload the site tree
*
*/
function switchlanguage($lang, $donotcreate = null) {
//is it's a clean switch (to an existing language deselect the current page)
if (is_string($lang)) $dontunloadPage = true;
$lang = (is_string($lang) ? $lang : urldecode($this->urlParams['ID']));
if ($lang != Translatable::default_lang()) {
Translatable::set_reading_lang(Translatable::default_lang());
$tree_class = $this->stat('tree_class');
$obj = new $tree_class;
$allIDs = $obj->getDescendantIDList();
$allChildren = $obj->AllChildren();
$classesMap = $allChildren->map('ID','ClassName');
$titlesMap = $allChildren->map();
Translatable::set_reading_lang($lang);
$obj = new $tree_class;
$languageIDs = $obj->getDescendantIDList();
$notcreatedlist = array_diff($allIDs,$languageIDs);
FormResponse::add("$('addpage').getElementsByTagName('button')[0].disabled=true;");
//FormResponse::add("$('Form_AddPageOptionsForm').getElementsByTagName('div')[1].getElementsByTagName('input')[0].disabled=true;");
FormResponse::add("$('Translating_Message').innerHTML = 'Translating mode - ".i18n::get_language_name($lang)."';");
FormResponse::add("Element.removeClassName('Translating_Message','nonTranslating');");
} else {
Translatable::set_reading_lang($lang);
FormResponse::add("$('addpage').getElementsByTagName('button')[0].disabled=false;");
//FormResponse::add("$('Form_AddPageOptionsForm').getElementsByTagName('div')[1].getElementsByTagName('input')[0].disabled=false;");
FormResponse::add("Element.addClassName('Translating_Message','nonTranslating');");
}
$obj = singleton($this->stat('tree_class'));
$obj->markPartialTree();
$siteTree = $obj->getChildrenAsUL("", '
"<li id=\"record-$child->ID\" class=\"" . $child->CMSTreeClasses($extraArg) . "\">" .
"<a href=\"" . Director::link(substr($extraArg->Link(),0,-1), "show", $child->ID) . "\" " . (($child->canEdit() || $child->canAddChildren()) ? "" : "class=\"disabled\"") . " title=\"' . _t('LeftAndMain.PAGETYPE') . '".$child->class."\" >" .
(Convert::raw2js($child->TreeTitle())) .
"</a>"
'
,$this, true);
$rootLink = $this->Link() . '0';
$siteTree = "<li id=\"record-0\" class=\"Root nodelete\"><a href=\"$rootLink\">" .
_t('LeftAndMain.SITECONTENT') . "</a>"
. $siteTree . "</li></ul>";
FormResponse::add("$('sitetree').innerHTML ='". ereg_replace("[\n]","\\\n",$siteTree) ."';");
FormResponse::add("SiteTree.applyTo('#sitetree');");
if (isset($notcreatedlist)) {
foreach ($notcreatedlist as $notcreated) {
if ($notcreated == $donotcreate) continue;
$id = "new-{$classesMap[$notcreated]}-0-$notcreated";
Session::set($id . '_originalLangID',$notcreated);
$treeTitle = Convert::raw2js($titlesMap[$notcreated]);
$response = <<<JS
var tree = $('sitetree');
var newNode = tree.createTreeNode("$id", "$treeTitle", "$classesMap[$notcreated] (untranslated)");
addClass(newNode, 'untranslated');
node = tree.getTreeNodeByIdx(0);
node.open();
node.appendTreeNode(newNode);
JS;
FormResponse::add($response);
}
}
if (!isset($dontunloadPage)) FormResponse::add("node = $('sitetree').getTreeNodeByIdx(0); node.selectTreeNode();");
return FormResponse::respond();
}
/**
* Create a new translation from an existing item, switch to this language and reload the tree.
*/
function createtranslation () {
if(!Director::is_ajax()) {
Director::redirectBack();
return;
}
$langCode = $_REQUEST['newlang'];
$originalLangID = $_REQUEST['ID'];
$langCode = Convert::raw2sql($_REQUEST['newlang']);
$originalLangID = (int)$_REQUEST['ID'];
$record = $this->getRecord($originalLangID);
$this->Lang = $langCode;
Translatable::set_reading_lang($langCode);
// creating a record in-memory, which means setting the $Lang property
// will have no effect as the record is loaded through another javascript
// call and CMSMain->getitem(). The CMS submits the currently selected language
// through javascript, which will cause $Lang to be written to the database.
// @todo Explicitly set $Lang property for in-memory object so we don't need javascript modifying GET-calls to save the new object
// Create a new record in the database - this is different
// to the usual "create page" pattern of storing the record
// in-memory until a "save" is performed by the user, mainly
// to simplify things a bit.
// @todo Allow in-memory creation of translations that don't persist in the database before the user requests it
$translatedRecord = $record->createTranslation($langCode);
$url = sprintf(
@ -1439,16 +1362,35 @@ JS;
}
/**
* Return a dropdown with existing languages
*/
function LangSelector() {
$langs = Translatable::get_existing_content_languages('SiteTree');
return new DropdownField("LangSelector","Language",$langs,Translatable::current_lang());
}
* Returns all languages with languages already used appearing first.
* Called by the SSViewer when rendering the template.
*/
function LangSelector() {
$member = Member::currentUser(); //check to see if the current user can switch langs or not
if(Permission::checkMember($member, 'VIEW_LANGS')) {
$allKey = _t('Form.LANGAOTHER', "Other languages");
$all = i18n::get_common_languages(); //all languages
$used = Translatable::get_existing_content_languages(); //languages currently in use
if( $used && count($used) ) {
foreach($used as $index => $code) {
if(!$code) continue;
$available[$index] = $all[$index];
unset($all[$index]);
}
$langs[ _t('Form.LANGAVAIL', "Available languages") ] = (isset( $available )) ? $available : array();
}
$langs[ _t('Form.LANGAOTHER', "Other languages") ] = $all;
return new GroupedDropdownField('LangSelector', 'Language', $langs, Translatable::current_lang());
}
//user doesn't have permission to switch langs so just show a string displaying current language
return i18n::get_language_name( Translatable::current_lang() );
}
/**
* Determine if there are more than one languages in our site tree
* Determine if there are more than one languages in our site tree.
*
* @return boolean
*/
function MultipleLanguages() {
$langs = Translatable::get_existing_content_languages('SiteTree');
@ -1456,6 +1398,13 @@ JS;
return (count($langs) > 1);
}
/**
* @return boolean
*/
function IsTranslatableEnabled() {
return Translatable::is_enabled();
}
/**
* Get the name of the language that we are translating in
*/

View File

@ -9,7 +9,7 @@
<div id="sitetree_holder">
<div id="TreeTools">
<ul id="TreeActions">
<li class="action" id="addpage"><button<% if EditingLang %> disabled="disabled" class="disabled"<% end_if %>><% _t('CREATE','Create',PR_HIGH) %></button></li>
<li class="action" id="addpage"><button><% _t('CREATE','Create',PR_HIGH) %></button></li>
<li class="action" id="search"><button><% _t('SEARCH','Search',PR_HIGH) %></button></li>
<li class="action" id="batchactions"><button><% _t('BATCHACTIONS','Batch Actions',PR_HIGH) %></button></li>
</ul>
@ -64,7 +64,7 @@
<div id="SortItems">
<input type="checkbox" id="sortitems" /> <label for="sortitems"><% _t('ENABLEDRAGGING','Allow drag &amp; drop reordering', PR_HIGH) %></label>
</div>
<% if MultipleLanguages %>
<% if IsTranslatableEnabled %>
<div id="LangSelector_holder">
Language: $LangSelector
</div>