|
|
|
@ -6,8 +6,10 @@ use Exception;
|
|
|
|
|
use SilverStripe\Admin\ModelAdmin;
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
|
use SilverStripe\Forms\GridField\AbstractGridFieldComponent;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
@ -16,11 +18,11 @@ use SilverStripe\Forms\GridField\GridField_DataManipulator;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridField_SaveHandler;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
|
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldStateAware;
|
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
|
use SilverStripe\ORM\Filterable;
|
|
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
@ -39,26 +41,61 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
{
|
|
|
|
|
use Configurable, GridFieldStateAware;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The key used in the post data to identify nested form data
|
|
|
|
|
*/
|
|
|
|
|
const POST_KEY = 'GridFieldNestedForm';
|
|
|
|
|
|
|
|
|
|
private static $allowed_actions = [
|
|
|
|
|
'handleNestedItem'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
private static $max_nesting_level = 10;
|
|
|
|
|
/**
|
|
|
|
|
* The default max nesting level. Nesting further than this will throw an exception.
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
private static $default_max_nesting_level = 10;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
protected $name;
|
|
|
|
|
protected $expandNested = false;
|
|
|
|
|
protected $forceCloseNested = false;
|
|
|
|
|
protected $gridField = null;
|
|
|
|
|
protected $record = null;
|
|
|
|
|
protected $relationName = 'Children';
|
|
|
|
|
protected $inlineEditable = false;
|
|
|
|
|
protected $canExpandCheck = null;
|
|
|
|
|
protected $maxNestingLevel = null;
|
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $expandNested = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $forceCloseNested = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var GridField
|
|
|
|
|
*/
|
|
|
|
|
private $gridField = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $relationName = 'Children';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $inlineEditable = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var callable|string
|
|
|
|
|
*/
|
|
|
|
|
private $canExpandCheck = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var int
|
|
|
|
|
*/
|
|
|
|
|
private $maxNestingLevel = null;
|
|
|
|
|
|
|
|
|
|
public function __construct($name = 'NestedForm')
|
|
|
|
|
{
|
|
|
|
@ -67,27 +104,24 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the grid field that this component is attached to
|
|
|
|
|
* @return GridField
|
|
|
|
|
*/
|
|
|
|
|
public function getGridField()
|
|
|
|
|
public function getGridField(): GridField
|
|
|
|
|
{
|
|
|
|
|
return $this->gridField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the relation name to use for the nested grid fields
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getRelationName()
|
|
|
|
|
public function getRelationName(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->relationName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the relation name to use for the nested grid fields
|
|
|
|
|
* @param string $relationName
|
|
|
|
|
*/
|
|
|
|
|
public function setRelationName($relationName)
|
|
|
|
|
public function setRelationName(string $relationName)
|
|
|
|
|
{
|
|
|
|
|
$this->relationName = $relationName;
|
|
|
|
|
return $this;
|
|
|
|
@ -95,18 +129,16 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get whether the nested grid fields should be inline editable
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function getInlineEditable()
|
|
|
|
|
public function getInlineEditable(): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->inlineEditable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set whether the nested grid fields should be inline editable
|
|
|
|
|
* @param boolean $editable
|
|
|
|
|
*/
|
|
|
|
|
public function setInlineEditable($editable)
|
|
|
|
|
public function setInlineEditable(bool $editable)
|
|
|
|
|
{
|
|
|
|
|
$this->inlineEditable = $editable;
|
|
|
|
|
return $this;
|
|
|
|
@ -114,9 +146,8 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set whether the nested grid fields should be expanded by default
|
|
|
|
|
* @param boolean $expandNested
|
|
|
|
|
*/
|
|
|
|
|
public function setExpandNested($expandNested)
|
|
|
|
|
public function setExpandNested(bool $expandNested)
|
|
|
|
|
{
|
|
|
|
|
$this->expandNested = $expandNested;
|
|
|
|
|
return $this;
|
|
|
|
@ -124,49 +155,54 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set whether the nested grid fields should be forced closed on load
|
|
|
|
|
* @param boolean $forceClosed
|
|
|
|
|
*/
|
|
|
|
|
public function setForceClosedNested($forceClosed)
|
|
|
|
|
public function setForceClosedNested(bool $forceClosed)
|
|
|
|
|
{
|
|
|
|
|
$this->forceCloseNested = $forceClosed;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a callback to check which items in this grid that should show the expand link
|
|
|
|
|
* Set a callback function to check which items in this grid that should show the expand link
|
|
|
|
|
* for nested gridfields. The callback should return a boolean value.
|
|
|
|
|
* @param callable $checkFunction
|
|
|
|
|
* You can either pass a callable or a method name as a string.
|
|
|
|
|
*/
|
|
|
|
|
public function setCanExpandCheck($checkFunction)
|
|
|
|
|
public function setCanExpandCheck(callable|string $callback)
|
|
|
|
|
{
|
|
|
|
|
$this->canExpandCheck = $checkFunction;
|
|
|
|
|
$this->canExpandCheck = $callback;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the maximum nesting level allowed for nested grid fields
|
|
|
|
|
* @param int $level
|
|
|
|
|
*/
|
|
|
|
|
public function setMaxNestingLevel($level)
|
|
|
|
|
public function setMaxNestingLevel(int $level)
|
|
|
|
|
{
|
|
|
|
|
$this->maxNestingLevel = $level;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getMaxNestingLevel()
|
|
|
|
|
/**
|
|
|
|
|
* Get the max nesting level allowed for this grid field.
|
|
|
|
|
*/
|
|
|
|
|
public function getMaxNestingLevel(): int
|
|
|
|
|
{
|
|
|
|
|
return $this->maxNestingLevel ?: $this->config()->max_nesting_level;
|
|
|
|
|
return $this->maxNestingLevel ?: static::config()->get('default_max_nesting_level');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getNestingLevel($gridField)
|
|
|
|
|
/**
|
|
|
|
|
* Check if we are currently at the max nesting level allowed.
|
|
|
|
|
*/
|
|
|
|
|
protected function atMaxNestingLevel(GridField $gridField): bool
|
|
|
|
|
{
|
|
|
|
|
$level = 0;
|
|
|
|
|
$c = $gridField->getForm()->getController();
|
|
|
|
|
while ($c && $c instanceof GridFieldDetailForm_ItemRequest) {
|
|
|
|
|
$c = $c->getController();
|
|
|
|
|
$controller = $gridField->getForm()->getController();
|
|
|
|
|
$maxLevel = $this->getMaxNestingLevel();
|
|
|
|
|
while ($level < $maxLevel && $controller && $controller instanceof GridFieldNestedFormItemRequest) {
|
|
|
|
|
$controller = $controller->getController();
|
|
|
|
|
$level++;
|
|
|
|
|
}
|
|
|
|
|
return $level;
|
|
|
|
|
return $level >= $maxLevel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getColumnMetadata($gridField, $columnName)
|
|
|
|
@ -193,14 +229,12 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
|
|
|
|
|
public function getColumnContent($gridField, $record, $columnName)
|
|
|
|
|
{
|
|
|
|
|
$nestingLevel = $this->getNestingLevel($gridField);
|
|
|
|
|
if ($nestingLevel >= $this->getMaxNestingLevel()) {
|
|
|
|
|
if ($this->atMaxNestingLevel($gridField)) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
$gridField->addExtraClass('has-nested');
|
|
|
|
|
if ($record->ID && $record->exists()) {
|
|
|
|
|
$this->gridField = $gridField;
|
|
|
|
|
$this->record = $record;
|
|
|
|
|
$relationName = $this->getRelationName();
|
|
|
|
|
if (!$record->hasMethod($relationName)) {
|
|
|
|
|
return '';
|
|
|
|
@ -212,7 +246,7 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
return '';
|
|
|
|
|
} elseif (is_string($this->canExpandCheck)
|
|
|
|
|
&& $record->hasMethod($this->canExpandCheck)
|
|
|
|
|
&& !$this->record->{$this->canExpandCheck}($record)
|
|
|
|
|
&& !$record->{$this->canExpandCheck}($record)
|
|
|
|
|
) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
@ -251,15 +285,30 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param GridField $field
|
|
|
|
|
* @param GridField $gridField
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getHTMLFragments($field)
|
|
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
|
{
|
|
|
|
|
if (DataObject::has_extension($field->getModelClass(), Hierarchy::class)) {
|
|
|
|
|
$field->setAttribute('data-url-movetoparent', $field->Link('movetoparent'));
|
|
|
|
|
/**
|
|
|
|
|
* If we have a DataObject with the hierarchy extension, we want to allow moving items to a new parent.
|
|
|
|
|
* This is enabled by setting the data-url-movetoparent attribute on the grid field, so that the client
|
|
|
|
|
* javascript can handle the move.
|
|
|
|
|
* Implemented in getHTMLFragments since this attribute needs to be added before any rendering happens.
|
|
|
|
|
*/
|
|
|
|
|
if (is_a($gridField->getModelClass(), DataObject::class, true)
|
|
|
|
|
&& DataObject::has_extension($gridField->getModelClass(), Hierarchy::class)
|
|
|
|
|
) {
|
|
|
|
|
$gridField->setAttribute('data-url-movetoparent', $gridField->Link('movetoparent'));
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle moving a record to a new parent
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function handleMoveToParent(GridField $gridField, $request)
|
|
|
|
|
{
|
|
|
|
|
$move = $request->postVar('move');
|
|
|
|
@ -326,22 +375,30 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
return $gridField->FieldHolder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the request to show a nested item
|
|
|
|
|
*
|
|
|
|
|
* @param GridField $gridField
|
|
|
|
|
* @param HTTPRequest|null $request
|
|
|
|
|
* @param ViewableData|null $record
|
|
|
|
|
* @return HTTPResponse|RequestHandler
|
|
|
|
|
*/
|
|
|
|
|
public function handleNestedItem(GridField $gridField, $request = null, $record = null)
|
|
|
|
|
{
|
|
|
|
|
$nestingLevel = $this->getNestingLevel($gridField);
|
|
|
|
|
if ($nestingLevel >= $this->getMaxNestingLevel()) {
|
|
|
|
|
if ($this->atMaxNestingLevel($gridField)) {
|
|
|
|
|
throw new Exception('Max nesting level reached');
|
|
|
|
|
}
|
|
|
|
|
if (!$record && $request) {
|
|
|
|
|
$list = $gridField->getList();
|
|
|
|
|
if (!$record && $request && $list instanceof Filterable) {
|
|
|
|
|
$recordID = $request->param('RecordID');
|
|
|
|
|
$record = $gridField->getList()->byID($recordID);
|
|
|
|
|
$record = $list->byID($recordID);
|
|
|
|
|
}
|
|
|
|
|
if (!$record) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
$relationName = $this->getRelationName();
|
|
|
|
|
if (!$record->hasMethod($relationName)) {
|
|
|
|
|
return '';
|
|
|
|
|
throw new Exception('Invalid relation name');
|
|
|
|
|
}
|
|
|
|
|
$manager = $this->getStateManager();
|
|
|
|
|
$stateRequest = $request ?: $gridField->getForm()->getRequestHandler()->getRequest();
|
|
|
|
@ -349,7 +406,6 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
$gridField->getState(false)->setValue($gridStateStr);
|
|
|
|
|
}
|
|
|
|
|
$this->gridField = $gridField;
|
|
|
|
|
$this->record = $record;
|
|
|
|
|
$itemRequest = GridFieldNestedFormItemRequest::create(
|
|
|
|
|
$gridField,
|
|
|
|
|
$this,
|
|
|
|
@ -373,11 +429,19 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle the request to toggle a nested item in the gridfield state
|
|
|
|
|
*
|
|
|
|
|
* @param GridField $gridField
|
|
|
|
|
* @param HTTPRequest|null $request
|
|
|
|
|
* @param ViewableData|null $record
|
|
|
|
|
*/
|
|
|
|
|
public function toggleNestedItem(GridField $gridField, $request = null, $record = null)
|
|
|
|
|
{
|
|
|
|
|
if (!$record) {
|
|
|
|
|
$list = $gridField->getList();
|
|
|
|
|
if (!$record && $request && $list instanceof Filterable) {
|
|
|
|
|
$recordID = $request->param('RecordID');
|
|
|
|
|
$record = $gridField->getList()->byID($recordID);
|
|
|
|
|
$record = $list->byID($recordID);
|
|
|
|
|
}
|
|
|
|
|
$manager = $this->getStateManager();
|
|
|
|
|
if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) {
|
|
|
|
@ -389,14 +453,20 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
$state->$stateRelation = (int)$request->getVar('toggle');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function Link($action = null)
|
|
|
|
|
/**
|
|
|
|
|
* Get the link for the nested grid field
|
|
|
|
|
*/
|
|
|
|
|
public function Link($action = null): string
|
|
|
|
|
{
|
|
|
|
|
$link = Director::absoluteURL(Controller::join_links($this->gridField->Link('nested'), $action));
|
|
|
|
|
$manager = $this->getStateManager();
|
|
|
|
|
return $manager->addStateToURL($this->gridField, $link);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function ToggleLink($action = null)
|
|
|
|
|
/**
|
|
|
|
|
* Get the link for the toggle action
|
|
|
|
|
*/
|
|
|
|
|
public function ToggleLink($action = null): string
|
|
|
|
|
{
|
|
|
|
|
$link = Director::absoluteURL(Controller::join_links($this->gridField->Link('toggle'), $action, '?toggle='));
|
|
|
|
|
$manager = $this->getStateManager();
|
|
|
|
@ -417,11 +487,13 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
$gridField->getState(false)->setValue($gridStateStr);
|
|
|
|
|
}
|
|
|
|
|
foreach ($request->postVars() as $key => $val) {
|
|
|
|
|
if (preg_match("/{$gridField->getName()}-{$postKey}-(\d+)/", $key, $matches)) {
|
|
|
|
|
$list = $gridField->getList();
|
|
|
|
|
if ($list instanceof Filterable && preg_match("/{$gridField->getName()}-{$postKey}-(\d+)/", $key, $matches)) {
|
|
|
|
|
$recordID = $matches[1];
|
|
|
|
|
$nestedData = $val;
|
|
|
|
|
$record = $gridField->getList()->byID($recordID);
|
|
|
|
|
$record = $list->byID($recordID);
|
|
|
|
|
if ($record) {
|
|
|
|
|
/** @var GridField */
|
|
|
|
|
$nestedGridField = $this->handleNestedItem($gridField, null, $record);
|
|
|
|
|
$nestedGridField->setValue($nestedData);
|
|
|
|
|
$nestedGridField->saveInto($record);
|
|
|
|
@ -433,8 +505,10 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList)
|
|
|
|
|
{
|
|
|
|
|
if ($this->relationName == 'Children'
|
|
|
|
|
&& is_a($gridField->getModelClass(), DataObject::class, true)
|
|
|
|
|
&& DataObject::has_extension($gridField->getModelClass(), Hierarchy::class)
|
|
|
|
|
&& $gridField->getForm()->getController() instanceof ModelAdmin
|
|
|
|
|
&& $dataList instanceof Filterable
|
|
|
|
|
) {
|
|
|
|
|
$dataList = $dataList->filter('ParentID', 0);
|
|
|
|
|
}
|
|
|
|
|