2013-02-02 04:14:59 +01:00
|
|
|
<?php
|
2016-09-09 09:00:05 +02:00
|
|
|
|
2017-06-16 06:07:09 +02:00
|
|
|
namespace Symbiote\GridFieldExtensions;
|
2016-11-29 18:20:15 +01:00
|
|
|
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-09-15 06:01:32 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-11-30 01:03:24 +01:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-06-25 21:29:43 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
2017-09-15 06:01:32 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
2016-11-29 18:30:01 +01:00
|
|
|
use ReflectionClass;
|
2017-02-17 03:14:50 +01:00
|
|
|
use Exception;
|
2016-09-09 09:00:05 +02:00
|
|
|
|
2013-02-02 04:14:59 +01:00
|
|
|
/**
|
|
|
|
* 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()}.
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URLHandler
|
|
|
|
{
|
2017-09-15 06:01:32 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
const POST_KEY = 'GridFieldAddNewMultiClass';
|
2016-12-21 03:34:58 +01:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'handleAdd'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Should we add an empty string to the add class dropdown?
|
|
|
|
private static $showEmptyString = true;
|
|
|
|
|
|
|
|
private $fragment;
|
|
|
|
|
|
|
|
private $title;
|
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
private $classes;
|
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
private $defaultClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-06-16 06:07:09 +02:00
|
|
|
protected $itemRequestClass = 'Symbiote\\GridFieldExtensions\\GridFieldAddNewMultiClassHandler';
|
2016-12-21 03:34:58 +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
|
|
|
|
* @return GridFieldAddNewMultiClass $this
|
|
|
|
*/
|
|
|
|
public function setFragment($fragment)
|
|
|
|
{
|
|
|
|
$this->fragment = $fragment;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the button title text.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the button title text.
|
|
|
|
*
|
|
|
|
* @param string $title
|
|
|
|
* @return GridFieldAddNewMultiClass $this
|
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function getClasses(GridField $grid)
|
|
|
|
{
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
if (is_null($this->classes)) {
|
|
|
|
$classes = array_values(ClassInfo::subclassesFor($grid->getModelClass()));
|
|
|
|
sort($classes);
|
|
|
|
} else {
|
|
|
|
$classes = $this->classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
$kill_ancestors = array();
|
|
|
|
foreach ($classes as $class => $title) {
|
|
|
|
if (!is_string($class)) {
|
|
|
|
$class = $title;
|
|
|
|
}
|
|
|
|
if (!class_exists($class)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$is_abstract = (($reflection = new ReflectionClass($class)) && $reflection->isAbstract());
|
|
|
|
if (!$is_abstract && $class === $title) {
|
|
|
|
$title = singleton($class)->i18n_singular_name();
|
|
|
|
}
|
|
|
|
|
2017-05-04 03:41:58 +02:00
|
|
|
if ($ancestor_to_hide = Config::inst()->get($class, 'hide_ancestor')) {
|
2016-12-21 03:34:58 +01:00
|
|
|
$kill_ancestors[$ancestor_to_hide] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_abstract || !singleton($class)->canCreate()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result[$class] = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($kill_ancestors) {
|
|
|
|
foreach ($kill_ancestors as $class => $bool) {
|
|
|
|
unset($result[$class]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 17:15:20 +02:00
|
|
|
$sanitised = array();
|
2017-09-01 03:02:18 +02:00
|
|
|
foreach ($result as $class => $title) {
|
2017-07-05 17:15:20 +02:00
|
|
|
$sanitised[$this->sanitiseClassName($class)] = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sanitised;
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the classes that can be created using this button.
|
|
|
|
*
|
|
|
|
* @param array $classes a set of class names, optionally mapped to titles
|
2017-09-15 06:01:32 +02:00
|
|
|
* @param string $default
|
2016-12-21 03:34:58 +01:00
|
|
|
* @return GridFieldAddNewMultiClass $this
|
|
|
|
*/
|
|
|
|
public function setClasses(array $classes, $default = null)
|
|
|
|
{
|
|
|
|
$this->classes = $classes;
|
|
|
|
if ($default) {
|
|
|
|
$this->defaultClass = $default;
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles adding a new instance of a selected class.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
2017-09-15 06:01:32 +02:00
|
|
|
* @param HTTPRequest $request
|
2016-12-21 03:34:58 +01:00
|
|
|
* @return GridFieldAddNewMultiClassHandler
|
2017-09-15 06:01:32 +02:00
|
|
|
* @throws Exception
|
|
|
|
* @throws HTTPResponse_Exception
|
2016-12-21 03:34:58 +01:00
|
|
|
*/
|
|
|
|
public function handleAdd($grid, $request)
|
|
|
|
{
|
|
|
|
$class = $request->param('ClassName');
|
|
|
|
$classes = $this->getClasses($grid);
|
2017-09-15 06:01:32 +02:00
|
|
|
/** @var GridFieldDetailForm $component */
|
|
|
|
$component = $grid->getConfig()->getComponentByType(GridFieldDetailForm::class);
|
2016-12-21 03:34:58 +01:00
|
|
|
|
|
|
|
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 HTTPResponse_Exception(400);
|
|
|
|
}
|
|
|
|
|
2017-07-05 17:15:20 +02:00
|
|
|
$unsanitisedClass = $this->unsanitiseClassName($class);
|
2017-06-25 21:29:43 +02:00
|
|
|
$handler = Injector::inst()->create(
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->itemRequestClass,
|
|
|
|
$grid,
|
|
|
|
$component,
|
2017-07-05 17:15:20 +02:00
|
|
|
new $unsanitisedClass(),
|
2016-12-21 03:34:58 +01:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
GridFieldExtensions::include_requirements();
|
|
|
|
|
2018-02-21 10:49:04 +01:00
|
|
|
$field = new DropdownField(sprintf('%s[%s]', __CLASS__, $grid->getName()), '', $classes, $this->defaultClass);
|
2016-12-21 03:34:58 +01:00
|
|
|
if (Config::inst()->get(__CLASS__, 'showEmptyString')) {
|
|
|
|
$field->setEmptyString(_t('GridFieldExtensions.SELECTTYPETOCREATE', '(Select type to create)'));
|
|
|
|
}
|
|
|
|
$field->addExtraClass('no-change-track');
|
|
|
|
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'Title' => $this->getTitle(),
|
|
|
|
'Link' => Controller::join_links($grid->Link(), 'add-multi-class', '{class}'),
|
|
|
|
'ClassField' => $field
|
|
|
|
));
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$this->getFragment() => $data->renderWith(__CLASS__)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getURLHandlers($grid)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'add-multi-class/$ClassName!' => 'handleAdd'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setItemRequestClass($class)
|
|
|
|
{
|
|
|
|
$this->itemRequestClass = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
2017-07-05 17:15:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitise a model class' name for inclusion in a link
|
2017-09-15 06:01:32 +02:00
|
|
|
*
|
|
|
|
* @param string $class
|
2017-07-05 17:15:20 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-06 07:27:07 +02:00
|
|
|
protected function sanitiseClassName($class)
|
|
|
|
{
|
2017-07-05 17:15:20 +02:00
|
|
|
return str_replace('\\', '-', $class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsanitise a model class' name from a URL param
|
2017-09-15 06:01:32 +02:00
|
|
|
*
|
|
|
|
* @param string $class
|
2017-07-05 17:15:20 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-06 07:27:07 +02:00
|
|
|
protected function unsanitiseClassName($class)
|
|
|
|
{
|
2017-07-05 17:15:20 +02:00
|
|
|
return str_replace('-', '\\', $class);
|
|
|
|
}
|
2013-02-02 04:14:59 +01:00
|
|
|
}
|