silverstripe-blog/forms/gridfield/GridFieldSiteTreeAddNewButton.php

117 lines
3.3 KiB
PHP
Raw Normal View History

2013-07-21 12:23:35 +02:00
<?php
/**
* This component provides a button for opening the add new form provided by
* {@link GridFieldDetailForm}.
*
* Only returns a button if {@link DataObject->canCreate()} for this record
* returns true.
*
* @package framework
* @subpackage fields-gridfield
*/
2013-08-04 18:38:26 +02:00
class GridFieldSiteTreeAddNewButton extends GridFieldAddNewButton
implements GridField_ActionProvider {
2013-07-21 12:23:35 +02:00
public function getHTMLFragments($gridField) {
$singleton = singleton($gridField->getModelClass());
if(!$singleton->canCreate()) {
return array();
}
2013-08-04 18:38:26 +02:00
$parent = SiteTree::get()->byId(Controller::curr()->currentPageID());
2013-07-21 12:23:35 +02:00
if(!$this->buttonName) {
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
$objectName = $singleton->i18n_singular_name();
$this->buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName));
}
2013-08-04 18:38:26 +02:00
$state = $gridField->State->GridFieldSiteTreeAddNewButton;
$state->currentPageID = $parent->ID;
$state->pageType = $parent->defaultChild();
$addAction = new GridField_FormAction($gridField, 'add',
sprintf(_t('GridField.ADD', "Add %s"), $singleton->i18n_singular_name()), 'add', 'add');
$addAction->setAttribute('data-icon', 'add')->addExtraClass("no-ajax");
$allowedChildren = $parent->allowedChildren();
$children = array();
foreach($allowedChildren as $class) {
$children[$class] = singleton($class)->i18n_singular_name();
}
$pageTypes = DropdownField::create(
"PageType",
"Page Type",
$children,
$singleton->defaultChild()
);
$pageTypes->setFieldHolderTemplate("BlogDropdownField_holder")
->addExtraClass("gridfield-dropdown");
$forTemplate = new ArrayData(array());
$forTemplate->Fields = new ArrayList();
$forTemplate->Fields->push($pageTypes);
$forTemplate->Fields->push($addAction);
Requirements::css(blog_dir() . "/css/cms.css");
Requirements::javascript(blog_dir() . "/javascript/GridField.js");
2013-07-21 12:23:35 +02:00
return array(
2013-08-04 18:38:26 +02:00
$this->targetFragment => $forTemplate->renderWith("GridFieldSiteTreeAddNewButton")
2013-07-21 12:23:35 +02:00
);
}
2013-08-04 18:38:26 +02:00
/**
* Provide actions to this component.
*
* @param $gridField GridField
*
* @return array
**/
public function getActions($gridField) {
return array("add");
}
/**
* Handles the add action, but only acts as a wrapper for {@link CMSPageAddController::doAdd()}
*
* @param $gridFIeld GridFIeld
* @param $actionName string
* @param $arguments mixed
* @param $data array
**/
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName == "add") {
$tmpData = json_decode($data['BlogPost']['GridState'], true);
$tmpData = $tmpData['GridFieldSiteTreeAddNewButton'];
$data = array(
"ParentID" => $tmpData['currentPageID'],
"PageType" => $tmpData['pageType']
);
$controller = Injector::inst()->create("CMSPageAddController");
$form = $controller->AddForm();
$form->loadDataFrom($data);
$controller->doAdd($data, $form);
$response = $controller->getResponseNegotiator()->getResponse();
// Get the current record
$record = SiteTree::get()->byId($controller->currentPageID());
if($record) {
$response->redirect(Director::absoluteBaseURL() . $record->CMSEditLink(), 301);
}
return $response;
}
}
2013-07-21 12:23:35 +02:00
}