UPDATED: Refactored Translation module specific code out of the SiteConfig

This commit is contained in:
Damian Mooyman 2012-08-16 14:22:56 +12:00
parent e486a16a8f
commit aca242e31c
2 changed files with 44 additions and 84 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)
@ -48,25 +50,25 @@ class SiteConfig extends DataObject implements PermissionProvider {
asort($groupsMap); asort($groupsMap);
$fields = new FieldList( $fields = new FieldList(
new TabSet("Root", new TabSet("Root",
$tabMain = new Tab('Main', $tabMain = new Tab('Main',
$titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")), $titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
$taglineField = new TextField("Tagline", _t('SiteConfig.SITETAGLINE', "Site Tagline/Slogan")), $taglineField = new TextField("Tagline", _t('SiteConfig.SITETAGLINE', "Site Tagline/Slogan")),
$themeDropdownField = new DropdownField("Theme", _t('SiteConfig.THEME', 'Theme'), $this->getAvailableThemes()) $themeDropdownField = new DropdownField("Theme", _t('SiteConfig.THEME', 'Theme'), $this->getAvailableThemes())
), ),
$tabAccess = new Tab('Access', $tabAccess = new Tab('Access',
$viewersOptionsField = new OptionsetField("CanViewType", _t('SiteConfig.VIEWHEADER', "Who can view pages on this site?")), $viewersOptionsField = new OptionsetField("CanViewType", _t('SiteConfig.VIEWHEADER', "Who can view pages on this site?")),
$viewerGroupsField = ListboxField::create("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups")) $viewerGroupsField = ListboxField::create("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups"))
->setMultiple(true)->setSource($groupsMap), ->setMultiple(true)->setSource($groupsMap),
$editorsOptionsField = new OptionsetField("CanEditType", _t('SiteConfig.EDITHEADER', "Who can edit pages on this site?")), $editorsOptionsField = new OptionsetField("CanEditType", _t('SiteConfig.EDITHEADER', "Who can edit pages on this site?")),
$editorGroupsField = ListboxField::create("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups")) $editorGroupsField = ListboxField::create("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups"))
->setMultiple(true)->setSource($groupsMap), ->setMultiple(true)->setSource($groupsMap),
$topLevelCreatorsOptionsField = new OptionsetField("CanCreateTopLevelType", _t('SiteConfig.TOPLEVELCREATE', "Who can create pages in the root of the site?")), $topLevelCreatorsOptionsField = new OptionsetField("CanCreateTopLevelType", _t('SiteConfig.TOPLEVELCREATE', "Who can create pages in the root of the site?")),
$topLevelCreatorsGroupsField = ListboxField::create("CreateTopLevelGroups", _t('SiteTree.TOPLEVELCREATORGROUPS', "Top level creators")) $topLevelCreatorsGroupsField = ListboxField::create("CreateTopLevelGroups", _t('SiteTree.TOPLEVELCREATORGROUPS', "Top level creators"))
->setMultiple(true)->setSource($groupsMap) ->setMultiple(true)->setSource($groupsMap)
) )
), ),
new HiddenField('ID') new HiddenField('ID')
); );
$themeDropdownField->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)')); $themeDropdownField->setEmptyString(_t('SiteConfig.DEFAULTTHEME', '(Use default theme)'));
@ -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,15 @@ 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'))
$locale = isset($locale) ? $locale : Translatable::get_current_locale(); return $siteConfig;
$siteConfig = Translatable::get_one_by_locale('SiteConfig', $locale);
} else { return self::make_site_config();
$siteConfig = DataObject::get_one('SiteConfig');
}
if (!$siteConfig) $siteConfig = self::make_site_config($locale);
return $siteConfig;
} }
/** /**
* Setup a default SiteConfig record if none exists * Setup a default SiteConfig record if none exists
*/ */
@ -191,39 +179,14 @@ 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(); return SiteConfig::create();
}
$siteConfig = new SiteConfig();
$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,14 @@ 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();
}
} }
/** /**