2011-12-20 15:57:02 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Controllers;
|
|
|
|
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Session;
|
2016-09-09 01:26:24 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2019-01-14 01:16:30 +01:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-08-24 11:39:25 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\OptionsetField;
|
|
|
|
use SilverStripe\Forms\SelectionGroup;
|
|
|
|
use SilverStripe\Forms\SelectionGroup_Item;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
2016-06-16 06:57:19 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-07-14 01:49:22 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-12-09 04:00:46 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-06-23 01:51:20 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2020-10-22 10:21:15 +02:00
|
|
|
use SilverStripe\SiteConfig\SiteConfig;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
class CMSPageAddController extends CMSPageEditController
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $url_segment = 'pages/add';
|
|
|
|
private static $url_rule = '/$Action/$ID/$OtherID';
|
|
|
|
private static $url_priority = 42;
|
|
|
|
private static $menu_title = 'Add page';
|
|
|
|
private static $required_permission_codes = 'CMS_ACCESS_CMSMain';
|
|
|
|
|
2020-04-19 06:18:01 +02:00
|
|
|
private static $allowed_actions = [
|
2017-01-25 21:59:25 +01:00
|
|
|
'AddForm',
|
|
|
|
'doAdd',
|
|
|
|
'doCancel'
|
2020-04-19 06:18:01 +02:00
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function AddForm()
|
|
|
|
{
|
2020-04-19 06:18:01 +02:00
|
|
|
$pageTypes = [];
|
2019-05-08 06:13:33 +02:00
|
|
|
$defaultIcon = Config::inst()->get(SiteTree::class, 'icon_class');
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
foreach ($this->PageTypes() as $type) {
|
2019-05-08 06:13:33 +02:00
|
|
|
$class = $type->getField('ClassName');
|
|
|
|
$icon = Config::inst()->get($class, 'icon_class') ?: $defaultIcon;
|
|
|
|
|
|
|
|
// If the icon is the SiteTree default and there's some specific icon being provided by `getPageIconURL`
|
|
|
|
// then we don't need to add the icon class. Otherwise the class take precedence.
|
|
|
|
if ($icon === $defaultIcon && !empty(singleton($class)->getPageIconURL())) {
|
|
|
|
$icon = '';
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$html = sprintf(
|
2019-01-14 01:16:30 +01:00
|
|
|
'<span class="page-icon %s class-%s"></span><span class="title">%s</span><span class="form__field-description">%s</span>',
|
2019-05-08 06:13:33 +02:00
|
|
|
$icon,
|
|
|
|
Convert::raw2htmlid($class),
|
2017-01-25 21:59:25 +01:00
|
|
|
$type->getField('AddAction'),
|
|
|
|
$type->getField('Description')
|
|
|
|
);
|
2019-05-08 06:13:33 +02:00
|
|
|
$pageTypes[$class] = DBField::create_field('HTMLFragment', $html);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
// Ensure generic page type shows on top
|
|
|
|
if (isset($pageTypes['Page'])) {
|
|
|
|
$pageTitle = $pageTypes['Page'];
|
2020-04-19 06:18:01 +02:00
|
|
|
$pageTypes = array_merge(['Page' => $pageTitle], $pageTypes);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$numericLabelTmpl = '<span class="step-label"><span class="flyout">Step %d. </span><span class="title">%s</span></span>';
|
|
|
|
|
2017-04-20 03:15:29 +02:00
|
|
|
$topTitle = _t('SilverStripe\\CMS\\Controllers\\CMSPageAddController.ParentMode_top', 'Top level');
|
|
|
|
$childTitle = _t('SilverStripe\\CMS\\Controllers\\CMSPageAddController.ParentMode_child', 'Under another page');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
$fields = new FieldList(
|
|
|
|
$parentModeField = new SelectionGroup(
|
|
|
|
"ParentModeField",
|
2020-04-19 06:18:01 +02:00
|
|
|
[
|
2020-10-22 10:21:15 +02:00
|
|
|
$topField = new SelectionGroup_Item(
|
2017-01-25 21:59:25 +01:00
|
|
|
"top",
|
|
|
|
null,
|
|
|
|
$topTitle
|
|
|
|
),
|
|
|
|
new SelectionGroup_Item(
|
|
|
|
'child',
|
|
|
|
$parentField = new TreeDropdownField(
|
|
|
|
"ParentID",
|
|
|
|
"",
|
2017-09-20 03:51:07 +02:00
|
|
|
SiteTree::class,
|
2017-01-25 21:59:25 +01:00
|
|
|
'ID',
|
|
|
|
'TreeTitle'
|
|
|
|
),
|
|
|
|
$childTitle
|
|
|
|
)
|
2020-04-19 06:18:01 +02:00
|
|
|
]
|
2017-01-25 21:59:25 +01:00
|
|
|
),
|
|
|
|
new LiteralField(
|
|
|
|
'RestrictedNote',
|
|
|
|
sprintf(
|
2019-05-03 05:35:08 +02:00
|
|
|
'<p class="alert alert-info message-restricted">%s</p>',
|
2017-01-25 21:59:25 +01:00
|
|
|
_t(
|
2017-04-20 03:15:29 +02:00
|
|
|
'SilverStripe\\CMS\\Controllers\\CMSMain.AddPageRestriction',
|
2017-01-25 21:59:25 +01:00
|
|
|
'Note: Some page types are not allowed for this selection'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$typeField = new OptionsetField(
|
|
|
|
"PageType",
|
|
|
|
DBField::create_field(
|
|
|
|
'HTMLFragment',
|
2017-04-20 03:15:29 +02:00
|
|
|
sprintf($numericLabelTmpl, 2, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageType', 'Choose page type'))
|
2017-01-25 21:59:25 +01:00
|
|
|
),
|
|
|
|
$pageTypes,
|
|
|
|
'Page'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$parentModeField->setTitle(DBField::create_field(
|
|
|
|
'HTMLFragment',
|
2017-04-20 03:15:29 +02:00
|
|
|
sprintf($numericLabelTmpl, 1, _t('SilverStripe\\CMS\\Controllers\\CMSMain.ChoosePageParentMode', 'Choose where to create this page'))
|
2017-01-25 21:59:25 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
$parentField->setSearchFunction(function ($sourceObject, $labelField, $search) {
|
|
|
|
return DataObject::get($sourceObject)
|
|
|
|
->filterAny([
|
|
|
|
'MenuTitle:PartialMatch' => $search,
|
|
|
|
'Title:PartialMatch' => $search,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO Re-enable search once it allows for HTML title display,
|
|
|
|
// see http://open.silverstripe.org/ticket/7455
|
|
|
|
// $parentField->setShowSearch(true);
|
|
|
|
|
|
|
|
$parentModeField->addExtraClass('parent-mode');
|
|
|
|
|
|
|
|
// CMSMain->currentPageID() automatically sets the homepage,
|
|
|
|
// which we need to counteract in the default selection (which should default to root, ID=0)
|
|
|
|
if ($parentID = $this->getRequest()->getVar('ParentID')) {
|
|
|
|
$parentModeField->setValue('child');
|
|
|
|
$parentField->setValue((int)$parentID);
|
|
|
|
} else {
|
|
|
|
$parentModeField->setValue('top');
|
|
|
|
}
|
|
|
|
|
2020-10-22 10:21:15 +02:00
|
|
|
// Check if the current user has enough permissions to create top level pages
|
|
|
|
// If not, then disable the option to do that
|
|
|
|
if (!SiteConfig::current_site_config()->canCreateTopLevel()) {
|
|
|
|
$topField->setDisabled(true);
|
|
|
|
$parentModeField->setValue('child');
|
|
|
|
}
|
|
|
|
|
2017-01-25 21:59:25 +01:00
|
|
|
$actions = new FieldList(
|
2017-04-20 03:15:29 +02:00
|
|
|
FormAction::create("doAdd", _t('SilverStripe\\CMS\\Controllers\\CMSMain.Create', "Create"))
|
2017-01-25 21:59:25 +01:00
|
|
|
->addExtraClass('btn-primary font-icon-plus-circled')
|
|
|
|
->setUseButtonTag(true),
|
2017-04-20 03:15:29 +02:00
|
|
|
FormAction::create("doCancel", _t('SilverStripe\\CMS\\Controllers\\CMSMain.Cancel', "Cancel"))
|
2017-01-25 21:59:25 +01:00
|
|
|
->addExtraClass('btn-secondary')
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->extend('updatePageOptions', $fields);
|
|
|
|
|
|
|
|
$negotiator = $this->getResponseNegotiator();
|
|
|
|
$form = Form::create(
|
|
|
|
$this,
|
|
|
|
"AddForm",
|
|
|
|
$fields,
|
|
|
|
$actions
|
2017-06-15 03:53:38 +02:00
|
|
|
)->setHTMLID('Form_AddForm')->setStrictFormMethodCheck(false);
|
2017-01-25 21:59:25 +01:00
|
|
|
$form->setAttribute('data-hints', $this->SiteTreeHints());
|
|
|
|
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
|
|
|
|
$form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isAjax() && $negotiator) {
|
|
|
|
$result = $form->forTemplate();
|
2020-04-19 06:18:01 +02:00
|
|
|
return $negotiator->respond($request, [
|
2017-01-25 21:59:25 +01:00
|
|
|
'CurrentForm' => function () use ($result) {
|
|
|
|
return $result;
|
|
|
|
}
|
2020-04-19 06:18:01 +02:00
|
|
|
]);
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
$form->addExtraClass('flexbox-area-grow fill-height cms-add-form cms-content cms-edit-form ' . $this->BaseCSSClasses());
|
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
public function doAdd($data, $form)
|
|
|
|
{
|
|
|
|
$className = isset($data['PageType']) ? $data['PageType'] : "Page";
|
|
|
|
$parentID = isset($data['ParentID']) ? (int)$data['ParentID'] : 0;
|
|
|
|
|
|
|
|
if (!$parentID && isset($data['Parent'])) {
|
|
|
|
$page = SiteTree::get_by_link($data['Parent']);
|
|
|
|
if ($page) {
|
|
|
|
$parentID = $page->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($parentID) && $parentID > 0) {
|
|
|
|
$parentObj = SiteTree::get()->byID($parentID);
|
|
|
|
} else {
|
|
|
|
$parentObj = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$parentObj || !$parentObj->ID) {
|
|
|
|
$parentID = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-19 06:18:01 +02:00
|
|
|
if (!singleton($className)->canCreate(Security::getCurrentUser(), ['Parent' => $parentObj])) {
|
2017-01-25 21:59:25 +01:00
|
|
|
return Security::permissionFailure($this);
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
$record = $this->getNewItem("new-$className-$parentID", false);
|
2017-01-25 21:59:25 +01:00
|
|
|
$this->extend('updateDoAdd', $record, $form);
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$editController = CMSPageEditController::singleton();
|
2017-06-21 06:29:40 +02:00
|
|
|
$editController->setRequest($this->getRequest());
|
2017-01-25 21:59:25 +01:00
|
|
|
$editController->setCurrentPageID($record->ID);
|
|
|
|
|
2017-06-08 08:02:18 +02:00
|
|
|
$session = $this->getRequest()->getSession();
|
|
|
|
$session->set(
|
2017-01-25 21:59:25 +01:00
|
|
|
"FormInfo.Form_EditForm.formError.message",
|
2017-04-20 03:15:29 +02:00
|
|
|
_t('SilverStripe\\CMS\\Controllers\\CMSMain.PageAdded', 'Successfully created page')
|
2017-01-25 21:59:25 +01:00
|
|
|
);
|
2017-06-08 08:02:18 +02:00
|
|
|
$session->set("FormInfo.Form_EditForm.formError.type", 'good');
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
return $this->redirect(Controller::join_links($editController->Link('show'), $record->ID));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doCancel($data, $form)
|
|
|
|
{
|
|
|
|
return $this->redirect(CMSMain::singleton()->Link());
|
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|