BUG Consistently save SiteConfig, and refactor Translatable hooks

Squashed commit of the following:

commit a60eddfacc710d3047bd1d5107e5df0cc6dba93c
Merge: c847b55 76be14b
Author: Ingo Schommer <ingo@silverstripe.com>
Date:   Fri Sep 7 17:02:47 2012 +0200

    Merge branch '3.0-translation-migration' of git://github.com/tractorcow/silverstripe-cms into tractorcow-3.0-translation-migration

commit 76be14b1fc
Author: Damian Mooyman <damian.mooyman@gmail.com>
Date:   Fri Aug 24 08:28:18 2012 +1200

    FIXED: Indentation

commit 715b60387c
Author: Damian Mooyman <damian.mooyman@gmail.com>
Date:   Fri Aug 24 08:25:14 2012 +1200

    FIXED: Coding style inconsistencies

commit 6395f9030e
Author: Damian Mooyman <damian.mooyman@gmail.com>
Date:   Thu Aug 16 16:30:11 2012 +1200

    FIXED: Issue where new SiteConfig instances weren't always saved to the database

commit aca242e31c
Author: Damian Mooyman <damian.mooyman@gmail.com>
Date:   Thu Aug 16 14:22:56 2012 +1200

    UPDATED: Refactored Translation module specific code out of the SiteConfig
This commit is contained in:
Damian Mooyman 2012-09-07 17:06:29 +02:00 committed by Ingo Schommer
parent c847b55608
commit 02e95adb4c
2 changed files with 24 additions and 64 deletions

View File

@ -1,15 +1,8 @@
<?php <?php
/** /**
* Sitewide configuration. * Sitewide configuration.
* *
* h2. Translation
*
* To enable translation of configurations alongside the {@link Translatable} extension.
* This also allows assigning language-specific toplevel permissions for viewing and editing
* pages, in addition to the normal `TRANSLATE_*`/`TRANSLATE_ALL` permissions.
*
* Object::add_extension('SiteConfig', 'Translatable');
*
* @author Tom Rix * @author Tom Rix
* @package cms * @package cms
*/ */
@ -34,7 +27,16 @@ class SiteConfig extends DataObject implements PermissionProvider {
public static function disable_theme($theme) { public static function disable_theme($theme) {
self::$disabled_themes[$theme] = $theme; self::$disabled_themes[$theme] = $theme;
} }
function populateDefaults()
{
$this->Title = _t('SiteConfig.SITENAMEDEFAULT', "Your Site Name");
$this->Tagline = _t('SiteConfig.TAGLINEDEFAULT', "your tagline here");
// Allow these defaults to be overridden
parent::populateDefaults();
}
/** /**
* Get the fields that are sent to the CMS. In * Get the fields that are sent to the CMS. In
* your extensions: updateCMSFields($fields) * your extensions: updateCMSFields($fields)
@ -84,13 +86,6 @@ class SiteConfig extends DataObject implements PermissionProvider {
$topLevelCreatorsOptionsField->setSource($editorsOptionsSource); $topLevelCreatorsOptionsField->setSource($editorsOptionsSource);
// Translatable doesn't handle updateCMSFields on DataObjects,
// so add it here to save the current Locale,
// because onBeforeWrite does not work.
if(class_exists('Translatable') && Object::has_extension('SiteConfig',"Translatable")){
$fields->push(new HiddenField("Locale"));
}
if (!Permission::check('EDIT_SITECONFIG')) { if (!Permission::check('EDIT_SITECONFIG')) {
$fields->makeFieldReadonly($viewersOptionsField); $fields->makeFieldReadonly($viewersOptionsField);
$fields->makeFieldReadonly($viewerGroupsField); $fields->makeFieldReadonly($viewerGroupsField);
@ -161,22 +156,14 @@ class SiteConfig extends DataObject implements PermissionProvider {
* Get the current sites SiteConfig, and creates a new one * Get the current sites SiteConfig, and creates a new one
* through {@link make_site_config()} if none is found. * through {@link make_site_config()} if none is found.
* *
* @param string $locale
* @return SiteConfig * @return SiteConfig
*/ */
static function current_site_config($locale = null) { static function current_site_config() {
if(class_exists('Translatable') && Object::has_extension('SiteConfig',"Translatable")){ if ($siteConfig = DataObject::get_one('SiteConfig')) return $siteConfig;
$locale = isset($locale) ? $locale : Translatable::get_current_locale();
$siteConfig = Translatable::get_one_by_locale('SiteConfig', $locale);
} else {
$siteConfig = DataObject::get_one('SiteConfig');
}
if (!$siteConfig) $siteConfig = self::make_site_config($locale); return self::make_site_config();
return $siteConfig;
} }
/** /**
* Setup a default SiteConfig record if none exists * Setup a default SiteConfig record if none exists
*/ */
@ -191,39 +178,16 @@ class SiteConfig extends DataObject implements PermissionProvider {
/** /**
* Create SiteConfig with defaults from language file. * Create SiteConfig with defaults from language file.
* if Translatable is enabled on SiteConfig, see if one already exist
* and use those values for the translated defaults.
* *
* @param string $locale * @param string $locale
* @return SiteConfig * @return SiteConfig
*/ */
static function make_site_config($locale = null) { static function make_site_config() {
if(class_exists('Translatable') && !$locale) $locale = Translatable::get_current_locale(); $config = SiteConfig::create();
$config->write();
$siteConfig = new SiteConfig(); return $config;
$siteConfig->Title = _t('SiteConfig.SITENAMEDEFAULT',"Your Site Name"); }
$siteConfig->Tagline = _t('SiteConfig.TAGLINEDEFAULT',"your tagline here");
if(class_exists('Translatable') && $siteConfig->hasExtension('Translatable')){
Translatable::disable_locale_filter();
$defaultConfig = SiteConfig::get()->first();
Translatable::enable_locale_filter();
if($defaultConfig){
return $defaultConfig->createTranslation($locale);
}
// TODO Copy view/edit group settings
// set the correct Locale
$siteConfig->Locale = $locale;
}
$siteConfig->write();
return $siteConfig;
}
/** /**
* Can a user view pages on this site? This method is only * Can a user view pages on this site? This method is only
* called if a page is set to Inherit, but there is nothing * called if a page is set to Inherit, but there is nothing

View File

@ -991,17 +991,13 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
* Stub method to get the site config, provided so it's easy to override * Stub method to get the site config, provided so it's easy to override
*/ */
function getSiteConfig() { function getSiteConfig() {
$altConfig = false;
if($this->hasMethod('alternateSiteConfig')) { if($this->hasMethod('alternateSiteConfig')) {
$altConfig = $this->alternateSiteConfig(); $altConfig = $this->alternateSiteConfig();
if($altConfig) return $altConfig;
} }
if($altConfig) {
return $altConfig; return SiteConfig::current_site_config();
} elseif(class_exists('Translatable') && $this->hasExtension('Translatable')) {
return SiteConfig::current_site_config($this->Locale);
} else {
return SiteConfig::current_site_config();
}
} }
/** /**