From a0a6ec2f913d7bd9e8de8bf8546bb95f8fb570b7 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 1 Jun 2015 20:52:07 +1200 Subject: [PATCH 1/2] Consistently use uppercase "Locale" GET param Causes inconsistent behaviour between form field name ("Locale") and view state in GET parameters ("Locale" and "locale"). Related to #156. --- code/controller/TranslatableCMSMainExtension.php | 12 ++++-------- code/model/Translatable.php | 2 +- javascript/CMSMain.Translatable.js | 4 ++-- javascript/HtmlEditorField.Translatable.js | 2 +- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/code/controller/TranslatableCMSMainExtension.php b/code/controller/TranslatableCMSMainExtension.php index 7f5778f..77ca390 100644 --- a/code/controller/TranslatableCMSMainExtension.php +++ b/code/controller/TranslatableCMSMainExtension.php @@ -26,8 +26,6 @@ class TranslatableCMSMainExtension extends Extension { $id = $req->param('ID'); if($req->requestVar("Locale")) { $this->owner->Locale = $req->requestVar("Locale"); - } elseif($req->requestVar("locale")) { - $this->owner->Locale = $req->requestVar("locale"); } else if($id && is_numeric($id)) { $record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); if($record && $record->Locale) $this->owner->Locale = $record->Locale; @@ -53,8 +51,6 @@ class TranslatableCMSMainExtension extends Extension { // if a locale is set, it needs to match to the current record if($req->requestVar("Locale")) { $requestLocale = $req->requestVar("Locale"); - } else { - $requestLocale = $req->requestVar("locale"); } $page = $this->owner->currentPage(); @@ -75,7 +71,7 @@ class TranslatableCMSMainExtension extends Extension { // If the record is not translated, redirect to pages overview return $this->owner->redirect(Controller::join_links( singleton('CMSPagesController')->Link(), - '?locale=' . $requestLocale + '?Locale=' . $requestLocale )); } } @@ -140,12 +136,12 @@ class TranslatableCMSMainExtension extends Extension { function updateLink(&$link) { $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); - if($locale) $link = Controller::join_links($link, '?locale=' . $locale); + if($locale) $link = Controller::join_links($link, '?Locale=' . $locale); } function updateLinkWithSearch(&$link) { $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); - if($locale) $link = Controller::join_links($link, '?locale=' . $locale); + if($locale) $link = Controller::join_links($link, '?Locale=' . $locale); } function updateExtraTreeTools(&$html) { @@ -155,7 +151,7 @@ class TranslatableCMSMainExtension extends Extension { function updateLinkPageAdd(&$link) { $locale = $this->owner->Locale ? $this->owner->Locale : Translatable::get_current_locale(); - if($locale) $link = Controller::join_links($link, '?locale=' . $locale); + if($locale) $link = Controller::join_links($link, '?Locale=' . $locale); } /** diff --git a/code/model/Translatable.php b/code/model/Translatable.php index 5a6d358..dd19233 100755 --- a/code/model/Translatable.php +++ b/code/model/Translatable.php @@ -1119,7 +1119,7 @@ class Translatable extends DataExtension implements PermissionProvider { '
  • %s
  • ', Controller::join_links( $existingTranslation->CMSEditLink(), - '?locale=' . $existingTranslation->Locale + '?Locale=' . $existingTranslation->Locale ), i18n::get_locale_name($existingTranslation->Locale) ); diff --git a/javascript/CMSMain.Translatable.js b/javascript/CMSMain.Translatable.js index 196c17d..161f13c 100755 --- a/javascript/CMSMain.Translatable.js +++ b/javascript/CMSMain.Translatable.js @@ -37,10 +37,10 @@ }, onchange: function(e) { // Get new locale code - locale = {locale: $(e.target).val()}; + var locale = {Locale: $(e.target).val()}; // Check existing url - search = /locale=[^&]*/; + search = /Locale=[^&]*/; url = document.location.href; if(url.match(search)) { // Replace locale code diff --git a/javascript/HtmlEditorField.Translatable.js b/javascript/HtmlEditorField.Translatable.js index 0aec82d..8830659 100644 --- a/javascript/HtmlEditorField.Translatable.js +++ b/javascript/HtmlEditorField.Translatable.js @@ -31,7 +31,7 @@ onchange: function(e) { // reload tree with selected locale var treeDropdown = $(this).parents('form').find('#internal .treedropdown'); - treeDropdown.data('urlTree', $.path.addSearchParams(treeDropdown.data('urlTree').replace(/locale=[^&]*/, ''), 'locale='+$(this).val())); + treeDropdown.data('urlTree', $.path.addSearchParams(treeDropdown.data('urlTree').replace(/Locale=[^&]*/, ''), 'Locale='+$(this).val())); treeDropdown.loadTree(); } }); From da30096d65741198cd4ddbf2afd55a55bed8d906 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 1 Jun 2015 20:53:53 +1200 Subject: [PATCH 2/2] Set current locale on POST requests (fixes #156) --- code/controller/TranslatableCMSMainExtension.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/code/controller/TranslatableCMSMainExtension.php b/code/controller/TranslatableCMSMainExtension.php index 77ca390..8ed5dd3 100644 --- a/code/controller/TranslatableCMSMainExtension.php +++ b/code/controller/TranslatableCMSMainExtension.php @@ -16,9 +16,6 @@ class TranslatableCMSMainExtension extends Extension { // as an intermediary rather than the endpoint controller if(!$this->owner->stat('tree_class')) return; - // Leave form submissions alone - if($req->httpMethod() != 'GET') return; - // Locale" attribute is either explicitly added by LeftAndMain Javascript logic, // or implied on a translated record (see {@link Translatable->updateCMSFields()}). // $Lang serves as a "context" which can be inspected by Translatable - hence it @@ -48,14 +45,14 @@ class TranslatableCMSMainExtension extends Extension { } Translatable::set_current_locale($this->owner->Locale); - // if a locale is set, it needs to match to the current record - if($req->requestVar("Locale")) { - $requestLocale = $req->requestVar("Locale"); - } - + // If a locale is set, it needs to match to the current record + $requestLocale = $req->requestVar("Locale"); $page = $this->owner->currentPage(); if( - $requestLocale && $page && $page->hasExtension('Translatable') + $req->httpMethod() == 'GET' // leave form submissions alone + && $requestLocale + && $page + && $page->hasExtension('Translatable') && $page->Locale != $requestLocale && $req->latestParam('Action') != 'EditorToolbar' ) {