Skip to content

Commit

Permalink
use handleSave, refactor X reload, ajax save
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed May 16, 2024
1 parent 115177e commit 9352c73
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 20 deletions.
25 changes: 20 additions & 5 deletions src/ActionsGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,7 @@ protected function forwardActionToRecord($action, $data = [], $form = null)

//@phpstan-ignore-next-line
if (method_exists($clickedAction, 'getShouldRefresh') && $clickedAction->getShouldRefresh()) {
$controller->getResponse()->addHeader('X-Reload', "true");
// Requires a ControllerURL as well, see https://github.com/silverstripe/silverstripe-admin/blob/a3aa41cea4c4df82050eef65ad5efcfae7bfde69/client/src/legacy/LeftAndMain.js#L773-L780
$url = $controller->getReferer();
$controller->getResponse()->addHeader('X-ControllerURL', $url);
self::addXReload($controller);
}
// 4xx status makes a red box
if ($error) {
Expand All @@ -722,7 +719,8 @@ protected function forwardActionToRecord($action, $data = [], $form = null)
// Custom redirect
//@phpstan-ignore-next-line
if (method_exists($clickedAction, 'getRedirectURL') && $clickedAction->getRedirectURL()) {
$controller->getResponse()->addHeader('X-Reload', "true"); // we probably need a full ui refresh
// we probably need a full ui refresh
self::addXReload($clickedAction->getRedirectURL());
//@phpstan-ignore-next-line
return $controller->redirect($clickedAction->getRedirectURL());
}
Expand All @@ -731,6 +729,23 @@ protected function forwardActionToRecord($action, $data = [], $form = null)
return $this->redirectAfterAction($isNewRecord, $record);
}

/**
* Requires a ControllerURL as well, see
* https://github.com/silverstripe/silverstripe-admin/blob/a3aa41cea4c4df82050eef65ad5efcfae7bfde69/client/src/legacy/LeftAndMain.js#L773-L780
*
* @param Controller $controller
* @param string|null $url
* @return void
*/
public static function addXReload(Controller $controller, ?string $url = null): void
{
if (!$url) {
$url = $controller->getReferer();
}
$controller->getResponse()->addHeader('X-ControllerURL', $url);
$controller->getResponse()->addHeader('X-Reload', "true");
}

/**
* Handles custom links
*
Expand Down
82 changes: 67 additions & 15 deletions src/GridFieldSaveAllButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class GridFieldSaveAllButton extends GridFieldTableButton
*/
protected $noAjax = false;
protected ?string $completeMessage = null;
protected ?bool $useHandleSave = true;
protected $allowEmptyResponse = true;
protected bool $shouldReload = false;

public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null)
{
Expand All @@ -44,33 +47,44 @@ public function handle(GridField $gridField, Controller $controller, $arguments
if (!$record) {
continue;
}
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class);
$component->handleSave($gridField, $record);
// foreach ($values as $k => $v) {
// $record->$k = $v;
// }
// $record->write();
// You can use the grid field component or a simple loop with write
if ($this->useHandleSave) {
/** @var \Symbiote\GridFieldExtensions\GridFieldEditableColumns $component */
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class);
$component->handleSave($gridField, $record);
} else {
foreach ($values as $k => $v) {
$record->$k = $v;
}
$record->write();
}
}
$newData = $data[$fieldName]['GridFieldAddNewInlineButton'] ?? [];
foreach ($newData as $idx => $values) {
$record = new $model;
foreach ($values as $k => $v) {
$record->$k = $v;
if ($this->useHandleSave) {
/** @var \Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton $component */
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton::class);
$component->handleSave($gridField, $record);
} else {
$record = new $model;
foreach ($values as $k => $v) {
$record->$k = $v;
}
$record->write();
}
$record->write();
}

$response = $controller->getResponse();

if (Director::is_ajax()) {
if (!$this->completeMessage) {
$this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'ALL SAVED!');
$this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'All saved');
}
if ($this->shouldReload) {
ActionsGridFieldItemRequest::addXReload($controller);
}
// Reload for now since we mess up with the PJAX fragment
$url = $controller->getReferer();
$response->addHeader('X-ControllerURL', $url);
$response->addHeader('X-Reload', true);
$response->addHeader('X-Status', rawurlencode($this->completeMessage));
return null;
} else {
return $controller->redirectBack();
}
Expand All @@ -94,4 +108,42 @@ public function setCompleteMessage($completeMessage): self
$this->completeMessage = $completeMessage;
return $this;
}

/**
* Get the value of useHandleSave
*/
public function getUseHandleSave(): bool
{
return $this->useHandleSave;
}

/**
* Set the value of useHandleSave
*
* @param bool $useHandleSave
*/
public function setUseHandleSave($useHandleSave): self
{
$this->useHandleSave = $useHandleSave;
return $this;
}

/**
* Get the value of shouldReload
*/
public function getShouldReload(): bool
{
return $this->shouldReload;
}

/**
* Set the value of shouldReload
*
* @param bool $shouldReload
*/
public function setShouldReload($shouldReload): self
{
$this->shouldReload = $shouldReload;
return $this;
}
}
1 change: 1 addition & 0 deletions src/GridFieldTableButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public function handleAction(GridField $gridField, $actionName, $arguments, $dat
return $result;
}

// This can be helpful if you want to refresh the whole form for PJAX requests
if ($this->allowEmptyResponse) {
return;
}
Expand Down

0 comments on commit 9352c73

Please sign in to comment.