diff --git a/composer.json b/composer.json index afa8dce..6538076 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,10 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "silverstripe/recipe-plugin": "^1 || ^2", - "silverstripe/vendor-plugin": "^1 || ^2", - "silverstripe/framework": "^4.4 || ^5" + "php": "^8.1", + "silverstripe/recipe-plugin": "^2", + "silverstripe/vendor-plugin": "^2", + "silverstripe/framework": "^5" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/src/ActionsGridFieldItemRequest.php b/src/ActionsGridFieldItemRequest.php index e64a2f1..174fa02 100644 --- a/src/ActionsGridFieldItemRequest.php +++ b/src/ActionsGridFieldItemRequest.php @@ -25,8 +25,6 @@ use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; -use SilverStripe\Forms\GridField\GridFieldStateManagerInterface; -use SilverStripe\Forms\GridField\GridFieldPaginator; use ReflectionObject; /** @@ -443,16 +441,11 @@ public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) if (Controller::has_curr()) { $prevLink = $nextLink = null; if (!$isCustom && $this->owner instanceof GridFieldDetailForm_ItemRequest) { - $this->owner->getStateManager(); - $reflObject = new ReflectionObject($this->owner); - $reflMethod = $reflObject->getMethod('getEditLinkForAdjacentRecord'); - $reflMethod->setAccessible(true); - if ($getPreviousRecordID) { - $prevLink = $reflMethod->invoke($this->owner, -1); + $prevLink = $this->getPublicEditLinkForAdjacentRecord(-1); } if ($getNextRecordID) { - $nextLink = $reflMethod->invoke($this->owner, +1); + $nextLink = $this->getPublicEditLinkForAdjacentRecord(+1); } } @@ -483,6 +476,20 @@ public function addSaveNextAndPrevious(FieldList $actions, DataObject $record) } } + public function getPublicEditLinkForAdjacentRecord(int $offset): ?string + { + $this->owner->getStateManager(); + $reflObject = new ReflectionObject($this->owner); + $reflMethod = $reflObject->getMethod('getEditLinkForAdjacentRecord'); + $reflMethod->setAccessible(true); + + try { + return $reflMethod->invoke($this->owner, $offset); + } catch (Exception $e) { + return null; + } + } + /** * @param FieldList $actions * @param DataObject $record @@ -773,24 +780,22 @@ public function doSaveAndClose($data, $form) $controller = $this->getToplevelController(); $link = $this->getBackLink(); - $link = $this->addGridState($link, $data); - $controller->getResponse()->addHeader("X-Pjax", "Content"); + // Doesn't seem to be needed anymore + // $link = $this->addGridState($link, $data); - // Prevent Already directed to errors - // $controller->getResponse()->addHeader("Location", $link); + $controller->getResponse()->addHeader("X-Pjax", "Content"); return $controller->redirect($link); } /** - * Saves the form and goes back to the next item - * + * @param string $dir prev|next * @param array $data The form data - * @param Form $form The form object - * @return HTTPResponse + * @param Form|null $form + * @return void */ - public function doSaveAndNext($data, $form) + protected function doSaveAndAdjacent(string $dir, array $data, ?Form $form) { $record = $this->owner->record; $this->owner->doSave($data, $form); @@ -803,25 +808,48 @@ public function doSaveAndNext($data, $form) throw new Exception("Could not get class"); } - $getNextRecordID = $this->getCustomNextRecordID($record); + $method = match ($dir) { + 'prev' => 'getCustomPreviousRecordID', + 'next' => 'getCustomNextRecordID', + }; - /** @var ?DataObject $next */ - $next = $class::get()->byID($getNextRecordID); + $offset = match ($dir) { + 'prev' => -1, + 'next' => +1, + }; - $link = $this->owner->getEditLink($getNextRecordID); - $link = $this->addGridState($link, $data); + $adjRecordID = $this->$method($record); + + /** @var ?DataObject $adj */ + $adj = $class::get()->byID($adjRecordID); + + $useCustom = $this->useCustomPrevNext($record); + $link = $this->getPublicEditLinkForAdjacentRecord($offset); + if (!$link || $useCustom) { + $link = $this->owner->getEditLink($adjRecordID); + $link = $this->addGridState($link, $data); + } // Link to a specific tab if set, see cms-actions.js - if ($next && !empty($data['_activetab'])) { + if ($adj && !empty($data['_activetab'])) { $link .= sprintf('#%s', $data['_activetab']); } - // Prevent Already directed to errors - // $controller->getResponse()->addHeader("Location", $link); - return $controller->redirect($link); } + /** + * Saves the form and goes back to the next item + * + * @param array $data The form data + * @param Form $form The form object + * @return HTTPResponse + */ + public function doSaveAndNext($data, $form) + { + return $this->doSaveAndAdjacent('next', $data, $form); + } + /** * Saves the form and goes to the previous item * @@ -831,40 +859,12 @@ public function doSaveAndNext($data, $form) */ public function doSaveAndPrev($data, $form) { - $record = $this->owner->record; - $this->owner->doSave($data, $form); - // Redirect after save - $controller = $this->getToplevelController(); - $controller->getResponse()->addHeader("X-Pjax", "Content"); - - $class = get_class($record); - if (!$class) { - throw new Exception("Could not get class"); - } - - $getPreviousRecordID = $this->getCustomPreviousRecordID($record); - if (!$class) { - throw new Exception("Could not get class"); - } - - /** @var ?DataObject $prev */ - $prev = $class::get()->byID($getPreviousRecordID); - - $link = $this->owner->getEditLink($getPreviousRecordID); - $link = $this->addGridState($link, $data); - - // Link to a specific tab if set, see cms-actions.js - if ($prev && !empty($data['_activetab'])) { - $link .= sprintf('#%s', $data['_activetab']); - } - - // Prevent Already directed to errors - // $controller->getResponse()->addHeader("Location", $link); - - return $controller->redirect($link); + return $this->doSaveAndAdjacent('prev', $data, $form); } /** + * Check if we can remove this safely + * @deprecated * @param string $url * @param array $data * @return string