Skip to content

Commit

Permalink
API Use the new SingleRecordAdmin class
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 6, 2024
1 parent 3f9f53c commit 0aa9555
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 193 deletions.
45 changes: 10 additions & 35 deletions code/SiteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace SilverStripe\SiteConfig;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\ListboxField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
Expand All @@ -21,7 +18,6 @@
use SilverStripe\Security\PermissionProvider;
use SilverStripe\Security\Security;
use SilverStripe\View\TemplateGlobalProvider;
use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\Forms\SearchableMultiDropdownField;
use SilverStripe\Security\InheritedPermissions;

Expand Down Expand Up @@ -284,31 +280,6 @@ public function getCMSFields()
return $fields;
}

/**
* Get the actions that are sent to the CMS.
*
* In your extensions: updateEditFormActions($actions)
*
* @return FieldList
*/
public function getCMSActions()
{
if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) {
$actions = FieldList::create(
FormAction::create(
'save_siteconfig',
_t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVE', 'Save')
)->addExtraClass('btn-primary font-icon-save')
);
} else {
$actions = FieldList::create();
}

$this->extend('updateCMSActions', $actions);

return $actions;
}

public function CMSEditLink(): ?string
{
return SiteConfigLeftAndMain::singleton()->Link();
Expand All @@ -317,18 +288,13 @@ public function CMSEditLink(): ?string
/**
* Get the current sites SiteConfig, and creates a new one through
* {@link make_site_config()} if none is found.
*
* @return SiteConfig
*/
public static function current_site_config()
public static function current_site_config(): SiteConfig
{
$siteConfig = DataObject::get_one(SiteConfig::class);
if (!$siteConfig) {
$siteConfig = SiteConfig::make_site_config();
}

static::singleton()->extend('updateCurrentSiteConfig', $siteConfig);

return $siteConfig;
}

Expand Down Expand Up @@ -490,6 +456,15 @@ public function canEdit($member = null)
return Permission::checkMember($member, "EDIT_SITECONFIG");
}

public function canDelete($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}
return false;
}

/**
* @return array
*/
Expand Down
169 changes: 11 additions & 158 deletions code/SiteConfigLeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,179 +2,32 @@

namespace SilverStripe\SiteConfig;

use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Admin\SingleRecordAdmin;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Director;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Model\List\ArrayList;
use SilverStripe\ORM\CMSPreviewable;
use SilverStripe\ORM\DataObject;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Versioned\RecursivePublishable;
use SilverStripe\Model\ArrayData;
use SilverStripe\View\Requirements;

class SiteConfigLeftAndMain extends LeftAndMain
class SiteConfigLeftAndMain extends SingleRecordAdmin
{
/**
* @var string
*/
private static $url_segment = 'settings';
private static string $url_segment = 'settings';

/**
* @var string
*/
private static $url_rule = '/$Action/$ID/$OtherID';
private static int $menu_priority = -1;

/**
* @var int
*/
private static $menu_priority = -1;
private static string $menu_title = 'Settings';

/**
* @var string
*/
private static $menu_title = 'Settings';
private static string $menu_icon_class = 'font-icon-cog';

/**
* @var string
*/
private static $menu_icon_class = 'font-icon-cog';
private static string $model_class = SiteConfig::class;

/**
* @var string
* @deprecated 5.4.0 Will be renamed to model_class
*/
private static $tree_class = SiteConfig::class;
private static array $required_permission_codes = [
'EDIT_SITECONFIG',
];

/**
* @var array
*/
private static $required_permission_codes = array('EDIT_SITECONFIG');

/**
* Initialises the {@link SiteConfig} controller.
*/
public function init()
{
parent::init();
// Add JS required for some aspects of the access tab
if (class_exists(SiteTree::class)) {
Requirements::javascript('silverstripe/cms: client/dist/js/bundle.js');
}
}

/**
* @param null $id Not used.
* @param null $fields Not used.
*
* @return Form
*/
public function getEditForm($id = null, $fields = null)
{
$siteConfig = SiteConfig::current_site_config();
$fields = $siteConfig->getCMSFields();

// Tell the CMS what URL the preview should show
$home = Director::absoluteBaseURL();
$fields->push(new HiddenField('PreviewURL', 'Preview URL', $home));

if ($siteConfig instanceof CMSPreviewable || $siteConfig->has_extension(CMSPreviewable::class)) {
// Added in-line to the form, but plucked into different view by LeftAndMain.Preview.js upon load
$fields->push($navField = new LiteralField(
'SilverStripeNavigator',
$this->getSilverStripeNavigator($siteConfig)
));
$navField->setAllowHTML(true);
}

$validator = $siteConfig->getCMSCompositeValidator();

$actions = $siteConfig->getCMSActions();
$negotiator = $this->getResponseNegotiator();
$form = Form::create(
$this,
'EditForm',
$fields,
$actions,
$validator
)->setHTMLID('Form_EditForm');
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
$request = $this->getRequest();
if ($request->isAjax() && $negotiator) {
$result = $form->forTemplate();
return $negotiator->respond($request, array(
'CurrentForm' => function () use ($result) {
return $result;
}
));
}
});
$form->addExtraClass('flexbox-area-grow fill-height cms-content cms-edit-form');
$form->setAttribute('data-pjax-fragment', 'CurrentForm');

if ($form->Fields()->hasTabSet()) {
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
}
$form->setHTMLID('Form_EditForm');
$form->loadDataFrom($siteConfig);
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));


// Announce the capability so the frontend can decide whether to allow preview or not.
if ($siteConfig instanceof CMSPreviewable || $siteConfig->has_extension(CMSPreviewable::class)) {
$form->addExtraClass('cms-previewable');
}

// Use <button> to allow full jQuery UI styling
$actions = $actions->dataFields();
if ($actions) {
/** @var FormAction $action */
foreach ($actions as $action) {
$action->setUseButtonTag(true);
}
}

$this->extend('updateEditForm', $form);

return $form;
}

/**
* Save the current sites {@link SiteConfig} into the database.
*
* @param array $data
* @param Form $form
* @return String
* @deprecated 5.4.0 Will be replaced with save()
*/
public function save_siteconfig($data, $form)
{
Deprecation::noticeWithNoReplacment('5.4.0', 'Will be replaced with save()');
$data = $form->getData();
$siteConfig = DataObject::get_by_id(SiteConfig::class, $data['ID']);
$form->saveInto($siteConfig);
$siteConfig->write();
if ($siteConfig->hasExtension(RecursivePublishable::class)) {
$siteConfig->publishRecursive();
}
$this->response->addHeader(
'X-Status',
rawurlencode(_t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'))
);
return $form->forTemplate();
}

public function Breadcrumbs($unlinked = false)
{
return new ArrayList(array(
new ArrayData(array(
'Title' => static::menu_title(),
'Link' => $this->Link()
))
));
}
}

0 comments on commit 0aa9555

Please sign in to comment.