MINOR Flattened AddForm and SiteConfig CMS logic, moved from base classes to specialised classes for better overview, and less inheritance where its not necessary.

This commit is contained in:
Ingo Schommer 2012-03-09 23:35:43 +01:00
parent 04a195b111
commit 4225c0a966
5 changed files with 151 additions and 160 deletions

View File

@ -328,21 +328,23 @@ JS
} }
public function AddForm() { public function AddForm() {
$form = parent::AddForm();
$folder = singleton('Folder'); $folder = singleton('Folder');
$form = new Form(
$form->Actions()->fieldByName('action_doAdd') $this,
->setTitle(_t('AssetAdmin.ActionAdd', 'Add folder')) 'AddForm',
->setAttribute('data-icon', 'accept'); new FieldList(
new TextField("Name", _t('File.Name')),
$fields = $folder->getCMSFields(); new HiddenField('ParentID', false, $this->request->getVar('ParentID'))
$fields->replaceField('Name', new TextField("Name", _t('File.Name'))); ),
$fields->dataFieldByName('ParentID')->setValue($this->request->getVar('ParentID')); new FieldList(
$form->setFields($fields); FormAction::create('doAdd', _t('AssetAdmin_left.ss.GO','Go'))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
->setTitle(_t('AssetAdmin.ActionAdd', 'Add folder'))
)
);
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// TODO Can't merge $FormAttributes in template at the moment // TODO Can't merge $FormAttributes in template at the moment
$form->addExtraClass('cms-add-form cms-edit-form cms-panel-padded center ' . $this->BaseCSSClasses()); $form->addExtraClass('add-form cms-add-form cms-edit-form cms-panel-padded center ' . $this->BaseCSSClasses());
return $form; return $form;
} }

View File

@ -28,7 +28,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
static $subitem_class = "Member"; static $subitem_class = "Member";
static $allowed_actions = array( static $allowed_actions = array(
'addpage',
'buildbrokenlinks', 'buildbrokenlinks',
'deleteitems', 'deleteitems',
'DeleteItemsForm', 'DeleteItemsForm',
@ -40,7 +39,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'PublishItemsForm', 'PublishItemsForm',
'submit', 'submit',
'EditForm', 'EditForm',
'AddForm',
'SearchForm', 'SearchForm',
'SiteTreeAsUL', 'SiteTreeAsUL',
'getshowdeletedsubtree', 'getshowdeletedsubtree',
@ -372,23 +370,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return $result; return $result;
} }
/**
* Save the current sites {@link SiteConfig} into the database
*
* @param array $data
* @param Form $form
* @return FormResponse
*/
function save_siteconfig($data, $form) {
$siteConfig = SiteConfig::current_site_config();
$form->saveInto($siteConfig);
$siteConfig->write();
$this->response->addHeader('X-Status', _t('LeftAndMain.SAVEDUP'));
return $form->forTemplate();
}
/** /**
* Get a database record to be managed by the CMS. * Get a database record to be managed by the CMS.
* *
@ -618,51 +599,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return $form->forTemplate(); return $form->forTemplate();
} }
public function doAdd($data, $form) {
$className = isset($data['PageType']) ? $data['PageType'] : "Page";
$parentMode = isset($data['ParentModeField']) ? $data['ParentModeField'] : "top";
$parentID = isset($data['ParentID']) ? (int)$data['ParentID'] : 0;
if ($parentMode == "top") $parentID = 0;
$suffix = isset($data['Suffix']) ? "-" . $data['Suffix'] : null;
if(!$parentID && isset($data['Parent'])) {
$page = SiteTree:: get_by_link(Convert::raw2sql($data['Parent']));
if($page) $parentID = $page->ID;
}
if(is_numeric($parentID) && $parentID > 0) $parentObj = DataObject::get_by_id("SiteTree", $parentID);
else $parentObj = null;
if(!$parentObj || !$parentObj->ID) $parentID = 0;
if($parentObj) {
if(!$parentObj->canAddChildren()) return Security::permissionFailure($this);
if(!singleton($className)->canCreate()) return Security::permissionFailure($this);
} else {
if(!SiteConfig::current_site_config()->canCreateTopLevel())
return Security::permissionFailure($this);
}
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
if(class_exists('Translatable') && $record->hasExtension('Translatable')) $record->Locale = $data['Locale'];
$record->write();
$form = $this->getEditForm($record->ID);
$link = Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID);
$this->getResponse()->addHeader('X-ControllerURL', $link);
if(isset($data['returnID'])) {
return $record->ID;
} else if(Director::is_ajax()) {
$form = $this->getEditForm($record->ID);
return $form->forTemplate();
} else {
return $this->redirect($link);
}
}
/** /**
* @uses LeftAndMainExtension->augmentNewSiteTreeItem() * @uses LeftAndMainExtension->augmentNewSiteTreeItem()
@ -938,77 +875,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
} }
} }
/**
* @return Form
*/
function AddForm() {
$record = $this->currentPage();
$pageTypes = array();
foreach($this->PageTypes() as $type) {
$html = sprintf('<span class="icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>',
$type->getField('ClassName'),
$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>';
$fields = new FieldList(
// new HiddenField("ParentID", false, ($this->parentRecord) ? $this->parentRecord->ID : null),
// TODO Should be part of the form attribute, but not possible in current form API
$hintsField = new LiteralField('Hints', sprintf('<span class="hints" data-hints="%s"></span>', $this->SiteTreeHints())),
new LiteralField('PageModeHeader', sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page'))),
$parentModeField = new SelectionGroup(
"ParentModeField",
array(
"top//Top level" => null, //new LiteralField("Dummy", ''),
"child//Under another page" => $parentField = new TreeDropdownField(
"ParentID",
"",
'SiteTree'
)
)
),
$typeField = new OptionsetField(
"PageType",
sprintf($numericLabelTmpl, 2, _t('CMSMain.ChoosePageType', 'Choose page type')),
$pageTypes,
'Page'
)
);
$parentField->setShowSearch(true);
$parentModeField->setValue("child");
// CMSMain->currentPageID() automatically sets the homepage,
// which we need to counteract in the default selection (which should default to root, ID=0)
$homepageSegment = RootURLController::get_homepage_link();
if($record && $record->URLSegment != $homepageSegment) {
$parentField->setValue($record->ID);
}
$actions = new FieldList(
// $resetAction = new ResetFormAction('doCancel', _t('CMSMain.Cancel', 'Cancel')),
FormAction::create("doAdd", _t('CMSMain.Create',"Create"))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
->setUseButtonTag(true)
);
$this->extend('updatePageOptions', $fields);
$form = new Form($this, "AddForm", $fields, $actions);
$form->addExtraClass('cms-add-form stacked');
return $form;
}
function publishall($request) { function publishall($request) {
if(!Permission::check('ADMIN')) return Security::permissionFailure($this); if(!Permission::check('ADMIN')) return Security::permissionFailure($this);

View File

@ -7,11 +7,79 @@ class CMSPageAddController extends CMSMain {
static $menu_title = 'Add page'; static $menu_title = 'Add page';
static $required_permission_codes = 'CMS_ACCESS_CMSMain'; static $required_permission_codes = 'CMS_ACCESS_CMSMain';
function AddForm() { static $allowed_actions = array(
$form = parent::AddForm(); 'AddForm'
);
$form->addExtraClass('cms-content center cms-edit-form ' . $this->BaseCSSClasses()); /**
* @return Form
*/
function AddForm() {
$record = $this->currentPage();
$pageTypes = array();
foreach($this->PageTypes() as $type) {
$html = sprintf('<span class="icon class-%s"></span><strong class="title">%s</strong><span class="description">%s</span>',
$type->getField('ClassName'),
$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>';
$fields = new FieldList(
// new HiddenField("ParentID", false, ($this->parentRecord) ? $this->parentRecord->ID : null),
// TODO Should be part of the form attribute, but not possible in current form API
$hintsField = new LiteralField('Hints', sprintf('<span class="hints" data-hints="%s"></span>', $this->SiteTreeHints())),
new LiteralField('PageModeHeader', sprintf($numericLabelTmpl, 1, _t('CMSMain.ChoosePageParentMode', 'Choose where to create this page'))),
$parentModeField = new SelectionGroup(
"ParentModeField",
array(
"top//Top level" => null, //new LiteralField("Dummy", ''),
"child//Under another page" => $parentField = new TreeDropdownField(
"ParentID",
"",
'SiteTree'
)
)
),
$typeField = new OptionsetField(
"PageType",
sprintf($numericLabelTmpl, 2, _t('CMSMain.ChoosePageType', 'Choose page type')),
$pageTypes,
'Page'
)
);
$parentField->setShowSearch(true);
$parentModeField->setValue("child");
// CMSMain->currentPageID() automatically sets the homepage,
// which we need to counteract in the default selection (which should default to root, ID=0)
$homepageSegment = RootURLController::get_homepage_link();
if($record && $record->URLSegment != $homepageSegment) {
$parentField->setValue($record->ID);
}
$actions = new FieldList(
// $resetAction = new ResetFormAction('doCancel', _t('CMSMain.Cancel', 'Cancel')),
FormAction::create("doAdd", _t('CMSMain.Create',"Create"))
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept')
->setUseButtonTag(true)
);
$this->extend('updatePageOptions', $fields);
$form = new Form($this, "AddForm", $fields, $actions);
$form->addExtraClass('cms-add-form stacked cms-content center cms-edit-form ' . $this->BaseCSSClasses());
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
if($parentID = $this->request->getVar('ParentID')) { if($parentID = $this->request->getVar('ParentID')) {
$form->Fields()->dataFieldByName('ParentID')->setValue((int)$parentID); $form->Fields()->dataFieldByName('ParentID')->setValue((int)$parentID);
} }
@ -19,10 +87,48 @@ class CMSPageAddController extends CMSMain {
return $form; return $form;
} }
function doAdd($data, $form) { public function doAdd($data, $form) {
$return = parent::doAdd($data, $form); $className = isset($data['PageType']) ? $data['PageType'] : "Page";
$this->getResponse()->addHeader('X-Controller', 'CMSPageEditController'); $parentMode = isset($data['ParentModeField']) ? $data['ParentModeField'] : "top";
return $return; $parentID = isset($data['ParentID']) ? (int)$data['ParentID'] : 0;
if ($parentMode == "top") $parentID = 0;
$suffix = isset($data['Suffix']) ? "-" . $data['Suffix'] : null;
if(!$parentID && isset($data['Parent'])) {
$page = SiteTree:: get_by_link(Convert::raw2sql($data['Parent']));
if($page) $parentID = $page->ID;
}
if(is_numeric($parentID) && $parentID > 0) $parentObj = DataObject::get_by_id("SiteTree", $parentID);
else $parentObj = null;
if(!$parentObj || !$parentObj->ID) $parentID = 0;
if($parentObj) {
if(!$parentObj->canAddChildren()) return Security::permissionFailure($this);
if(!singleton($className)->canCreate()) return Security::permissionFailure($this);
} else {
if(!SiteConfig::current_site_config()->canCreateTopLevel())
return Security::permissionFailure($this);
}
$record = $this->getNewItem("new-$className-$parentID".$suffix, false);
if(class_exists('Translatable') && $record->hasExtension('Translatable')) $record->Locale = $data['Locale'];
$record->write();
$form = $this->getEditForm($record->ID);
$link = Controller::join_links(singleton('CMSPageEditController')->Link('show'), $record->ID);
$this->getResponse()->addHeader('X-ControllerURL', $link);
if(Director::is_ajax()) {
$form = $this->getEditForm($record->ID);
return $form->forTemplate();
} else {
return $this->redirect($link);
}
} }
} }

View File

@ -33,6 +33,23 @@ class CMSSettingsController extends CMSMain {
return $form; return $form;
} }
/**
* Save the current sites {@link SiteConfig} into the database
*
* @param array $data
* @param Form $form
* @return String
*/
function save_siteconfig($data, $form) {
$siteConfig = SiteConfig::current_site_config();
$form->saveInto($siteConfig);
$siteConfig->write();
$this->response->addHeader('X-Status', _t('LeftAndMain.SAVEDUP'));
return $form->forTemplate();
}
function PreviewLink() { function PreviewLink() {
return false; return false;
} }

View File

@ -198,9 +198,9 @@ class CMSMainTest extends FunctionalTest {
// with insufficient permissions // with insufficient permissions
$cmsUser->logIn(); $cmsUser->logIn();
$this->get('admin/pages'); $this->get('admin/page/add');
$response = $this->post( $response = $this->post(
'admin/pages/AddForm', 'admin/page/add/AddForm',
array('ParentID' => '0', 'ClassName' => 'Page', 'Locale' => 'en_US', 'action_doAdd' => 1) array('ParentID' => '0', 'ClassName' => 'Page', 'Locale' => 'en_US', 'action_doAdd' => 1)
); );
// should redirect, which is a permission error // should redirect, which is a permission error
@ -208,10 +208,10 @@ class CMSMainTest extends FunctionalTest {
// with correct permissions // with correct permissions
$rootEditUser->logIn(); $rootEditUser->logIn();
$response = $this->get('admin/pages/'); $response = $this->get('admin/page/add');
$response = $this->post( $response = $this->post(
'admin/pages/AddForm', 'admin/page/add/AddForm',
array('ParentID' => '0', 'ClassName' => 'Page', 'Locale' => 'en_US', 'action_doAdd' => 1) array('ParentID' => '0', 'ClassName' => 'Page', 'Locale' => 'en_US', 'action_doAdd' => 1)
); );