Permalink
Browse files

ENHANCEMENT Using request parameters for locale setting instead of se…

…ssion/Translatable statics - this is more in line with CMSMain

ENHANCEMENT Including "create translation" button by default
ENHANCEMENT Listing existing translations
  • Loading branch information...
1 parent 56c61bf commit 9230b15006bb404cec8aa952d302f9afacece502 @chillu chillu committed Nov 4, 2009
Showing with 115 additions and 10 deletions.
  1. +102 −10 code/TranslatableModelAdmin.php
  2. 0 lang/_manifest_exclude
  3. +13 −0 lang/en_US.php
@@ -1,28 +1,33 @@
<?php
/**
- * Subclass of ModelAdmin with user-interface hooks for
+ * Subclass of ModelAdmin with user-interface hooks for {@link Translatable} functionality.
*/
abstract class TranslatableModelAdmin extends ModelAdmin {
public static $record_controller_class = 'TranslatableModelAdmin_RecordController';
public static $collection_controller_class = 'TranslatableModelAdmin_CollectionController';
+ /**
+ * @var String $Locale
+ */
+ public $Locale = null;
+
function init() {
parent::init();
Requirements::customScript("SiteTreeHandlers.controller_url = '" . $this->Link() . "';");
Requirements::block(CMS_DIR . '/javascript/TranslationTab.js');
Requirements::block(CMS_DIR . '/javascript/LangSelector.js');
Requirements::javascript('translatablemodeladmin/javascript/TranslatableModelAdmin.js');
-
- // Set locale from the get param if one is given
- if(isset($_GET['locale'])) {
- Session::set('TranslatableModelAdmin.Locale', $_GET['locale']);
- }
- // We store the locale in the session because it was a less invasive change to ModelAdmin
- if($locale = Session::get('TranslatableModelAdmin.Locale')) {
- Translatable::set_current_locale($locale);
+ // Similar to CMSMain->init()
+ if($this->getRequest()->requestVar("Locale")) {
+ $this->Locale = $this->getRequest()->requestVar("Locale");
+ } elseif($this->getRequest()->requestVar("locale")) {
+ $this->Locale = $this->getRequest()->requestVar("locale");
+ } else {
+ $this->Locale = Translatable::default_locale();
}
+ Translatable::set_current_locale($this->Locale);
}
/**
@@ -59,6 +64,33 @@ function ClassName() {
function Title() {
return singleton($this->modelClass)->singular_name();
}
+
+ function CreateForm() {
+ $form = parent::CreateForm();
+ $form->Fields()->push(new HiddenField('Locale', null, $this->parentController->Locale));
+
+ return $form;
+ }
+
+ function AddForm() {
+ $form = parent::AddForm();
+ $form->Fields()->push(new HiddenField('Locale', null, $this->parentController->Locale));
+
+ return $form;
+ }
+
+ function SearchForm() {
+ $form = parent::SearchForm();
+ $form->Fields()->push(new HiddenField('Locale', null, $this->parentController->Locale));
+
+ return $form;
+ }
+
+ function getSearchQuery($searchCriteria) {
+ $context = singleton($this->modelClass)->getDefaultSearchContext();
+ $context->addFilter(new ExactMatchFilter('Locale', $this->parentController->Locale));
+ return $context->getQuery($searchCriteria);
+ }
/**
* Returns all languages with languages already used appearing first.
@@ -86,6 +118,64 @@ function LangSelector() {
}
class TranslatableModelAdmin_RecordController extends ModelAdmin_RecordController {
+
+ function EditForm() {
+ $form = parent::EditForm();
+
+ if($this->currentRecord->hasExtension('Translatable')) {
+ // TODO Exclude languages which are already translated into
+ $dropdown = new LanguageDropdownField(
+ 'NewTransLang',
+ _t('TranslatableModelAdmin.LANGDROPDOWNLABEL', 'Language'),
+ array(),
+ $this->currentRecord->class,
+ 'Locale-English'
+ );
+ $action = new InlineFormAction(
+ 'createtranslation',
+ _t('TranslatableModelAdmin.CREATETRANSBUTTON',
+ "Create translation")
+ );
+ $header = new HeaderField(
+ 'ExistingTransHeader',
+ _t('TranslatableModelAdmin.EXISTINGTRANSTABLE', 'Existing Translations'),
+ 4
+ );
+ // TODO Exclude the current language
+ $table = new TableListField(
+ 'Translations',
+ $this->currentRecord->class
+ );
+ $table->setPermissions(array('show'));
+ $table->setCustomSourceItems($this->currentRecord->getTranslations());
+ $action->includeDefaultJS = false;
+ if($form->Fields()->hasTabSet()) {
+ $form->Fields()->findOrMakeTab(
+ 'Root.Translations',
+ _t("TranslatableModelAdmin.TRANSLATIONSTAB", "Translations")
+ );
+ $form->Fields()->addFieldToTab('Root.Translations', $header);
+ $form->Fields()->addFieldToTab('Root.Translations', $table);
+ $form->Fields()->addFieldToTab('Root.Translations', $dropdown);
+ $form->Fields()->addFieldToTab('Root.Translations', $action);
+ } else {
+ $form->Fields()->push(new HeaderField(
+ 'TranslationsHeader',
+ _t("TranslatableModelAdmin.TRANSLATIONSTAB", "Translations")
+ ));
+ $form->Fields()->push($header);
+ $form->Fields()->push($table);
+ $form->Fields()->push($dropdown);
+ $form->Fields()->push($action);
+ }
+ // TODO This is hacky, but necessary to get proper identifiers
+ $form->Fields()->setForm($form);
+
+ }
+
+ return $form;
+ }
+
/**
* Create a new translation from an existing item, switch to this language and reload the tree.
*/
@@ -94,8 +184,10 @@ function createtranslation($data, $form, $edit) {
Translatable::set_current_locale($langCode);
$translatedRecord = $this->currentRecord->createTranslation($langCode);
-
+
$this->currentRecord = $translatedRecord;
+
+ // TODO Return current language as GET parameter
return $this->edit(null);
}
}
View
No changes.
View
@@ -0,0 +1,13 @@
+<?php
+
+global $lang;
+
+$lang['en_US']['TranslatableModelAdmin']['CREATETRANSBUTTON'] = 'Create translation';
+$lang['en_US']['TranslatableModelAdmin']['EXISTINGTRANSTABLE'] = 'Existing Translations';
+$lang['en_US']['TranslatableModelAdmin']['LANGDROPDOWNLABEL'] = 'Language';
+$lang['en_US']['TranslatableModelAdmin']['TRANSLATIONSTAB'] = 'Translations';
+$lang['en_US']['TranslatableModelAdmin_left.ss']['ADDLISTING'] = 'Add';
+$lang['en_US']['TranslatableModelAdmin_left.ss']['IMPORT_TAB_HEADER'] = 'Import';
+$lang['en_US']['TranslatableModelAdmin_left.ss']['SEARCHLISTINGS'] = 'Search';
+
+?>

0 comments on commit 9230b15

Please sign in to comment.