mirror of
https://github.com/symbiote/silverstripe-gridfieldextensions.git
synced 2024-10-22 15:05:39 +00:00
Refactored nested GridField move to parent functionality.
This commit is contained in:
parent
32d980e13c
commit
cfcf8d2e8e
@ -266,9 +266,10 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|||||||
/** @var DataList */
|
/** @var DataList */
|
||||||
$list = $gridField->getList();
|
$list = $gridField->getList();
|
||||||
$id = isset($move['id']) ? (int) $move['id'] : null;
|
$id = isset($move['id']) ? (int) $move['id'] : null;
|
||||||
|
if (!$id) {
|
||||||
|
throw new HTTPResponse_Exception('Missing ID', 400);
|
||||||
|
}
|
||||||
$to = isset($move['parent']) ? (int)$move['parent'] : null;
|
$to = isset($move['parent']) ? (int)$move['parent'] : null;
|
||||||
$parent = null;
|
|
||||||
if ($id) {
|
|
||||||
// should be possible either on parent or child grid field, or nested grid field from parent
|
// should be possible either on parent or child grid field, or nested grid field from parent
|
||||||
$parent = $to ? $list->byID($to) : null;
|
$parent = $to ? $list->byID($to) : null;
|
||||||
if (!$parent
|
if (!$parent
|
||||||
@ -279,7 +280,11 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|||||||
$parent = $gridField->getForm()->getController()->getRecord();
|
$parent = $gridField->getForm()->getController()->getRecord();
|
||||||
}
|
}
|
||||||
$child = $list->byID($id);
|
$child = $list->byID($id);
|
||||||
if ($parent || $child || $to === 0) {
|
// we need either a parent or a child, or a move to top level at this stage
|
||||||
|
if (!($parent || $child || $to === 0)) {
|
||||||
|
throw new HTTPResponse_Exception('Invalid request', 400);
|
||||||
|
}
|
||||||
|
// parent or child might be from another grid field, so we need to search via DataList in some cases
|
||||||
if (!$parent && $to) {
|
if (!$parent && $to) {
|
||||||
$parent = DataList::create($gridField->getModelClass())->byID($to);
|
$parent = DataList::create($gridField->getModelClass())->byID($to);
|
||||||
}
|
}
|
||||||
@ -287,9 +292,13 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|||||||
$child = DataList::create($gridField->getModelClass())->byID($id);
|
$child = DataList::create($gridField->getModelClass())->byID($id);
|
||||||
}
|
}
|
||||||
if ($child) {
|
if ($child) {
|
||||||
|
if (!$child->canEdit()) {
|
||||||
|
throw new HTTPResponse_Exception('Not allowed', 403);
|
||||||
|
}
|
||||||
if ($child->hasExtension(Hierarchy::class)) {
|
if ($child->hasExtension(Hierarchy::class)) {
|
||||||
$child->ParentID = $parent ? $parent->ID : 0;
|
$child->ParentID = $parent ? $parent->ID : 0;
|
||||||
}
|
}
|
||||||
|
// validate that the record is still valid
|
||||||
$validationResult = $child->validate();
|
$validationResult = $child->validate();
|
||||||
if ($validationResult->isValid()) {
|
if ($validationResult->isValid()) {
|
||||||
if ($child->hasExtension(Versioned::class)) {
|
if ($child->hasExtension(Versioned::class)) {
|
||||||
@ -298,6 +307,7 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|||||||
$child->write();
|
$child->write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reorder items at the same time, if applicable
|
||||||
/** @var GridFieldOrderableRows */
|
/** @var GridFieldOrderableRows */
|
||||||
$orderableRows = $gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class);
|
$orderableRows = $gridField->getConfig()->getComponentByType(GridFieldOrderableRows::class);
|
||||||
if ($orderableRows) {
|
if ($orderableRows) {
|
||||||
@ -313,8 +323,6 @@ class GridFieldNestedForm extends AbstractGridFieldComponent implements
|
|||||||
throw new HTTPResponse_Exception($message['message'], 400);
|
throw new HTTPResponse_Exception($message['message'], 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return $gridField->FieldHolder();
|
return $gridField->FieldHolder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user