From 7530becfb0d9c64fd327a092d68377ccdfe3cf1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20B=C3=BCnte?= Date: Tue, 18 Feb 2014 14:19:07 +0100 Subject: [PATCH] Fixed LanguageDropdownField (above Tree) to redirect to the correct url after an other locale has been selected --- .../TranslatableCMSMainExtension.php | 1 + code/forms/LanguageDropdownField.php | 29 ++++++++++-- javascript/CMSMain.Translatable.js | 45 ++++++++++++++----- 3 files changed, 60 insertions(+), 15 deletions(-) mode change 100644 => 100755 code/controller/TranslatableCMSMainExtension.php diff --git a/code/controller/TranslatableCMSMainExtension.php b/code/controller/TranslatableCMSMainExtension.php old mode 100644 new mode 100755 index 7f5778f..1b146dd --- a/code/controller/TranslatableCMSMainExtension.php +++ b/code/controller/TranslatableCMSMainExtension.php @@ -6,6 +6,7 @@ class TranslatableCMSMainExtension extends Extension { private static $allowed_actions = array( 'createtranslation', + 'LangForm' ); function init() { diff --git a/code/forms/LanguageDropdownField.php b/code/forms/LanguageDropdownField.php index d81a1b8..a42de12 100755 --- a/code/forms/LanguageDropdownField.php +++ b/code/forms/LanguageDropdownField.php @@ -8,7 +8,8 @@ class LanguageDropdownField extends GroupedDropdownField { private static $allowed_actions = array( - 'getLocaleForObject' + 'getLocaleForObject', + 'getURLForTranslationAndLocale' ); /** @@ -84,7 +85,10 @@ class LanguageDropdownField extends GroupedDropdownField { public function getAttributes() { return array_merge( parent::getAttributes(), - array('data-locale-url' => $this->Link('getLocaleForObject')) + array( + 'data-locale-url' => $this->Link('getLocaleForObject'), + 'data-translation-url' => $this->Link('getURLForTranslationAndLocale') + ) ); } @@ -108,5 +112,24 @@ class LanguageDropdownField extends GroupedDropdownField { } return $locale; } - + + + /** + * @param $request + * @return string + */ + function getURLForTranslationAndLocale($request) { + $id = (int)$request->requestVar('id'); + $class = Convert::raw2sql($request->requestVar('class')); + $requestedLocale = Convert::raw2sql($request->requestVar('requestedLocale')); + $url = ''; + if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) { + if(($record = $class::get()->byId($id)) && ($translation = $record->getTranslations($requestedLocale)->First())) { + $controller = $translation instanceOf SiteTree ? singleton('CMSPageEditController') : Controller::curr(); + $url = Controller::join_links($controller->Link('show'), $translation->ID, '?locale=' . $translation->Locale); + } + } + return $url; + } + } diff --git a/javascript/CMSMain.Translatable.js b/javascript/CMSMain.Translatable.js index 196c17d..cba1a8d 100755 --- a/javascript/CMSMain.Translatable.js +++ b/javascript/CMSMain.Translatable.js @@ -37,18 +37,39 @@ }, onchange: function(e) { // Get new locale code - locale = {locale: $(e.target).val()}; - - // Check existing url - search = /locale=[^&]*/; - url = document.location.href; - if(url.match(search)) { - // Replace locale code - url = url.replace(search, $.param(locale)); - } else { - // Add locale code - url = $.path.addSearchParams(url, locale); - } + var locale = {locale: $(e.target).val()}; + var url = document.location.href; + + // Check existing url + search = /locale=[^&]*/; + if(url.match(search)) { + // Replace locale code + url = url.replace(search, $.param(locale)); + } else { + // Add locale code + url = $.path.addSearchParams(url, locale); + } + + // Get id for translation and fix redirect url + var selectedTreeItem = $('.cms-tree').jstree('get_selected'); + if(selectedTreeItem.length > 0) { + selectedTreeItem = selectedTreeItem.eq(0); + $.get( + $.path.addSearchParams(this.data('translationUrl'), { + 'requestedLocale': $(e.target).val(), + 'id': selectedTreeItem.data('id'), + 'class': selectedTreeItem.data('pagetype') + }), + function(data) { + if(data != '') { + url = data; + } + $('.cms-container').loadPanel(url); + } + ); + return false; + } + $('.cms-container').loadPanel(url); return false; }