2009-10-16 00:30:34 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-01-21 11:00:40 +01:00
|
|
|
* 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('SiteConig', 'Translatable');
|
2009-10-16 00:30:34 +02:00
|
|
|
*
|
|
|
|
* @author Tom Rix
|
2010-01-12 23:48:03 +01:00
|
|
|
* @package cms
|
2009-10-16 00:30:34 +02:00
|
|
|
*/
|
2010-01-12 23:48:03 +01:00
|
|
|
class SiteConfig extends DataObject {
|
2009-10-16 00:30:34 +02:00
|
|
|
static $db = array(
|
|
|
|
"Title" => "Varchar(255)",
|
|
|
|
"Tagline" => "Varchar(255)",
|
2010-02-04 02:58:29 +01:00
|
|
|
"Theme" => "Varchar(255)",
|
2009-10-16 00:30:34 +02:00
|
|
|
"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
|
2010-01-12 23:48:03 +01:00
|
|
|
"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
|
|
|
|
"CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')",
|
2009-10-16 00:30:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
static $many_many = array(
|
|
|
|
"ViewerGroups" => "Group",
|
2010-01-12 23:48:03 +01:00
|
|
|
"EditorGroups" => "Group",
|
|
|
|
"CreateTopLevelGroups" => "Group"
|
2009-10-16 00:30:34 +02:00
|
|
|
);
|
|
|
|
|
2010-02-04 02:58:29 +01:00
|
|
|
protected static $disabled_themes = array();
|
|
|
|
|
|
|
|
public static function disable_theme($theme) {
|
|
|
|
self::$disabled_themes[$theme] = $theme;
|
|
|
|
}
|
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
/**
|
|
|
|
* Get the fields that are sent to the CMS. In
|
|
|
|
* your decorators: updateEditFormFields(&$fields)
|
|
|
|
*
|
|
|
|
* @return Fieldset
|
|
|
|
*/
|
|
|
|
function getFormFields() {
|
2010-02-22 06:34:53 +01:00
|
|
|
Requirements::javascript(CMS_DIR . "/javascript/SitetreeAccess.js");
|
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
$fields = new FieldSet(
|
|
|
|
new TabSet("Root",
|
|
|
|
new Tab('Main',
|
2010-01-12 23:48:03 +01:00
|
|
|
$titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
|
2010-02-04 02:58:29 +01:00
|
|
|
$taglineField = new TextField("Tagline", _t('SiteConfig.SITETAGLINE', "Site Tagline/Slogan")),
|
|
|
|
new DropdownField("Theme", _t('SiteConfig.THEME', 'Theme'), $this->getAvailableThemes(), '', null, _t('SiteConfig.DEFAULTTHEME', '(Use default theme)'))
|
2009-10-16 00:30:34 +02:00
|
|
|
),
|
|
|
|
new Tab('Access',
|
2010-01-12 23:48:03 +01:00
|
|
|
new HeaderField('WhoCanViewHeader', _t('SiteConfig.VIEWHEADER', "Who can view pages on this site?"), 2),
|
2009-10-16 00:30:34 +02:00
|
|
|
$viewersOptionsField = new OptionsetField("CanViewType"),
|
|
|
|
$viewerGroupsField = new TreeMultiselectField("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups")),
|
2010-01-12 23:48:03 +01:00
|
|
|
new HeaderField('WhoCanEditHeader', _t('SiteConfig.EDITHEADER', "Who can edit pages on this site?"), 2),
|
2009-10-16 00:30:34 +02:00
|
|
|
$editorsOptionsField = new OptionsetField("CanEditType"),
|
2010-01-12 23:48:03 +01:00
|
|
|
$editorGroupsField = new TreeMultiselectField("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups")),
|
|
|
|
new HeaderField('WhoCanCreateTopLevelHeader', _t('SiteConfig.TOPLEVELCREATE', "Who can create pages in the root of the site?"), 2),
|
|
|
|
$topLevelCreatorsOptionsField = new OptionsetField("CanCreateTopLevelType"),
|
|
|
|
$topLevelCreatorsGroupsField = new TreeMultiselectField("CreateTopLevelGroups", _t('SiteTree.TOPLEVELCREATORGROUPS', "Top level creators"))
|
2009-10-16 00:30:34 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$viewersOptionsSource = array();
|
|
|
|
$viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
|
|
|
|
$viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
|
|
|
|
$viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
|
|
|
|
$viewersOptionsField->setSource($viewersOptionsSource);
|
|
|
|
|
|
|
|
$editorsOptionsSource = array();
|
|
|
|
$editorsOptionsSource["LoggedInUsers"] = _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS");
|
|
|
|
$editorsOptionsSource["OnlyTheseUsers"] = _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)");
|
|
|
|
$editorsOptionsField->setSource($editorsOptionsSource);
|
2010-01-12 23:48:03 +01:00
|
|
|
|
|
|
|
$topLevelCreatorsOptionsField->setSource($editorsOptionsSource);
|
2010-01-21 11:00:40 +01:00
|
|
|
|
|
|
|
// 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")){
|
|
|
|
$fields->push(new HiddenField("Locale"));
|
|
|
|
}
|
2009-10-16 00:30:34 +02:00
|
|
|
|
2010-01-12 23:36:13 +01:00
|
|
|
if (!Permission::check('EDIT_SITECONFIG')) {
|
2009-10-16 00:30:34 +02:00
|
|
|
$fields->makeFieldReadonly($viewersOptionsField);
|
|
|
|
$fields->makeFieldReadonly($viewerGroupsField);
|
|
|
|
$fields->makeFieldReadonly($editorsOptionsField);
|
|
|
|
$fields->makeFieldReadonly($editorGroupsField);
|
2010-01-12 23:48:03 +01:00
|
|
|
$fields->makeFieldReadonly($topLevelCreatorsOptionsField);
|
|
|
|
$fields->makeFieldReadonly($topLevelCreatorsGroupsField);
|
2009-10-16 00:30:34 +02:00
|
|
|
$fields->makeFieldReadonly($taglineField);
|
|
|
|
$fields->makeFieldReadonly($titleField);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extend('updateEditFormFields', $fields);
|
|
|
|
return $fields;
|
|
|
|
}
|
2010-02-04 02:58:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all available themes that haven't been marked as disabled.
|
|
|
|
* @param string $baseDir Optional alternative theme base directory for testing
|
|
|
|
* @return array of theme directory names
|
|
|
|
*/
|
|
|
|
public function getAvailableThemes($baseDir = null) {
|
|
|
|
$themes = ManifestBuilder::get_themes($baseDir);
|
|
|
|
foreach(self::$disabled_themes as $theme) {
|
|
|
|
if(isset($themes[$theme])) unset($themes[$theme]);
|
|
|
|
}
|
|
|
|
return $themes;
|
|
|
|
}
|
2009-10-16 00:30:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the actions that are sent to the CMS. In
|
|
|
|
* your decorators: updateEditFormActions(&$actions)
|
|
|
|
*
|
|
|
|
* @return Fieldset
|
|
|
|
*/
|
|
|
|
function getFormActions() {
|
2009-10-16 00:36:15 +02:00
|
|
|
if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) {
|
2009-10-16 00:30:34 +02:00
|
|
|
$actions = new FieldSet(
|
|
|
|
new FormAction('save_siteconfig', 'Save')
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$actions = new FieldSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->extend('updateEditFormActions', $actions);
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-01-21 11:00:40 +01:00
|
|
|
* Get the current sites SiteConfig, and creates a new one
|
|
|
|
* through {@link make_site_config()} if none is found.
|
2009-10-16 00:30:34 +02:00
|
|
|
*
|
2010-01-21 11:00:40 +01:00
|
|
|
* @param string $locale
|
2009-10-16 00:30:34 +02:00
|
|
|
* @return SiteConfig
|
|
|
|
*/
|
2010-01-21 11:00:40 +01:00
|
|
|
static function current_site_config($locale = null) {
|
|
|
|
if(Object::has_extension('SiteConfig',"Translatable")){
|
|
|
|
$locale = isset($locale) ? $locale : Translatable::get_current_locale();
|
|
|
|
$siteConfig = Translatable::get_one_by_locale('SiteConfig', $locale);
|
|
|
|
} else {
|
2009-10-16 00:30:34 +02:00
|
|
|
$siteConfig = DataObject::get_one('SiteConfig');
|
|
|
|
}
|
2010-01-21 11:00:40 +01:00
|
|
|
|
|
|
|
if (!$siteConfig) $siteConfig = self::make_site_config($locale);
|
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
return $siteConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup a default SiteConfig record if none exists
|
|
|
|
*/
|
|
|
|
function requireDefaultRecords() {
|
|
|
|
parent::requireDefaultRecords();
|
2009-10-22 09:01:37 +02:00
|
|
|
$siteConfig = DataObject::get_one('SiteConfig');
|
|
|
|
if(!$siteConfig) {
|
|
|
|
self::make_site_config();
|
2009-10-26 23:03:29 +01:00
|
|
|
DB::alteration_message("Added default site config","created");
|
2009-10-22 09:01:37 +02:00
|
|
|
}
|
2009-10-16 00:30:34 +02:00
|
|
|
}
|
|
|
|
|
2010-01-12 23:48:03 +01:00
|
|
|
/**
|
2010-01-21 11:00:40 +01:00
|
|
|
* 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
|
|
|
|
* @return SiteConfig
|
2010-01-12 23:48:03 +01:00
|
|
|
*/
|
2010-01-21 11:00:40 +01:00
|
|
|
static function make_site_config($locale = null) {
|
|
|
|
if(!$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')){
|
|
|
|
$defaultConfig = DataObject::get_one('SiteConfig');
|
|
|
|
if($defaultConfig){
|
|
|
|
$siteConfig->Title = $defaultConfig->Title;
|
|
|
|
$siteConfig->Tagline = $defaultConfig->Tagline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Copy view/edit group settings
|
|
|
|
|
|
|
|
// set the correct Locale
|
|
|
|
$siteConfig->Locale = $locale;
|
2009-10-16 00:30:34 +02:00
|
|
|
}
|
2010-01-21 11:00:40 +01:00
|
|
|
|
|
|
|
$siteConfig->write();
|
|
|
|
|
|
|
|
return $siteConfig;
|
|
|
|
}
|
2009-10-16 00:30:34 +02:00
|
|
|
|
2010-01-12 23:48:03 +01:00
|
|
|
/**
|
|
|
|
* Can a user view pages on this site? This method is only
|
|
|
|
* called if a page is set to Inherit, but there is nothing
|
|
|
|
* to inherit from.
|
|
|
|
*
|
|
|
|
* @param mixed $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-10-16 00:30:34 +02:00
|
|
|
public function canView($member = null) {
|
2010-01-21 11:00:40 +01:00
|
|
|
if(!$member) $member = Member::currentUserID();
|
|
|
|
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
|
|
|
|
|
|
|
|
if (!$this->CanViewType || $this->CanViewType == 'Anyone') return true;
|
2009-10-16 00:30:34 +02:00
|
|
|
|
|
|
|
// check for any logged-in users
|
|
|
|
if($this->CanViewType == 'LoggedInUsers' && $member) return true;
|
2010-01-21 11:00:40 +01:00
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
// check for specific groups
|
|
|
|
if($this->CanViewType == 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-12 23:48:03 +01:00
|
|
|
/**
|
|
|
|
* Can a user edit pages on this site? This method is only
|
|
|
|
* called if a page is set to Inherit, but there is nothing
|
|
|
|
* to inherit from.
|
|
|
|
*
|
|
|
|
* @param mixed $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-10-16 00:30:34 +02:00
|
|
|
public function canEdit($member = null) {
|
2010-01-21 11:00:40 +01:00
|
|
|
if(!$member) $member = Member::currentUserID();
|
|
|
|
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
|
2010-01-12 23:48:03 +01:00
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
// check for any logged-in users
|
2010-01-21 11:00:40 +01:00
|
|
|
if(!$this->CanEditType || $this->CanEditType == 'LoggedInUsers' && $member) return true;
|
|
|
|
|
2009-10-16 00:30:34 +02:00
|
|
|
// check for specific groups
|
|
|
|
if($this->CanEditType == 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-16 00:35:45 +02:00
|
|
|
|
|
|
|
function providePermissions() {
|
|
|
|
return array(
|
2009-10-29 01:55:20 +01:00
|
|
|
'EDIT_SITECONFIG' => array(
|
2009-10-30 02:43:34 +01:00
|
|
|
'name' => _t('SiteConfig.EDIT_PERMISSION', 'Manage site configuration'),
|
|
|
|
'category' => _t('Permissions.PERMISSIONS_CATEGORY', 'Roles and access permissions'),
|
|
|
|
'help' => _t('SiteConfig.EDIT_PERMISSION_HELP', 'Ability to edit global access settings/top-level page permissions.'),
|
2009-10-29 01:55:20 +01:00
|
|
|
'sort' => 400
|
|
|
|
)
|
2009-10-16 00:35:45 +02:00
|
|
|
);
|
|
|
|
}
|
2010-01-12 23:48:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Can a user create pages in the root of this site?
|
|
|
|
*
|
|
|
|
* @param mixed $member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canCreateTopLevel($member = null) {
|
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
|
|
|
|
$member = Member::currentUserID();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for any logged-in users
|
|
|
|
if($this->CanCreateTopLevelType == 'LoggedInUsers' && $member) return true;
|
|
|
|
|
|
|
|
// check for specific groups
|
|
|
|
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
|
|
|
|
if($this->CanCreateTopLevelType == 'OnlyTheseUsers' && $member && $member->inGroups($this->CreateTopLevelGroups())) return true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|