From 8c04bafb4e89c91f50f99e8e8cea70def7bf6c8d Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 22 Mar 2011 21:30:10 +1300 Subject: [PATCH] MINOR Added class_exists() check for Translatable, which has moved into a new 'translatable' module --- code/CMSBatchActionHandler.php | 4 +- code/CMSMain.php | 122 +---------------------------- code/ContentController.php | 20 ++--- code/ErrorPage.php | 7 +- code/LeftAndMain.php | 2 +- code/ModelAsController.php | 6 +- code/RootURLController.php | 5 +- code/SearchForm.php | 6 +- code/SiteConfig.php | 8 +- code/SiteTree.php | 4 +- css/CMSMain.css | 4 - javascript/CMSMain.Translatable.js | 77 ------------------ tests/SiteTreeTest.php | 34 ++++---- 13 files changed, 56 insertions(+), 243 deletions(-) delete mode 100755 javascript/CMSMain.Translatable.js diff --git a/code/CMSBatchActionHandler.php b/code/CMSBatchActionHandler.php index 0341d8e3..0cfb63dd 100644 --- a/code/CMSBatchActionHandler.php +++ b/code/CMSBatchActionHandler.php @@ -85,7 +85,7 @@ class CMSBatchActionHandler extends RequestHandler { foreach($ids as $k => $v) if(!is_numeric($v)) unset($ids[$k]); if($ids) { - if(Object::has_extension('SiteTree','Translatable')) Translatable::disable_locale_filter(); + if(class_exists('Translatable') && Object::has_extension('SiteTree','Translatable')) Translatable::disable_locale_filter(); $pages = DataObject::get( $this->recordClass, @@ -96,7 +96,7 @@ class CMSBatchActionHandler extends RequestHandler { ) ); - if(Object::has_extension('SiteTree','Translatable')) Translatable::enable_locale_filter(); + if(class_exists('Translatable') && Object::has_extension('SiteTree','Translatable')) Translatable::enable_locale_filter(); if(Object::has_extension($this->recordClass, 'Versioned')) { // If we didn't query all the pages, then find the rest on the live site diff --git a/code/CMSMain.php b/code/CMSMain.php index dddd6807..ab487284 100755 --- a/code/CMSMain.php +++ b/code/CMSMain.php @@ -30,7 +30,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr 'addpage', 'buildbrokenlinks', 'compareversions', - 'createtranslation', 'deleteitems', 'DeleteItemsForm', 'dialog', @@ -79,28 +78,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr public function init() { parent::init(); - - // 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 - // has the same name as the database property on Translatable. - 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); - - // collect languages for TinyMCE spellchecker plugin. - // see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker - $langName = i18n::get_locale_name($this->Locale); - HtmlEditorConfig::get('cms')->setOption('spellchecker_languages', "+{$langName}={$this->Locale}"); Requirements::javascript(CMS_DIR . '/javascript/CMSMain.js'); Requirements::javascript(CMS_DIR . '/javascript/CMSMain.EditForm.js'); - Requirements::javascript(CMS_DIR . '/javascript/CMSMain.Translatable.js'); Requirements::css(CMS_DIR . '/css/CMSMain.css'); @@ -500,9 +480,6 @@ JS; function RootForm() { $siteConfig = SiteConfig::current_site_config(); $fields = $siteConfig->getCMSFields(); - if(Object::has_extension('SiteConfig',"Translatable")) { - $fields->push(new HiddenField('Locale','', $siteConfig->Locale)); - } $form = new Form($this, 'RootForm', $fields, $siteConfig->getCMSActions()); $form->setHTMLID('Form_EditForm'); @@ -611,7 +588,7 @@ JS; } $record = $this->getNewItem("new-$className-$parentID".$suffix, false); - $record->Locale = $data['Locale']; + if(class_exists('Translatable') && $record->hasExtension('Translatable')) $record->Locale = $data['Locale']; $record->write(); $form = $this->getEditForm($record->ID); @@ -848,6 +825,8 @@ JS; $form->unsetValidator(); $form->addExtraClass('oneline'); + $this->extend('updateSideReportsForm', $form); + return $form; } @@ -1285,7 +1264,6 @@ JS; $fields = new FieldSet( new HiddenField("ParentID"), - new HiddenField("Locale", 'Locale', Translatable::get_current_locale()), new DropdownField("PageType", "", $pageTypes, 'Page') ); @@ -1550,38 +1528,6 @@ JS; user_error("CMSMain::duplicate() Bad ID: '$id'", E_USER_WARNING); } } - - /** - * Create a new translation from an existing item, switch to this language and reload the tree. - */ - function createtranslation($request) { - // Protect against CSRF on destructive action - if(!SecurityToken::inst()->checkRequest($request)) return $this->httpError(400); - - $langCode = Convert::raw2sql($request->getVar('newlang')); - $originalLangID = (int)$request->getVar('ID'); - - $record = $this->getRecord($originalLangID); - - $this->Locale = $langCode; - Translatable::set_current_locale($langCode); - - // 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( - "%s/%d/?locale=%s", - $this->Link('show'), - $translatedRecord->ID, - $langCode - ); - - return Director::redirect($url); - } /** * Provide the permission codes used by LeftAndMain. @@ -1622,69 +1568,7 @@ JS; return $perms; } - - /** - * Returns a form with all languages with languages already used appearing first. - * - * @return Form - */ - function LangForm() { - $member = Member::currentUser(); //check to see if the current user can switch langs or not - if(Permission::checkMember($member, 'VIEW_LANGS')) { - $field = new LanguageDropdownField( - 'Locale', - _t('CMSMAIN.LanguageDropdownLabel', 'Language'), - array(), - 'SiteTree', - 'Locale-English', - singleton('SiteTree') - ); - $field->setValue(Translatable::get_current_locale()); - } else { - // user doesn't have permission to switch langs - // so just show a string displaying current language - $field = new LiteralField( - 'Locale', - i18n::get_locale_name( Translatable::get_current_locale()) - ); - } - - $form = new Form( - $this, - 'LangForm', - new FieldSet( - $field - ), - new FieldSet( - new FormAction('selectlang', _t('CMSMain_left.ss.GO','Go')) - ) - ); - $form->unsetValidator(); - - return $form; - } - - function selectlang($data, $form) { - return $this; - } - - /** - * Determine if there are more than one languages in our site tree. - * - * @return boolean - */ - function MultipleLanguages() { - $langs = Translatable::get_existing_content_languages('SiteTree'); - return (count($langs) > 1); - } - - /** - * @return boolean - */ - function IsTranslatableEnabled() { - return Object::has_extension('SiteTree', 'Translatable'); - } } /** diff --git a/code/ContentController.php b/code/ContentController.php index 18177edb..e443f66e 100755 --- a/code/ContentController.php +++ b/code/ContentController.php @@ -142,12 +142,12 @@ class ContentController extends Controller { // nested URL. if($action && SiteTree::nested_urls() && !$this->hasAction($action)) { // See ModelAdController->getNestedController() for similar logic - Translatable::disable_locale_filter(); + if(class_exists('Translatable')) Translatable::disable_locale_filter(); // look for a page with this URLSegment $child = DataObject::get_one('SiteTree', sprintf ( "\"ParentID\" = %s AND \"URLSegment\" = '%s'", $this->ID, Convert::raw2sql($action) )); - Translatable::enable_locale_filter(); + if(class_exists('Translatable')) Translatable::enable_locale_filter(); // if we can't find a page with this URLSegment try to find one that used to have // that URLSegment but changed. See ModelAsController->getNestedController() for similiar logic. @@ -185,12 +185,14 @@ class ContentController extends Controller { // If a specific locale is requested, and it doesn't match the page found by URLSegment, // look for a translation and redirect (see #5001). Only happens on the last child in // a potentially nested URL chain. - if($request->getVar('locale') && $this->dataRecord && $this->dataRecord->Locale != $request->getVar('locale')) { - $translation = $this->dataRecord->getTranslation($request->getVar('locale')); - if($translation) { - $response = new SS_HTTPResponse(); - $response->redirect($translation->Link(), 301); - throw new SS_HTTPResponse_Exception($response); + if(class_exists('Translatable')) { + if($request->getVar('locale') && $this->dataRecord && $this->dataRecord->Locale != $request->getVar('locale')) { + $translation = $this->dataRecord->getTranslation($request->getVar('locale')); + if($translation) { + $response = new SS_HTTPResponse(); + $response->redirect($translation->Link(), 301); + throw new SS_HTTPResponse_Exception($response); + } } } @@ -415,7 +417,7 @@ HTML; function ContentLocale() { if($this->dataRecord && $this->dataRecord->hasExtension('Translatable')) { $locale = $this->dataRecord->Locale; - } elseif(Object::has_extension('SiteTree', 'Translatable')) { + } elseif(class_exists('Translatable') && Object::has_extension('SiteTree', 'Translatable')) { $locale = Translatable::get_current_locale(); } else { $locale = i18n::get_locale(); diff --git a/code/ErrorPage.php b/code/ErrorPage.php index 8a4fdc0b..3b441daf 100755 --- a/code/ErrorPage.php +++ b/code/ErrorPage.php @@ -39,7 +39,10 @@ class ErrorPage extends Page { } // then fall back on a cached version - $cachedPath = self::get_filepath_for_errorcode($statusCode, Translatable::get_current_locale()); + $cachedPath = self::get_filepath_for_errorcode( + $statusCode, + class_exists('Translatable') ? Translatable::get_current_locale() : null + ); if(file_exists($cachedPath)) { $response = new SS_HTTPResponse(); @@ -234,7 +237,7 @@ class ErrorPage extends Page { if (singleton('ErrorPage')->hasMethod('alternateFilepathForErrorcode')) { return singleton('ErrorPage')-> alternateFilepathForErrorcode($statusCode, $locale); } - if(singleton('SiteTree')->hasExtension('Translatable') && $locale && $locale != Translatable::default_locale()) { + if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && $locale && $locale != Translatable::default_locale()) { return self::$static_filepath . "/error-{$statusCode}-{$locale}.html"; } else { return self::$static_filepath . "/error-{$statusCode}.html"; diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index f33fc091..6ef6f341 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -125,7 +125,7 @@ class LeftAndMain extends Controller { ); // set reading lang - if(Object::has_extension('SiteTree', 'Translatable') && !$this->isAjax()) { + if(class_exists('Translatable') && Object::has_extension('SiteTree', 'Translatable') && !$this->isAjax()) { Translatable::choose_site_locale(array_keys(Translatable::get_existing_content_languages('SiteTree'))); } diff --git a/code/ModelAsController.php b/code/ModelAsController.php index bbc7efb7..ccadc125 100755 --- a/code/ModelAsController.php +++ b/code/ModelAsController.php @@ -88,7 +88,7 @@ class ModelAsController extends Controller implements NestedController { } // Find page by link, regardless of current locale settings - Translatable::disable_locale_filter(); + if(class_exists('Translatable')) Translatable::disable_locale_filter(); $sitetree = DataObject::get_one( 'SiteTree', sprintf( @@ -97,7 +97,7 @@ class ModelAsController extends Controller implements NestedController { (SiteTree::nested_urls() ? 'AND "ParentID" = 0' : null) ) ); - Translatable::enable_locale_filter(); + if(class_exists('Translatable')) Translatable::enable_locale_filter(); if(!$sitetree) { // If a root page has been renamed, redirect to the new location. @@ -133,7 +133,7 @@ class ModelAsController extends Controller implements NestedController { } // Enforce current locale setting to the loaded SiteTree object - if($sitetree->Locale) Translatable::set_current_locale($sitetree->Locale); + if(class_exists('Translatable') && $sitetree->Locale) Translatable::set_current_locale($sitetree->Locale); if(isset($_REQUEST['debug'])) { Debug::message("Using record #$sitetree->ID of type $sitetree->class with link {$sitetree->Link()}"); diff --git a/code/RootURLController.php b/code/RootURLController.php index 27f39195..c9ebfacf 100755 --- a/code/RootURLController.php +++ b/code/RootURLController.php @@ -40,7 +40,8 @@ class RootURLController extends Controller { if(!self::$cached_homepage_link) { if ( - Object::has_extension('SiteTree', 'Translatable') + class_exists('Translatable') + && Object::has_extension('SiteTree', 'Translatable') && $link = Translatable::get_homepage_link_by_locale(Translatable::get_current_locale()) ) { self::$cached_homepage_link = $link; @@ -72,7 +73,7 @@ class RootURLController extends Controller { public static function should_be_on_root(SiteTree $page) { if(!self::$is_at_root && self::get_homepage_link() == trim($page->RelativeLink(true), '/')) { return !( - $page->hasExtension('Translatable') && $page->Locale && $page->Locale != Translatable::default_locale() + class_exists('Translatable') && $page->hasExtension('Translatable') && $page->Locale && $page->Locale != Translatable::default_locale() ); } diff --git a/code/SearchForm.php b/code/SearchForm.php index edb87ebd..8a82f3fb 100755 --- a/code/SearchForm.php +++ b/code/SearchForm.php @@ -42,7 +42,7 @@ class SearchForm extends Form { )); } - if(singleton('SiteTree')->hasExtension('Translatable')) { + if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable')) { $fields->push(new HiddenField('locale', 'locale', Translatable::get_current_locale())); } @@ -101,7 +101,7 @@ class SearchForm extends Form { if(!isset($data) || !is_array($data)) $data = $_REQUEST; // set language (if present) - if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { + if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { $origLocale = Translatable::get_current_locale(); Translatable::set_current_locale($data['locale']); } @@ -137,7 +137,7 @@ class SearchForm extends Form { } // reset locale - if(singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { + if(class_exists('Translatable') && singleton('SiteTree')->hasExtension('Translatable') && isset($data['locale'])) { Translatable::set_current_locale($origLocale); } diff --git a/code/SiteConfig.php b/code/SiteConfig.php index 02fb87ef..a0843bc7 100644 --- a/code/SiteConfig.php +++ b/code/SiteConfig.php @@ -81,7 +81,7 @@ class SiteConfig extends DataObject implements PermissionProvider { // Translatable doesn't handle updateCMSFields on DataObjects, // so add it here to save the current Locale, // because onBeforeWrite does not work. - if(Object::has_extension('SiteConfig',"Translatable")){ + if(class_exists('Translatable') && Object::has_extension('SiteConfig',"Translatable")){ $fields->push(new HiddenField("Locale")); } @@ -151,7 +151,7 @@ class SiteConfig extends DataObject implements PermissionProvider { * @return SiteConfig */ static function current_site_config($locale = null) { - if(Object::has_extension('SiteConfig',"Translatable")){ + if(class_exists('Translatable') && Object::has_extension('SiteConfig',"Translatable")){ $locale = isset($locale) ? $locale : Translatable::get_current_locale(); $siteConfig = Translatable::get_one_by_locale('SiteConfig', $locale); } else { @@ -184,13 +184,13 @@ class SiteConfig extends DataObject implements PermissionProvider { * @return SiteConfig */ static function make_site_config($locale = null) { - if(!$locale) $locale = Translatable::get_current_locale(); + if(class_exists('Translatable') && !$locale) $locale = Translatable::get_current_locale(); $siteConfig = new SiteConfig(); $siteConfig->Title = _t('SiteConfig.SITENAMEDEFAULT',"Your Site Name"); $siteConfig->Tagline = _t('SiteConfig.TAGLINEDEFAULT',"your tagline here"); - if($siteConfig->hasExtension('Translatable')){ + if(class_exists('Translatable') && $siteConfig->hasExtension('Translatable')){ $defaultConfig = DataObject::get_one('SiteConfig'); if($defaultConfig){ $siteConfig->Title = $defaultConfig->Title; diff --git a/code/SiteTree.php b/code/SiteTree.php index ffd48b88..265e3c4b 100755 --- a/code/SiteTree.php +++ b/code/SiteTree.php @@ -412,7 +412,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid // is on the root level. if(!$action && $base == RootURLController::get_homepage_link() && !$this->ParentID) { $base = null; - if($this->hasExtension('Translatable') && $this->Locale != Translatable::default_locale()){ + if(class_exists('Translatable') && $this->hasExtension('Translatable') && $this->Locale != Translatable::default_locale()){ $base = $this->URLSegment; } } @@ -976,7 +976,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid } if($altConfig) { return $altConfig; - } elseif($this->hasExtension('Translatable')) { + } elseif(class_exists('Translatable') && $this->hasExtension('Translatable')) { return SiteConfig::current_site_config($this->Locale); } else { return SiteConfig::current_site_config(); diff --git a/css/CMSMain.css b/css/CMSMain.css index 58fdba0f..e59b97e0 100644 --- a/css/CMSMain.css +++ b/css/CMSMain.css @@ -1,7 +1,3 @@ -.right form#Form_EditForm div.createTranslation { - margin-left: 0; -} - div#SideReportsHolder { clear: left; } \ No newline at end of file diff --git a/javascript/CMSMain.Translatable.js b/javascript/CMSMain.Translatable.js deleted file mode 100755 index 4a34ad70..00000000 --- a/javascript/CMSMain.Translatable.js +++ /dev/null @@ -1,77 +0,0 @@ -/** - * File: CMSMain.Translatable.js - */ -(function($) { - $.entwine('ss', function($){ - - /** - * Class: .CMSMain #Form_LangForm - * - * Dropdown with languages above CMS tree, causing a redirect upon translation - */ - $('.CMSMain #Form_LangForm').entwine({ - /** - * Constructor: onmatch - */ - onmatch: function() { - var self = this; - - // monitor form loading for any locale changes - $('#Form_EditForm').bind('loadnewpage', function(e) { - var newLocale = $(this).find(':input[name=Locale]').val(); - if(newLocale) self.val(newLocale); - }); - - // whenever a new value is selected, reload the whole CMS in the new locale - this.find(':input[name=Locale]').bind('change', function(e) { - var url = document.location.href; - url += (url.indexOf('?') != -1) ? '&' : '?'; - // TODO Replace existing locale GET params - url += 'locale=' + $(e.target).val(); - document.location = url; - return false; - }); - - this._super(); - } - }); - - /** - * Class: .CMSMain .createTranslation - * - * Loads /admin/createtranslation, which will create the new record, - * and redirect to an edit form. - * - * Dropdown in "Translation" tab in CMS forms, with button to - * trigger translating the currently loaded record. - * - * Requires: - * jquery.metadata - */ - $('.CMSMain .createTranslation').entwine({ - - /** - * Constructor: onmatch - */ - onmatch: function() { - var self = this; - - this.find(':input[name=action_createtranslation]').bind('click', function(e) { - var form = self.parents('form'); - // redirect to new URL - // TODO This should really be a POST request - - document.location.href = $('base').attr('href') + - jQuery(self).metadata().url + - '?ID=' + form.find(':input[name=ID]').val() + - '&newlang=' + self.find(':input[name=NewTransLang]').val() + - '&locale=' + form.find(':input[name=Locale]').val(); - - return false; - }); - - this._super(); - } - }); - }); -}(jQuery)); \ No newline at end of file diff --git a/tests/SiteTreeTest.php b/tests/SiteTreeTest.php index 44e5c41d..50ea18a6 100755 --- a/tests/SiteTreeTest.php +++ b/tests/SiteTreeTest.php @@ -20,16 +20,18 @@ class SiteTreeTest extends SapphireTest { self::kill_temp_db(); // store old defaults - self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable'); - self::$origTranslatableSettings['default_locale'] = Translatable::default_locale(); + if(class_exists('Translatable')) { + self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable'); + self::$origTranslatableSettings['default_locale'] = Translatable::default_locale(); + + // overwrite locale + Translatable::set_default_locale("en_US"); - // overwrite locale - Translatable::set_default_locale("en_US"); - - // refresh the decorated statics - different fields in $db with Translatable enabled - if(self::$origTranslatableSettings['has_extension']) { - Object::remove_extension('SiteTree', 'Translatable'); - Object::remove_extension('SiteConfig', 'Translatable'); + // refresh the decorated statics - different fields in $db with Translatable enabled + if(self::$origTranslatableSettings['has_extension']) { + Object::remove_extension('SiteTree', 'Translatable'); + Object::remove_extension('SiteConfig', 'Translatable'); + } } // recreate database with new settings @@ -40,14 +42,16 @@ class SiteTreeTest extends SapphireTest { } static function tear_down_once() { - if(self::$origTranslatableSettings['has_extension']) { - Object::add_extension('SiteTree', 'Translatable'); - Object::add_extension('SiteConfig', 'Translatable'); - } + if(class_exists('Translatable')) { + if(self::$origTranslatableSettings['has_extension']) { + Object::add_extension('SiteTree', 'Translatable'); + Object::add_extension('SiteConfig', 'Translatable'); + } - Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']); - Translatable::set_current_locale(self::$origTranslatableSettings['default_locale']); + Translatable::set_default_locale(self::$origTranslatableSettings['default_locale']); + Translatable::set_current_locale(self::$origTranslatableSettings['default_locale']); + } self::kill_temp_db(); self::create_temp_db();