2015-08-12 02:45:25 +02:00
|
|
|
<?php
|
|
|
|
|
2017-08-09 01:55:09 +02:00
|
|
|
namespace SilverStripe\UserForms\Form;
|
|
|
|
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
2022-05-23 04:01:31 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2015-08-12 02:45:25 +02:00
|
|
|
/**
|
|
|
|
* A button which allows objects to be created with a specified classname(s)
|
|
|
|
*/
|
2017-08-11 01:33:06 +02:00
|
|
|
class GridFieldAddClassesButton implements GridField_HTMLProvider, GridField_ActionProvider
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Name of fragment to insert into
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $targetFragment;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Button title
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $buttonName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additonal CSS classes for the button
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $buttonClass = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class names
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $modelClasses = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $classes Class or list of classes to create.
|
|
|
|
* If you enter more than one class, each click of the "add" button will create one of each
|
|
|
|
* @param string $targetFragment The fragment to render the button into
|
|
|
|
*/
|
|
|
|
public function __construct($classes, $targetFragment = 'buttons-before-left')
|
|
|
|
{
|
|
|
|
$this->setClasses($classes);
|
|
|
|
$this->setFragment($targetFragment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the button name
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setButtonName($name)
|
|
|
|
{
|
|
|
|
$this->buttonName = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the button name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getButtonName()
|
|
|
|
{
|
|
|
|
return $this->buttonName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the fragment name this button is rendered into.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFragment()
|
|
|
|
{
|
|
|
|
return $this->targetFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the fragment name this button is rendered into.
|
|
|
|
*
|
|
|
|
* @param string $fragment
|
|
|
|
* @return GridFieldAddNewInlineButton $this
|
|
|
|
*/
|
|
|
|
public function setFragment($fragment)
|
|
|
|
{
|
|
|
|
$this->targetFragment = $fragment;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get extra button class
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getButtonClass()
|
|
|
|
{
|
|
|
|
return $this->buttonClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets extra CSS classes for this button
|
|
|
|
*
|
|
|
|
* @param string $buttonClass
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setButtonClass($buttonClass)
|
|
|
|
{
|
|
|
|
$this->buttonClass = $buttonClass;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the classes of the objects to create
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getClasses()
|
|
|
|
{
|
|
|
|
return $this->modelClasses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the list of classes which can be created, with checks for permissions.
|
|
|
|
* Will fallback to the default model class for the given DataGrid
|
|
|
|
*
|
|
|
|
* @param DataGrid $grid
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getClassesCreate($grid)
|
|
|
|
{
|
|
|
|
// Get explicit or fallback class list
|
|
|
|
$classes = $this->getClasses();
|
|
|
|
if (empty($classes) && $grid) {
|
|
|
|
$classes = array($grid->getModelClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter out classes without permission
|
2022-04-13 03:52:56 +02:00
|
|
|
return array_filter($classes ?? [], function ($class) {
|
2016-07-21 07:53:59 +02:00
|
|
|
return singleton($class)->canCreate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify the classes to create
|
|
|
|
*
|
|
|
|
* @param array $classes
|
|
|
|
*/
|
|
|
|
public function setClasses($classes)
|
|
|
|
{
|
|
|
|
if (!is_array($classes)) {
|
|
|
|
$classes = $classes ? array($classes) : array();
|
|
|
|
}
|
|
|
|
$this->modelClasses = $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHTMLFragments($grid)
|
|
|
|
{
|
|
|
|
// Check create permission
|
|
|
|
$singleton = singleton($grid->getModelClass());
|
|
|
|
if (!$singleton->canCreate()) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get button name
|
|
|
|
$buttonName = $this->getButtonName();
|
|
|
|
if (!$buttonName) {
|
|
|
|
// provide a default button name, can be changed by calling {@link setButtonName()} on this component
|
|
|
|
$objectName = $singleton->i18n_singular_name();
|
2017-08-24 05:15:36 +02:00
|
|
|
$buttonName = _t('SilverStripe\\Forms\\GridField\\GridField.Add', 'Add {name}', ['name' => $objectName]);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 05:15:36 +02:00
|
|
|
$addAction = GridField_FormAction::create(
|
2016-07-21 07:53:59 +02:00
|
|
|
$grid,
|
|
|
|
$this->getAction(),
|
|
|
|
$buttonName,
|
|
|
|
$this->getAction(),
|
|
|
|
array()
|
|
|
|
);
|
2017-09-05 03:41:43 +02:00
|
|
|
$addAction->addExtraClass('font-icon-plus btn');
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
if ($this->getButtonClass()) {
|
|
|
|
$addAction->addExtraClass($this->getButtonClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$this->targetFragment => $addAction->forTemplate()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getActions($gridField)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
$this->getAction()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the action suburl for this component
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getAction()
|
|
|
|
{
|
|
|
|
return 'add-classes-' . strtolower(implode('-', $this->getClasses()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data)
|
|
|
|
{
|
2022-04-13 03:52:56 +02:00
|
|
|
switch (strtolower($actionName ?? '')) {
|
2016-07-21 07:53:59 +02:00
|
|
|
case $this->getAction():
|
|
|
|
return $this->handleAdd($gridField);
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles adding a new instance of a selected class.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function handleAdd($grid)
|
|
|
|
{
|
|
|
|
$classes = $this->getClassesCreate($grid);
|
|
|
|
if (empty($classes)) {
|
2017-08-09 01:55:09 +02:00
|
|
|
throw new HTTPResponse_Exception(400);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add item to gridfield
|
|
|
|
$list = $grid->getList();
|
|
|
|
foreach ($classes as $class) {
|
|
|
|
$item = $class::create();
|
|
|
|
$item->write();
|
|
|
|
$list->add($item);
|
|
|
|
}
|
|
|
|
|
2022-05-23 04:01:31 +02:00
|
|
|
$this->writeModifiedFieldData($grid);
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
// Should trigger a simple reload
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-23 04:01:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write any existing form field data that was modified before clicking add button
|
|
|
|
*/
|
|
|
|
private function writeModifiedFieldData(GridField $gridField): void
|
|
|
|
{
|
|
|
|
$vars = $gridField->getRequest()->requestVars();
|
|
|
|
$data = $vars['Fields'][GridFieldEditableColumns::POST_KEY] ?? [];
|
|
|
|
$ids = array_keys($data);
|
|
|
|
if (empty($ids)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$list = EditableFormField::get()->filter(['ID' => $ids]);
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$row = $data[$id];
|
|
|
|
$field = $list->byID($id);
|
|
|
|
if (!$field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$update = false;
|
|
|
|
foreach ($row as $name => $value) {
|
|
|
|
if ($field->{$name} !== $value) {
|
|
|
|
$field->{$name} = $value;
|
|
|
|
$update = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($update) {
|
|
|
|
$field->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-12 02:45:25 +02:00
|
|
|
}
|