2016-08-19 10:51:35 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
2017-06-22 22:50:45 +12:00
|
|
|
use Closure;
|
2016-09-09 18:43:05 +12:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-05-17 17:40:13 +12:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-03-02 15:24:38 +13:00
|
|
|
use SilverStripe\Control\RequestHandler;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Core\ClassInfo;
|
2017-06-22 22:50:45 +12:00
|
|
|
use SilverStripe\Core\Extensible;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-06-22 22:50:45 +12:00
|
|
|
use SilverStripe\Forms\Validator;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-05-17 17:40:13 +12:00
|
|
|
use SilverStripe\ORM\Filterable;
|
2016-08-19 10:51:35 +12:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides view and edit forms at GridField-specific URLs.
|
|
|
|
*
|
|
|
|
* These can be placed into pop-ups by an appropriate front-end.
|
|
|
|
*
|
|
|
|
* Usually added to a {@link GridField} alongside of a
|
|
|
|
* {@link GridFieldEditButton} which takes care of linking the
|
|
|
|
* individual rows to their edit view.
|
|
|
|
*
|
|
|
|
* The URLs provided will be off the following form:
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>/edit
|
|
|
|
*/
|
2016-11-29 12:31:16 +13:00
|
|
|
class GridFieldDetailForm implements GridField_URLHandler
|
|
|
|
{
|
2016-08-19 10:51:35 +12:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
use Extensible;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $template = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
|
|
|
|
2018-11-19 11:06:47 +13:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $showPagination;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $showAdd;
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* @var Validator The form validator used for both add and edit fields.
|
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FieldList Falls back to {@link DataObject->getCMSFields()} if not defined.
|
|
|
|
*/
|
|
|
|
protected $fields;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $itemRequestClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable With two parameters: $form and $component
|
|
|
|
*/
|
|
|
|
protected $itemEditFormCallback;
|
|
|
|
|
|
|
|
public function getURLHandlers($gridField)
|
|
|
|
{
|
|
|
|
return array(
|
2017-10-24 12:57:08 +13:00
|
|
|
'item/$ID' => 'handleItem'
|
2016-11-29 12:31:16 +13:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a popup component. The two arguments will specify how the popup form's HTML and
|
|
|
|
* behaviour is created. The given controller will be customised, putting the edit form into the
|
|
|
|
* template with the given name.
|
|
|
|
*
|
|
|
|
* The arguments are experimental API's to support partial content to be passed back to whatever
|
|
|
|
* controller who wants to display the getCMSFields
|
|
|
|
*
|
|
|
|
* @param string $name The name of the edit form to place into the pop-up form
|
2018-11-19 11:06:47 +13:00
|
|
|
* @param bool $showPagination Whether the `Previous` and `Next` buttons should display or not, leave as null to use default
|
|
|
|
* @param bool $showAdd Whether the `Add` button should display or not, leave as null to use default
|
2016-11-29 12:31:16 +13:00
|
|
|
*/
|
2018-11-19 11:06:47 +13:00
|
|
|
public function __construct($name = null, $showPagination = null, $showAdd = null)
|
2016-11-29 12:31:16 +13:00
|
|
|
{
|
2018-11-19 11:06:47 +13:00
|
|
|
$this->setName($name ?: 'DetailForm');
|
|
|
|
$this->setShowPagination($showPagination);
|
|
|
|
$this->setShowAdd($showAdd);
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param HTTPRequest $request
|
2017-05-17 17:40:13 +12:00
|
|
|
* @return HTTPResponse
|
2016-11-29 12:31:16 +13:00
|
|
|
*/
|
|
|
|
public function handleItem($gridField, $request)
|
|
|
|
{
|
2018-11-19 11:06:47 +13:00
|
|
|
if ($gridStateStr = $request->getVar('gridState')) {
|
|
|
|
$gridField->getState(false)->setValue($gridStateStr);
|
|
|
|
}
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
// Our getController could either give us a true Controller, if this is the top-level GridField.
|
|
|
|
// It could also give us a RequestHandler in the form of GridFieldDetailForm_ItemRequest if this is a
|
|
|
|
// nested GridField.
|
|
|
|
$requestHandler = $gridField->getForm()->getController();
|
|
|
|
|
|
|
|
/** @var DataObject $record */
|
|
|
|
if (is_numeric($request->param('ID'))) {
|
2017-05-17 17:40:13 +12:00
|
|
|
/** @var Filterable $dataList */
|
|
|
|
$dataList = $gridField->getList();
|
2018-11-19 11:06:47 +13:00
|
|
|
$record = $dataList->byID($request->param('ID'));
|
2016-11-29 12:31:16 +13:00
|
|
|
} else {
|
2017-05-17 17:40:13 +12:00
|
|
|
$record = Injector::inst()->create($gridField->getModelClass());
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
$handler = $this->getItemRequestHandler($gridField, $record, $requestHandler);
|
|
|
|
|
|
|
|
// if no validator has been set on the GridField and the record has a
|
|
|
|
// CMS validator, use that.
|
2017-05-17 17:40:13 +12:00
|
|
|
if (!$this->getValidator() && ClassInfo::hasMethod($record, 'getCMSValidator')) {
|
2016-11-29 12:31:16 +13:00
|
|
|
$this->setValidator($record->getCMSValidator());
|
|
|
|
}
|
|
|
|
|
2017-06-22 22:50:45 +12:00
|
|
|
return $handler->handleRequest($request);
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a request handler for the given record
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param DataObject $record
|
2017-03-02 15:24:38 +13:00
|
|
|
* @param RequestHandler $requestHandler
|
2016-11-29 12:31:16 +13:00
|
|
|
* @return GridFieldDetailForm_ItemRequest
|
|
|
|
*/
|
|
|
|
protected function getItemRequestHandler($gridField, $record, $requestHandler)
|
|
|
|
{
|
|
|
|
$class = $this->getItemRequestClass();
|
2018-06-15 17:58:53 +12:00
|
|
|
$assignedClass = $this->itemRequestClass;
|
|
|
|
$this->extend('updateItemRequestClass', $class, $gridField, $record, $requestHandler, $assignedClass);
|
2016-11-29 12:31:16 +13:00
|
|
|
/** @var GridFieldDetailForm_ItemRequest $handler */
|
|
|
|
$handler = Injector::inst()->createWithArgs(
|
|
|
|
$class,
|
|
|
|
array($gridField, $this, $record, $requestHandler, $this->name)
|
|
|
|
);
|
|
|
|
if ($template = $this->getTemplate()) {
|
|
|
|
$handler->setTemplate($template);
|
|
|
|
}
|
|
|
|
$this->extend('updateItemRequestHandler', $handler);
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $template
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
|
|
|
$this->template = $template;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getTemplate()
|
|
|
|
{
|
|
|
|
return $this->template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2018-11-19 11:06:47 +13:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function getDefaultShowPagination()
|
|
|
|
{
|
|
|
|
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
|
|
|
|
return isset($formActionsConfig['showPagination']) ? (boolean) $formActionsConfig['showPagination'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getShowPagination()
|
|
|
|
{
|
|
|
|
if ($this->showPagination === null) {
|
|
|
|
return $this->getDefaultShowPagination();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (boolean) $this->showPagination;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool|null $showPagination
|
|
|
|
* @return GridFieldDetailForm
|
|
|
|
*/
|
|
|
|
public function setShowPagination($showPagination)
|
|
|
|
{
|
|
|
|
$this->showPagination = $showPagination;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function getDefaultShowAdd()
|
|
|
|
{
|
|
|
|
$formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions');
|
|
|
|
return isset($formActionsConfig['showAdd']) ? (boolean) $formActionsConfig['showAdd'] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getShowAdd()
|
|
|
|
{
|
|
|
|
if ($this->showAdd === null) {
|
|
|
|
return $this->getDefaultShowAdd();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (boolean) $this->showAdd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool|null $showAdd
|
|
|
|
* @return GridFieldDetailForm
|
|
|
|
*/
|
|
|
|
public function setShowAdd($showAdd)
|
|
|
|
{
|
|
|
|
$this->showAdd = $showAdd;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setValidator(Validator $validator)
|
|
|
|
{
|
|
|
|
$this->validator = $validator;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Validator
|
|
|
|
*/
|
|
|
|
public function getValidator()
|
|
|
|
{
|
|
|
|
return $this->validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FieldList $fields
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$this->fields = $fields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getFields()
|
|
|
|
{
|
|
|
|
return $this->fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setItemRequestClass($class)
|
|
|
|
{
|
|
|
|
$this->itemRequestClass = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string name of {@see GridFieldDetailForm_ItemRequest} subclass
|
|
|
|
*/
|
|
|
|
public function getItemRequestClass()
|
|
|
|
{
|
|
|
|
if ($this->itemRequestClass) {
|
|
|
|
return $this->itemRequestClass;
|
2018-11-19 11:06:47 +13:00
|
|
|
} elseif (ClassInfo::exists(static::class . '_ItemRequest')) {
|
|
|
|
return static::class . '_ItemRequest';
|
2016-11-29 12:31:16 +13:00
|
|
|
} else {
|
2018-06-15 17:58:53 +12:00
|
|
|
return GridFieldDetailForm_ItemRequest::class;
|
2016-11-29 12:31:16 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Closure $cb Make changes on the edit form after constructing it.
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setItemEditFormCallback(Closure $cb)
|
|
|
|
{
|
|
|
|
$this->itemEditFormCallback = $cb;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Closure
|
|
|
|
*/
|
|
|
|
public function getItemEditFormCallback()
|
|
|
|
{
|
|
|
|
return $this->itemEditFormCallback;
|
|
|
|
}
|
2016-08-19 10:51:35 +12:00
|
|
|
}
|