silverstripe-blog/forms/gridfield/GridFieldBlogPostAddNewButton.php

126 lines
3.5 KiB
PHP
Raw Normal View History

2013-07-21 12:23:35 +02:00
<?php
2013-07-21 12:23:35 +02:00
/**
* 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
*
* @author Michael Strong <github@michaelstrong.co.uk>
2013-07-21 12:23:35 +02:00
*/
class GridFieldBlogPostAddNewButton extends GridFieldAddNewButton
2013-08-04 18:38:26 +02:00
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('GridFieldSiteTreeAddNewButton.Add', 'Add {name}', "Add button text", array('name' => $singleton->i18n_singular_name()));
2013-07-21 12:23:35 +02:00
}
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',
$this->buttonName,
'add',
'add'
);
2013-08-04 18:38:26 +02:00
$addAction->setAttribute('data-icon', 'add')->addExtraClass("no-ajax");
$allowedChildren = $parent->allowedChildren();
$children = array();
foreach($allowedChildren as $class) {
if(!Config::inst()->get($class, "show_in_sitetree")) {
$children[$class] = singleton($class)->i18n_singular_name();
}
2013-08-04 18:38:26 +02:00
}
$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
}