Skip to content

Commit

Permalink
API Make SubsiteXHRController a subclass of AdminController
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 12, 2024
1 parent ada2901 commit 18c5cd8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
41 changes: 17 additions & 24 deletions src/Controller/SubsiteXHRController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

namespace SilverStripe\Subsites\Controller;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Admin\AdminController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\PjaxResponseNegotiator;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Subsites\Model\Subsite;

/**
* Section-agnostic PJAX controller.
* Section-agnostic PJAX controller that renders the subsites swapper dropdown
*/
class SubsiteXHRController extends LeftAndMain
class SubsiteXHRController extends AdminController
{
private static $url_segment = 'subsite_xhr';

private static $ignore_menuitem = true;
public function index(HTTPRequest $request): HTTPResponse
{
return $this->getResponseNegotiator()->respond($request);
}

/**
* Relax the access permissions, so anyone who has access to any CMS subsite can access this controller.
Expand All @@ -36,33 +41,21 @@ public function canView($member = null)
}

/**
* Allow access if user allowed into the CMS at all.
* Get a Pjax response negotiator for the subsite list
*/
public function canAccess()
{
// Allow if any cms access is available
return Permission::check([
'CMS_ACCESS', // Supported by 3.1.14 and up
'CMS_ACCESS_LeftAndMain'
]);
}

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

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

return $negotiator;
return new PjaxResponseNegotiator([
'SubsiteList' => function () {
return $this->SubsiteList();
},
]);
}

/**
* Provide the list of available subsites as a cms-section-agnostic PJAX handler.
*/
public function SubsiteList()
public function SubsiteList(): DBHTMLText
{
return $this->renderWith(['type' => 'Includes', SubsiteXHRController::class . '_subsitelist']);
}
Expand Down
71 changes: 39 additions & 32 deletions src/Extensions/LeftAndMainSubsites.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
use SilverStripe\Subsites\Model\Subsite;
use SilverStripe\Subsites\State\SubsiteState;
use SilverStripe\Model\ArrayData;
use SilverStripe\Model\List\SS_List;
use SilverStripe\View\Requirements;
use SilverStripe\View\TemplateGlobalProvider;

/**
* Decorator designed to add subsites support to LeftAndMain
*/
class LeftAndMainSubsites extends Extension
class LeftAndMainSubsites extends Extension implements TemplateGlobalProvider
{
private static $allowed_actions = ['CopyToSubsite'];

Expand All @@ -45,6 +47,42 @@ protected function onInit()
Requirements::javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js');
}

public static function get_template_global_variables()
{
return [
'SubsiteSwitchList',
];
}

/**
* Generates a list of subsites with the data needed to
* produce a dropdown site switcher
* @return SS_List<Subsite>
*/
public static function SubsiteSwitchList(): SS_List
{
$list = Subsite::all_accessible_sites();
$currentSubsiteID = SubsiteState::singleton()->getSubsiteId();

if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) {
return false;
}

$output = ArrayList::create();

foreach ($list as $subsite) {
$currentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';

$output->push(ArrayData::create([
'CurrentState' => $currentState,
'ID' => $subsite->ID,
'Title' => $subsite->Title,
]));
}

return $output;
}

/**
* Set the title of the CMS tree
*/
Expand Down Expand Up @@ -139,37 +177,6 @@ public function Subsites()
return Subsite::all_accessible_sites();
}

/**
* Generates a list of subsites with the data needed to
* produce a dropdown site switcher
* @return ArrayList<Subsite>
*/
public function ListSubsites()
{
$list = $this->Subsites();
$currentSubsiteID = SubsiteState::singleton()->getSubsiteId();

if ($list == null || $list->count() == 1 && $list->first()->DefaultSite == true) {
return false;
}

Requirements::javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js');

$output = ArrayList::create();

foreach ($list as $subsite) {
$currentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';

$output->push(ArrayData::create([
'CurrentState' => $currentState,
'ID' => $subsite->ID,
'Title' => $subsite->Title,
]));
}

return $output;
}

public function alternateMenuDisplayCheck($controllerName)
{
if (!class_exists($controllerName ?? '')) {
Expand Down
3 changes: 2 additions & 1 deletion templates/SilverStripe/Admin/LeftAndMain_Menu.ss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<% include SilverStripe\\Admin\\LeftAndMain_MenuLogo %>
<% include SilverStripe\\Admin\\LeftAndMain_MenuStatus %>

<% if $ListSubsites.Count > 1 %>
<% if $SubsiteSwitchList.Count > 1 %>
<% require javascript('silverstripe/subsites:client/dist/js/LeftAndMain_Subsites.js') %>
<% include SilverStripe\\Subsites\\Controller\\SubsiteXHRController_subsitelist %>
<% end_if %>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="cms-subsites" data-pjax-fragment="SubsiteList">
<div class="field dropdown">
<select id="SubsitesSelect">
<% loop $ListSubsites %>
<% loop $SubsiteSwitchList %>
<option value="$ID" $CurrentState>$Title</option>
<% end_loop %>
</select>
Expand Down

0 comments on commit 18c5cd8

Please sign in to comment.