Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Use the new SingleRecordAdmin class #179

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines -294 to -310
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the admin handle the actions. LeftAndMain automatically adds a save action.
This method exists on DataObject so we don't need to deprecate this.


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);

Comment on lines -329 to -331
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this because we no longer call SiteConfig::current_site_config() in the admin to get the config we want to edit. Instead, this same logic is duplicated there (wtihout the extension hook).

I've checked and nothing in core or supported modules (including fluent and subsites) uses this hook (and according to GitHub nor does anything else in the universe) and I also checked with our in-house development teams who weren't able to find any usage of it in their private repositories either. Seems perfectly safe to remove - but I'll mention it in the changelog to be cover our bases.

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;
}
Comment on lines +459 to +466
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to prevent a delete button from being presented to admin users. No one should be able to delete SiteConfig records by default.


/**
* @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()
))
));
}
}
Loading