Skip to content

Commit

Permalink
update requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed May 10, 2024
1 parent 54716a4 commit 1399ea3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 62 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
116 changes: 58 additions & 58 deletions src/ActionsGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<string,mixed> $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);
Expand All @@ -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<string,mixed> $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
*
Expand All @@ -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<mixed> $data
* @return string
Expand Down

0 comments on commit 1399ea3

Please sign in to comment.