2011-03-18 04:01:06 +01:00
< ? php
2012-09-07 17:06:29 +02:00
2011-03-18 04:01:06 +01:00
/**
* Sitewide configuration .
2014-02-10 00:20:55 +01:00
*
* @ property string Title Title of the website .
* @ property string Tagline Tagline of the website .
* @ property string Theme Current theme .
* @ property string CanViewType Type of restriction used for view permissions .
* @ property string CanEditType Type of restriction used for edit permissions .
* @ property string CanCreateTopLevelType Type of restriction used for creation of root - level pages .
*
* @ method ManyManyList ViewerGroups () List of groups that can view SiteConfig .
* @ method ManyManyList EditorGroups () List of groups that can edit SiteConfig .
* @ method ManyManyList CreateTopLevelGroups () List of groups that can create root - level pages .
*
2011-03-18 04:01:06 +01:00
* @ author Tom Rix
* @ package cms
*/
class SiteConfig extends DataObject implements PermissionProvider {
2013-03-18 11:47:15 +01:00
private static $db = array (
2011-03-18 04:01:06 +01:00
" Title " => " Varchar(255) " ,
" Tagline " => " Varchar(255) " ,
" Theme " => " Varchar(255) " ,
" CanViewType " => " Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone') " ,
" CanEditType " => " Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers') " ,
" CanCreateTopLevelType " => " Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers') " ,
);
2013-03-18 11:47:15 +01:00
private static $many_many = array (
2011-03-18 04:01:06 +01:00
" ViewerGroups " => " Group " ,
" EditorGroups " => " Group " ,
" CreateTopLevelGroups " => " Group "
);
2013-03-18 11:47:15 +01:00
/**
* @ config
* @ var array
*/
private static $disabled_themes = array ();
2011-03-18 04:01:06 +01:00
2013-03-18 11:47:15 +01:00
/**
* @ deprecated 3.2 Use the " SiteConfig.disabled_themes " config setting instead
*/
2012-09-19 12:07:46 +02:00
static public function disable_theme ( $theme ) {
2013-03-18 11:47:15 +01:00
Deprecation :: notice ( '3.2' , 'Use the "SiteConfig.disabled_themes" config setting instead' );
Config :: inst () -> update ( 'SiteConfig' , 'disabled_themes' , array ( $theme ));
2011-03-18 04:01:06 +01:00
}
2012-09-07 17:06:29 +02:00
2012-09-19 12:07:46 +02:00
public function populateDefaults ()
2012-09-07 17:06:29 +02:00
{
$this -> Title = _t ( 'SiteConfig.SITENAMEDEFAULT' , " Your Site Name " );
$this -> Tagline = _t ( 'SiteConfig.TAGLINEDEFAULT' , " your tagline here " );
// Allow these defaults to be overridden
parent :: populateDefaults ();
}
2011-03-18 04:01:06 +01:00
/**
* Get the fields that are sent to the CMS . In
2011-04-15 11:37:15 +02:00
* your extensions : updateCMSFields ( $fields )
2011-03-18 04:01:06 +01:00
*
2011-10-29 06:39:40 +02:00
* @ return FieldList
2011-03-18 04:01:06 +01:00
*/
2012-09-19 12:07:46 +02:00
public function getCMSFields () {
2011-03-18 04:01:06 +01:00
2013-10-11 00:27:14 +02:00
$groupsMap = array ();
foreach ( Group :: get () as $group ) {
// Listboxfield values are escaped, use ASCII char instead of »
$groupsMap [ $group -> ID ] = $group -> getBreadcrumbs ( ' > ' );
}
2012-03-04 22:10:25 +01:00
asort ( $groupsMap );
2011-10-26 07:35:51 +02:00
$fields = new FieldList (
2011-03-18 04:01:06 +01:00
new TabSet ( " Root " ,
$tabMain = new Tab ( 'Main' ,
$titleField = new TextField ( " Title " , _t ( 'SiteConfig.SITETITLE' , " Site title " )),
$taglineField = new TextField ( " Tagline " , _t ( 'SiteConfig.SITETAGLINE' , " Site Tagline/Slogan " )),
2012-05-24 04:38:00 +02:00
$themeDropdownField = new DropdownField ( " Theme " , _t ( 'SiteConfig.THEME' , 'Theme' ), $this -> getAvailableThemes ())
2011-03-18 04:01:06 +01:00
),
$tabAccess = new Tab ( 'Access' ,
2011-04-19 12:37:48 +02:00
$viewersOptionsField = new OptionsetField ( " CanViewType " , _t ( 'SiteConfig.VIEWHEADER' , " Who can view pages on this site? " )),
2012-04-04 16:59:22 +02:00
$viewerGroupsField = ListboxField :: create ( " ViewerGroups " , _t ( 'SiteTree.VIEWERGROUPS' , " Viewer Groups " ))
2012-12-15 19:57:14 +01:00
-> setMultiple ( true )
-> setSource ( $groupsMap )
-> setAttribute (
'data-placeholder' ,
_t ( 'SiteTree.GroupPlaceholder' , 'Click to select group' )
),
2011-04-19 12:37:48 +02:00
$editorsOptionsField = new OptionsetField ( " CanEditType " , _t ( 'SiteConfig.EDITHEADER' , " Who can edit pages on this site? " )),
2012-04-04 16:59:22 +02:00
$editorGroupsField = ListboxField :: create ( " EditorGroups " , _t ( 'SiteTree.EDITORGROUPS' , " Editor Groups " ))
2012-12-15 19:57:14 +01:00
-> setMultiple ( true )
-> setSource ( $groupsMap )
-> setAttribute (
'data-placeholder' ,
_t ( 'SiteTree.GroupPlaceholder' , 'Click to select group' )
),
2011-04-19 12:37:48 +02:00
$topLevelCreatorsOptionsField = new OptionsetField ( " CanCreateTopLevelType " , _t ( 'SiteConfig.TOPLEVELCREATE' , " Who can create pages in the root of the site? " )),
2012-04-04 16:59:22 +02:00
$topLevelCreatorsGroupsField = ListboxField :: create ( " CreateTopLevelGroups " , _t ( 'SiteTree.TOPLEVELCREATORGROUPS' , " Top level creators " ))
2012-12-15 19:57:14 +01:00
-> setMultiple ( true )
-> setSource ( $groupsMap )
-> setAttribute (
'data-placeholder' ,
_t ( 'SiteTree.GroupPlaceholder' , 'Click to select group' )
)
2011-03-18 04:01:06 +01:00
)
2012-08-06 09:36:25 +02:00
),
new HiddenField ( 'ID' )
2011-03-18 04:01:06 +01:00
);
2011-04-19 12:37:48 +02:00
2012-05-24 04:38:00 +02:00
$themeDropdownField -> setEmptyString ( _t ( 'SiteConfig.DEFAULTTHEME' , '(Use default theme)' ));
2011-03-18 04:01:06 +01: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 );
$topLevelCreatorsOptionsField -> setSource ( $editorsOptionsSource );
if ( ! Permission :: check ( 'EDIT_SITECONFIG' )) {
$fields -> makeFieldReadonly ( $viewersOptionsField );
$fields -> makeFieldReadonly ( $viewerGroupsField );
$fields -> makeFieldReadonly ( $editorsOptionsField );
$fields -> makeFieldReadonly ( $editorGroupsField );
$fields -> makeFieldReadonly ( $topLevelCreatorsOptionsField );
$fields -> makeFieldReadonly ( $topLevelCreatorsGroupsField );
$fields -> makeFieldReadonly ( $taglineField );
$fields -> makeFieldReadonly ( $titleField );
}
if ( file_exists ( BASE_PATH . '/install.php' )) {
$fields -> addFieldToTab ( " Root.Main " , new LiteralField ( " InstallWarningHeader " ,
" <p class= \" message warning \" > " . _t ( " SiteTree.REMOVE_INSTALL_WARNING " ,
" Warning: You should remove install.php from this SilverStripe install for security reasons. " )
. " </p> " ), " Title " );
}
$tabMain -> setTitle ( _t ( 'SiteConfig.TABMAIN' , " Main " ));
$tabAccess -> setTitle ( _t ( 'SiteConfig.TABACCESS' , " Access " ));
$this -> extend ( 'updateCMSFields' , $fields );
return $fields ;
}
/**
* 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 ) {
2011-03-25 05:04:41 +01:00
$themes = SSViewer :: get_themes ( $baseDir );
2013-03-18 11:47:15 +01:00
$disabled = ( array ) $this -> config () -> disabled_themes ;
foreach ( $disabled as $theme ) {
2011-03-18 04:01:06 +01:00
if ( isset ( $themes [ $theme ])) unset ( $themes [ $theme ]);
}
return $themes ;
}
/**
* Get the actions that are sent to the CMS . In
2011-04-15 11:37:15 +02:00
* your extensions : updateEditFormActions ( $actions )
2011-03-18 04:01:06 +01:00
*
* @ return Fieldset
*/
2012-09-19 12:07:46 +02:00
public function getCMSActions () {
2011-03-18 04:01:06 +01:00
if ( Permission :: check ( 'ADMIN' ) || Permission :: check ( 'EDIT_SITECONFIG' )) {
2011-10-26 07:35:51 +02:00
$actions = new FieldList (
2012-02-16 22:59:47 +01:00
FormAction :: create ( 'save_siteconfig' , _t ( 'CMSMain.SAVE' , 'Save' ))
-> addExtraClass ( 'ss-ui-action-constructive' ) -> setAttribute ( 'data-icon' , 'accept' )
2011-03-18 04:01:06 +01:00
);
} else {
2011-10-26 07:35:51 +02:00
$actions = new FieldList ();
2011-03-18 04:01:06 +01:00
}
$this -> extend ( 'updateCMSActions' , $actions );
return $actions ;
}
2012-08-05 20:52:22 +02:00
/**
* @ return String
*/
2012-09-19 12:07:46 +02:00
public function CMSEditLink () {
2012-08-05 20:52:22 +02:00
return singleton ( 'CMSSettingsController' ) -> Link ();
}
2011-03-18 04:01:06 +01:00
/**
* Get the current sites SiteConfig , and creates a new one
* through { @ link make_site_config ()} if none is found .
*
* @ return SiteConfig
*/
2012-09-19 12:07:46 +02:00
static public function current_site_config () {
2012-09-07 17:06:29 +02:00
if ( $siteConfig = DataObject :: get_one ( 'SiteConfig' )) return $siteConfig ;
2011-03-18 04:01:06 +01:00
2012-09-07 17:06:29 +02:00
return self :: make_site_config ();
2011-03-18 04:01:06 +01:00
}
2012-09-07 17:06:29 +02:00
2011-03-18 04:01:06 +01:00
/**
* Setup a default SiteConfig record if none exists
*/
2012-09-19 12:07:46 +02:00
public function requireDefaultRecords () {
2011-03-18 04:01:06 +01:00
parent :: requireDefaultRecords ();
$siteConfig = DataObject :: get_one ( 'SiteConfig' );
if ( ! $siteConfig ) {
self :: make_site_config ();
DB :: alteration_message ( " Added default site config " , " created " );
}
}
/**
* Create SiteConfig with defaults from language file .
*
* @ return SiteConfig
*/
2012-09-19 12:07:46 +02:00
static public function make_site_config () {
2012-09-07 17:06:29 +02:00
$config = SiteConfig :: create ();
$config -> write ();
return $config ;
}
2011-03-18 04:01:06 +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
*/
public function canView ( $member = null ) {
if ( ! $member ) $member = Member :: currentUserID ();
if ( $member && is_numeric ( $member )) $member = DataObject :: get_by_id ( 'Member' , $member );
2009-10-15 05:06:37 +02:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2011-03-18 04:01:06 +01:00
if ( ! $this -> CanViewType || $this -> CanViewType == 'Anyone' ) return true ;
// check for any logged-in users
if ( $this -> CanViewType == 'LoggedInUsers' && $member ) return true ;
// check for specific groups
if ( $this -> CanViewType == 'OnlyTheseUsers' && $member && $member -> inGroups ( $this -> ViewerGroups ())) return true ;
return false ;
}
/**
* 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
*/
public function canEdit ( $member = null ) {
if ( ! $member ) $member = Member :: currentUserID ();
if ( $member && is_numeric ( $member )) $member = DataObject :: get_by_id ( 'Member' , $member );
2009-10-15 05:06:37 +02:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2011-03-18 04:01:06 +01:00
// check for any logged-in users
if ( ! $this -> CanEditType || $this -> CanEditType == 'LoggedInUsers' && $member ) return true ;
// check for specific groups
if ( $this -> CanEditType == 'OnlyTheseUsers' && $member && $member -> inGroups ( $this -> EditorGroups ())) return true ;
return false ;
}
2012-09-19 12:07:46 +02:00
public function providePermissions () {
2011-03-18 04:01:06 +01:00
return array (
'EDIT_SITECONFIG' => array (
'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.' ),
'sort' => 400
)
);
}
/**
* 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 ();
}
if ( Permission :: check ( 'ADMIN' )) return true ;
2009-10-15 05:06:37 +02:00
if ( $member && Permission :: checkMember ( $member , " ADMIN " )) return true ;
2011-03-18 04:01:06 +01:00
// 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 ;
}
}