2011-12-20 15:57:02 +01:00
|
|
|
<?php
|
2012-03-12 02:16:00 +01:00
|
|
|
class CMSPageAddController extends CMSPageEditController {
|
2011-12-20 15:57:02 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
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';
|
2012-03-09 23:35:43 +01:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $allowed_actions = array(
|
2012-03-12 05:39:45 +01:00
|
|
|
'AddForm',
|
|
|
|
'doAdd',
|
2013-07-04 16:12:25 +02:00
|
|
|
'doCancel'
|
2012-03-09 23:35:43 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
2015-03-11 06:54:08 +01:00
|
|
|
public function AddForm() {
|
2012-03-09 23:35:43 +01:00
|
|
|
$pageTypes = array();
|
|
|
|
foreach($this->PageTypes() as $type) {
|
2012-06-13 09:46:43 +02:00
|
|
|
$html = sprintf('<span class="page-icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>',
|
2013-01-17 19:59:54 +01:00
|
|
|
$type->getField('ClassName'),
|
2012-03-09 23:35:43 +01:00
|
|
|
$type->getField('AddAction'),
|
|
|
|
$type->getField('Description')
|
|
|
|
);
|
|
|
|
$pageTypes[$type->getField('ClassName')] = $html;
|
|
|
|
}
|
|
|
|
// Ensure generic page type shows on top
|
|
|
|
if(isset($pageTypes['Page'])) {
|
|
|
|
$pageTitle = $pageTypes['Page'];
|
|
|
|
$pageTypes = array_merge(array('Page' => $pageTitle), $pageTypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
$numericLabelTmpl = '<span class="step-label"><span class="flyout">%d</span><span class="arrow"></span><span class="title">%s</span></span>';
|
2012-03-10 02:32:50 +01:00
|
|
|
|
|
|
|
$topTitle = _t('CMSPageAddController.ParentMode_top', 'Top level');
|
|
|
|
$childTitle = _t('CMSPageAddController.ParentMode_child', 'Under another page');
|
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
$fields = new FieldList(
|
|
|
|
new LiteralField('PageModeHeader', sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page'))),
|
|
|
|
$parentModeField = new SelectionGroup(
|
|
|
|
"ParentModeField",
|
|
|
|
array(
|
2012-11-30 15:34:10 +01:00
|
|
|
new SelectionGroup_Item(
|
|
|
|
"top",
|
|
|
|
null,
|
|
|
|
$topTitle
|
|
|
|
),
|
|
|
|
new SelectionGroup_Item(
|
|
|
|
'child',
|
|
|
|
$parentField = new TreeDropdownField(
|
2016-03-08 21:50:55 +01:00
|
|
|
"ParentID",
|
2012-11-30 15:34:10 +01:00
|
|
|
"",
|
|
|
|
'SiteTree',
|
|
|
|
'ID',
|
|
|
|
'TreeTitle'
|
|
|
|
),
|
|
|
|
$childTitle
|
2012-03-09 23:35:43 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$typeField = new OptionsetField(
|
2016-03-08 21:50:55 +01:00
|
|
|
"PageType",
|
|
|
|
sprintf($numericLabelTmpl, 2, _t('CMSMain.ChoosePageType', 'Choose page type')),
|
2015-03-11 06:54:08 +01:00
|
|
|
$pageTypes,
|
2012-03-09 23:35:43 +01:00
|
|
|
'Page'
|
2012-12-03 16:33:06 +01:00
|
|
|
),
|
|
|
|
new LiteralField(
|
|
|
|
'RestrictedNote',
|
|
|
|
sprintf(
|
|
|
|
'<p class="message notice message-restricted">%s</p>',
|
|
|
|
_t(
|
2016-03-08 21:50:55 +01:00
|
|
|
'CMSMain.AddPageRestriction',
|
2012-12-03 16:33:06 +01:00
|
|
|
'Note: Some page types are not allowed for this selection'
|
2015-03-11 06:54:08 +01:00
|
|
|
)
|
2012-12-03 16:33:06 +01:00
|
|
|
)
|
2012-03-09 23:35:43 +01:00
|
|
|
)
|
|
|
|
);
|
2013-08-29 17:08:25 +02:00
|
|
|
$parentField->setSearchFunction(function ($sourceObject, $labelField, $search) {
|
|
|
|
return DataObject::get(
|
2016-03-08 21:50:55 +01:00
|
|
|
$sourceObject,
|
2013-08-29 17:08:25 +02:00
|
|
|
sprintf(
|
|
|
|
"\"MenuTitle\" LIKE '%%%s%%' OR \"Title\" LIKE '%%%s%%'",
|
|
|
|
Convert::raw2sql($search),
|
|
|
|
Convert::raw2sql($search)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2012-09-04 01:40:41 +02:00
|
|
|
|
2016-03-08 21:50:55 +01:00
|
|
|
// TODO Re-enable search once it allows for HTML title display,
|
2012-06-13 16:11:14 +02:00
|
|
|
// see http://open.silverstripe.org/ticket/7455
|
|
|
|
// $parentField->setShowSearch(true);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-03-12 10:54:14 +01:00
|
|
|
$parentModeField->addExtraClass('parent-mode');
|
2012-03-09 23:35:43 +01:00
|
|
|
|
|
|
|
// CMSMain->currentPageID() automatically sets the homepage,
|
|
|
|
// which we need to counteract in the default selection (which should default to root, ID=0)
|
2015-04-30 01:04:02 +02:00
|
|
|
if($parentID = $this->getRequest()->getVar('ParentID')) {
|
2012-09-04 01:40:41 +02:00
|
|
|
$parentModeField->setValue('child');
|
|
|
|
$parentField->setValue((int)$parentID);
|
|
|
|
} else {
|
|
|
|
$parentModeField->setValue('top');
|
2012-03-09 23:35:43 +01:00
|
|
|
}
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
$actions = new FieldList(
|
|
|
|
FormAction::create("doAdd", _t('CMSMain.Create',"Create"))
|
|
|
|
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
|
2013-07-04 16:12:25 +02:00
|
|
|
->setUseButtonTag(true),
|
|
|
|
FormAction::create("doCancel", _t('CMSMain.Cancel',"Cancel"))
|
2013-11-11 13:31:32 +01:00
|
|
|
->addExtraClass('ss-ui-action-destructive ss-ui-action-cancel')
|
2012-03-09 23:35:43 +01:00
|
|
|
->setUseButtonTag(true)
|
|
|
|
);
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
$this->extend('updatePageOptions', $fields);
|
2016-03-01 04:14:35 +01:00
|
|
|
|
|
|
|
$negotiator = $this->getResponseNegotiator();
|
|
|
|
$form = Form::create(
|
2013-05-10 15:00:57 +02:00
|
|
|
$this, "AddForm", $fields, $actions
|
|
|
|
)->setHTMLID('Form_AddForm');
|
2015-03-11 06:54:08 +01:00
|
|
|
$form->setAttribute('data-hints', $this->SiteTreeHints());
|
|
|
|
$form->setAttribute('data-childfilter', $this->Link('childfilter'));
|
2016-03-01 04:14:35 +01:00
|
|
|
$form->setValidationResponseCallback(function() use ($negotiator, $form) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
if($request->isAjax() && $negotiator) {
|
|
|
|
$this->setupFormErrors();
|
|
|
|
$result = $this->forTemplate();
|
2015-03-11 06:54:08 +01:00
|
|
|
|
2016-03-01 04:14:35 +01:00
|
|
|
return $negotiator->respond($request, array(
|
|
|
|
'CurrentForm' => function() use($result) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
});
|
2012-03-09 23:35:43 +01:00
|
|
|
$form->addExtraClass('cms-add-form stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses());
|
2011-12-20 15:57:02 +01:00
|
|
|
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
|
2012-03-09 23:35:43 +01:00
|
|
|
|
2011-12-20 15:57:02 +01:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
public function doAdd($data, $form) {
|
|
|
|
$className = isset($data['PageType']) ? $data['PageType'] : "Page";
|
|
|
|
$parentID = isset($data['ParentID']) ? (int)$data['ParentID'] : 0;
|
|
|
|
|
|
|
|
$suffix = isset($data['Suffix']) ? "-" . $data['Suffix'] : null;
|
|
|
|
|
|
|
|
if(!$parentID && isset($data['Parent'])) {
|
2013-08-29 03:59:45 +02:00
|
|
|
$page = SiteTree::get_by_link($data['Parent']);
|
2012-03-09 23:35:43 +01:00
|
|
|
if($page) $parentID = $page->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_numeric($parentID) && $parentID > 0) $parentObj = DataObject::get_by_id("SiteTree", $parentID);
|
|
|
|
else $parentObj = null;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
if(!$parentObj || !$parentObj->ID) $parentID = 0;
|
|
|
|
|
2015-03-11 06:54:08 +01:00
|
|
|
if(!singleton($className)->canCreate(Member::currentUser(), array('Parent' => $parentObj))) {
|
|
|
|
return Security::permissionFailure($this);
|
2012-03-09 23:35:43 +01:00
|
|
|
}
|
2012-06-11 15:15:18 +02:00
|
|
|
|
2012-03-09 23:35:43 +01:00
|
|
|
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
|
2016-03-03 23:38:37 +01:00
|
|
|
$this->extend('updateDoAdd', $record, $form);
|
2012-06-11 15:15:18 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$record->write();
|
|
|
|
} catch(ValidationException $ex) {
|
|
|
|
$form->sessionMessage($ex->getResult()->message(), 'bad');
|
2015-04-30 01:04:02 +02:00
|
|
|
return $this->getResponseNegotiator()->respond($this->getRequest());
|
2012-06-11 15:15:18 +02:00
|
|
|
}
|
|
|
|
|
2012-03-12 06:32:25 +01:00
|
|
|
$editController = singleton('CMSPageEditController');
|
|
|
|
$editController->setCurrentPageID($record->ID);
|
2012-06-04 09:57:40 +02:00
|
|
|
|
|
|
|
Session::set(
|
2016-03-08 21:50:55 +01:00
|
|
|
"FormInfo.Form_EditForm.formError.message",
|
2012-06-04 09:57:40 +02:00
|
|
|
_t('CMSMain.PageAdded', 'Successfully created page')
|
|
|
|
);
|
|
|
|
Session::set("FormInfo.Form_EditForm.formError.type", 'good');
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-03-12 13:45:10 +01:00
|
|
|
return $this->redirect(Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID));
|
2011-12-20 15:57:02 +01:00
|
|
|
}
|
|
|
|
|
2013-07-04 16:12:25 +02:00
|
|
|
public function doCancel($data, $form) {
|
|
|
|
return $this->redirect(singleton('CMSMain')->Link());
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|