Skip to content
Browse files

MINOR Added class_exists() check for Translatable, which has moved in…

…to a new 'translatable' module
  • Loading branch information...
1 parent 6bd9c56 commit 8c04bafb4e89c91f50f99e8e8cea70def7bf6c8d @chillu chillu committed
View
4 code/CMSBatchActionHandler.php
@@ -85,7 +85,7 @@ function handleAction($request) {
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 @@ function handleAction($request) {
)
);
- 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
View
122 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 @@ static function T_SiteTreeFilterOptions(){
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 @@ public function getEditForm($id = null) {
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 @@ public function doAdd($data, $form) {
}
$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 @@ function SideReportsForm() {
$form->unsetValidator();
$form->addExtraClass('oneline');
+ $this->extend('updateSideReportsForm', $form);
+
return $form;
}
@@ -1285,7 +1264,6 @@ function AddForm() {
$fields = new FieldSet(
new HiddenField("ParentID"),
- new HiddenField("Locale", 'Locale', Translatable::get_current_locale()),
new DropdownField("PageType", "", $pageTypes, 'Page')
);
@@ -1550,38 +1528,6 @@ function duplicatewithchildren($request) {
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 @@ function providePermissions() {
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');
- }
}
/**
View
20 code/ContentController.php
@@ -142,12 +142,12 @@ public function handleRequest(SS_HTTPRequest $request) {
// 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 @@ public function handleRequest(SS_HTTPRequest $request) {
// 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 @@ function LangAttributes() {
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();
View
7 code/ErrorPage.php
@@ -39,7 +39,10 @@ public static function response_for($statusCode) {
}
// 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 @@ static function get_filepath_for_errorcode($statusCode, $locale = null) {
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";
View
2 code/LeftAndMain.php
@@ -125,7 +125,7 @@ function init() {
);
// 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')));
}
View
6 code/ModelAsController.php
@@ -88,7 +88,7 @@ public function getNestedController() {
}
// 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 @@ public function getNestedController() {
(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 @@ public function getNestedController() {
}
// 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()}");
View
5 code/RootURLController.php
@@ -40,7 +40,8 @@ public static function get_homepage_link() {
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 @@ public static function get_default_homepage_link() {
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()
);
}
View
6 code/SearchForm.php
@@ -42,7 +42,7 @@ function __construct($controller, $name, $fields = null, $actions = null) {
));
}
- 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 @@ public function getResults($pageLength = null, $data = null){
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 @@ public function getResults($pageLength = null, $data = null){
}
// 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);
}
View
8 code/SiteConfig.php
@@ -81,7 +81,7 @@ function getCMSFields() {
// 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 @@ function getCMSActions() {
* @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 @@ function requireDefaultRecords() {
* @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;
View
4 code/SiteTree.php
@@ -412,7 +412,7 @@ public function RelativeLink($action = null) {
// 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 @@ function getSiteConfig() {
}
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();
View
4 css/CMSMain.css
@@ -1,7 +1,3 @@
-.right form#Form_EditForm div.createTranslation {
- margin-left: 0;
-}
-
div#SideReportsHolder {
clear: left;
}
View
77 javascript/CMSMain.Translatable.js
@@ -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));
View
34 tests/SiteTreeTest.php
@@ -20,16 +20,18 @@ static function set_up_once() {
self::kill_temp_db();
// store old defaults
- self::$origTranslatableSettings['has_extension'] = singleton('SiteTree')->hasExtension('Translatable');
- self::$origTranslatableSettings['default_locale'] = Translatable::default_locale();
-
- // overwrite locale
- Translatable::set_default_locale("en_US");
+ 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");
- // 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 @@ static function set_up_once() {
}
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();

0 comments on commit 8c04baf

Please sign in to comment.
Something went wrong with that request. Please try again.