- namespacing all classes

- moving all phpunit tests into tests/php
- moving all extensions from _config.php into config.yml and removing obsolete _config.php
- moving GridFieldSubsiteDetailForm_ItemRequest into own file

(cherry picked from commit ee02828)
This commit is contained in:
Tim Kung 2017-04-23 22:23:34 +02:00 committed by Werner M. Krauß
parent d8e72f3ac9
commit 5d3af16aaf
32 changed files with 721 additions and 609 deletions

View File

@ -1,60 +1,11 @@
--- ---
Name: subsiteconfig Name: subsiteconfig
After:
- 'framework/*'
--- ---
SilverStripe\AssetAdmin\Controller\AssetAdmin:
treats_subsite_0_as_global: true
SilverStripe\Control\Director: SilverStripe\Control\Director:
rules: rules:
SubsiteXHRController: SilverStripe\Subsites\Controller\SubsiteXHRController SubsiteXHRController: SilverStripe\Subsites\Controller\SubsiteXHRController
SilverStripe\AssetAdmin\Controller\AssetAdmin:
treats_subsite_0_as_global: true
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Model\SiteTree:
extensions:
- SilverStripe\Subsites\Extensions\SiteTreeSubsites
SilverStripe\CMS\Controllers\ContentController:
extensions:
- SilverStripe\Subsites\Extensions\ControllerSubsites
SilverStripe\CMS\Controllers\CMSPageAddController:
extensions:
- SilverStripe\Subsites\Extensions\CMSPageAddControllerExtension
SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\Subsites\Extensions\LeftAndMainSubsites
- SilverStripe\Subsites\Extensions\ControllerSubsites
SilverStripe\Security\Group:
extensions:
- SilverStripe\Subsites\Extensions\GroupSubsites
SilverStripe\Assets\File:
extensions:
- SilverStripe\Subsites\Extensions\FileSubsites
SilverStripe\CMS\Model\ErrorPage:
extensions:
- SilverStripe\Subsites\Extensions\ErrorPageSubsite
SilverStripe\SiteConfig\SiteConfig:
extensions:
- SilverStripe\Subsites\Extensions\SiteConfigSubsites
SilverStripe\Admin\SecurityAdmin:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Controllers\CMSMain:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Controllers\CMSPagesController:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\Subsites\Admin\SubsiteAdmin:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension

65
_config/extensions.yml Normal file
View File

@ -0,0 +1,65 @@
---
Name: subsiteextensions
After:
- 'framework/*'
---
SilverStripe\CMS\Model\SiteTree:
extensions:
- SilverStripe\Subsites\Extensions\SiteTreeSubsites
SilverStripe\CMS\Controllers\ContentController:
extensions:
- SilverStripe\Subsites\Extensions\ControllerSubsites
SilverStripe\CMS\Controllers\CMSPageAddController:
extensions:
- SilverStripe\Subsites\Extensions\CMSPageAddControllerExtension
SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\Subsites\Extensions\LeftAndMainSubsites
- SilverStripe\Subsites\Extensions\ControllerSubsites
SilverStripe\Security\Group:
extensions:
- SilverStripe\Subsites\Extensions\GroupSubsites
SilverStripe\Assets\File:
extensions:
- SilverStripe\Subsites\Extensions\FileSubsites
SilverStripe\CMS\Model\ErrorPage:
extensions:
- SilverStripe\Subsites\Extensions\ErrorPageSubsite
SilverStripe\SiteConfig\SiteConfig:
extensions:
- SilverStripe\Subsites\Extensions\SiteConfigSubsites
SilverStripe\Reports\Report:
extensions:
- SilverStripe\Subsites\SubsiteReportWrapper
SilverStripe\AssetAdmin\Controller\AssetAdmin:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\Admin\SecurityAdmin:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Controllers\CMSMain:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Controllers\CMSPagesController:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\Subsites\SubsiteAdmin:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension
SilverStripe\CMS\Controllers\CMSPageSettingsController:
extensions:
- SilverStripe\Subsites\Extensions\SubsiteMenuExtension

View File

@ -1,8 +1,6 @@
<?php <?php
namespace SilverStripe\Subsites\Extensions; namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\HiddenField;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;

View File

@ -1,8 +1,6 @@
<?php <?php
namespace SilverStripe\Subsites\Extensions; namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\SSViewer; use SilverStripe\View\SSViewer;
@ -12,6 +10,7 @@ use SilverStripe\View\SSViewer;
*/ */
class ControllerSubsites extends Extension class ControllerSubsites extends Extension
{ {
public function controllerAugmentInit() public function controllerAugmentInit()
{ {
if ($subsite = Subsite::currentSubsite()) { if ($subsite = Subsite::currentSubsite()) {
@ -27,4 +26,5 @@ class ControllerSubsites extends Extension
return $subsite; return $subsite;
} }
} }
} }

View File

@ -10,6 +10,7 @@ use SilverStripe\Subsites\Model\Subsite;
class ErrorPageSubsite extends DataExtension class ErrorPageSubsite extends DataExtension
{ {
/** /**
* Alter file path to generated a static (static) error page file to handle error page template on different sub-sites * Alter file path to generated a static (static) error page file to handle error page template on different sub-sites
* *
@ -49,4 +50,5 @@ class ErrorPageSubsite extends DataExtension
return $filepath; return $filepath;
} }
} }

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Subsites\Extensions; namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Assets\Folder; use SilverStripe\Assets\Folder;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
@ -13,8 +12,6 @@ use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\Queries\SQLSelect; use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
/** /**
* Extension for the File object to add subsites support * Extension for the File object to add subsites support
* *

View File

@ -1,20 +1,24 @@
<?php <?php
use SilverStripe\View\Requirements; namespace SilverStripe\Subsites\Extensions;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Security\Member;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\Config\Config;
use SilverStripe\View\ArrayData;
use SilverStripe\Security\Permission;
use SilverStripe\Control\Session;
use SilverStripe\Admin\CMSMenu; use SilverStripe\Admin\CMSMenu;
use SilverStripe\Security\Security;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Session;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\HiddenField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
/** /**
* Decorator designed to add subsites support to LeftAndMain * Decorator designed to add subsites support to LeftAndMain
* *
@ -22,6 +26,7 @@ use SilverStripe\Core\Extension;
*/ */
class LeftAndMainSubsites extends Extension class LeftAndMainSubsites extends Extension
{ {
private static $allowed_actions = array('CopyToSubsite'); private static $allowed_actions = array('CopyToSubsite');
/** /**
@ -351,4 +356,5 @@ class LeftAndMainSubsites extends Extension
$newPage->ID $newPage->ID
)); ));
} }
} }

View File

@ -17,9 +17,9 @@ use SilverStripe\Subsites\Model\Subsite;
*/ */
class SiteConfigSubsites extends DataExtension class SiteConfigSubsites extends DataExtension
{ {
private static $has_one = [ private static $has_one = [
'Subsite' => Subsite::class, // The subsite that this page belongs to 'Subsite' => Subsite::class, // The subsite that this page belongs to
]; ];
/** /**
* Update any requests to limit the results to the current site * Update any requests to limit the results to the current site
@ -66,4 +66,5 @@ class SiteConfigSubsites extends DataExtension
{ {
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID())); $fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
} }
} }

View File

@ -5,24 +5,24 @@ namespace SilverStripe\Subsites\Extensions;
use Page; use Page;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DataQuery;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\InlineFormAction;
use SilverStripe\Forms\ToggleCompositeField;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
use SilverStripe\View\SSViewer;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
use SilverStripe\Control\HTTP; use SilverStripe\Control\HTTP;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\InlineFormAction;
use SilverStripe\Forms\ToggleCompositeField;
use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\Security\Member;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\SSViewer;
/** /**
@ -30,6 +30,7 @@ use SilverStripe\Subsites\Model\Subsite;
*/ */
class SiteTreeSubsites extends DataExtension class SiteTreeSubsites extends DataExtension
{ {
private static $has_one = [ private static $has_one = [
'Subsite' => Subsite::class, // The subsite that this page belongs to 'Subsite' => Subsite::class, // The subsite that this page belongs to
]; ];
@ -50,11 +51,11 @@ class SiteTreeSubsites extends DataExtension
return false; return false;
} }
/** /**
* Update any requests to limit the results to the current site * Update any requests to limit the results to the current site
* @param SQLSelect $query * @param SQLSelect $query
* @param DataQuery $dataQuery * @param DataQuery $dataQuery
*/ */
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null) public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
{ {
if (Subsite::$disable_subsite_filter) { if (Subsite::$disable_subsite_filter) {
@ -114,16 +115,16 @@ class SiteTreeSubsites extends DataExtension
$fields->addFieldsToTab( $fields->addFieldsToTab(
'Root.Main', 'Root.Main',
ToggleCompositeField::create('SubsiteOperations', ToggleCompositeField::create('SubsiteOperations',
_t('SiteTreeSubsites.SubsiteOperations', 'Subsite Operations'), _t('SiteTreeSubsites.SubsiteOperations', 'Subsite Operations'),
array( array(
new DropdownField("CopyToSubsiteID", _t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"), $subsitesMap), new DropdownField("CopyToSubsiteID", _t('SiteTreeSubsites.CopyToSubsite', "Copy page to subsite"), $subsitesMap),
new CheckboxField("CopyToSubsiteWithChildren", _t('SiteTreeSubsites.CopyToSubsiteWithChildren', 'Include children pages?')), new CheckboxField("CopyToSubsiteWithChildren", _t('SiteTreeSubsites.CopyToSubsiteWithChildren', 'Include children pages?')),
$copyAction = new InlineFormAction( $copyAction = new InlineFormAction(
"copytosubsite", "copytosubsite",
_t('SiteTreeSubsites.CopyAction', "Copy") _t('SiteTreeSubsites.CopyAction', "Copy")
) )
) )
)->setHeadingLevel(4) )->setHeadingLevel(4)
); );
@ -276,10 +277,10 @@ class SiteTreeSubsites extends DataExtension
Subsite::changeSubsite($oldSubsite); Subsite::changeSubsite($oldSubsite);
if($includeChildren) { if($includeChildren) {
foreach($this->owner->AllChildren() as $child) { foreach($this->owner->AllChildren() as $child) {
$child->duplicateToSubsite($subsiteID, $includeChildren, $page->ID); $child->duplicateToSubsite($subsiteID, $includeChildren, $page->ID);
} }
} }
return $page; return $page;
} }
@ -420,4 +421,5 @@ class SiteTreeSubsites extends DataExtension
} }
} }
} }
} }

View File

@ -2,65 +2,11 @@
namespace SilverStripe\Subsites\Forms; namespace SilverStripe\Subsites\Forms;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\Subsites\Model\Subsite;
class GridFieldSubsiteDetailForm extends GridFieldDetailForm class GridFieldSubsiteDetailForm extends GridFieldDetailForm
{ {
protected $itemRequestClass = GridFieldSubsiteDetailForm_ItemRequest::class;
} protected $itemRequestClass = 'GridFieldSubsiteDetailForm_ItemRequest';
class GridFieldSubsiteDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = [
'ItemEditForm',
];
/**
* Builds an item edit form. The arguments to getCMSFields() are the popupController and
* popupFormName, however this is an experimental API and may change.
*
* @todo In the future, we will probably need to come up with a tigher object representing a partially
* complete controller with gaps for extra functionality. This, for example, would be a better way
* of letting Security/login put its log-in form inside a UI specified elsewhere.
*
* @return Form
* @see GridFieldDetailForm_ItemRequest::ItemEditForm()
*/
public function ItemEditForm()
{
$form = parent::ItemEditForm();
if ($this->record->ID == 0) {
$templates = Subsite::get()->sort('Title');
$templateArray = [];
if ($templates) {
$templateArray = $templates->map('ID', 'Title');
}
$templateDropdown = new DropdownField('TemplateID', _t('Subsite.COPYSTRUCTURE', 'Copy structure from:'),
$templateArray);
$templateDropdown->setEmptyString('(' . _t('Subsite.NOTEMPLATE', 'No template') . ')');
$form->Fields()->addFieldToTab('Root.Configuration', $templateDropdown);
}
return $form;
}
public function doSave($data, $form)
{
$new_record = $this->record->ID == 0;
if ($new_record && isset($data['TemplateID']) && !empty($data['TemplateID'])) {
$template = Subsite::get()->byID(intval($data['TemplateID']));
if ($template) {
$this->record = $template->duplicate();
}
}
return parent::doSave($data, $form);
}
} }

View File

@ -0,0 +1,59 @@
<?php
namespace SilverStripe\Subsites\Forms;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\Subsites\Model\Subsite;
class GridFieldSubsiteDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = array(
'ItemEditForm',
);
/**
* Builds an item edit form. The arguments to getCMSFields() are the popupController and
* popupFormName, however this is an experimental API and may change.
*
* @todo In the future, we will probably need to come up with a tigher object representing a partially
* complete controller with gaps for extra functionality. This, for example, would be a better way
* of letting Security/login put its log-in form inside a UI specified elsewhere.
*
* @return Form
* @see GridFieldDetailForm_ItemRequest::ItemEditForm()
*/
public function ItemEditForm()
{
$form=parent::ItemEditForm();
if ($this->record->ID == 0) {
$templates = Subsite::get()->sort('Title');
$templateArray = array();
if ($templates) {
$templateArray = $templates->map('ID', 'Title');
}
$templateDropdown = new DropdownField('TemplateID', _t('Subsite.COPYSTRUCTURE', 'Copy structure from:'), $templateArray);
$templateDropdown->setEmptyString('(' . _t('Subsite.NOTEMPLATE', 'No template') . ')');
$form->Fields()->addFieldToTab('Root.Configuration', $templateDropdown);
}
return $form;
}
public function doSave($data, $form)
{
$new_record = $this->record->ID == 0;
if ($new_record && isset($data['TemplateID']) && !empty($data['TemplateID'])) {
$template = Subsite::get()->byID(intval($data['TemplateID']));
if ($template) {
$this->record = $template->duplicate();
}
}
return parent::doSave($data, $form);
}
}

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Forms;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
/** /**
@ -7,6 +9,7 @@ use SilverStripe\Forms\TextField;
*/ */
class WildcardDomainField extends TextField class WildcardDomainField extends TextField
{ {
/** /**
* Validate this field as a valid hostname * Validate this field as a valid hostname
* *
@ -24,6 +27,7 @@ class WildcardDomainField extends TextField
_t("DomainNameField.INVALID_DOMAIN", "Invalid domain name"), _t("DomainNameField.INVALID_DOMAIN", "Invalid domain name"),
"validation" "validation"
); );
return false; return false;
} }
@ -42,4 +46,5 @@ class WildcardDomainField extends TextField
{ {
return 'text wildcarddomain'; return 'text wildcarddomain';
} }
} }

View File

@ -2,7 +2,6 @@
namespace SilverStripe\Subsites\Model; namespace SilverStripe\Subsites\Model;
use SilverStripe\Admin\CMSMenu; use SilverStripe\Admin\CMSMenu;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;
@ -28,9 +27,9 @@ use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\Group;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use UnexpectedValueException; use UnexpectedValueException;
@ -106,7 +105,7 @@ class Subsite extends DataObject
*/ */
public static $check_is_public = true; public static $check_is_public = true;
/*** @return array /*** @return array
*/ */
private static $summary_fields = [ private static $summary_fields = [
'Title', 'Title',
@ -115,39 +114,41 @@ class Subsite extends DataObject
]; ];
/** /**
* Set allowed themes * Set allowed themes
* *
* @param array $themes - Numeric array of all themes which are allowed to be selected for all subsites. * @param array $themes - Numeric array of all themes which are allowed to be selected for all subsites.
*/ */
public static function set_allowed_themes($themes) public static function set_allowed_themes($themes)
{self::$allowed_themes = $themes; {
} self::$allowed_themes = $themes;
}
/** /**
* Gets the subsite currently set in the session. * Gets the subsite currently set in the session.
* *
* @uses ControllerSubsites->controllerAugmentInit() * @uses ControllerSubsites->controllerAugmentInit()
* @return Subsite * @return Subsite
*/ */
public static function currentSubsite() public static function currentSubsite()
{// get_by_id handles caching so we don't have to {// get_by_id handles caching so we don't have to
return DataObject::get_by_id(Subsite::class, self::currentSubsiteID()); return DataObject::get_by_id(Subsite::class, self::currentSubsiteID());
} }
/** /**
* This function gets the current subsite ID from the session. It used in the backend so Ajax requests * 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 * use the correct subsite. The frontend handles subsites differently. It calls getSubsiteIDForDomain
* directly from ModelAsController::getNestedController. Only gets Subsite instances which have their * directly from ModelAsController::getNestedController. Only gets Subsite instances which have their
* {@link IsPublic} flag set to TRUE. * {@link IsPublic} flag set to TRUE.
* *
* You can simulate subsite access without creating virtual hosts by appending ?SubsiteID=<ID> to the request. * You can simulate subsite access without creating virtual hosts by appending ?SubsiteID=<ID> to the request.
* *
* @todo Pass $request object from controller so we don't have to rely on $_GET * @todo Pass $request object from controller so we don't have to rely on $_GET
* *
* @return int ID of the current subsite instance * @return int ID of the current subsite instance
*/ */
public static function currentSubsiteID() public static function currentSubsiteID()
{$id = null; {
$id = null;
if (isset($_GET['SubsiteID'])) { if (isset($_GET['SubsiteID'])) {
$id = (int)$_GET['SubsiteID']; $id = (int)$_GET['SubsiteID'];
@ -155,114 +156,126 @@ class Subsite extends DataObject
$id = Session::get('SubsiteID'); $id = Session::get('SubsiteID');
} }
if($id === null) { if ($id === null) {
$id = self::getSubsiteIDForDomain(); $id = self::getSubsiteIDForDomain();
} }
return (int)$id; return (int)$id;
} }
/** /**
* Switch to another subsite through storing the subsite identifier in the current PHP session. * Switch to another subsite through storing the subsite identifier in the current PHP session.
* Only takes effect when {@link Subsite::$use_session_subsiteid} is set to TRUE. * Only takes effect when {@link Subsite::$use_session_subsiteid} is set to TRUE.
* *
* @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself * @param int|Subsite $subsite Either the ID of the subsite, or the subsite object itself
*/ */
public static function changeSubsite($subsite) { 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. // Session subsite change only meaningful if the session is active.
if (!Subsite::$use_session_subsiteid) return; // Otherwise we risk setting it to wrong value, e.g. if we rely on currentSubsiteID.
if (!Subsite::$use_session_subsiteid) {
return;
}
if(is_object($subsite)) $subsiteID = $subsite->ID; if (is_object($subsite)) {
else $subsiteID = $subsite; $subsiteID = $subsite->ID;
} else {
$subsiteID = $subsite;
}
Session::set('SubsiteID', (int)$subsiteID); Session::set('SubsiteID', (int)$subsiteID);
// Set locale // Set locale
if (is_object($subsite) && $subsite->Language != '') { if (is_object($subsite) && $subsite->Language != '') {
$locale = i18n::get_locale_from_lang($subsite->Language); $locale = i18n::get_locale_from_lang($subsite->Language);
if($locale) { if ($locale) {
i18n::set_locale($locale); i18n::set_locale($locale);
} }
} }
Permission::flush_permission_cache(); Permission::flush_permission_cache();
} }
/** /**
* Get a matching subsite for the given host, or for the current HTTP_HOST. * 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, * 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, * for example matching all subdomains on *.example.com with one subsite,
* and all subdomains on *.example.org on another. * and all subdomains on *.example.org on another.
* *
* @param $host The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST'] is used. * @param $host The host to find the subsite for. If not specified, $_SERVER['HTTP_HOST'] is used.
* @return int Subsite ID * @return int Subsite ID
*/ */
public static function getSubsiteIDForDomain($host = null, $checkPermissions = true) { public static function getSubsiteIDForDomain($host = null, $checkPermissions = true)
if($host == null && isset($_SERVER['HTTP_HOST'])) { {
$host = $_SERVER['HTTP_HOST']; if ($host == null && isset($_SERVER['HTTP_HOST'])) {
} $host = $_SERVER['HTTP_HOST'];
}
$matchingDomains = null; $matchingDomains = null;
$cacheKey = null; $cacheKey = null;
if ($host) { if ($host) {
if(!self::$strict_subdomain_matching) $host = preg_replace('/^www\./', '', $host); if (!self::$strict_subdomain_matching) {
$host = preg_replace('/^www\./', '', $host);
}
$cacheKey = implode('_', [$host, Member::currentUserID(), self::$check_is_public]); $cacheKey = implode('_', [$host, Member::currentUserID(), self::$check_is_public]);
if (isset(self::$_cache_subsite_for_domain[$cacheKey])) { if (isset(self::$_cache_subsite_for_domain[$cacheKey])) {
return self::$_cache_subsite_for_domain[$cacheKey]; return self::$_cache_subsite_for_domain[$cacheKey];
} }
$SQL_host = Convert::raw2sql($host); $SQL_host = Convert::raw2sql($host);
$matchingDomains = DataObject::get( $matchingDomains = DataObject::get(
SubsiteDomain::class, SubsiteDomain::class,
"'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')", "'$SQL_host' LIKE replace(\"SubsiteDomain\".\"Domain\",'*','%')",
"\"IsPrimary\" DESC" "\"IsPrimary\" DESC"
)->innerJoin('Subsite', "\"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" AND \"Subsite\".\"IsPublic\"=1"); )->innerJoin('Subsite',
} "\"Subsite\".\"ID\" = \"SubsiteDomain\".\"SubsiteID\" AND \"Subsite\".\"IsPublic\"=1");
}
if($matchingDomains && $matchingDomains->Count()) { if ($matchingDomains && $matchingDomains->Count()) {
$subsiteIDs = array_unique($matchingDomains->column('SubsiteID')); $subsiteIDs = array_unique($matchingDomains->column('SubsiteID'));
$subsiteDomains = array_unique($matchingDomains->column('Domain')); $subsiteDomains = array_unique($matchingDomains->column('Domain'));
if(sizeof($subsiteIDs) > 1) { if (sizeof($subsiteIDs) > 1) {
throw new UnexpectedValueException(sprintf( throw new UnexpectedValueException(sprintf(
"Multiple subsites match on '%s': %s", "Multiple subsites match on '%s': %s",
$host, $host,
implode(',', $subsiteDomains) implode(',', $subsiteDomains)
)); ));
} }
$subsiteID = $subsiteIDs[0]; $subsiteID = $subsiteIDs[0];
} else { } else {
if($default = DataObject::get_one(Subsite::class, "\"DefaultSite\" = 1")) { if ($default = DataObject::get_one(Subsite::class, "\"DefaultSite\" = 1")) {
// Check for a 'default' subsite // Check for a 'default' subsite
$subsiteID = $default->ID; $subsiteID = $default->ID;
} else { } else {
// Default subsite id = 0, the main site // Default subsite id = 0, the main site
$subsiteID = 0; $subsiteID = 0;
}} }
}
if ($cacheKey) { if ($cacheKey) {
self::$_cache_subsite_for_domain[$cacheKey] = $subsiteID; self::$_cache_subsite_for_domain[$cacheKey] = $subsiteID;
} }
return $subsiteID; return $subsiteID;
} }
/** /**
* *
* @param string $className * @param string $className
* @param string $filter * @param string $filter
* @param string $sort * @param string $sort
* @param string $join * @param string $join
* @param string $limit * @param string $limit
* @return DataList * @return DataList
*/ */
public static function get_from_all_subsites($className, $filter = "", $sort = "", $join = "", $limit = "") { public static function get_from_all_subsites($className, $filter = "", $sort = "", $join = "", $limit = "")
$result = DataObject::get($className, $filter, $sort, $join, $limit); {
$result = $result->setDataQueryParam('Subsite.filter', false); $result = DataObject::get($className, $filter, $sort, $join, $limit);
return $result; $result = $result->setDataQueryParam('Subsite.filter', false);
} return $result;
}
/** /**
* Disable the sub-site filtering; queries will select from all subsites * Disable the sub-site filtering; queries will select from all subsites
@ -290,18 +303,18 @@ class Subsite extends DataObject
{ {
$subsites = Subsite::get(); $subsites = Subsite::get();
if($includeMainSite) { if ($includeMainSite) {
$subsites = $subsites->toArray(); $subsites = $subsites->toArray();
$mainSite = new Subsite(); $mainSite = new Subsite();
$mainSite->Title = $mainSiteTitle; $mainSite->Title = $mainSiteTitle;
array_unshift($subsites, $mainSite); array_unshift($subsites, $mainSite);
$subsites = ArrayList::create($subsites); $subsites = ArrayList::create($subsites);
} }
return $subsites; return $subsites;
} }
/* /*
* Returns an ArrayList of the subsites accessible to the current user. * Returns an ArrayList of the subsites accessible to the current user.
@ -322,27 +335,27 @@ class Subsite extends DataObject
$member = DataObject::get_by_id(Member::class, $member); $member = DataObject::get_by_id(Member::class, $member);
} }
$subsites = new ArrayList(); $subsites = new ArrayList();
// Collect subsites for all sections. // Collect subsites for all sections.
$menu = CMSMenu::get_viewable_menu_items(); $menu = CMSMenu::get_viewable_menu_items();
foreach($menu as $candidate) { foreach ($menu as $candidate) {
if ($candidate->controller) { if ($candidate->controller) {
$accessibleSites = singleton($candidate->controller)->sectionSites( $accessibleSites = singleton($candidate->controller)->sectionSites(
$includeMainSite, $includeMainSite,
$mainSiteTitle, $mainSiteTitle,
$member $member
); );
// Replace existing keys so no one site appears twice. // Replace existing keys so no one site appears twice.
$subsites->merge($accessibleSites); $subsites->merge($accessibleSites);
} }
} }
$subsites->removeDuplicates(); $subsites->removeDuplicates();
return $subsites; return $subsites;
} }
/** /**
* Return the subsites that the current user can access by given permission. * Return the subsites that the current user can access by given permission.
@ -354,7 +367,12 @@ class Subsite extends DataObject
* @param $member * @param $member
* @return DataList of {@link Subsite} instances * @return DataList of {@link Subsite} instances
*/ */
public static function accessible_sites($permCode, $includeMainSite = true, $mainSiteTitle = "Main site", $member = null) public static function accessible_sites(
$permCode,
$includeMainSite = true,
$mainSiteTitle = "Main site",
$member = null
)
{ {
// Rationalise member arguments // Rationalise member arguments
if (!$member) { if (!$member) {
@ -367,15 +385,18 @@ class Subsite extends DataObject
$member = DataObject::get_by_id(Member::class, $member); $member = DataObject::get_by_id(Member::class, $member);
} }
// Rationalise permCode argument // Rationalise permCode argument
if(is_array($permCode)) $SQL_codes = "'" . implode("', '", Convert::raw2sql($permCode)) . "'"; if (is_array($permCode)) {
else $SQL_codes = "'" . Convert::raw2sql($permCode) . "'"; $SQL_codes = "'" . implode("', '", Convert::raw2sql($permCode)) . "'";
} else {
$SQL_codes = "'" . Convert::raw2sql($permCode) . "'";
}
// Cache handling // Cache handling
$cacheKey = $SQL_codes . '-' . $member->ID . '-' . $includeMainSite . '-' . $mainSiteTitle; $cacheKey = $SQL_codes . '-' . $member->ID . '-' . $includeMainSite . '-' . $mainSiteTitle;
if(isset(self::$_cache_accessible_sites[$cacheKey])) { if (isset(self::$_cache_accessible_sites[$cacheKey])) {
return self::$_cache_accessible_sites[$cacheKey]; return self::$_cache_accessible_sites[$cacheKey];
} }
$subsites = DataList::create(Subsite::class) $subsites = DataList::create(Subsite::class)
->where("\"Subsite\".\"Title\" != ''") ->where("\"Subsite\".\"Title\" != ''")
@ -387,8 +408,11 @@ class Subsite extends DataObject
->innerJoin('Permission', ->innerJoin('Permission',
"\"Group\".\"ID\"=\"Permission\".\"GroupID\" AND \"Permission\".\"Code\" IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')"); "\"Group\".\"ID\"=\"Permission\".\"GroupID\" AND \"Permission\".\"Code\" IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')");
if(!$subsites) $subsites = new ArrayList(); if (!$subsites) {
$subsites = new ArrayList();
}
/** @var DataList $rolesSubsites */
$rolesSubsites = DataList::create(Subsite::class) $rolesSubsites = DataList::create(Subsite::class)
->where("\"Subsite\".\"Title\" != ''") ->where("\"Subsite\".\"Title\" != ''")
->leftJoin('Group_Subsites', "\"Group_Subsites\".\"SubsiteID\" = \"Subsite\".\"ID\"") ->leftJoin('Group_Subsites', "\"Group_Subsites\".\"SubsiteID\" = \"Subsite\".\"ID\"")
@ -401,15 +425,19 @@ class Subsite extends DataObject
->innerJoin('PermissionRoleCode', ->innerJoin('PermissionRoleCode',
"\"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\" AND \"PermissionRoleCode\".\"Code\" IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')"); "\"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\" AND \"PermissionRoleCode\".\"Code\" IN ($SQL_codes, 'CMS_ACCESS_LeftAndMain', 'ADMIN')");
if(!$subsites && $rolesSubsites) return $rolesSubsites; if (!$subsites && $rolesSubsites) {
return $rolesSubsites;
}
$subsites = new ArrayList($subsites->toArray()); $subsites = new ArrayList($subsites->toArray());
if($rolesSubsites) foreach($rolesSubsites as $subsite) { if ($rolesSubsites) {
if(!$subsites->find('ID', $subsite->ID)) { foreach ($rolesSubsites as $subsite) {
$subsites->push($subsite); if (!$subsites->find('ID', $subsite->ID)) {
} $subsites->push($subsite);
} }
}
}
if ($includeMainSite) { if ($includeMainSite) {
if (!is_array($permCode)) { if (!is_array($permCode)) {
@ -501,43 +529,46 @@ class Subsite extends DataObject
$member = Member::currentUser(); $member = Member::currentUser();
} }
if(!$member) return false; if (!$member) {
return false;
}
if(!in_array("ADMIN", $permissionCodes)) $permissionCodes[] = "ADMIN"; if (!in_array("ADMIN", $permissionCodes)) {
$permissionCodes[] = "ADMIN";
}
$SQLa_perm = Convert::raw2sql($permissionCodes); $SQLa_perm = Convert::raw2sql($permissionCodes);
$SQL_perms = join("','", $SQLa_perm); $SQL_perms = join("','", $SQLa_perm);
$memberID = (int)$member->ID; $memberID = (int)$member->ID;
// Count this user's groups which can access the main site // Count this user's groups which can access the main site
$groupCount = DB::query(" $groupCount = DB::query("
SELECT COUNT(\"Permission\".\"ID\") SELECT COUNT(\"Permission\".\"ID\")
FROM \"Permission\" FROM \"Permission\"
INNER JOIN \"Group\" ON \"Group\".\"ID\" = \"Permission\".\"GroupID\" AND \"Group\".\"AccessAllSubsites\" = 1 INNER JOIN \"Group\" ON \"Group\".\"ID\" = \"Permission\".\"GroupID\" AND \"Group\".\"AccessAllSubsites\" = 1
INNER JOIN \"Group_Members\" ON \"Group_Members\".\"GroupID\" = \"Permission\".\"GroupID\" INNER JOIN \"Group_Members\" ON \"Group_Members\".\"GroupID\" = \"Permission\".\"GroupID\"
WHERE \"Permission\".\"Code\" IN ('$SQL_perms') WHERE \"Permission\".\"Code\" IN ('$SQL_perms')
AND \"MemberID\" = {$memberID} AND \"MemberID\" = {$memberID}
")->value(); ")->value();
// Count this user's groups which have a role that can access the main site // Count this user's groups which have a role that can access the main site
$roleCount = DB::query(" $roleCount = DB::query("
SELECT COUNT(\"PermissionRoleCode\".\"ID\") SELECT COUNT(\"PermissionRoleCode\".\"ID\")
FROM \"Group\" FROM \"Group\"
INNER JOIN \"Group_Members\" ON \"Group_Members\".\"GroupID\" = \"Group\".\"ID\" INNER JOIN \"Group_Members\" ON \"Group_Members\".\"GroupID\" = \"Group\".\"ID\"
INNER JOIN \"Group_Roles\" ON \"Group_Roles\".\"GroupID\"=\"Group\".\"ID\" INNER JOIN \"Group_Roles\" ON \"Group_Roles\".\"GroupID\"=\"Group\".\"ID\"
INNER JOIN \"PermissionRole\" ON \"Group_Roles\".\"PermissionRoleID\"=\"PermissionRole\".\"ID\" INNER JOIN \"PermissionRole\" ON \"Group_Roles\".\"PermissionRoleID\"=\"PermissionRole\".\"ID\"
INNER JOIN \"PermissionRoleCode\" ON \"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\" INNER JOIN \"PermissionRoleCode\" ON \"PermissionRole\".\"ID\"=\"PermissionRoleCode\".\"RoleID\"
WHERE \"PermissionRoleCode\".\"Code\" IN ('$SQL_perms') WHERE \"PermissionRoleCode\".\"Code\" IN ('$SQL_perms')
AND \"Group\".\"AccessAllSubsites\" = 1 AND \"Group\".\"AccessAllSubsites\" = 1
AND \"MemberID\" = {$memberID} AND \"MemberID\" = {$memberID}
")->value(); ")->value();
// There has to be at least one that allows access. // There has to be at least one that allows access.
return ($groupCount + $roleCount > 0); return ($groupCount + $roleCount > 0);
} }
/** /**
*
* @var array * @var array
*/ */
private static $db = [ private static $db = [
@ -547,16 +578,15 @@ class Subsite extends DataObject
'Theme' => 'Varchar', 'Theme' => 'Varchar',
'Language' => 'Varchar(6)', 'Language' => 'Varchar(6)',
// Used to hide unfinished/private subsites from public view. // Used to hide unfinished/private subsites from public view.
// If unset, will default to true // If unset, will default to true
'IsPublic' => 'Boolean', 'IsPublic' => 'Boolean',
// Comma-separated list of disallowed page types // Comma-separated list of disallowed page types
'PageTypeBlacklist' => 'Text', 'PageTypeBlacklist' => 'Text',
]; ];
/** /**
*
* @var array * @var array
*/ */
private static $has_many = [ private static $has_many = [
@ -564,44 +594,41 @@ class Subsite extends DataObject
]; ];
/** /**
*
* @var array * @var array
*/ */
private static $belongs_many_many = [ private static $belongs_many_many = [
"Groups" => Group::class, "Groups" => Group::class,
]; ];
/** /**
* * @var array
* @var array */
*/ private static $defaults = [
private static $defaults = [ 'IsPublic' => 1
'IsPublic' => 1 ];
];
/** /**
* * @var array
* @var array */
*/ private static $searchable_fields = [
private static $searchable_fields = [ 'Title',
'Title', 'Domains.Domain',
'Domains.Domain', 'IsPublic',
'IsPublic', ];
];
/** /**
* * @var string
* @var string */
*/ private static $default_sort = "\"Title\" ASC";
private static $default_sort = "\"Title\" ASC";
/** /**
* @todo Possible security issue, don't grant edit permissions to everybody. * @todo Possible security issue, don't grant edit permissions to everybody.
* @return boolean * @return boolean
*/ */
public function canEdit($member = false) public function canEdit($member = false)
{return true; {
} return true;
}
/** /**
* Show the configuration fields for each subsite * Show the configuration fields for each subsite
@ -610,7 +637,7 @@ class Subsite extends DataObject
*/ */
public function getCMSFields() public function getCMSFields()
{ {
if ($this->ID!=0) { if ($this->ID != 0) {
$domainTable = new GridField( $domainTable = new GridField(
"Domains", "Domains",
_t('Subsite.DomainsListTitle', "Domains"), _t('Subsite.DomainsListTitle', "Domains"),
@ -620,7 +647,8 @@ class Subsite extends DataObject
} else { } else {
$domainTable = new LiteralField( $domainTable = new LiteralField(
'Domains', 'Domains',
'<p>'._t('Subsite.DOMAINSAVEFIRST', 'You can only add domains after saving for the first time').'</p>' '<p>' . _t('Subsite.DOMAINSAVEFIRST',
'You can only add domains after saving for the first time') . '</p>'
); );
} }
@ -630,7 +658,7 @@ class Subsite extends DataObject
Injector::inst()->get(IntlLocales::class)->getLocales() Injector::inst()->get(IntlLocales::class)->getLocales()
); );
$pageTypeMap = array(); $pageTypeMap = [];
$pageTypes = SiteTree::page_type_classes(); $pageTypes = SiteTree::page_type_classes();
foreach ($pageTypes as $pageType) { foreach ($pageTypes as $pageType) {
$pageTypeMap[$pageType] = singleton($pageType)->i18n_singular_name(); $pageTypeMap[$pageType] = singleton($pageType)->i18n_singular_name();
@ -655,98 +683,103 @@ class Subsite extends DataObject
new CheckboxField('DefaultSite', $this->fieldLabel('DefaultSite'), $this->DefaultSite), new CheckboxField('DefaultSite', $this->fieldLabel('DefaultSite'), $this->DefaultSite),
new CheckboxField('IsPublic', $this->fieldLabel('IsPublic'), $this->IsPublic), new CheckboxField('IsPublic', $this->fieldLabel('IsPublic'), $this->IsPublic),
new DropdownField('Theme',$this->fieldLabel('Theme'), $this->allowedThemes(), $this->Theme), new DropdownField('Theme', $this->fieldLabel('Theme'), $this->allowedThemes(), $this->Theme),
new LiteralField( new LiteralField(
'PageTypeBlacklistToggle', 'PageTypeBlacklistToggle',
sprintf( sprintf(
'<div class="field"><a href="#" id="PageTypeBlacklistToggle">%s</a></div>', '<div class="field"><a href="#" id="PageTypeBlacklistToggle">%s</a></div>',
_t('Subsite.PageTypeBlacklistField', 'Disallow page types?') _t('Subsite.PageTypeBlacklistField', 'Disallow page types?')
) )
), ),
new CheckboxSetField( new CheckboxSetField(
'PageTypeBlacklist', 'PageTypeBlacklist',
false, false,
$pageTypeMap $pageTypeMap
) )
) )
), ),
new HiddenField('ID', '', $this->ID), new HiddenField('ID', '', $this->ID),
new HiddenField('IsSubsite', '', 1) new HiddenField('IsSubsite', '', 1)
); );
$subsiteTabs->addExtraClass('subsite-model'); $subsiteTabs->addExtraClass('subsite-model');
$this->extend('updateCMSFields', $fields); $this->extend('updateCMSFields', $fields);
return $fields; return $fields;
} }
/** /**
* *
* @param boolean $includerelations * @param boolean $includerelations
* @return array * @return array
*/ */
public function fieldLabels($includerelations = true) { public function fieldLabels($includerelations = true)
$labels = parent::fieldLabels($includerelations); {
$labels['Title'] = _t('Subsites.TitleFieldLabel', 'Subsite Name'); $labels = parent::fieldLabels($includerelations);
$labels['RedirectURL'] = _t('Subsites.RedirectURLFieldLabel', 'Redirect URL'); $labels['Title'] = _t('Subsites.TitleFieldLabel', 'Subsite Name');
$labels['DefaultSite'] = _t('Subsites.DefaultSiteFieldLabel', 'Default site'); $labels['RedirectURL'] = _t('Subsites.RedirectURLFieldLabel', 'Redirect URL');
$labels['Theme'] = _t('Subsites.ThemeFieldLabel', 'Theme'); $labels['DefaultSite'] = _t('Subsites.DefaultSiteFieldLabel', 'Default site');
$labels['Language'] = _t('Subsites.LanguageFieldLabel', 'Language'); $labels['Theme'] = _t('Subsites.ThemeFieldLabel', 'Theme');
$labels['IsPublic'] = _t('Subsites.IsPublicFieldLabel', 'Enable public access'); $labels['Language'] = _t('Subsites.LanguageFieldLabel', 'Language');
$labels['PageTypeBlacklist'] = _t('Subsites.PageTypeBlacklistFieldLabel', 'Page Type Blacklist'); $labels['IsPublic'] = _t('Subsites.IsPublicFieldLabel', 'Enable public access');
$labels['Domains.Domain'] = _t('Subsites.DomainFieldLabel', 'Domain'); $labels['PageTypeBlacklist'] = _t('Subsites.PageTypeBlacklistFieldLabel', 'Page Type Blacklist');
$labels['PrimaryDomain'] = _t('Subsites.PrimaryDomainFieldLabel', 'Primary Domain'); $labels['Domains.Domain'] = _t('Subsites.DomainFieldLabel', 'Domain');
$labels['PrimaryDomain'] = _t('Subsites.PrimaryDomainFieldLabel', 'Primary Domain');
return $labels; return $labels;
} }
/** /**
* Return the themes that can be used with this subsite, as an array of themecode => description
*
* @return array
*/
public function allowedThemes()
{
if ($themes = $this->stat('allowed_themes')) {
return ArrayLib::valuekey($themes);
} else {
$themes = [];
if (is_dir(THEMES_PATH)) {
foreach (scandir(THEMES_PATH) as $theme) {
if ($theme[0] == '.') {
continue;
}
$theme = strtok($theme, '_');
$themes[$theme] = $theme;
}
ksort($themes);
}
return $themes;
}
}
* Return the themes that can be used with this subsite, as an array of themecode => description /**
* * @return string Current locale of the subsite
* @return array */
*/ public function getLanguage()
public function allowedThemes() {
{if($themes = $this->stat('allowed_themes')) { if ($this->getField('Language')) {
return ArrayLib::valuekey($themes); return $this->getField('Language');
} else { } else {
$themes = []; return i18n::get_locale();
if(is_dir(THEMES_PATH)) { }
foreach(scandir(THEMES_PATH) as $theme) { }
if($theme[0] == '.') {continue;}
$theme = strtok($theme,'_');
$themes[$theme] = $theme;
}
ksort($themes);
}
return $themes;
}
}
/** /**
* @return string Current locale of the subsite *
*/ * @return ValidationResult
public function getLanguage() { */
if($this->getField('Language')) { public function validate()
return $this->getField('Language'); {
} else { $result = parent::validate();
return i18n::get_locale(); if (!$this->Title) {
} $result->error(_t('Subsite.ValidateTitle', 'Please add a "Title"'));
} }
return $result;
/** }
*
* @return ValidationResult
*/
public function validate() {
$result = parent::validate();
if(!$this->Title) {
$result->error(_t('Subsite.ValidateTitle', 'Please add a "Title"'));
}
return $result;
}
/** /**
* Whenever a Subsite is written, rewrite the hostmap * Whenever a Subsite is written, rewrite the hostmap
@ -815,38 +848,41 @@ class Subsite extends DataObject
return Director::absoluteBaseURL(); return Director::absoluteBaseURL();
} }
/** /**
* @todo getClassName is redundant, already stored as a database field? * @todo getClassName is redundant, already stored as a database field?
*/ */
public function getClassName() { public function getClassName()
return $this->class; {
} return $this->class;
}
/** /**
* Javascript admin action to duplicate this subsite * Javascript admin action to duplicate this subsite
* *
* @return string - javascript * @return string - javascript
*/ */
public function adminDuplicate() public function adminDuplicate()
{$newItem = $this->duplicate(); {
$message = _t( $newItem = $this->duplicate();
'Subsite.CopyMessage', $message = _t(
'Created a copy of {title}', 'Subsite.CopyMessage',
['title' => Convert::raw2js($this->Title)] 'Created a copy of {title}',
); ['title' => Convert::raw2js($this->Title)]
);
return <<<JS return <<<JS
statusMessage($message, 'good'); statusMessage($message, 'good');
$('Form_EditForm').loadURLFromServer('admin/subsites/show/$newItem->ID'); $('Form_EditForm').loadURLFromServer('admin/subsites/show/$newItem->ID');
JS; JS;
} }
/** /**
* Make this subsite the current one * Make this subsite the current one
*/ */
public function activate() { public function activate()
Subsite::changeSubsite($this); {
} Subsite::changeSubsite($this);
}
/** /**
* *
@ -860,26 +896,27 @@ JS;
} }
$SQL_permissionCodes = Convert::raw2sql($permissionCodes); $SQL_permissionCodes = Convert::raw2sql($permissionCodes);
$SQL_permissionCodes = join("','", $SQL_permissionCodes); $SQL_permissionCodes = join("','", $SQL_permissionCodes);
return DataObject::get( return DataObject::get(
Member::class, Member::class,
"\"Group\".\"SubsiteID\" = $this->ID AND \"Permission\".\"Code\" IN ('$SQL_permissionCodes')", "\"Group\".\"SubsiteID\" = $this->ID AND \"Permission\".\"Code\" IN ('$SQL_permissionCodes')",
'', '',
"LEFT JOIN \"Group_Members\" ON \"Member\".\"ID\" = \"Group_Members\".\"MemberID\" "LEFT JOIN \"Group_Members\" ON \"Member\".\"ID\" = \"Group_Members\".\"MemberID\"
LEFT JOIN \"Group\" ON \"Group\".\"ID\" = \"Group_Members\".\"GroupID\" LEFT JOIN \"Group\" ON \"Group\".\"ID\" = \"Group_Members\".\"GroupID\"
LEFT JOIN \"Permission\" ON \"Permission\".\"GroupID\" = \"Group\".\"ID\"" LEFT JOIN \"Permission\" ON \"Permission\".\"GroupID\" = \"Group\".\"ID\""
); );
} }
/** /**
* Duplicate this subsite * Duplicate this subsite
*/ */
public function duplicate($doWrite = true) { public function duplicate($doWrite = true)
$duplicate = parent::duplicate($doWrite); {
$duplicate = parent::duplicate($doWrite);
$oldSubsiteID = Session::get('SubsiteID'); $oldSubsiteID = Session::get('SubsiteID');
self::changeSubsite($this->ID); self::changeSubsite($this->ID);
/* /*
* Copy data from this object to the given subsite. Does this using an iterative depth-first search. * Copy data from this object to the given subsite. Does this using an iterative depth-first search.
@ -892,24 +929,24 @@ JS;
list($sourceParentID, $destParentID) = array_pop($stack); list($sourceParentID, $destParentID) = array_pop($stack);
$children = Versioned::get_by_stage('Page', 'Live', "\"ParentID\" = $sourceParentID", ''); $children = Versioned::get_by_stage('Page', 'Live', "\"ParentID\" = $sourceParentID", '');
if($children) { if ($children) {
foreach($children as $child) { foreach ($children as $child) {
self::changeSubsite($duplicate->ID); //Change to destination subsite self::changeSubsite($duplicate->ID); //Change to destination subsite
$childClone = $child->duplicateToSubsite($duplicate, false); $childClone = $child->duplicateToSubsite($duplicate, false);
$childClone->ParentID = $destParentID; $childClone->ParentID = $destParentID;
$childClone->writeToStage('Stage'); $childClone->writeToStage('Stage');
$childClone->publish('Stage', 'Live'); $childClone->publish('Stage', 'Live');
self::changeSubsite($this->ID); //Change Back to this subsite self::changeSubsite($this->ID); //Change Back to this subsite
array_push($stack, [$child->ID, $childClone->ID]); array_push($stack, [$child->ID, $childClone->ID]);
} }
} }
} }
self::changeSubsite($oldSubsiteID); self::changeSubsite($oldSubsiteID);
return $duplicate; return $duplicate;
} }
} }

View File

@ -2,12 +2,13 @@
namespace SilverStripe\Subsites\Model; namespace SilverStripe\Subsites\Model;
use SilverStripe\Control\Director;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\OptionsetField;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Subsites\Forms\WildcardDomainField;
/** /**
@ -21,7 +22,7 @@ use SilverStripe\ORM\DataObject;
class SubsiteDomain extends DataObject class SubsiteDomain extends DataObject
{ {
private static $table_name = 'SubsiteDomain'; private static $table_name = 'SubsiteDomain';
/** /**
* *
@ -30,15 +31,16 @@ class SubsiteDomain extends DataObject
private static $default_sort = "\"IsPrimary\" DESC"; private static $default_sort = "\"IsPrimary\" DESC";
/** * /** *
* @var array * @var array
*/ */
private static $db = [ private static $db = [
"Domain" => "Varchar(255)", "Domain" => "Varchar(255)",
"Protocol" => "Enum('http,https,automatic','automatic')", "Protocol" => "Enum('http,https,automatic','automatic')",
"IsPrimary" => "Boolean", "IsPrimary" => "Boolean",
]; ];
/*** Specifies that this subsite is http only /**
* Specifies that this subsite is http only
*/ */
const PROTOCOL_HTTP = 'http'; const PROTOCOL_HTTP = 'http';
@ -63,53 +65,55 @@ class SubsiteDomain extends DataObject
} }
/** /**
* *
* @var array
*/
private static $has_one = [
"Subsite" => Subsite::class,
];
/**
*@config
* @var array
*/
private static $summary_fields=[
'Domain',
'IsPrimary',
];
/*** @config
* @var array * @var array
*/ */
private static $casting = array( private static $has_one = [
"Subsite" => Subsite::class,
];
/**
* @config
* @var array
*/
private static $summary_fields = [
'Domain',
'IsPrimary',
];
/*** @config
* @var array
*/
private static $casting = [
'SubstitutedDomain' => 'Varchar', 'SubstitutedDomain' => 'Varchar',
'FullProtocol' => 'Varchar', 'FullProtocol' => 'Varchar',
'AbsoluteLink' => 'Varchar', 'AbsoluteLink' => 'Varchar',
); ];
/** /**
* Whenever a Subsite Domain is written, rewrite the hostmap * Whenever a Subsite Domain is written, rewrite the hostmap
* *
* @return void * @return void
*/ */
public function onAfterWrite() public function onAfterWrite()
{Subsite::writeHostMap(); {
} Subsite::writeHostMap();
}
/** /**
* *
* @return \FieldList * @return \FieldList
*/ */
public function getCMSFields() public function getCMSFields()
{ {
$protocols = array( $protocols = [
self::PROTOCOL_HTTP => _t('SubsiteDomain.PROTOCOL_HTTP', 'http://'), self::PROTOCOL_HTTP => _t('SubsiteDomain.PROTOCOL_HTTP', 'http://'),
self::PROTOCOL_HTTPS => _t('SubsiteDomain.PROTOCOL_HTTPS', 'https://'), self::PROTOCOL_HTTPS => _t('SubsiteDomain.PROTOCOL_HTTPS', 'https://'),
self::PROTOCOL_AUTOMATIC => _t('SubsiteDomain.PROTOCOL_AUTOMATIC', 'Automatic') self::PROTOCOL_AUTOMATIC => _t('SubsiteDomain.PROTOCOL_AUTOMATIC', 'Automatic')
);$fields = new FieldList( ];
WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255) $fields = new FieldList(
->setDescription(_t( WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255)
->setDescription(_t(
'SubsiteDomain.DOMAIN_DESCRIPTION', 'SubsiteDomain.DOMAIN_DESCRIPTION',
'Hostname of this subsite (exclude protocol). Allows wildcards (*).' 'Hostname of this subsite (exclude protocol). Allows wildcards (*).'
)), )),
@ -119,10 +123,10 @@ class SubsiteDomain extends DataObject
'When generating links to this subsite, use the selected protocol. <br />' . 'When generating links to this subsite, use the selected protocol. <br />' .
'Selecting \'Automatic\' means subsite links will default to the current protocol.' 'Selecting \'Automatic\' means subsite links will default to the current protocol.'
)), CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary')) )), CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary'))
->setDescription(_t( ->setDescription(_t(
'SubsiteDomain.PROTOCOL_DESCRIPTION', 'SubsiteDomain.PROTOCOL_DESCRIPTION',
'Mark this as the default domain for this subsite' 'Mark this as the default domain for this subsite'
))); )));
$this->extend('updateCMSFields', $fields); $this->extend('updateCMSFields', $fields);
return $fields; return $fields;
@ -162,7 +166,7 @@ class SubsiteDomain extends DataObject
return $this->getFullProtocol() . $this->Domain; return $this->getFullProtocol() . $this->Domain;
} }
/** /**
* Gets the full protocol (including ://) for this domain * Gets the full protocol (including ://) for this domain
* *
* @return string * @return string
@ -170,16 +174,13 @@ class SubsiteDomain extends DataObject
public function getFullProtocol() public function getFullProtocol()
{ {
switch ($this->Protocol) { switch ($this->Protocol) {
case self::PROTOCOL_HTTPS: case self::PROTOCOL_HTTPS: {
{
return 'https://'; return 'https://';
} }
case self::PROTOCOL_HTTP: case self::PROTOCOL_HTTP: {
{
return 'http://'; return 'http://';
} }
default: default: {
{
return Director::protocol(); return Director::protocol();
} }
} }
@ -198,7 +199,7 @@ class SubsiteDomain extends DataObject
// If there are wildcards in the primary domain (not recommended), make some // If there are wildcards in the primary domain (not recommended), make some
// educated guesses about what to replace them with: // educated guesses about what to replace them with:
$domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain ); $domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain);
// Default to "subsite." prefix for first wildcard // Default to "subsite." prefix for first wildcard
// TODO Whats the significance of "subsite" inthiscontext?! // TODO Whats the significance of "subsite" inthiscontext?!
@ -230,5 +231,5 @@ class SubsiteDomain extends DataObject
return Controller::join_links( return Controller::join_links(
$this->getAbsoluteLink(), $this->getAbsoluteLink(),
Director::baseURL()); Director::baseURL());
} }
} }

View File

@ -18,7 +18,6 @@ use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
class SubsitesVirtualPage extends VirtualPage class SubsitesVirtualPage extends VirtualPage
{ {

View File

@ -15,9 +15,10 @@ use SilverStripe\Subsites\Model\Subsite;
*/ */
class SubsiteReportWrapper extends ReportWrapper class SubsiteReportWrapper extends ReportWrapper
{ {
///////////////////////////////////////////////////////////////////////////////////////////
// Filtering
/**
* @return FieldList
*/
public function parameterFields() public function parameterFields()
{ {
$subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true); $subsites = Subsite::accessible_sites('CMS_ACCESS_CMSMain', true);
@ -44,9 +45,9 @@ class SubsiteReportWrapper extends ReportWrapper
return $fields; return $fields;
} }
/////////////////////////////////////////////////////////////////////////////////////////// /**
// Columns * @return array
*/
public function columns() public function columns()
{ {
$columns = parent::columns(); $columns = parent::columns();
@ -57,6 +58,10 @@ class SubsiteReportWrapper extends ReportWrapper
/////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////
// Querying // Querying
/**
* @param arary $params
* @return void
*/
public function beforeQuery($params) public function beforeQuery($params)
{ {
// The user has select a few specific sites // The user has select a few specific sites
@ -70,6 +75,10 @@ class SubsiteReportWrapper extends ReportWrapper
Subsite::$force_subsite = join(',', array_keys($options)); Subsite::$force_subsite = join(',', array_keys($options));
} }
} }
/**
* @return void
*/
public function afterQuery() public function afterQuery()
{ {
// Manually manage the subsite filtering // Manually manage the subsite filtering

7
phpunit.xml.dist Normal file
View File

@ -0,0 +1,7 @@
<phpunit bootstrap="framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/php</directory>
</testsuite>
</phpunit>

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Subsites\Test\Behaviour; namespace SilverStripe\Subsites\Tests\Behaviour;
if (!class_exists('SilverStripe\BehatExtension\Context\SilverStripeContext')) { if (!class_exists('SilverStripe\BehatExtension\Context\SilverStripeContext')) {
return; return;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
@ -10,8 +12,9 @@ class BaseSubsiteTest extends SapphireTest
{ {
parent::setUp(); parent::setUp();
Subsite::$use_session_subsiteid = true; Subsite::$use_session_subsiteid = true;
Subsite::$force_subsite = null;} Subsite::$force_subsite = null;
}
/** /**
* Avoid subsites filtering on fixture fetching. * Avoid subsites filtering on fixture fetching.

View File

@ -1,13 +1,17 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Assets\File; use SilverStripe\Assets\File;
use SilverStripe\Assets\Folder; use SilverStripe\Assets\Folder;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Subsites\Tests\BaseSubsiteTest;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
class FileSubsitesTest extends BaseSubsiteTest class FileSubsitesTest extends BaseSubsiteTest
{ {
public static $fixture_file = 'subsites/tests/SubsiteTest.yml'; public static $fixture_file = 'subsites/tests/SubsiteTest.yml';
/** /**
@ -15,15 +19,15 @@ class FileSubsitesTest extends BaseSubsiteTest
* *
* @var array * @var array
*/ */
protected $illegalExtensions = array( protected $illegalExtensions = array(
'File' => array( 'File' => array(
'SecureFileExtension', 'SecureFileExtension',
'VersionedFileExtension' 'VersionedFileExtension'
), ),
'SiteTree' => array( 'SiteTree' => array(
'Translatable', 'Translatable',
) )
); );
public function testTrivialFeatures() public function testTrivialFeatures()
{ {

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Security\Group; use SilverStripe\Security\Group;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Subsites\Extensions\GroupSubsites; use SilverStripe\Subsites\Extensions\GroupSubsites;
@ -10,7 +12,6 @@ class GroupSubsitesTest extends BaseSubsiteTest
public static $fixture_file = 'subsites/tests/SubsiteTest.yml'; public static $fixture_file = 'subsites/tests/SubsiteTest.yml';
protected $requireDefaultRecordsFrom = [GroupSubsites::class]; protected $requireDefaultRecordsFrom = [GroupSubsites::class];
public function testTrivialFeatures() public function testTrivialFeatures()
{ {
$this->assertTrue(is_array(singleton('GroupSubsites')->extraStatics())); $this->assertTrue(is_array(singleton('GroupSubsites')->extraStatics()));

View File

@ -1,5 +1,8 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\CMS\Controllers\CMSMain; use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Admin\LeftAndMain; use SilverStripe\Admin\LeftAndMain;
@ -56,7 +59,6 @@ class LeftAndMainSubsitesTest extends FunctionalTest
$subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1'); $subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1');
$subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2'); $subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2');
$subsite3 = $this->objFromFixture(Subsite::class, 'domaintest3'); $subsite3 = $this->objFromFixture(Subsite::class, 'domaintest3');
$ids[] = $subsite1->ID; $ids[] = $subsite1->ID;
$ids[] = $subsite2->ID; $ids[] = $subsite2->ID;
$ids[] = $subsite3->ID; $ids[] = $subsite3->ID;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Subsites\Extensions\SiteConfigSubsites; use SilverStripe\Subsites\Extensions\SiteConfigSubsites;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
@ -7,9 +9,9 @@ use SilverStripe\Subsites\Model\Subsite;
class SiteConfigSubsitesTest extends BaseSubsiteTest class SiteConfigSubsitesTest extends BaseSubsiteTest
{ {
static $fixture_file = 'subsites/tests/SubsiteTest.yml'; public static $fixture_file = 'subsites/tests/SubsiteTest.yml';
function testEachSubsiteHasAUniqueSiteConfig() public function testEachSubsiteHasAUniqueSiteConfig()
{ {
$subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1'); $subsite1 = $this->objFromFixture(Subsite::class, 'domaintest1');
$subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2'); $subsite2 = $this->objFromFixture(Subsite::class, 'domaintest2');

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;

View File

@ -1,9 +1,11 @@
<?php <?php
use SilverStripe\Core\Config\Config; namespace SilverStripe\Subsites\Tests;
use SilverStripe\Control\Director;
use SilverStripe\ORM\DataObject;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Subsites\Model\Subsite; use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\Model\SubsiteDomain; use SilverStripe\Subsites\Model\SubsiteDomain;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Control\Session; use SilverStripe\Control\Session;
use SilverStripe\Dev\FunctionalTest; use SilverStripe\Dev\FunctionalTest;

View File

@ -1,5 +1,7 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Assets\Filesystem; use SilverStripe\Assets\Filesystem;
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
use SilverStripe\Control\Director; use SilverStripe\Control\Director;

View File

@ -1,6 +1,9 @@
<?php <?php
namespace SilverStripe\Subsites\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Subsites\Forms\WildcardDomainField;
/** /**
* Tests {@see WildcardDomainField} * Tests {@see WildcardDomainField}
@ -61,7 +64,7 @@ class WildcardDomainFieldTest extends SapphireTest {
array('*.mysite.') array('*.mysite.')
); );
} }
public function validWildcards() { public function validWildcards() {
return array( return array(
array('*.mysite.com'), array('*.mysite.com'),