2013-02-02 04:14:59 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A component which lets the user select from a list of classes to create a new record form.
|
|
|
|
*
|
|
|
|
* By default the list of classes that are createable is the grid field's model class, and any
|
|
|
|
* subclasses. This can be customised using {@link setClasses()}.
|
|
|
|
*/
|
|
|
|
class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URLHandler {
|
|
|
|
|
2013-07-11 04:40:13 +02:00
|
|
|
private static $allowed_actions = array(
|
|
|
|
'handleAdd'
|
|
|
|
);
|
2015-09-21 12:49:02 +02:00
|
|
|
|
2014-10-06 17:50:40 +02:00
|
|
|
// Should we add an empty string to the add class dropdown?
|
|
|
|
private static $showEmptyString = true;
|
2013-07-11 04:40:13 +02:00
|
|
|
|
2013-02-02 04:14:59 +01:00
|
|
|
private $fragment;
|
|
|
|
|
|
|
|
private $title;
|
|
|
|
|
|
|
|
private $classes;
|
2015-09-21 12:49:02 +02:00
|
|
|
|
2015-09-15 01:16:36 +02:00
|
|
|
private $defaultClass;
|
2013-02-02 04:14:59 +01:00
|
|
|
|
2014-12-06 23:04:29 +01:00
|
|
|
/**
|
2015-09-28 10:55:01 +02:00
|
|
|
* @var string
|
2014-12-06 23:04:29 +01:00
|
|
|
*/
|
|
|
|
protected $itemRequestClass = 'GridFieldAddNewMultiClassHandler';
|
|
|
|
|
2013-02-02 04:14:59 +01:00
|
|
|
/**
|
|
|
|
* @param string $fragment the fragment to render the button in
|
|
|
|
*/
|
|
|
|
public function __construct($fragment = 'before') {
|
|
|
|
$this->setFragment($fragment);
|
|
|
|
$this->setTitle(_t('GridFieldExtensions.ADD', 'Add'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the fragment name this button is rendered into.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFragment() {
|
|
|
|
return $this->fragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the fragment name this button is rendered into.
|
|
|
|
*
|
|
|
|
* @param string $fragment
|
2013-04-24 08:28:47 +02:00
|
|
|
* @return GridFieldAddNewMultiClass $this
|
2013-02-02 04:14:59 +01:00
|
|
|
*/
|
|
|
|
public function setFragment($fragment) {
|
|
|
|
$this->fragment = $fragment;
|
2013-04-24 08:28:47 +02:00
|
|
|
return $this;
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the button title text.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle() {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the button title text.
|
|
|
|
*
|
|
|
|
* @param string $title
|
2013-04-24 08:28:47 +02:00
|
|
|
* @return GridFieldAddNewMultiClass $this
|
2013-02-02 04:14:59 +01:00
|
|
|
*/
|
|
|
|
public function setTitle($title) {
|
|
|
|
$this->title = $title;
|
2013-04-24 08:28:47 +02:00
|
|
|
return $this;
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the classes that can be created using this button, defaulting to the model class and
|
|
|
|
* its subclasses.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
|
|
|
* @return array a map of class name to title
|
|
|
|
*/
|
2013-11-14 13:36:57 +01:00
|
|
|
public function getClasses(GridField $grid) {
|
2013-02-02 04:14:59 +01:00
|
|
|
$result = array();
|
|
|
|
|
|
|
|
if(is_null($this->classes)) {
|
|
|
|
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()));
|
2013-11-14 13:43:23 +01:00
|
|
|
sort($classes);
|
2013-02-02 04:14:59 +01:00
|
|
|
} else {
|
|
|
|
$classes = $this->classes;
|
|
|
|
}
|
|
|
|
|
2016-01-20 04:29:44 +01:00
|
|
|
$kill_ancestors = array();
|
2013-02-02 04:14:59 +01:00
|
|
|
foreach($classes as $class => $title) {
|
|
|
|
if(!is_string($class)) {
|
|
|
|
$class = $title;
|
2016-01-20 04:29:44 +01:00
|
|
|
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
|
|
|
|
if (!$is_abstract) {
|
|
|
|
$title = singleton($class)->i18n_singular_name();
|
2015-12-05 08:36:51 +01:00
|
|
|
}
|
2016-01-20 04:29:44 +01:00
|
|
|
} else {
|
|
|
|
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ancestor_to_hide = Config::inst()->get($class, 'hide_ancestor', Config::FIRST_SET)) {
|
|
|
|
$kill_ancestors[$ancestor_to_hide] = true;
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|
|
|
|
|
2016-01-20 04:29:44 +01:00
|
|
|
if($is_abstract || !singleton($class)->canCreate()) {
|
2013-02-02 04:14:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result[$class] = $title;
|
|
|
|
}
|
|
|
|
|
2016-01-20 04:29:44 +01:00
|
|
|
if($kill_ancestors) {
|
|
|
|
foreach($kill_ancestors as $class => $bool) {
|
|
|
|
unset($result[$class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-02 04:14:59 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the classes that can be created using this button.
|
|
|
|
*
|
|
|
|
* @param array $classes a set of class names, optionally mapped to titles
|
2013-04-24 08:28:47 +02:00
|
|
|
* @return GridFieldAddNewMultiClass $this
|
2013-02-02 04:14:59 +01:00
|
|
|
*/
|
2015-09-15 01:16:36 +02:00
|
|
|
public function setClasses(array $classes, $default = null) {
|
2013-02-02 04:14:59 +01:00
|
|
|
$this->classes = $classes;
|
2015-09-15 01:16:36 +02:00
|
|
|
if($default) $this->defaultClass = $default;
|
2013-04-24 08:28:47 +02:00
|
|
|
return $this;
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-15 01:16:36 +02:00
|
|
|
/**
|
|
|
|
* Sets the default class that is selected automatically.
|
|
|
|
*
|
|
|
|
* @param string $default the class name to use as default
|
|
|
|
* @return GridFieldAddNewMultiClass $this
|
|
|
|
*/
|
|
|
|
public function setDefaultClass($default) {
|
|
|
|
$this->defaultClass = $default;
|
2015-09-21 12:49:02 +02:00
|
|
|
return $this;
|
2015-09-15 01:16:36 +02:00
|
|
|
}
|
|
|
|
|
2013-02-02 04:14:59 +01:00
|
|
|
/**
|
|
|
|
* Handles adding a new instance of a selected class.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
|
|
|
* @param SS_HTTPRequest $request
|
2013-11-14 13:36:28 +01:00
|
|
|
* @return GridFieldAddNewMultiClassHandler
|
2013-02-02 04:14:59 +01:00
|
|
|
*/
|
|
|
|
public function handleAdd($grid, $request) {
|
|
|
|
$class = $request->param('ClassName');
|
|
|
|
$classes = $this->getClasses($grid);
|
|
|
|
$component = $grid->getConfig()->getComponentByType('GridFieldDetailForm');
|
|
|
|
|
|
|
|
if(!$component) {
|
|
|
|
throw new Exception('The add new multi class component requires the detail form component.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$class || !array_key_exists($class, $classes)) {
|
|
|
|
throw new SS_HTTPResponse_Exception(400);
|
|
|
|
}
|
|
|
|
|
2014-12-06 23:04:29 +01:00
|
|
|
$handler = Object::create($this->itemRequestClass,
|
2013-02-02 04:14:59 +01:00
|
|
|
$grid, $component, new $class(), $grid->getForm()->getController(), 'add-multi-class'
|
|
|
|
);
|
|
|
|
$handler->setTemplate($component->getTemplate());
|
|
|
|
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($grid) {
|
|
|
|
$classes = $this->getClasses($grid);
|
|
|
|
|
|
|
|
if(!count($classes)) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2013-02-08 15:42:48 +01:00
|
|
|
GridFieldExtensions::include_requirements();
|
|
|
|
|
2015-09-15 01:16:36 +02:00
|
|
|
$field = new DropdownField(sprintf('%s[ClassName]', __CLASS__), '', $classes, $this->defaultClass);
|
2014-10-06 17:50:40 +02:00
|
|
|
if (Config::inst()->get('GridFieldAddNewMultiClass', 'showEmptyString')) {
|
|
|
|
$field->setEmptyString(_t('GridFieldExtensions.SELECTTYPETOCREATE', '(Select type to create)'));
|
|
|
|
}
|
2013-02-02 04:14:59 +01:00
|
|
|
$field->addExtraClass('no-change-track');
|
|
|
|
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'Title' => $this->getTitle(),
|
2013-10-09 11:34:12 +02:00
|
|
|
'Link' => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'),
|
2013-02-02 04:14:59 +01:00
|
|
|
'ClassField' => $field
|
|
|
|
));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$this->getFragment() => $data->renderWith(__CLASS__)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getURLHandlers($grid) {
|
|
|
|
return array(
|
|
|
|
'add-multi-class/$ClassName!' => 'handleAdd'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-06 23:04:29 +01:00
|
|
|
public function setItemRequestClass($class) {
|
|
|
|
$this->itemRequestClass = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|