2007-08-18 13:38:11 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2010-03-01 03:49:35 +01:00
|
|
|
* A dynamically created subsite. SiteTree objects can now belong to a subsite.
|
|
|
|
* You can simulate subsite access without setting up virtual hosts by appending ?SubsiteID=<ID> to the request.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @package subsites
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
2007-08-27 07:07:27 +02:00
|
|
|
class Subsite extends DataObject implements PermissionProvider {
|
2007-09-05 06:47:05 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* @var boolean $disable_subsite_filter If enabled, bypasses the query decoration
|
|
|
|
* to limit DataObject::get*() calls to a specific subsite. Useful for debugging.
|
|
|
|
*/
|
2007-09-05 06:47:05 +02:00
|
|
|
static $disable_subsite_filter = false;
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2010-02-04 20:45:14 +01:00
|
|
|
static $default_sort = "\"Title\"";
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
static $db = array(
|
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'RedirectURL' => 'Varchar(255)',
|
|
|
|
'DefaultSite' => 'Boolean',
|
|
|
|
'Theme' => 'Varchar',
|
2010-03-01 03:48:45 +01:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
// Used to hide unfinished/private subsites from public view.
|
2009-05-04 07:03:44 +02:00
|
|
|
// If unset, will default to
|
2007-08-18 13:38:11 +02:00
|
|
|
'IsPublic' => 'Boolean'
|
|
|
|
);
|
2010-03-01 03:53:00 +01:00
|
|
|
|
2008-06-19 02:46:51 +02:00
|
|
|
static $has_one = array(
|
|
|
|
);
|
2010-03-01 03:53:00 +01:00
|
|
|
|
|
|
|
static $has_many = array(
|
|
|
|
'Domains' => 'SubsiteDomain',
|
|
|
|
);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-06-24 04:31:26 +02:00
|
|
|
static $defaults = array(
|
|
|
|
'IsPublic' => 1,
|
|
|
|
);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2010-03-01 03:53:00 +01:00
|
|
|
static $searchable_fields = array(
|
|
|
|
'Title' => array(
|
|
|
|
'title' => 'Subsite Name'
|
|
|
|
),
|
|
|
|
'Domains.Domain' => array(
|
|
|
|
'title' => 'Domain name'
|
|
|
|
),
|
|
|
|
'IsPublic' => array(
|
|
|
|
'title' => 'Active subsite',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
static $summary_fields = array(
|
|
|
|
'Title' => 'Subsite Name',
|
|
|
|
'PrimaryDomain' => 'Primary Domain',
|
|
|
|
'IsPublic' => 'Active subsite',
|
|
|
|
);
|
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* @var array $allowed_themes Numeric array of all themes which are allowed to be selected for all subsites.
|
|
|
|
* Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder
|
|
|
|
* are listed.
|
|
|
|
*/
|
2009-05-04 07:03:44 +02:00
|
|
|
protected static $allowed_themes = array();
|
|
|
|
|
2008-04-22 08:53:35 +02:00
|
|
|
static function set_allowed_domains($domain){
|
2010-03-01 03:48:45 +01:00
|
|
|
user_error('Subsite::set_allowed_domains() is deprecated; it is no longer necessary '
|
|
|
|
. 'because users can now enter any domain name', E_USER_NOTICE);
|
2008-11-24 05:56:47 +01:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-06-24 04:31:26 +02:00
|
|
|
static function set_allowed_themes($themes) {
|
|
|
|
self::$allowed_themes = $themes;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-20 00:25:43 +01:00
|
|
|
/**
|
|
|
|
* Return the themes that can be used with this subsite, as an array of themecode => description
|
|
|
|
*/
|
|
|
|
function allowedThemes() {
|
|
|
|
if($themes = $this->stat('allowed_themes')) {
|
|
|
|
return ArrayLib::valuekey($themes);
|
|
|
|
} else {
|
|
|
|
$themes = array();
|
2009-02-17 02:08:57 +01:00
|
|
|
if(is_dir('../themes/')) {
|
|
|
|
foreach(scandir('../themes/') as $theme) {
|
|
|
|
if($theme[0] == '.') continue;
|
|
|
|
$theme = strtok($theme,'_');
|
|
|
|
$themes[$theme] = $theme;
|
|
|
|
}
|
|
|
|
ksort($themes);
|
2008-11-20 00:25:43 +01:00
|
|
|
}
|
|
|
|
return $themes;
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2010-03-01 03:53:34 +01:00
|
|
|
/**
|
|
|
|
* Whenever a Subsite is written, rewrite the hostmap
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function onAfterWrite() {
|
|
|
|
Subsite::writeHostMap();
|
|
|
|
}
|
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/**
|
|
|
|
* Return the domain of this site
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2010-03-01 03:49:35 +01:00
|
|
|
* @return string The full domain name of this subsite (without protocol prefix)
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
|
|
|
function domain() {
|
2010-03-01 03:48:45 +01:00
|
|
|
if($this->ID) {
|
|
|
|
$domains = DataObject::get("SubsiteDomain", "SubsiteID = $this->ID", "IsPrimary DESC",
|
|
|
|
"", 1);
|
|
|
|
if($domains) {
|
|
|
|
$domain = $domains->First()->Domain;
|
|
|
|
// If there are wildcards in the primary domain (not recommended), make some
|
|
|
|
// educated guesses about what to replace them with
|
|
|
|
$domain = preg_replace("/\\.\\*\$/",".$_SERVER[HTTP_HOST]", $domain);
|
|
|
|
$domain = preg_replace("/^\\*\\./","subsite.", $domain);
|
|
|
|
$domain = str_replace('.www.','.', $domain);
|
|
|
|
return $domain;
|
|
|
|
}
|
2010-03-01 03:53:15 +01:00
|
|
|
|
|
|
|
// SubsiteID = 0 is often used to refer to the main site, just return $_SERVER['HTTP_HOST']
|
|
|
|
} else {
|
|
|
|
return $_SERVER['HTTP_HOST'];
|
2010-03-01 03:48:45 +01:00
|
|
|
}
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2010-03-01 03:53:00 +01:00
|
|
|
|
|
|
|
function getPrimaryDomain() {
|
|
|
|
return $this->domain();
|
|
|
|
}
|
2008-12-04 22:36:06 +01:00
|
|
|
|
|
|
|
function absoluteBaseURL() {
|
|
|
|
return "http://" . $this->domain() . Director::baseURL();
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* Show the configuration fields for each subsite
|
|
|
|
*/
|
2007-08-18 13:38:11 +02:00
|
|
|
function getCMSFields() {
|
2010-03-01 03:48:45 +01:00
|
|
|
$domainTable = new TableField("Domains", "SubsiteDomain",
|
|
|
|
array("Domain" => "Domain (use * as a wildcard)", "IsPrimary" => "Primary domain?"),
|
|
|
|
array("Domain" => "TextField", "IsPrimary" => "CheckboxField"),
|
2010-03-01 03:53:00 +01:00
|
|
|
"SubsiteID", $this->ID);
|
2010-03-01 03:48:45 +01:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
$fields = new FieldSet(
|
|
|
|
new TabSet('Root',
|
|
|
|
new Tab('Configuration',
|
|
|
|
new HeaderField($this->getClassName() . ' configuration', 2),
|
|
|
|
new TextField('Title', 'Name of subsite:', $this->Title),
|
2010-03-01 03:48:45 +01:00
|
|
|
|
|
|
|
new HeaderField("Domains for this subsite"),
|
|
|
|
$domainTable,
|
2007-08-18 13:38:11 +02:00
|
|
|
// new TextField('RedirectURL', 'Redirect to URL', $this->RedirectURL),
|
2008-11-24 05:56:47 +01:00
|
|
|
new CheckboxField('DefaultSite', 'Default site', $this->DefaultSite),
|
|
|
|
new CheckboxField('IsPublic', 'Enable public access', $this->IsPublic),
|
2008-06-24 04:31:26 +02:00
|
|
|
|
2008-11-20 00:25:43 +01:00
|
|
|
new DropdownField('Theme','Theme', $this->allowedThemes(), $this->Theme)
|
2007-08-18 13:38:11 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
new HiddenField('ID', '', $this->ID),
|
|
|
|
new HiddenField('IsSubsite', '', 1)
|
|
|
|
);
|
|
|
|
|
2008-07-15 01:48:37 +02:00
|
|
|
$this->extend('updateCMSFields', $fields);
|
2007-08-18 13:38:11 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* @todo getClassName is redundant, already stored as a database field?
|
|
|
|
*/
|
2007-08-18 13:38:11 +02:00
|
|
|
function getClassName() {
|
|
|
|
return $this->class;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
function getCMSActions() {
|
|
|
|
return new FieldSet(
|
2007-08-29 00:29:44 +02:00
|
|
|
new FormAction('callPageMethod', "Create copy", null, 'adminDuplicate')
|
2007-08-18 13:38:11 +02:00
|
|
|
);
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-29 00:29:44 +02:00
|
|
|
function adminDuplicate() {
|
|
|
|
$newItem = $this->duplicate();
|
|
|
|
$JS_title = Convert::raw2js($this->Title);
|
|
|
|
return <<<JS
|
|
|
|
statusMessage('Created a copy of $JS_title', 'good');
|
|
|
|
$('Form_EditForm').loadURLFromServer('admin/subsites/show/$newItem->ID');
|
|
|
|
JS;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* Gets the subsite currently set in the session.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @uses ControllerSubsites->controllerAugmentInit()
|
2010-03-01 03:53:28 +01:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @return Subsite
|
|
|
|
*/
|
2010-03-01 03:53:28 +01:00
|
|
|
static function currentSubsite() {
|
|
|
|
// get_by_id handles caching so we don't have to
|
|
|
|
return DataObject::get_by_id('Subsite', self::currentSubsiteID());
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/**
|
|
|
|
* This function gets the current subsite ID from the session. It used in the backend so Ajax requests
|
|
|
|
* use the correct subsite. The frontend handles subsites differently. It calls getSubsiteIDForDomain
|
2008-11-24 05:56:47 +01:00
|
|
|
* directly from ModelAsController::getNestedController. Only gets Subsite instances which have their
|
|
|
|
* {@link IsPublic} flag set to TRUE.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2010-03-01 03:49:35 +01:00
|
|
|
* You can simulate subsite access without creating virtual hosts by appending ?SubsiteID=<ID> to the request.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-30 23:31:08 +01:00
|
|
|
* @todo Pass $request object from controller so we don't have to rely on $_REQUEST
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @param boolean $cache
|
|
|
|
* @return int ID of the current subsite instance
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
2010-03-01 03:53:07 +01:00
|
|
|
static function currentSubsiteID() {
|
2008-11-30 23:31:08 +01:00
|
|
|
if(isset($_REQUEST['SubsiteID'])) {
|
|
|
|
$id = (int)$_REQUEST['SubsiteID'];
|
2009-07-30 04:11:02 +02:00
|
|
|
} else {
|
2008-11-30 23:31:08 +01:00
|
|
|
$id = Session::get('SubsiteID');
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-30 23:31:08 +01:00
|
|
|
if(!isset($id) || $id === NULL) {
|
2010-03-01 03:53:07 +01:00
|
|
|
$id = self::getSubsiteIDForDomain();
|
2008-11-30 23:31:08 +01:00
|
|
|
Session::set('SubsiteID', $id);
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
return (int)$id;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Object::create() shoudln't be overloaded with different parameters.
|
|
|
|
*/
|
2007-08-18 13:38:11 +02:00
|
|
|
static function create($name) {
|
|
|
|
$newSubsite = Object::create('Subsite');
|
|
|
|
$newSubsite->Title = $name;
|
|
|
|
$newSubsite->Subdomain = str_replace(' ', '-', preg_replace('/[^0-9A-Za-z\s]/', '', strtolower(trim($name))));
|
|
|
|
$newSubsite->write();
|
|
|
|
$newSubsite->createInitialRecords();
|
|
|
|
return $newSubsite;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/**
|
2008-11-24 07:35:51 +01:00
|
|
|
* Switch to another subsite.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 07:35:51 +01:00
|
|
|
* @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
|
|
|
static function changeSubsite($subsite) {
|
2008-11-24 07:35:51 +01:00
|
|
|
if(is_object($subsite)) $subsiteID = $subsite->ID;
|
2008-11-30 22:31:49 +01:00
|
|
|
else $subsiteID = $subsite;
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 07:35:51 +01:00
|
|
|
Session::set('SubsiteID', $subsiteID);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
|
|
|
// And clear caches
|
|
|
|
Permission::flush_permission_cache() ;
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-31 02:29:25 +02:00
|
|
|
/**
|
|
|
|
* Make this subsite the current one
|
|
|
|
*/
|
|
|
|
public function activate() {
|
|
|
|
Subsite::changeSubsite($this);
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
|
|
|
* @todo Possible security issue, don't grant edit permissions to everybody.
|
|
|
|
*/
|
2007-08-18 13:38:11 +02:00
|
|
|
function canEdit() {
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 05:56:47 +01:00
|
|
|
/**
|
2010-03-01 03:48:45 +01:00
|
|
|
* Get a matching subsite for the given host, or for the current HTTP_HOST.
|
|
|
|
*
|
|
|
|
* @param $host The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST']
|
|
|
|
* is used.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @return int Subsite ID
|
|
|
|
*/
|
2010-03-01 03:48:45 +01:00
|
|
|
static function getSubsiteIDForDomain($host = null) {
|
|
|
|
if($host == null) $host = $_SERVER['HTTP_HOST'];
|
2009-11-02 02:59:15 +01:00
|
|
|
|
2010-03-01 03:48:45 +01:00
|
|
|
$host = str_replace('www.','',$host);
|
|
|
|
$SQL_host = Convert::raw2sql($host);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2010-03-01 03:48:45 +01:00
|
|
|
$matchingDomains = DataObject::get("SubsiteDomain", "'$SQL_host' LIKE replace({$q}SubsiteDomain{$q}.{$q}Domain{$q},'*','%')",
|
|
|
|
"{$q}IsPrimary{$q} DESC", "INNER JOIN {$q}Subsite{$q} ON {$q}Subsite{$q}.{$q}ID{$q} = {$q}SubsiteDomain{$q}.{$q}SubsiteID{$q} AND
|
|
|
|
{$q}Subsite{$q}.{$q}IsPublic{$q}");
|
|
|
|
|
|
|
|
if($matchingDomains) {
|
|
|
|
$subsiteIDs = array_unique($matchingDomains->column('SubsiteID'));
|
|
|
|
if(sizeof($subsiteIDs) > 1) user_error("Multiple subsites match '$host'", E_USER_WARNING);
|
|
|
|
return $subsiteIDs[0];
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
function getMembersByPermission($permissionCodes = array('ADMIN')){
|
|
|
|
if(!is_array($permissionCodes))
|
|
|
|
user_error('Permissions must be passed to Subsite::getMembersByPermission as an array', E_USER_ERROR);
|
|
|
|
$SQL_permissionCodes = Convert::raw2sql($permissionCodes);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
$SQL_permissionCodes = join("','", $SQL_permissionCodes);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-10-29 02:40:46 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
2009-07-14 03:41:51 +02:00
|
|
|
|
2008-11-24 07:35:51 +01:00
|
|
|
return DataObject::get(
|
2009-05-04 07:03:44 +02:00
|
|
|
'Member',
|
2009-10-29 02:40:46 +01:00
|
|
|
"{$q}Group{$q}.{$q}SubsiteID{$q} = $this->ID AND {$q}Permission{$q}.{$q}Code{$q} IN ('$SQL_permissionCodes')",
|
2009-05-04 07:03:44 +02:00
|
|
|
'',
|
2009-10-29 02:40:46 +01:00
|
|
|
"LEFT JOIN {$q}Group_Members{$q} ON {$q}Member{$q}.{$q}ID{$q} = {$q}Group_Members{$q}.{$q}MemberID{$q}
|
|
|
|
LEFT JOIN {$q}Group{$q} ON {$q}Group{$q}.{$q}ID{$q} = {$q}Group_Members{$q}.{$q}GroupID{$q}
|
|
|
|
LEFT JOIN {$q}Permission{$q} ON {$q}Permission{$q}.{$q}GroupID{$q} = {$q}Group{$q}.{$q}ID{$q}"
|
2008-11-24 07:35:51 +01:00
|
|
|
);
|
2009-10-29 02:40:46 +01:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-24 07:35:51 +01:00
|
|
|
/**
|
|
|
|
* Get all subsites.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 07:35:51 +01:00
|
|
|
* @return DataObjectSet Subsite instances
|
|
|
|
*/
|
2009-07-14 01:10:00 +02:00
|
|
|
static function getSubsitesForMember($member = null) {
|
2008-12-03 23:31:54 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentMember();
|
|
|
|
if(!$member) return false;
|
2007-08-18 13:38:11 +02:00
|
|
|
|
2008-11-24 07:35:51 +01:00
|
|
|
if(self::hasMainSitePermission($member)) {
|
2007-08-18 13:38:11 +02:00
|
|
|
return DataObject::get('Subsite');
|
2008-11-24 07:35:51 +01:00
|
|
|
}
|
2009-07-14 01:10:00 +02:00
|
|
|
|
2009-10-29 02:40:46 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
2009-07-14 03:41:51 +02:00
|
|
|
|
2009-07-14 01:10:00 +02:00
|
|
|
return DataObject::get(
|
|
|
|
'Subsite',
|
2009-10-29 02:40:46 +01:00
|
|
|
"{$q}MemberID{$q} = {$member->ID}",
|
2009-07-14 01:10:00 +02:00
|
|
|
'',
|
2009-10-29 02:40:46 +01:00
|
|
|
"LEFT JOIN {$q}Group{$q} ON {$q}Subsite{$q}.{$q}ID{$q} = {$q}SubsiteID{$q}
|
|
|
|
LEFT JOIN {$q}Group_Members{$q} ON {$q}Group{$q}.{$q}ID{$q} = {$q}Group_Members{$q}.{$q}GroupID{$q}"
|
2009-07-14 01:10:00 +02:00
|
|
|
);
|
2009-10-29 02:40:46 +01:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2009-07-14 01:10:00 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
static function hasMainSitePermission($member = null, $permissionCodes = array('ADMIN')) {
|
|
|
|
if(!is_array($permissionCodes))
|
|
|
|
user_error('Permissions must be passed to Subsite::hasMainSitePermission as an array', E_USER_ERROR);
|
|
|
|
|
2008-12-03 23:31:54 +01:00
|
|
|
if(!$member && $member !== FALSE) $member = Member::currentMember();
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-12-03 23:31:54 +01:00
|
|
|
if(!$member) return false;
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-11-30 23:10:55 +01:00
|
|
|
if(Permission::checkMember($member->ID, "ADMIN")) return true;
|
2007-08-18 13:38:11 +02:00
|
|
|
|
2008-12-01 03:43:57 +01:00
|
|
|
if(Permission::checkMember($member, "SUBSITE_ACCESS_ALL")) return true;
|
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
$SQLa_perm = Convert::raw2sql($permissionCodes);
|
2009-05-04 07:03:44 +02:00
|
|
|
$SQL_perms = join("','", $SQLa_perm);
|
2007-08-18 13:38:11 +02:00
|
|
|
$memberID = (int)$member->ID;
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
2009-07-14 03:41:51 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
$groupCount = DB::query("
|
|
|
|
SELECT COUNT({$q}Permission{$q}.{$q}ID{$q})
|
|
|
|
FROM {$q}Permission{$q}
|
|
|
|
INNER JOIN {$q}Group{$q} ON {$q}Group{$q}.{$q}ID{$q} = {$q}Permission{$q}.{$q}GroupID{$q} AND {$q}Group{$q}.{$q}SubsiteID{$q} = 0
|
|
|
|
INNER JOIN {$q}Group_Members{$q} USING({$q}GroupID{$q})
|
|
|
|
WHERE {$q}Permission{$q}.{$q}Code{$q} IN ('$SQL_perms') AND {$q}MemberID{$q} = {$memberID}
|
|
|
|
")->value();
|
|
|
|
|
2008-11-26 04:43:12 +01:00
|
|
|
return ($groupCount > 0);
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2010-03-01 03:51:19 +01:00
|
|
|
/**
|
|
|
|
* Overload this function to generate initial records in your newly created subsite.
|
|
|
|
*/
|
2007-08-18 13:38:11 +02:00
|
|
|
function createInitialRecords() {
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2007-08-29 00:29:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Duplicate this subsite
|
|
|
|
*/
|
|
|
|
function duplicate() {
|
|
|
|
$newTemplate = parent::duplicate();
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-29 00:29:44 +02:00
|
|
|
$oldSubsiteID = Session::get('SubsiteID');
|
|
|
|
self::changeSubsite($this->ID);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
|
|
|
|
2007-08-29 00:29:44 +02:00
|
|
|
/*
|
|
|
|
* Copy data from this template to the given subsite. Does this using an iterative depth-first search.
|
|
|
|
* This will make sure that the new parents on the new subsite are correct, and there are no funny
|
|
|
|
* issues with having to check whether or not the new parents have been added to the site tree
|
|
|
|
* when a page, etc, is duplicated
|
|
|
|
*/
|
|
|
|
$stack = array(array(0,0));
|
2009-05-04 07:03:44 +02:00
|
|
|
while(count($stack) > 0) {
|
2007-08-29 00:29:44 +02:00
|
|
|
list($sourceParentID, $destParentID) = array_pop($stack);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
$children = Versioned::get_by_stage('Page', 'Live', "{$q}ParentID{$q} = $sourceParentID", '');
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-29 00:29:44 +02:00
|
|
|
if($children) {
|
|
|
|
foreach($children as $child) {
|
|
|
|
$childClone = $child->duplicateToSubsite($newTemplate, false);
|
|
|
|
$childClone->ParentID = $destParentID;
|
|
|
|
$childClone->writeToStage('Stage');
|
|
|
|
$childClone->publish('Stage', 'Live');
|
|
|
|
array_push($stack, array($child->ID, $childClone->ID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::changeSubsite($oldSubsiteID);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-29 00:29:44 +02:00
|
|
|
return $newTemplate;
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
|
|
|
|
2007-08-31 02:29:25 +02:00
|
|
|
/**
|
|
|
|
* Return the subsites that the current user can access.
|
|
|
|
* Look for one of the given permission codes on the site.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2010-03-01 03:49:35 +01:00
|
|
|
* Sites and Templates will only be included if they have a Title
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2007-08-31 02:29:25 +02:00
|
|
|
* @param $permCode array|string Either a single permission code or an array of permission codes.
|
|
|
|
*/
|
|
|
|
function accessible_sites($permCode) {
|
|
|
|
$member = Member::currentUser();
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-31 02:29:25 +02:00
|
|
|
if(is_array($permCode)) $SQL_codes = "'" . implode("', '", Convert::raw2sql($permCode)) . "'";
|
|
|
|
else $SQL_codes = "'" . Convert::raw2sql($permCode) . "'";
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-31 02:29:25 +02:00
|
|
|
if(!$member) return new DataObjectSet();
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-02-24 23:09:15 +01:00
|
|
|
$templateClassList = "'" . implode("', '", ClassInfo::subclassesFor("Subsite_Template")) . "'";
|
2007-08-31 02:29:25 +02:00
|
|
|
|
2009-10-29 02:40:46 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
|
|
|
|
2009-07-14 03:41:51 +02:00
|
|
|
return DataObject::get(
|
2008-11-24 07:35:51 +01:00
|
|
|
'Subsite',
|
2009-10-29 02:40:46 +01:00
|
|
|
"{$q}Group_Members{$q}.{$q}MemberID{$q} = $member->ID
|
|
|
|
AND {$q}Permission{$q}.{$q}Code{$q} IN ($SQL_codes, 'ADMIN')
|
|
|
|
AND ({$q}Subdomain{$q} IS NOT NULL OR {$q}Subsite{$q}.{$q}ClassName{$q} IN ($templateClassList)) AND {$q}Subsite{$q}.{$q}Title{$q} != ''",
|
2008-11-24 07:35:51 +01:00
|
|
|
'',
|
2009-10-29 02:40:46 +01:00
|
|
|
"LEFT JOIN {$q}Group{$q} ON ({$q}SubsiteID{$q} = {$q}Subsite{$q}.{$q}ID{$q} OR {$q}SubsiteID{$q} = 0)
|
|
|
|
LEFT JOIN {$q}Group_Members{$q} ON {$q}Group_Members{$q}.{$q}GroupID{$q} = {$q}Group{$q}.{$q}ID{$q}
|
|
|
|
LEFT JOIN {$q}Permission{$q} ON {$q}Group{$q}.{$q}ID{$q} = {$q}Permission{$q}.{$q}GroupID{$q}"
|
|
|
|
);
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
2010-03-01 03:53:34 +01:00
|
|
|
|
|
|
|
/**
|
2010-03-01 03:53:45 +01:00
|
|
|
* Write a host->domain map to subsites/host-map.php
|
2010-03-01 03:53:34 +01:00
|
|
|
*
|
|
|
|
* This is used primarily when using subsites in conjunction with StaticPublisher
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
static function writeHostMap($file = null) {
|
2010-03-01 03:53:45 +01:00
|
|
|
if (!$file) $file = Director::baseFolder().'/subsites/host-map.php';
|
2010-03-01 03:53:34 +01:00
|
|
|
$hostmap = array();
|
|
|
|
|
|
|
|
$subsites = DataObject::get('Subsite');
|
|
|
|
|
|
|
|
if ($subsites) foreach($subsites as $subsite) {
|
|
|
|
$domains = $subsite->Domains();
|
|
|
|
if ($domains) foreach($domains as $domain) {
|
|
|
|
$hostmap[str_replace('www.', '', $domain->Domain)] = $subsite->domain();
|
|
|
|
}
|
2010-03-01 03:53:51 +01:00
|
|
|
if ($subsite->DefaultSite) $hostmap['default'] = $subsite->domain();
|
2010-03-01 03:53:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = '<?php $subsiteHostmap = '.var_export($hostmap, true).' ?>';
|
2010-03-01 03:53:51 +01:00
|
|
|
|
|
|
|
if (is_writable(dirname($file)) || is_writable($file)) {
|
|
|
|
file_put_contents($file, $data);
|
|
|
|
}
|
2010-03-01 03:53:34 +01:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CMS ADMINISTRATION HELPERS
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/**
|
|
|
|
* Return the FieldSet that will build the search form in the CMS
|
|
|
|
*/
|
|
|
|
function adminSearchFields() {
|
|
|
|
return new FieldSet(
|
|
|
|
new TextField('Name', 'Sub-site name')
|
2009-05-04 07:03:44 +02:00
|
|
|
);
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-27 07:07:27 +02:00
|
|
|
function providePermissions() {
|
|
|
|
return array(
|
|
|
|
'SUBSITE_EDIT' => 'Edit Sub-site Details',
|
2008-12-01 03:43:57 +01:00
|
|
|
'SUBSITE_ACCESS_ALL' => 'Access all subsites',
|
2009-05-04 07:03:44 +02:00
|
|
|
'SUBSITE_ASSETS_EDIT' => 'Edit Sub-site Assets Admin'
|
2007-08-27 07:07:27 +02:00
|
|
|
);
|
|
|
|
}
|
2007-09-05 06:47:05 +02:00
|
|
|
|
|
|
|
static function get_from_all_subsites($className, $filter = "", $sort = "", $join = "", $limit = "") {
|
|
|
|
self::$disable_subsite_filter = true;
|
|
|
|
$result = DataObject::get($className, $filter, $sort, $join, $limit);
|
|
|
|
self::$disable_subsite_filter = false;
|
|
|
|
return $result;
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2008-08-21 07:50:38 +02:00
|
|
|
/**
|
|
|
|
* Disable the sub-site filtering; queries will select from all subsites
|
2009-05-04 07:03:44 +02:00
|
|
|
*/
|
2008-08-21 07:50:38 +02:00
|
|
|
static function disable_subsite_filter($disabled = true) {
|
|
|
|
self::$disable_subsite_filter = $disabled;
|
|
|
|
}
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An instance of subsite that can be duplicated to provide a quick way to create new subsites.
|
2009-05-04 07:03:44 +02:00
|
|
|
*
|
2008-11-24 05:56:47 +01:00
|
|
|
* @package subsites
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
|
|
|
class Subsite_Template extends Subsite {
|
|
|
|
/**
|
2010-03-01 03:49:35 +01:00
|
|
|
* Create an instance of this template, with the given title & domain
|
2007-08-18 13:38:11 +02:00
|
|
|
*/
|
2010-03-01 03:53:00 +01:00
|
|
|
function createInstance($title, $domain = null) {
|
2007-08-18 13:38:11 +02:00
|
|
|
$intranet = Object::create('Subsite');
|
|
|
|
$intranet->Title = $title;
|
|
|
|
$intranet->TemplateID = $this->ID;
|
|
|
|
$intranet->write();
|
2010-03-01 03:48:45 +01:00
|
|
|
|
2010-03-01 03:53:00 +01:00
|
|
|
if($domain) {
|
|
|
|
$intranetDomain = Object::create('SubsiteDomain');
|
|
|
|
$intranetDomain->SubsiteID = $intranet->ID;
|
|
|
|
$intranetDomain->Domain = $domain;
|
|
|
|
$intranetDomain->write();
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
$oldSubsiteID = Session::get('SubsiteID');
|
|
|
|
self::changeSubsite($this->ID);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
if(defined('DB::USE_ANSI_SQL'))
|
|
|
|
$q="\"";
|
|
|
|
else $q='`';
|
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/*
|
2009-02-24 23:09:15 +01:00
|
|
|
* Copy site content from this template to the given subsite. Does this using an iterative depth-first search.
|
2007-08-18 13:38:11 +02:00
|
|
|
* This will make sure that the new parents on the new subsite are correct, and there are no funny
|
|
|
|
* issues with having to check whether or not the new parents have been added to the site tree
|
|
|
|
* when a page, etc, is duplicated
|
|
|
|
*/
|
|
|
|
$stack = array(array(0,0));
|
2009-05-04 07:03:44 +02:00
|
|
|
while(count($stack) > 0) {
|
2007-08-18 13:38:11 +02:00
|
|
|
list($sourceParentID, $destParentID) = array_pop($stack);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-11-02 02:59:15 +01:00
|
|
|
$children = Versioned::get_by_stage('SiteTree', 'Live', "{$q}ParentID{$q} = $sourceParentID", '');
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
if($children) {
|
|
|
|
foreach($children as $child) {
|
2007-08-29 00:29:44 +02:00
|
|
|
$childClone = $child->duplicateToSubsite($intranet);
|
2007-08-18 13:38:11 +02:00
|
|
|
$childClone->ParentID = $destParentID;
|
|
|
|
$childClone->writeToStage('Stage');
|
|
|
|
$childClone->publish('Stage', 'Live');
|
|
|
|
array_push($stack, array($child->ID, $childClone->ID));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2009-02-24 23:09:15 +01:00
|
|
|
/**
|
|
|
|
* Copy groups from the template to the given subsites. Each of the groups will be created and left
|
|
|
|
* empty.
|
|
|
|
*/
|
2009-11-02 02:59:15 +01:00
|
|
|
$groups = DataObject::get("Group", "{$q}SubsiteID{$q} = '$this->ID'");
|
2009-02-24 23:09:15 +01:00
|
|
|
if($groups) foreach($groups as $group) {
|
|
|
|
$group->duplicateToSubsite($intranet);
|
|
|
|
}
|
2007-08-18 13:38:11 +02:00
|
|
|
|
|
|
|
self::changeSubsite($oldSubsiteID);
|
2009-05-04 07:03:44 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
return $intranet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|