2013-02-07 12:31:12 +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
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
use Closure;
|
2017-08-24 02:43:33 +02:00
|
|
|
use Exception;
|
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;
|
2017-08-23 03:53:52 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormField;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_SaveHandler;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
2017-09-15 06:01:32 +02:00
|
|
|
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
2017-09-15 06:01:32 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
2018-02-21 10:49:04 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\ORM\ManyManyList;
|
|
|
|
|
2013-02-07 12:31:12 +01:00
|
|
|
/**
|
|
|
|
* Allows inline editing of grid field records without having to load a separate
|
|
|
|
* edit interface.
|
|
|
|
*
|
|
|
|
* The form fields used can be configured by setting the value in {@link setDisplayFields()} to one
|
|
|
|
* of the following forms:
|
|
|
|
* - A Closure which returns the field instance.
|
|
|
|
* - An array with a `callback` key pointing to a function which returns the field.
|
|
|
|
* - An array with a `field` key->response specifying the field class to use.
|
|
|
|
*/
|
|
|
|
class GridFieldEditableColumns extends GridFieldDataColumns implements
|
2016-12-21 03:34:58 +01:00
|
|
|
GridField_HTMLProvider,
|
|
|
|
GridField_SaveHandler,
|
|
|
|
GridField_URLHandler
|
|
|
|
{
|
2017-09-15 06:01:32 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
|
|
|
const POST_KEY = 'GridFieldEditableColumns';
|
2016-12-21 03:34:58 +01:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'handleForm'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Form[]
|
|
|
|
*/
|
|
|
|
protected $forms = array();
|
|
|
|
|
|
|
|
public function getColumnContent($grid, $record, $col)
|
|
|
|
{
|
|
|
|
$fields = $this->getForm($grid, $record)->Fields();
|
|
|
|
|
|
|
|
if (!$this->displayFields) {
|
|
|
|
// If setDisplayFields() not used, utilize $summary_fields
|
|
|
|
// in a way similar to base class
|
2022-04-13 07:44:44 +02:00
|
|
|
$colRelation = explode('.', $col ?? '');
|
2016-12-21 03:34:58 +01:00
|
|
|
$value = $grid->getDataFieldValue($record, $colRelation[0]);
|
|
|
|
$field = $fields->fieldByName($colRelation[0]);
|
|
|
|
if (!$field || $field->isReadonly() || $field->isDisabled()) {
|
|
|
|
return parent::getColumnContent($grid, $record, $col);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure this field is available to edit on the record
|
|
|
|
// (ie. Maybe its readonly due to certain circumstances, or removed and not editable)
|
|
|
|
$cmsFields = $record->getCMSFields();
|
|
|
|
$cmsField = $cmsFields->dataFieldByName($colRelation[0]);
|
|
|
|
if (!$cmsField || $cmsField->isReadonly() || $cmsField->isDisabled()) {
|
|
|
|
return parent::getColumnContent($grid, $record, $col);
|
|
|
|
}
|
|
|
|
$field = clone $field;
|
|
|
|
} else {
|
|
|
|
$value = $grid->getDataFieldValue($record, $col);
|
2019-08-26 02:37:19 +02:00
|
|
|
$field = $fields->dataFieldByName($col);
|
2016-12-21 03:34:58 +01:00
|
|
|
|
2020-07-30 00:54:53 +02:00
|
|
|
// Fall back to previous logic
|
|
|
|
if (!$field) {
|
2022-04-13 07:44:44 +02:00
|
|
|
$rel = (strpos($col ?? '', '.') === false); // field references a relation value
|
2022-02-18 04:44:54 +01:00
|
|
|
$field = ($rel) ? clone $fields->fieldByName($col) : ReadonlyField::create($col);
|
2020-07-30 00:54:53 +02:00
|
|
|
}
|
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
if (!$field) {
|
|
|
|
throw new Exception("Could not find the field '$col'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 07:44:44 +02:00
|
|
|
if (array_key_exists($col, $this->fieldCasting ?? [])) {
|
2016-12-21 03:34:58 +01:00
|
|
|
$value = $grid->getCastedValue($value, $this->fieldCasting[$col]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $this->formatValue($grid, $record, $col, $value);
|
|
|
|
|
|
|
|
$field->setName($this->getFieldName($field->getName(), $grid, $record));
|
|
|
|
$field->setValue($value);
|
2016-08-18 03:32:18 +02:00
|
|
|
|
2020-08-16 05:21:54 +02:00
|
|
|
if ($grid->isReadonly() || !$record->canEdit()) {
|
2020-08-07 02:30:21 +02:00
|
|
|
$field = $field->performReadonlyTransformation();
|
|
|
|
}
|
|
|
|
|
2016-04-11 06:15:15 +02:00
|
|
|
if ($field instanceof HtmlEditorField) {
|
2016-08-18 03:32:18 +02:00
|
|
|
return $field->FieldHolder();
|
2016-04-11 06:15:15 +02:00
|
|
|
}
|
2013-02-07 12:31:12 +01:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
return $field->forTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getHTMLFragments($grid)
|
|
|
|
{
|
|
|
|
GridFieldExtensions::include_requirements();
|
|
|
|
$grid->addExtraClass('ss-gridfield-editable');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handleSave(GridField $grid, DataObjectInterface $record)
|
|
|
|
{
|
2017-09-15 06:01:32 +02:00
|
|
|
/** @var DataList $list */
|
2016-12-21 03:34:58 +01:00
|
|
|
$list = $grid->getList();
|
|
|
|
$value = $grid->Value();
|
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
if (!isset($value[self::POST_KEY]) || !is_array($value[self::POST_KEY])) {
|
2016-12-21 03:34:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var GridFieldOrderableRows $sortable */
|
2017-09-15 06:01:32 +02:00
|
|
|
$sortable = $grid->getConfig()->getComponentByType(GridFieldOrderableRows::class);
|
2016-12-21 03:34:58 +01:00
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
foreach ($value[self::POST_KEY] as $id => $fields) {
|
2016-12-21 03:34:58 +01:00
|
|
|
if (!is_numeric($id) || !is_array($fields)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$item = $list->byID($id);
|
|
|
|
|
|
|
|
if (!$item || !$item->canEdit()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$extra = array();
|
|
|
|
|
2019-08-26 02:37:19 +02:00
|
|
|
$form = $this->getForm($grid, $item);
|
2016-12-21 03:34:58 +01:00
|
|
|
$form->loadDataFrom($fields, Form::MERGE_CLEAR_MISSING);
|
|
|
|
$form->saveInto($item);
|
|
|
|
|
2019-08-26 02:37:19 +02:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
// Check if we are also sorting these records
|
|
|
|
if ($sortable) {
|
|
|
|
$sortField = $sortable->getSortField();
|
2017-08-24 02:43:33 +02:00
|
|
|
if (isset($fields[$sortField])) {
|
|
|
|
$item->setField($sortField, $fields[$sortField]);
|
|
|
|
}
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($list instanceof ManyManyList) {
|
2022-04-13 07:44:44 +02:00
|
|
|
$extra = array_intersect_key($form->getData() ?? [], (array) $list->getExtraFields());
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
|
2019-08-26 02:37:19 +02:00
|
|
|
$item->write(false, false, false, true);
|
2016-12-21 03:34:58 +01:00
|
|
|
$list->add($item, $extra);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
/**
|
|
|
|
* @param GridField $grid
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return Form
|
|
|
|
* @throws HTTPResponse_Exception
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
public function handleForm(GridField $grid, $request)
|
|
|
|
{
|
|
|
|
$id = $request->param('ID');
|
|
|
|
$list = $grid->getList();
|
|
|
|
|
|
|
|
if (!ctype_digit($id)) {
|
|
|
|
throw new HTTPResponse_Exception(null, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$record = $list->byID($id)) {
|
|
|
|
throw new HTTPResponse_Exception(null, 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = $this->getForm($grid, $record);
|
|
|
|
|
|
|
|
foreach ($form->Fields() as $field) {
|
|
|
|
$field->setName($this->getFieldName($field->getName(), $grid, $record));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getURLHandlers($grid)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'editable/form/$ID' => 'handleForm'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the field list for a record.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
|
|
|
* @param DataObjectInterface $record
|
|
|
|
* @return FieldList
|
2017-09-15 06:01:32 +02:00
|
|
|
* @throws Exception
|
2016-12-21 03:34:58 +01:00
|
|
|
*/
|
|
|
|
public function getFields(GridField $grid, DataObjectInterface $record)
|
|
|
|
{
|
|
|
|
$cols = $this->getDisplayFields($grid);
|
2022-02-18 04:44:54 +01:00
|
|
|
$fields = FieldList::create();
|
2016-12-21 03:34:58 +01:00
|
|
|
|
2017-09-15 06:01:32 +02:00
|
|
|
/** @var DataList $list */
|
2016-12-21 03:34:58 +01:00
|
|
|
$list = $grid->getList();
|
|
|
|
$class = $list ? $list->dataClass() : null;
|
|
|
|
|
|
|
|
foreach ($cols as $col => $info) {
|
|
|
|
$field = null;
|
|
|
|
|
|
|
|
if ($info instanceof Closure) {
|
|
|
|
$field = call_user_func($info, $record, $col, $grid);
|
|
|
|
} elseif (is_array($info)) {
|
|
|
|
if (isset($info['callback'])) {
|
|
|
|
$field = call_user_func($info['callback'], $record, $col, $grid);
|
|
|
|
} elseif (isset($info['field'])) {
|
2017-09-15 06:01:32 +02:00
|
|
|
if ($info['field'] == LiteralField::class) {
|
2016-12-21 03:34:58 +01:00
|
|
|
$field = new $info['field']($col, null);
|
|
|
|
} else {
|
|
|
|
$field = new $info['field']($col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$field instanceof FormField) {
|
|
|
|
throw new Exception(sprintf(
|
|
|
|
'The field for column "%s" is not a valid form field',
|
|
|
|
$col
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$field && $list instanceof ManyManyList) {
|
|
|
|
$extra = $list->getExtraFields();
|
|
|
|
|
2022-04-13 07:44:44 +02:00
|
|
|
if ($extra && array_key_exists($col, $extra ?? [])) {
|
2017-08-23 03:53:52 +02:00
|
|
|
$field = Injector::inst()->create($extra[$col], $col)->scaffoldFormField();
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$field) {
|
|
|
|
if (!$this->displayFields) {
|
|
|
|
// If setDisplayFields() not used, utilize $summary_fields
|
|
|
|
// in a way similar to base class
|
|
|
|
//
|
|
|
|
// Allows use of 'MyBool.Nice' and 'MyHTML.NoHTML' so that
|
|
|
|
// GridFields not using inline editing still look good or
|
|
|
|
// revert to looking good in cases where the field isn't
|
|
|
|
// available or is readonly
|
|
|
|
//
|
2022-04-13 07:44:44 +02:00
|
|
|
$colRelation = explode('.', $col ?? '');
|
2017-09-15 06:01:32 +02:00
|
|
|
if ($class && $obj = DataObject::singleton($class)->dbObject($colRelation[0])) {
|
2016-12-21 03:34:58 +01:00
|
|
|
$field = $obj->scaffoldFormField();
|
|
|
|
} else {
|
2022-02-18 04:44:54 +01:00
|
|
|
$field = ReadonlyField::create($colRelation[0]);
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
2017-09-15 06:01:32 +02:00
|
|
|
} elseif ($class && $obj = DataObject::singleton($class)->dbObject($col)) {
|
2016-12-21 03:34:58 +01:00
|
|
|
$field = $obj->scaffoldFormField();
|
|
|
|
} else {
|
2022-02-18 04:44:54 +01:00
|
|
|
$field = ReadonlyField::create($col);
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$field instanceof FormField) {
|
|
|
|
throw new Exception(sprintf(
|
|
|
|
'Invalid form field instance for column "%s"',
|
|
|
|
$col
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add CSS class for interactive fields
|
|
|
|
if (!($field->isReadOnly() || $field instanceof LiteralField)) {
|
|
|
|
$field->addExtraClass('editable-column-field');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields->push($field);
|
|
|
|
}
|
2013-02-07 12:31:12 +01:00
|
|
|
|
2016-12-21 03:34:58 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the form instance for a record.
|
|
|
|
*
|
|
|
|
* @param GridField $grid
|
|
|
|
* @param DataObjectInterface $record
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function getForm(GridField $grid, DataObjectInterface $record)
|
|
|
|
{
|
|
|
|
$fields = $this->getFields($grid, $record);
|
|
|
|
|
2022-02-18 04:44:54 +01:00
|
|
|
$form = Form::create($grid, null, $fields, FieldList::create());
|
2016-12-21 03:34:58 +01:00
|
|
|
$form->loadDataFrom($record);
|
|
|
|
|
|
|
|
$form->setFormAction(Controller::join_links(
|
|
|
|
$grid->Link(),
|
|
|
|
'editable/form',
|
|
|
|
$record->ID
|
|
|
|
));
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getFieldName($name, GridField $grid, DataObjectInterface $record)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
'%s[%s][%s][%s]',
|
|
|
|
$grid->getName(),
|
2017-09-15 06:01:32 +02:00
|
|
|
self::POST_KEY,
|
2016-12-21 03:34:58 +01:00
|
|
|
$record->ID,
|
|
|
|
$name
|
|
|
|
);
|
|
|
|
}
|
2013-02-07 12:31:12 +01:00
|
|
|
}
|