2007-08-18 13:38:11 +02:00
|
|
|
<?php
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Model;
|
|
|
|
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Admin\CMSMenu;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Convert;
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2018-01-18 05:16:13 +01:00
|
|
|
use SilverStripe\Core\Manifest\ModuleLoader;
|
2017-10-04 09:21:58 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Forms\CheckboxSetField;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
|
|
|
|
use SilverStripe\Forms\HiddenField;
|
2017-09-11 22:06:22 +02:00
|
|
|
use SilverStripe\Forms\ToggleCompositeField;
|
2018-05-04 04:04:50 +02:00
|
|
|
use SilverStripe\i18n\Data\Intl\IntlLocales;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\ORM\ArrayLib;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2018-05-04 04:04:50 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2017-06-04 04:34:38 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\Security\Group;
|
2017-05-24 15:26:28 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Permission;
|
2017-08-30 05:29:13 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2017-08-30 01:06:07 +02:00
|
|
|
use SilverStripe\Subsites\State\SubsiteState;
|
2017-05-24 12:32:05 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use UnexpectedValueException;
|
2017-05-24 13:36:04 +02:00
|
|
|
|
2007-08-18 13:38:11 +02:00
|
|
|
/**
|
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
|
|
|
*/
|
2017-05-24 15:26:28 +02:00
|
|
|
class Subsite extends DataObject
|
|
|
|
{
|
2007-09-05 06:47:05 +02:00
|
|
|
|
2017-05-24 14:31:56 +02:00
|
|
|
private static $table_name = 'Subsite';
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
/**
|
|
|
|
* @var boolean $disable_subsite_filter If enabled, bypasses the query decoration
|
2018-01-18 05:16:13 +01:00
|
|
|
* to limit DataObject::get*() calls to a specific subsite. Useful for debugging. Note that
|
|
|
|
* for now this is left as a public static property to avoid having to nest and mutate the
|
|
|
|
* configuration manifest.
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
|
|
|
public static $disable_subsite_filter = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows you to force a specific subsite ID, or comma separated list of IDs.
|
|
|
|
* Only works for reading. An object cannot be written to more than 1 subsite.
|
2018-01-18 05:16:13 +01:00
|
|
|
*
|
|
|
|
* @deprecated 2.0.0..3.0.0 Use SubsiteState::singleton()->withState() instead.
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
|
|
|
public static $force_subsite = null;
|
|
|
|
|
|
|
|
/**
|
2018-01-18 05:16:13 +01:00
|
|
|
* Whether to write a host-map.php file
|
2017-05-24 15:26:28 +02:00
|
|
|
*
|
2018-01-18 05:16:13 +01:00
|
|
|
* @config
|
2017-05-24 15:26:28 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2018-01-18 05:16:13 +01:00
|
|
|
private static $write_hostmap = true;
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Memory cache of accessible sites
|
|
|
|
*
|
|
|
|
* @array
|
|
|
|
*/
|
2018-02-01 01:22:48 +01:00
|
|
|
protected static $cache_accessible_sites = [];
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Memory cache of subsite id for domains
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-02-01 01:22:48 +01:00
|
|
|
protected static $cache_subsite_for_domain = [];
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-18 05:16:13 +01:00
|
|
|
* Numeric array of all themes which are allowed to be selected for all subsites.
|
2017-05-24 15:26:28 +02:00
|
|
|
* Corresponds to subfolder names within the /themes folder. By default, all themes contained in this folder
|
|
|
|
* are listed.
|
2018-01-18 05:16:13 +01:00
|
|
|
*
|
|
|
|
* @var array
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2018-01-18 05:16:13 +01:00
|
|
|
protected static $allowed_themes = [];
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-18 05:16:13 +01:00
|
|
|
* If set to TRUE, don't assume 'www.example.com' and 'example.com' are the same.
|
2017-05-24 15:26:28 +02:00
|
|
|
* Doesn't affect wildcard matching, so '*.example.com' will match 'www.example.com' (but not 'example.com')
|
|
|
|
* in both TRUE or FALSE setting.
|
2018-01-18 05:16:13 +01:00
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2018-01-18 05:16:13 +01:00
|
|
|
private static $strict_subdomain_matching = false;
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2018-01-18 05:16:13 +01:00
|
|
|
* Respects the IsPublic flag when retrieving subsites
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var boolean
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2018-01-18 05:16:13 +01:00
|
|
|
private static $check_is_public = true;
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/*** @return array
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
|
|
|
private static $summary_fields = [
|
|
|
|
'Title',
|
|
|
|
'PrimaryDomain',
|
|
|
|
'IsPublic'
|
|
|
|
];
|
|
|
|
|
2017-09-19 04:51:14 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $db = [
|
|
|
|
'Title' => 'Varchar(255)',
|
|
|
|
'RedirectURL' => 'Varchar(255)',
|
|
|
|
'DefaultSite' => 'Boolean',
|
|
|
|
'Theme' => 'Varchar',
|
|
|
|
'Language' => 'Varchar(6)',
|
|
|
|
|
|
|
|
// Used to hide unfinished/private subsites from public view.
|
|
|
|
// If unset, will default to true
|
|
|
|
'IsPublic' => 'Boolean',
|
|
|
|
|
|
|
|
// Comma-separated list of disallowed page types
|
|
|
|
'PageTypeBlacklist' => 'Text',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_many = [
|
|
|
|
'Domains' => SubsiteDomain::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $belongs_many_many = [
|
|
|
|
'Groups' => Group::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $defaults = [
|
|
|
|
'IsPublic' => 1
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $searchable_fields = [
|
|
|
|
'Title',
|
|
|
|
'Domains.Domain',
|
|
|
|
'IsPublic',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $default_sort = '"Title" ASC';
|
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
/**
|
2017-04-23 22:23:34 +02:00
|
|
|
* Set allowed themes
|
|
|
|
*
|
|
|
|
* @param array $themes - Numeric array of all themes which are allowed to be selected for all subsites.
|
|
|
|
*/
|
|
|
|
public static function set_allowed_themes($themes)
|
|
|
|
{
|
|
|
|
self::$allowed_themes = $themes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the subsite currently set in the session.
|
|
|
|
*
|
|
|
|
* @uses ControllerSubsites->controllerAugmentInit()
|
2017-06-04 04:34:38 +02:00
|
|
|
* @return DataObject The current Subsite
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
|
|
|
public static function currentSubsite()
|
2017-06-04 04:34:38 +02:00
|
|
|
{
|
2017-08-30 01:06:07 +02:00
|
|
|
return Subsite::get()->byID(SubsiteState::singleton()->getSubsiteId());
|
2017-04-23 22:23:34 +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
|
|
|
|
* directly from ModelAsController::getNestedController. Only gets Subsite instances which have their
|
|
|
|
* {@link IsPublic} flag set to TRUE.
|
|
|
|
*
|
|
|
|
* You can simulate subsite access without creating virtual hosts by appending ?SubsiteID=<ID> to the request.
|
|
|
|
*
|
|
|
|
* @return int ID of the current subsite instance
|
2017-08-30 01:06:07 +02:00
|
|
|
*
|
|
|
|
* @deprecated 2.0..3.0 Use SubsiteState::singleton()->getSubsiteId() instead
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
|
|
|
public static function currentSubsiteID()
|
|
|
|
{
|
2017-08-30 01:06:07 +02:00
|
|
|
Deprecation::notice('3.0', 'Use SubsiteState::singleton()->getSubsiteId() instead');
|
|
|
|
return SubsiteState::singleton()->getSubsiteId();
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Switch to another subsite through storing the subsite identifier in the current PHP session.
|
2017-08-30 05:29:13 +02:00
|
|
|
* Only takes effect when {@link SubsiteState::singleton()->getUseSessions()} is set to TRUE.
|
2017-04-23 22:23:34 +02:00
|
|
|
*
|
|
|
|
* @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself
|
|
|
|
*/
|
|
|
|
public static function changeSubsite($subsite)
|
|
|
|
{
|
|
|
|
// Session subsite change only meaningful if the session is active.
|
|
|
|
// Otherwise we risk setting it to wrong value, e.g. if we rely on currentSubsiteID.
|
2017-08-30 05:29:13 +02:00
|
|
|
if (!SubsiteState::singleton()->getUseSessions()) {
|
2017-04-23 22:23:34 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_object($subsite)) {
|
|
|
|
$subsiteID = $subsite->ID;
|
|
|
|
} else {
|
|
|
|
$subsiteID = $subsite;
|
|
|
|
}
|
|
|
|
|
2017-08-30 01:06:07 +02:00
|
|
|
SubsiteState::singleton()->setSubsiteId($subsiteID);
|
2017-04-23 22:23:34 +02:00
|
|
|
|
|
|
|
// Set locale
|
2017-05-30 17:35:02 +02:00
|
|
|
if (is_object($subsite) && $subsite->Language !== '') {
|
|
|
|
$locale = (new IntlLocales())->localeFromLang($subsite->Language);
|
2017-04-23 22:23:34 +02:00
|
|
|
if ($locale) {
|
|
|
|
i18n::set_locale($locale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 04:33:04 +02:00
|
|
|
Permission::reset();
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a matching subsite for the given host, or for the current HTTP_HOST.
|
|
|
|
* Supports "fuzzy" matching of domains by placing an asterisk at the start of end of the string,
|
|
|
|
* for example matching all subdomains on *.example.com with one subsite,
|
|
|
|
* and all subdomains on *.example.org on another.
|
|
|
|
*
|
2017-06-04 04:34:38 +02:00
|
|
|
* @param $host string The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST'] is used.
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param bool $checkPermissions
|
2017-04-23 22:23:34 +02:00
|
|
|
* @return int Subsite ID
|
|
|
|
*/
|
|
|
|
public static function getSubsiteIDForDomain($host = null, $checkPermissions = true)
|
|
|
|
{
|
|
|
|
if ($host == null && isset($_SERVER['HTTP_HOST'])) {
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$matchingDomains = null;
|
|
|
|
$cacheKey = null;
|
|
|
|
if ($host) {
|
2018-01-18 05:16:13 +01:00
|
|
|
if (!static::config()->get('strict_subdomain_matching')) {
|
2017-04-23 22:23:34 +02:00
|
|
|
$host = preg_replace('/^www\./', '', $host);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-08-30 02:04:43 +02:00
|
|
|
$currentUserId = Security::getCurrentUser() ? Security::getCurrentUser()->ID : 0;
|
2018-01-18 05:16:13 +01:00
|
|
|
$cacheKey = implode('_', [$host, $currentUserId, static::config()->get('check_is_public')]);
|
2018-02-01 01:22:48 +01:00
|
|
|
if (isset(self::$cache_subsite_for_domain[$cacheKey])) {
|
|
|
|
return self::$cache_subsite_for_domain[$cacheKey];
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$SQL_host = Convert::raw2sql($host);
|
2017-08-30 05:29:13 +02:00
|
|
|
|
2018-01-18 05:16:13 +01:00
|
|
|
$schema = DataObject::getSchema();
|
|
|
|
|
2017-09-07 07:01:49 +02:00
|
|
|
/** @skipUpgrade */
|
2018-01-18 05:16:13 +01:00
|
|
|
$domainTableName = $schema->tableName(SubsiteDomain::class);
|
|
|
|
if (!in_array($domainTableName, DB::table_list())) {
|
2017-08-30 05:29:13 +02:00
|
|
|
// Table hasn't been created yet. Might be a dev/build, skip.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-18 05:16:13 +01:00
|
|
|
$subsiteTableName = $schema->tableName(__CLASS__);
|
2017-09-07 07:01:49 +02:00
|
|
|
/** @skipUpgrade */
|
2017-04-23 22:23:34 +02:00
|
|
|
$matchingDomains = DataObject::get(
|
|
|
|
SubsiteDomain::class,
|
2018-01-18 05:16:13 +01:00
|
|
|
"'$SQL_host' LIKE replace(\"{$domainTableName}\".\"Domain\",'*','%')",
|
2017-06-01 14:49:55 +02:00
|
|
|
'"IsPrimary" DESC'
|
2017-08-29 07:07:24 +02:00
|
|
|
)->innerJoin(
|
2018-01-18 05:16:13 +01:00
|
|
|
$subsiteTableName,
|
|
|
|
'"' . $subsiteTableName . '"."ID" = "SubsiteDomain"."SubsiteID" AND "'
|
|
|
|
. $subsiteTableName . '"."IsPublic"=1'
|
2017-08-29 07:07:24 +02:00
|
|
|
);
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
2017-06-01 14:44:09 +02:00
|
|
|
if ($matchingDomains && $matchingDomains->count()) {
|
2017-04-23 22:23:34 +02:00
|
|
|
$subsiteIDs = array_unique($matchingDomains->column('SubsiteID'));
|
|
|
|
$subsiteDomains = array_unique($matchingDomains->column('Domain'));
|
|
|
|
if (sizeof($subsiteIDs) > 1) {
|
|
|
|
throw new UnexpectedValueException(sprintf(
|
|
|
|
"Multiple subsites match on '%s': %s",
|
|
|
|
$host,
|
|
|
|
implode(',', $subsiteDomains)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
$subsiteID = $subsiteIDs[0];
|
|
|
|
} else {
|
2017-06-01 14:49:55 +02:00
|
|
|
if ($default = DataObject::get_one(Subsite::class, '"DefaultSite" = 1')) {
|
2017-04-23 22:23:34 +02:00
|
|
|
// Check for a 'default' subsite
|
|
|
|
$subsiteID = $default->ID;
|
|
|
|
} else {
|
|
|
|
// Default subsite id = 0, the main site
|
|
|
|
$subsiteID = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($cacheKey) {
|
2018-02-01 01:22:48 +01:00
|
|
|
self::$cache_subsite_for_domain[$cacheKey] = $subsiteID;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $subsiteID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @param string $filter
|
|
|
|
* @param string $sort
|
|
|
|
* @param string $join
|
|
|
|
* @param string $limit
|
|
|
|
* @return DataList
|
|
|
|
*/
|
2017-06-01 14:49:55 +02:00
|
|
|
public static function get_from_all_subsites($className, $filter = '', $sort = '', $join = '', $limit = '')
|
2017-04-23 22:23:34 +02:00
|
|
|
{
|
|
|
|
$result = DataObject::get($className, $filter, $sort, $join, $limit);
|
|
|
|
$result = $result->setDataQueryParam('Subsite.filter', false);
|
|
|
|
return $result;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable the sub-site filtering; queries will select from all subsites
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param bool $disabled
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
|
|
|
public static function disable_subsite_filter($disabled = true)
|
|
|
|
{
|
|
|
|
self::$disable_subsite_filter = $disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush caches on database reset
|
|
|
|
*/
|
|
|
|
public static function on_db_reset()
|
|
|
|
{
|
2018-02-01 01:22:48 +01:00
|
|
|
self::$cache_accessible_sites = [];
|
|
|
|
self::$cache_subsite_for_domain = [];
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all subsites, regardless of permissions (augmented with main site).
|
|
|
|
*
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param bool $includeMainSite
|
|
|
|
* @param string $mainSiteTitle
|
|
|
|
* @return SS_List List of <a href='psi_element://Subsite'>Subsite</a> objects (DataList or ArrayList).
|
|
|
|
* objects (DataList or ArrayList).
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-06-01 14:49:55 +02:00
|
|
|
public static function all_sites($includeMainSite = true, $mainSiteTitle = 'Main site')
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
$subsites = Subsite::get();
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if ($includeMainSite) {
|
|
|
|
$subsites = $subsites->toArray();
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$mainSite = new Subsite();
|
|
|
|
$mainSite->Title = $mainSiteTitle;
|
|
|
|
array_unshift($subsites, $mainSite);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$subsites = ArrayList::create($subsites);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
return $subsites;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an ArrayList of the subsites accessible to the current user.
|
|
|
|
* It's enough for any section to be accessible for the site to be included.
|
|
|
|
*
|
|
|
|
* @return ArrayList of {@link Subsite} instances.
|
|
|
|
*/
|
2017-06-01 14:49:55 +02:00
|
|
|
public static function all_accessible_sites($includeMainSite = true, $mainSiteTitle = 'Main site', $member = null)
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
|
|
|
// Rationalise member arguments
|
|
|
|
if (!$member) {
|
2017-08-30 02:04:43 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
if (!$member) {
|
2017-08-30 02:04:43 +02:00
|
|
|
return ArrayList::create();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
if (!is_object($member)) {
|
2017-05-29 13:42:42 +02:00
|
|
|
$member = DataObject::get_by_id(Member::class, $member);
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-08-30 02:04:43 +02:00
|
|
|
$subsites = ArrayList::create();
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
// Collect subsites for all sections.
|
|
|
|
$menu = CMSMenu::get_viewable_menu_items();
|
|
|
|
foreach ($menu as $candidate) {
|
|
|
|
if ($candidate->controller) {
|
|
|
|
$accessibleSites = singleton($candidate->controller)->sectionSites(
|
|
|
|
$includeMainSite,
|
|
|
|
$mainSiteTitle,
|
|
|
|
$member
|
|
|
|
);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
// Replace existing keys so no one site appears twice.
|
|
|
|
$subsites->merge($accessibleSites);
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$subsites->removeDuplicates();
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
return $subsites;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the subsites that the current user can access by given permission.
|
|
|
|
* Sites will only be included if they have a Title.
|
|
|
|
*
|
|
|
|
* @param $permCode array|string Either a single permission code or an array of permission codes.
|
2017-06-04 04:34:38 +02:00
|
|
|
* @param $includeMainSite bool If true, the main site will be included if appropriate.
|
|
|
|
* @param $mainSiteTitle string The label to give to the main site
|
|
|
|
* @param $member int|Member The member attempting to access the sites
|
|
|
|
* @return DataList|ArrayList of {@link Subsite} instances
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-04-23 22:23:34 +02:00
|
|
|
public static function accessible_sites(
|
|
|
|
$permCode,
|
|
|
|
$includeMainSite = true,
|
2017-06-01 14:49:55 +02:00
|
|
|
$mainSiteTitle = 'Main site',
|
2017-04-23 22:23:34 +02:00
|
|
|
$member = null
|
2017-08-29 07:07:24 +02:00
|
|
|
) {
|
2017-08-30 01:06:07 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
// Rationalise member arguments
|
|
|
|
if (!$member) {
|
|
|
|
$member = Member::currentUser();
|
|
|
|
}
|
|
|
|
if (!$member) {
|
|
|
|
return new ArrayList();
|
|
|
|
}
|
|
|
|
if (!is_object($member)) {
|
2017-05-29 13:42:42 +02:00
|
|
|
$member = DataObject::get_by_id(Member::class, $member);
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
// Rationalise permCode argument
|
|
|
|
if (is_array($permCode)) {
|
|
|
|
$SQL_codes = "'" . implode("', '", Convert::raw2sql($permCode)) . "'";
|
|
|
|
} else {
|
|
|
|
$SQL_codes = "'" . Convert::raw2sql($permCode) . "'";
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
// Cache handling
|
|
|
|
$cacheKey = $SQL_codes . '-' . $member->ID . '-' . $includeMainSite . '-' . $mainSiteTitle;
|
2018-02-01 01:22:48 +01:00
|
|
|
if (isset(self::$cache_accessible_sites[$cacheKey])) {
|
|
|
|
return self::$cache_accessible_sites[$cacheKey];
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-09-07 07:01:49 +02:00
|
|
|
/** @skipUpgrade */
|
2017-05-24 15:26:28 +02:00
|
|
|
$subsites = DataList::create(Subsite::class)
|
|
|
|
->where("\"Subsite\".\"Title\" != ''")
|
2017-06-01 14:49:55 +02:00
|
|
|
->leftJoin('Group_Subsites', '"Group_Subsites"."SubsiteID" = "Subsite"."ID"')
|
2017-08-29 07:07:24 +02:00
|
|
|
->innerJoin(
|
|
|
|
'Group',
|
|
|
|
'"Group"."ID" = "Group_Subsites"."GroupID" OR "Group"."AccessAllSubsites" = 1'
|
|
|
|
)
|
|
|
|
->innerJoin(
|
|
|
|
'Group_Members',
|
|
|
|
"\"Group_Members\".\"GroupID\"=\"Group\".\"ID\" AND \"Group_Members\".\"MemberID\" = $member->ID"
|
|
|
|
)
|
|
|
|
->innerJoin(
|
|
|
|
'Permission',
|
2018-02-01 01:19:02 +01:00
|
|
|
"\"Group\".\"ID\"=\"Permission\".\"GroupID\"
|
|
|
|
AND \"Permission\".\"Code\"
|
|
|
|
IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')"
|
2017-08-29 07:07:24 +02:00
|
|
|
);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if (!$subsites) {
|
|
|
|
$subsites = new ArrayList();
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/** @var DataList $rolesSubsites */
|
2017-09-07 07:01:49 +02:00
|
|
|
/** @skipUpgrade */
|
2017-05-24 15:26:28 +02:00
|
|
|
$rolesSubsites = DataList::create(Subsite::class)
|
|
|
|
->where("\"Subsite\".\"Title\" != ''")
|
2017-06-01 14:49:55 +02:00
|
|
|
->leftJoin('Group_Subsites', '"Group_Subsites"."SubsiteID" = "Subsite"."ID"')
|
2017-08-29 07:07:24 +02:00
|
|
|
->innerJoin(
|
|
|
|
'Group',
|
|
|
|
'"Group"."ID" = "Group_Subsites"."GroupID" OR "Group"."AccessAllSubsites" = 1'
|
|
|
|
)
|
|
|
|
->innerJoin(
|
|
|
|
'Group_Members',
|
|
|
|
"\"Group_Members\".\"GroupID\"=\"Group\".\"ID\" AND \"Group_Members\".\"MemberID\" = $member->ID"
|
|
|
|
)
|
2017-06-01 14:49:55 +02:00
|
|
|
->innerJoin('Group_Roles', '"Group_Roles"."GroupID"="Group"."ID"')
|
|
|
|
->innerJoin('PermissionRole', '"Group_Roles"."PermissionRoleID"="PermissionRole"."ID"')
|
2017-08-29 07:07:24 +02:00
|
|
|
->innerJoin(
|
|
|
|
'PermissionRoleCode',
|
2018-02-01 01:19:02 +01:00
|
|
|
"\"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\"
|
|
|
|
AND \"PermissionRoleCode\".\"Code\"
|
|
|
|
IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')"
|
2017-08-29 07:07:24 +02:00
|
|
|
);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if (!$subsites && $rolesSubsites) {
|
|
|
|
return $rolesSubsites;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$subsites = new ArrayList($subsites->toArray());
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if ($rolesSubsites) {
|
|
|
|
foreach ($rolesSubsites as $subsite) {
|
|
|
|
if (!$subsites->find('ID', $subsite->ID)) {
|
|
|
|
$subsites->push($subsite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
if ($includeMainSite) {
|
|
|
|
if (!is_array($permCode)) {
|
|
|
|
$permCode = [$permCode];
|
|
|
|
}
|
|
|
|
if (self::hasMainSitePermission($member, $permCode)) {
|
|
|
|
$subsites = $subsites->toArray();
|
|
|
|
|
|
|
|
$mainSite = new Subsite();
|
|
|
|
$mainSite->Title = $mainSiteTitle;
|
|
|
|
array_unshift($subsites, $mainSite);
|
|
|
|
$subsites = ArrayList::create($subsites);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 01:22:48 +01:00
|
|
|
self::$cache_accessible_sites[$cacheKey] = $subsites;
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
return $subsites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a host->domain map to subsites/host-map.php
|
|
|
|
*
|
|
|
|
* This is used primarily when using subsites in conjunction with StaticPublisher
|
|
|
|
*
|
|
|
|
* @param string $file - filepath of the host map to be written
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function writeHostMap($file = null)
|
|
|
|
{
|
2018-01-18 05:16:13 +01:00
|
|
|
if (!static::config()->get('write_hostmap')) {
|
2017-05-24 15:26:28 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$file) {
|
2018-01-18 05:16:13 +01:00
|
|
|
$subsitesPath = ModuleLoader::getModule('silverstripe/subsites')->getRelativePath();
|
|
|
|
$file = Director::baseFolder() . $subsitesPath . '/host-map.php';
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
$hostmap = [];
|
|
|
|
|
|
|
|
$subsites = DataObject::get(Subsite::class);
|
|
|
|
|
|
|
|
if ($subsites) {
|
|
|
|
foreach ($subsites as $subsite) {
|
|
|
|
$domains = $subsite->Domains();
|
|
|
|
if ($domains) {
|
|
|
|
foreach ($domains as $domain) {
|
|
|
|
$domainStr = $domain->Domain;
|
2018-01-18 05:16:13 +01:00
|
|
|
if (!static::config()->get('strict_subdomain_matching')) {
|
2017-05-24 15:26:28 +02:00
|
|
|
$domainStr = preg_replace('/^www\./', '', $domainStr);
|
|
|
|
}
|
|
|
|
$hostmap[$domainStr] = $subsite->domain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($subsite->DefaultSite) {
|
|
|
|
$hostmap['default'] = $subsite->domain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = "<?php \n";
|
2015-11-23 04:53:45 +01:00
|
|
|
$data .= "// Generated by Subsite::writeHostMap() on " . date('d/M/y') . "\n";
|
2017-05-24 15:26:28 +02:00
|
|
|
$data .= '$subsiteHostmap = ' . var_export($hostmap, true) . ';';
|
|
|
|
|
|
|
|
if (is_writable(dirname($file)) || is_writable($file)) {
|
|
|
|
file_put_contents($file, $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a member can be granted certain permissions, regardless of the subsite context.
|
|
|
|
* Similar logic to {@link Permission::checkMember()}, but only returns TRUE
|
|
|
|
* if the member is part of a group with the "AccessAllSubsites" flag set.
|
|
|
|
* If more than one permission is passed to the method, at least one of them must
|
|
|
|
* be granted for if to return TRUE.
|
|
|
|
*
|
|
|
|
* @todo Allow permission inheritance through group hierarchy.
|
|
|
|
*
|
|
|
|
* @param Member Member to check against. Defaults to currently logged in member
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param array $permissionCodes
|
|
|
|
* @return bool
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
|
|
|
public static function hasMainSitePermission($member = null, $permissionCodes = ['ADMIN'])
|
|
|
|
{
|
|
|
|
if (!is_array($permissionCodes)) {
|
|
|
|
user_error('Permissions must be passed to Subsite::hasMainSitePermission as an array', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
if (!$member && $member !== false) {
|
2017-08-30 02:04:43 +02:00
|
|
|
$member = Security::getCurrentUser();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if (!$member) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-01 14:49:55 +02:00
|
|
|
if (!in_array('ADMIN', $permissionCodes)) {
|
|
|
|
$permissionCodes[] = 'ADMIN';
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$SQLa_perm = Convert::raw2sql($permissionCodes);
|
|
|
|
$SQL_perms = join("','", $SQLa_perm);
|
|
|
|
$memberID = (int)$member->ID;
|
|
|
|
|
|
|
|
// Count this user's groups which can access the main site
|
|
|
|
$groupCount = DB::query("
|
|
|
|
SELECT COUNT(\"Permission\".\"ID\")
|
|
|
|
FROM \"Permission\"
|
2018-02-01 01:19:02 +01:00
|
|
|
INNER JOIN \"Group\"
|
|
|
|
ON \"Group\".\"ID\" = \"Permission\".\"GroupID\" AND \"Group\".\"AccessAllSubsites\" = 1
|
|
|
|
INNER JOIN \"Group_Members\"
|
|
|
|
ON \"Group_Members\".\"GroupID\" = \"Permission\".\"GroupID\"
|
|
|
|
WHERE \"Permission\".\"Code\"
|
|
|
|
IN ('$SQL_perms') AND \"Group_Members\".\"MemberID\" = {$memberID}
|
2017-04-23 22:23:34 +02:00
|
|
|
")->value();
|
|
|
|
|
|
|
|
// Count this user's groups which have a role that can access the main site
|
|
|
|
$roleCount = DB::query("
|
|
|
|
SELECT COUNT(\"PermissionRoleCode\".\"ID\")
|
|
|
|
FROM \"Group\"
|
|
|
|
INNER JOIN \"Group_Members\" ON \"Group_Members\".\"GroupID\" = \"Group\".\"ID\"
|
|
|
|
INNER JOIN \"Group_Roles\" ON \"Group_Roles\".\"GroupID\"=\"Group\".\"ID\"
|
|
|
|
INNER JOIN \"PermissionRole\" ON \"Group_Roles\".\"PermissionRoleID\"=\"PermissionRole\".\"ID\"
|
|
|
|
INNER JOIN \"PermissionRoleCode\" ON \"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\"
|
|
|
|
WHERE \"PermissionRoleCode\".\"Code\" IN ('$SQL_perms')
|
|
|
|
AND \"Group\".\"AccessAllSubsites\" = 1
|
2017-08-02 06:39:32 +02:00
|
|
|
AND \"Group_Members\".\"MemberID\" = {$memberID}
|
2017-04-23 22:23:34 +02:00
|
|
|
")->value();
|
2013-11-11 00:09:27 +01:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
// There has to be at least one that allows access.
|
|
|
|
return ($groupCount + $roleCount > 0);
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* @todo Possible security issue, don't grant edit permissions to everybody.
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param bool $member
|
|
|
|
* @return bool
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
|
|
|
public function canEdit($member = false)
|
|
|
|
{
|
2017-11-22 19:22:57 +01:00
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the configuration fields for each subsite
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-09-19 04:51:14 +02:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
if ($this->exists()) {
|
|
|
|
// Add a GridField for domains to a new tab if the subsite has already been created
|
|
|
|
$fields->addFieldsToTab('Root.Domains', [
|
|
|
|
GridField::create(
|
|
|
|
'Domains',
|
2017-10-02 05:34:21 +02:00
|
|
|
'',
|
2017-09-19 04:51:14 +02:00
|
|
|
$this->Domains(),
|
|
|
|
GridFieldConfig_RecordEditor::create(10)
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-09-19 04:51:14 +02:00
|
|
|
// Remove the default scaffolded blacklist field, we replace it with a checkbox set field
|
|
|
|
// in a wrapper further down. The RedirectURL field is currently not in use.
|
|
|
|
$fields->removeByName(['PageTypeBlacklist', 'RedirectURL']);
|
|
|
|
|
|
|
|
$fields->addFieldToTab('Root.Main', DropdownField::create(
|
|
|
|
'Language',
|
|
|
|
$this->fieldLabel('Language'),
|
|
|
|
Injector::inst()->get(IntlLocales::class)->getLocales()
|
|
|
|
), 'DefaultSite');
|
|
|
|
|
|
|
|
$fields->addFieldsToTab('Root.Main', [
|
|
|
|
ToggleCompositeField::create(
|
|
|
|
'PageTypeBlacklistToggle',
|
|
|
|
_t(__CLASS__ . '.PageTypeBlacklistField', 'Disallow page types?'),
|
|
|
|
[
|
|
|
|
CheckboxSetField::create('PageTypeBlacklist', '', $this->getPageTypeMap())
|
|
|
|
]
|
|
|
|
)->setHeadingLevel(4),
|
|
|
|
HiddenField::create('ID', '', $this->ID),
|
|
|
|
HiddenField::create('IsSubsite', '', 1)
|
|
|
|
]);
|
|
|
|
|
|
|
|
// If there are any themes available, add the dropdown
|
|
|
|
$themes = $this->allowedThemes();
|
|
|
|
if (!empty($themes)) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DropdownField::create('Theme', $this->fieldLabel('Theme'), $this->allowedThemes(), $this->Theme)
|
|
|
|
->setEmptyString(_t(__CLASS__ . '.ThemeFieldEmptyString', '-')),
|
|
|
|
'PageTypeBlacklistToggle'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Targetted by the XHR PJAX JavaScript to reload the subsite list in the CMS
|
|
|
|
$fields->fieldByName('Root.Main')->addExtraClass('subsite-model');
|
|
|
|
|
|
|
|
// We don't need the Groups many many tab
|
|
|
|
$fields->removeByName('Groups');
|
2017-10-02 05:34:21 +02:00
|
|
|
|
|
|
|
// Rename the main tab to configuration
|
|
|
|
$fields->fieldByName('Root.Main')->setTitle(_t(__CLASS__ . '.ConfigurationTab', 'Configuration'));
|
2017-09-19 04:51:14 +02:00
|
|
|
});
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-09-19 04:51:14 +02:00
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of the different page types available to the CMS
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getPageTypeMap()
|
|
|
|
{
|
2017-04-23 22:23:34 +02:00
|
|
|
$pageTypeMap = [];
|
2017-09-19 04:51:14 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
$pageTypes = SiteTree::page_type_classes();
|
|
|
|
foreach ($pageTypes as $pageType) {
|
|
|
|
$pageTypeMap[$pageType] = singleton($pageType)->i18n_singular_name();
|
|
|
|
}
|
2017-04-10 04:27:25 +02:00
|
|
|
|
2017-09-19 04:51:14 +02:00
|
|
|
asort($pageTypeMap);
|
2017-04-23 22:23:34 +02:00
|
|
|
|
2017-09-19 04:51:14 +02:00
|
|
|
return $pageTypeMap;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param boolean $includerelations
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fieldLabels($includerelations = true)
|
|
|
|
{
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['Title'] = _t('Subsites.TitleFieldLabel', 'Subsite Name');
|
|
|
|
$labels['RedirectURL'] = _t('Subsites.RedirectURLFieldLabel', 'Redirect URL');
|
|
|
|
$labels['DefaultSite'] = _t('Subsites.DefaultSiteFieldLabel', 'Default site');
|
|
|
|
$labels['Theme'] = _t('Subsites.ThemeFieldLabel', 'Theme');
|
|
|
|
$labels['Language'] = _t('Subsites.LanguageFieldLabel', 'Language');
|
|
|
|
$labels['IsPublic'] = _t('Subsites.IsPublicFieldLabel', 'Enable public access');
|
|
|
|
$labels['PageTypeBlacklist'] = _t('Subsites.PageTypeBlacklistFieldLabel', 'Page Type Blacklist');
|
|
|
|
$labels['Domains.Domain'] = _t('Subsites.DomainFieldLabel', 'Domain');
|
|
|
|
$labels['PrimaryDomain'] = _t('Subsites.PrimaryDomainFieldLabel', 'Primary Domain');
|
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the themes that can be used with this subsite, as an array of themecode => description
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function allowedThemes()
|
|
|
|
{
|
2018-01-18 05:16:13 +01:00
|
|
|
if ($themes = self::$allowed_themes) {
|
2017-04-23 22:23:34 +02:00
|
|
|
return ArrayLib::valuekey($themes);
|
2017-06-01 15:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$themes = [];
|
|
|
|
if (is_dir(THEMES_PATH)) {
|
|
|
|
foreach (scandir(THEMES_PATH) as $theme) {
|
|
|
|
if ($theme[0] == '.') {
|
|
|
|
continue;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2017-06-01 15:07:13 +02:00
|
|
|
$theme = strtok($theme, '_');
|
|
|
|
$themes[$theme] = $theme;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2017-06-01 15:07:13 +02:00
|
|
|
ksort($themes);
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2017-06-01 15:07:13 +02:00
|
|
|
return $themes;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string Current locale of the subsite
|
|
|
|
*/
|
|
|
|
public function getLanguage()
|
|
|
|
{
|
|
|
|
if ($this->getField('Language')) {
|
|
|
|
return $this->getField('Language');
|
|
|
|
}
|
2017-06-01 15:07:13 +02:00
|
|
|
|
|
|
|
return i18n::get_locale();
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-06-04 04:34:38 +02:00
|
|
|
* @return \SilverStripe\ORM\ValidationResult
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
if (!$this->Title) {
|
2017-09-07 06:38:20 +02:00
|
|
|
$result->addError(_t(__CLASS__ . '.ValidateTitle', 'Please add a "Title"'));
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2018-04-20 05:19:50 +02:00
|
|
|
* Whenever a Subsite is written, rewrite the hostmap and create some default pages
|
2017-05-24 15:26:28 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function onAfterWrite()
|
|
|
|
{
|
|
|
|
Subsite::writeHostMap();
|
2018-04-20 05:19:50 +02:00
|
|
|
if ($this->isChanged('ID')) {
|
2018-05-04 03:56:26 +02:00
|
|
|
$this->createDefaultPages();
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
parent::onAfterWrite();
|
|
|
|
}
|
|
|
|
|
2018-05-04 03:57:25 +02:00
|
|
|
/**
|
2018-05-04 03:56:26 +02:00
|
|
|
* Automatically create default pages for new subsites
|
|
|
|
*/
|
2018-04-20 05:19:50 +02:00
|
|
|
protected function createDefaultPages()
|
2018-05-04 03:56:26 +02:00
|
|
|
{
|
2018-05-04 04:04:50 +02:00
|
|
|
SubsiteState::singleton()->withState(function (SubsiteState $newState) {
|
|
|
|
$newState->setSubsiteId($this->ID);
|
2018-04-20 05:19:50 +02:00
|
|
|
|
2018-05-04 04:04:50 +02:00
|
|
|
// Silence DB schema output
|
|
|
|
DB::quiet();
|
|
|
|
$siteTree = new SiteTree();
|
|
|
|
$siteTree->requireDefaultRecords();
|
|
|
|
});
|
2018-05-04 03:56:26 +02:00
|
|
|
}
|
2018-04-20 05:19:50 +02:00
|
|
|
|
2017-05-24 15:26:28 +02:00
|
|
|
/**
|
|
|
|
* Return the primary domain of this site. Tries to "normalize" the domain name,
|
|
|
|
* by replacing potential wildcards.
|
|
|
|
*
|
|
|
|
* @return string The full domain name of this subsite (without protocol prefix)
|
|
|
|
*/
|
|
|
|
public function domain()
|
|
|
|
{
|
2017-05-29 13:42:42 +02:00
|
|
|
// Get best SubsiteDomain object
|
|
|
|
$domainObject = $this->getPrimarySubsiteDomain();
|
|
|
|
if ($domainObject) {
|
|
|
|
return $domainObject->SubstitutedDomain;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2017-05-29 13:42:42 +02:00
|
|
|
|
|
|
|
// If there are no objects, default to the current hostname
|
|
|
|
return $_SERVER['HTTP_HOST'];
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Finds the primary {@see SubsiteDomain} object for this subsite
|
2017-05-24 15:26:28 +02:00
|
|
|
*
|
2017-05-29 13:42:42 +02:00
|
|
|
* @return SubsiteDomain
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function getPrimarySubsiteDomain()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-29 13:42:42 +02:00
|
|
|
return $this
|
|
|
|
->Domains()
|
|
|
|
->sort('"IsPrimary" DESC')
|
|
|
|
->first();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-05-29 13:42:42 +02:00
|
|
|
* @return string - The full domain name of this subsite (without protocol prefix)
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function getPrimaryDomain()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-29 13:42:42 +02:00
|
|
|
return $this->domain();
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Get the absolute URL for this subsite
|
|
|
|
* @return string
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function absoluteBaseURL()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-29 13:42:42 +02:00
|
|
|
// Get best SubsiteDomain object
|
|
|
|
$domainObject = $this->getPrimarySubsiteDomain();
|
|
|
|
if ($domainObject) {
|
|
|
|
return $domainObject->absoluteBaseURL();
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
// Fall back to the current base url
|
|
|
|
return Director::absoluteBaseURL();
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* Javascript admin action to duplicate this subsite
|
|
|
|
*
|
|
|
|
* @return string - javascript
|
|
|
|
*/
|
|
|
|
public function adminDuplicate()
|
|
|
|
{
|
|
|
|
$newItem = $this->duplicate();
|
|
|
|
$message = _t(
|
2017-09-07 06:38:20 +02:00
|
|
|
__CLASS__ . '.CopyMessage',
|
2017-04-23 22:23:34 +02:00
|
|
|
'Created a copy of {title}',
|
|
|
|
['title' => Convert::raw2js($this->Title)]
|
|
|
|
);
|
|
|
|
|
|
|
|
return <<<JS
|
|
|
|
statusMessage($message, 'good');
|
|
|
|
$('Form_EditForm').loadURLFromServer('admin/subsites/show/$newItem->ID');
|
2013-11-11 00:09:27 +01:00
|
|
|
JS;
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* Make this subsite the current one
|
|
|
|
*/
|
|
|
|
public function activate()
|
|
|
|
{
|
|
|
|
Subsite::changeSubsite($this);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param array $permissionCodes
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
public function getMembersByPermission($permissionCodes = ['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);
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$SQL_permissionCodes = join("','", $SQL_permissionCodes);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
return DataObject::get(
|
2017-05-29 13:42:42 +02:00
|
|
|
Member::class,
|
2017-05-24 15:26:28 +02:00
|
|
|
"\"Group\".\"SubsiteID\" = $this->ID AND \"Permission\".\"Code\" IN ('$SQL_permissionCodes')",
|
|
|
|
'',
|
2017-06-01 14:49:55 +02:00
|
|
|
'LEFT JOIN "Group_Members" ON "Member"."ID" = "Group_Members"."MemberID"
|
|
|
|
LEFT JOIN "Group" ON "Group"."ID" = "Group_Members"."GroupID"
|
|
|
|
LEFT JOIN "Permission" ON "Permission"."GroupID" = "Group"."ID"'
|
2017-04-23 22:23:34 +02:00
|
|
|
);
|
|
|
|
}
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* Duplicate this subsite
|
2017-06-01 15:10:07 +02:00
|
|
|
* @param bool $doWrite
|
2017-06-04 04:33:04 +02:00
|
|
|
* @param string $manyMany
|
2017-06-01 15:10:07 +02:00
|
|
|
* @return DataObject
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
2017-06-04 04:33:04 +02:00
|
|
|
public function duplicate($doWrite = true, $manyMany = 'many_many')
|
2017-04-23 22:23:34 +02:00
|
|
|
{
|
|
|
|
$duplicate = parent::duplicate($doWrite);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-08-30 01:06:07 +02:00
|
|
|
$oldSubsiteID = SubsiteState::singleton()->getSubsiteId();
|
2017-04-23 22:23:34 +02:00
|
|
|
self::changeSubsite($this->ID);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy data from this object 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 = [[0, 0]];
|
|
|
|
while (count($stack) > 0) {
|
|
|
|
list($sourceParentID, $destParentID) = array_pop($stack);
|
|
|
|
$children = Versioned::get_by_stage('Page', 'Live', "\"ParentID\" = $sourceParentID", '');
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
if ($children) {
|
|
|
|
foreach ($children as $child) {
|
|
|
|
self::changeSubsite($duplicate->ID); //Change to destination subsite
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
$childClone = $child->duplicateToSubsite($duplicate, false);
|
|
|
|
$childClone->ParentID = $destParentID;
|
|
|
|
$childClone->writeToStage('Stage');
|
2017-06-01 14:57:28 +02:00
|
|
|
$childClone->copyVersionToStage('Stage', 'Live');
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
self::changeSubsite($this->ID); //Change Back to this subsite
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
array_push($stack, [$child->ID, $childClone->ID]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
self::changeSubsite($oldSubsiteID);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
return $duplicate;
|
|
|
|
}
|
2007-08-18 13:38:11 +02:00
|
|
|
}
|