Skip to content

Commit

Permalink
reformat code and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerkrauss committed May 24, 2017
1 parent 2295501 commit c5f507b
Show file tree
Hide file tree
Showing 30 changed files with 4,103 additions and 3,702 deletions.
34 changes: 18 additions & 16 deletions code/admin/SubsiteAdmin.php
Expand Up @@ -13,30 +13,32 @@
*
* @package subsites
*/
class SubsiteAdmin extends ModelAdmin {
class SubsiteAdmin extends ModelAdmin
{

private static $managed_models = array(Subsite::class);
private static $managed_models = [Subsite::class];

private static $url_segment = 'subsites';
private static $url_segment = 'subsites';

private static $menu_title = "Subsites";
private static $menu_title = "Subsites";

private static $menu_icon = "subsites/images/subsites.png";
private static $menu_icon = "subsites/images/subsites.png";

public $showImportForm=false;
public $showImportForm = false;

private static $tree_class = Subsite::class;
private static $tree_class = Subsite::class;

public function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id, $fields);
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);

$grid=$form->Fields()->dataFieldByName(Subsite::class);
if($grid) {
$grid->getConfig()->removeComponentsByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm');
$grid->getConfig()->addComponent(new GridFieldSubsiteDetailForm());
}
$grid = $form->Fields()->dataFieldByName(Subsite::class);
if ($grid) {
$grid->getConfig()->removeComponentsByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm');
$grid->getConfig()->addComponent(new GridFieldSubsiteDetailForm());
}

return $form;
}
return $form;
}

}
87 changes: 49 additions & 38 deletions code/controller/SubsiteXHRController.php
Expand Up @@ -10,43 +10,54 @@
/**
* Section-agnostic PJAX controller.
*/
class SubsiteXHRController extends LeftAndMain {

/**
* Relax the access permissions, so anyone who has access to any CMS subsite can access this controller.
*/
public function canView($member = null) {
if (parent::canView()) return true;

if (Subsite::all_accessible_sites()->count()>0) return true;

return false;
}

/**
* Similar as above, but for the LeftAndMainSubsites - allow access if user allowed into the CMS at all.
*/
public function canAccess() {
if (Subsite::all_accessible_sites()->count()>0) return true;
}

public function getResponseNegotiator() {
$negotiator = parent::getResponseNegotiator();
$self = $this;

// Register a new callback
$negotiator->setCallback('SubsiteList', function() use(&$self) {
return $self->SubsiteList();
});

return $negotiator;
}

/**
* Provide the list of available subsites as a cms-section-agnostic PJAX handler.
*/
public function SubsiteList() {
return $this->renderWith('Includes/SubsiteList');
}
class SubsiteXHRController extends LeftAndMain
{

/**
* Relax the access permissions, so anyone who has access to any CMS subsite can access this controller.
*/
public function canView($member = null)
{
if (parent::canView()) {
return true;
}

if (Subsite::all_accessible_sites()->count() > 0) {
return true;
}

return false;
}

/**
* Similar as above, but for the LeftAndMainSubsites - allow access if user allowed into the CMS at all.
*/
public function canAccess()
{
if (Subsite::all_accessible_sites()->count() > 0) {
return true;
}
}

public function getResponseNegotiator()
{
$negotiator = parent::getResponseNegotiator();
$self = $this;

// Register a new callback
$negotiator->setCallback('SubsiteList', function () use (&$self) {
return $self->SubsiteList();
});

return $negotiator;
}

/**
* Provide the list of available subsites as a cms-section-agnostic PJAX handler.
*/
public function SubsiteList()
{
return $this->renderWith('Includes/SubsiteList');
}

}
12 changes: 7 additions & 5 deletions code/extensions/CMSPageAddControllerExtension.php
Expand Up @@ -3,14 +3,16 @@
namespace SilverStripe\Subsites\Extensions;


use SilverStripe\Forms\HiddenField;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Subsites\Model\Subsite;

class CMSPageAddControllerExtension extends Extension {
class CMSPageAddControllerExtension extends Extension
{

function updatePageOptions(&$fields) {
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
}
function updatePageOptions(&$fields)
{
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
}

}
30 changes: 17 additions & 13 deletions code/extensions/ControllerSubsites.php
Expand Up @@ -3,26 +3,30 @@
namespace SilverStripe\Subsites\Extensions;


use SilverStripe\View\SSViewer;
use SilverStripe\Core\Extension;
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\View\SSViewer;

/**
* @package subsites
*/
class ControllerSubsites extends Extension {
function controllerAugmentInit(){
if($subsite = Subsite::currentSubsite()){
if($theme = $subsite->Theme)
SSViewer::set_theme($theme);
}
}
class ControllerSubsites extends Extension
{
function controllerAugmentInit()
{
if ($subsite = Subsite::currentSubsite()) {
if ($theme = $subsite->Theme) {
SSViewer::set_theme($theme);
}
}
}

function CurrentSubsite(){
if($subsite = Subsite::currentSubsite()){
return $subsite;
}
}
function CurrentSubsite()
{
if ($subsite = Subsite::currentSubsite()) {
return $subsite;
}
}
}

?>
72 changes: 37 additions & 35 deletions code/extensions/ErrorPageSubsite.php
Expand Up @@ -3,44 +3,46 @@
namespace SilverStripe\Subsites\Extensions;


use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataObject;
use SilverStripe\Subsites\Model\Subsite;


class ErrorPageSubsite extends DataExtension {

/**
* Alter file path to generated a static (static) error page file to handle error page template on different sub-sites
*
* {@see Error::get_error_filename()}
*
* FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including main site) between
* opening ErrorPage in the CMS and publish ErrorPage causes static error page to get generated incorrectly.
*
* @param string $name Filename to write to
* @param int $statusCode Integer error code
*/
public function updateErrorFilename(&$name, $statusCode) {

// Try to get current subsite from session
$subsite = Subsite::currentSubsite(false);

// since this function is called from Page class before the controller is created, we have to get subsite from domain instead
if(!$subsite) {
$subsiteID = Subsite::getSubsiteIDForDomain();
if($subsiteID != 0) {
$subsite = DataObject::get_by_id(Subsite::class, $subsiteID);
}
}

// Without subsite, don't rewrite
if($subsite) {
// Add subdomain to end of filename, just before .html
// This should preserve translatable locale in the filename as well
$subdomain = $subsite->domain();
$name = substr($name, 0, -5) . "-{$subdomain}.html";
}
}
class ErrorPageSubsite extends DataExtension
{

/**
* Alter file path to generated a static (static) error page file to handle error page template on different sub-sites
*
* {@see Error::get_error_filename()}
*
* FIXME since {@link Subsite::currentSubsite()} partly relies on Session, viewing other sub-site (including main site) between
* opening ErrorPage in the CMS and publish ErrorPage causes static error page to get generated incorrectly.
*
* @param string $name Filename to write to
* @param int $statusCode Integer error code
*/
public function updateErrorFilename(&$name, $statusCode)
{

// Try to get current subsite from session
$subsite = Subsite::currentSubsite(false);

// since this function is called from Page class before the controller is created, we have to get subsite from domain instead
if (!$subsite) {
$subsiteID = Subsite::getSubsiteIDForDomain();
if ($subsiteID != 0) {
$subsite = DataObject::get_by_id(Subsite::class, $subsiteID);
}
}

// Without subsite, don't rewrite
if ($subsite) {
// Add subdomain to end of filename, just before .html
// This should preserve translatable locale in the filename as well
$subdomain = $subsite->domain();
$name = substr($name, 0, -5) . "-{$subdomain}.html";
}
}

}

0 comments on commit c5f507b

Please sign in to comment.