Skip to content

Commit

Permalink
fix readonly actions / fix somes types
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Dec 2, 2024
1 parent 70f6ecd commit ce79a57
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
64 changes: 59 additions & 5 deletions src/ActionsGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LeKoala\CmsActions;

use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\MathTrig\Exp;
use ReflectionMethod;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\Form;
Expand Down Expand Up @@ -152,6 +153,7 @@ public function recordCmsUtils()
$reflectionMethod = new ReflectionMethod($owner, 'getRecord');
$record = count($reflectionMethod->getParameters()) > 0 ? $owner->getRecord(0) : $owner->getRecord();
if ($record && $record->hasMethod('getCMSUtils')) {
//@phpstan-ignore-next-line
$utils = $record->getCMSUtils();
$this->extend('onCMSUtils', $utils, $record);
$record->extend('onCMSUtils', $utils);
Expand All @@ -160,9 +162,38 @@ public function recordCmsUtils()
return false;
}

/**
* @param Form $form
* @return void
*/
public function updateItemEditForm($form)
{
$record = $this->owner->getRecord();
if (!$record) {
return;
}

// We get the actions as defined on our record
/** @var FieldList $CMSActions */
$CMSActions = $record->getCMSActions();

$FormActions = $form->Actions();

// Push our actions that are otherwise ignored by SilverStripe
foreach ($CMSActions as $CMSAction) {
$action = $FormActions->fieldByName($CMSAction->getName());

if ($action) {
// If it has been made readonly, revert
if ($CMSAction->isReadonly() != $action->isReadonly()) {
$FormActions->replaceField($action->getName(), $action->setReadonly($CMSAction->isReadonly()));
}
}
}
}

/**
* Called by GridField_ItemRequest
* GridField_ItemRequest defines its own set of actions so we need to add ours
* We add our custom save&close, save&next and other tweaks
* Actions can be made readonly after this extension point
* @param FieldList $actions
Expand Down Expand Up @@ -546,7 +577,7 @@ protected function getBtnClassForRecord(DataObject $record)
/**
* @param string $action
* @param array<FormField>|FieldList $definedActions
* @return mixed|CompositeField|null
* @return FormField|null
*/
protected static function findAction($action, $definedActions)
{
Expand Down Expand Up @@ -632,13 +663,26 @@ protected function forwardActionToRecord($action, $data = [], $form = null)
$availableActions
));
}

if ($clickedAction->isReadonly() || $clickedAction->isDisabled()) {
return $this->owner->httpError(403, sprintf(
'Action %s is disabled',
$clickedAction->getName(),
));
}

$message = null;
$error = false;

// Check record BEFORE the action
// It can be deleted by the action, and it will return to the list
$isNewRecord = $record->ID === 0;

$actionTitle = $clickedAction->getName();
if (method_exists($clickedAction, 'getTitle')) {
$actionTitle = $clickedAction->getTitle();
}

try {
$result = $record->$action($data, $form, $controller);

Expand All @@ -653,7 +697,10 @@ protected function forwardActionToRecord($action, $data = [], $form = null)
$message = _t(
'ActionsGridFieldItemRequest.FAILED',
'Action {action} failed on {name}',
['action' => $clickedAction->getTitle(), 'name' => $record->i18n_singular_name()]
[
'action' => $actionTitle,
'name' => $record->i18n_singular_name(),
]
);
} elseif (is_string($result)) {
// Result is a message
Expand All @@ -670,7 +717,10 @@ protected function forwardActionToRecord($action, $data = [], $form = null)
$message = _t(
'ActionsGridFieldItemRequest.DONE',
'Action {action} was done on {name}',
['action' => $clickedAction->getTitle(), 'name' => $record->i18n_singular_name()]
[
'action' => $actionTitle,
'name' => $record->i18n_singular_name(),
]
);
}
$status = 'good';
Expand Down Expand Up @@ -822,7 +872,7 @@ public function doSaveAndClose($data, $form)
* @param string $dir prev|next
* @param array<string,mixed> $data The form data
* @param Form|null $form
* @return void
* @return HTTPResponse
*/
protected function doSaveAndAdjacent(string $dir, array $data, ?Form $form)
{
Expand All @@ -837,6 +887,10 @@ protected function doSaveAndAdjacent(string $dir, array $data, ?Form $form)
throw new Exception("Could not get class");
}

if (!in_array($dir, ['prev', 'next'])) {
throw new Exception("Invalid dir $dir");
}

$method = match ($dir) {
'prev' => 'getCustomPreviousRecordID',
'next' => 'getCustomNextRecordID',
Expand Down
8 changes: 5 additions & 3 deletions src/CustomButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ trait CustomButton
* @var array
*/
private static $default_classes = [
'btn', 'btn-info'
'btn',
'btn-info'
];

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ public function getTitle()
/**
* Set the value of title
*
* @param string $is
* @param string $title
* @return $this
*/
public function setTitle($title)
Expand Down Expand Up @@ -206,6 +207,7 @@ public function renderLastIcon()

/**
* Get the value of confirmation
* @return bool|string
*/
public function getConfirmation()
{
Expand All @@ -215,7 +217,7 @@ public function getConfirmation()
/**
* Set the value of confirmation
*
* @param string|bool A confirm message or true for a generic message
* @param string|bool $confirmation A confirm message or true for a generic message
* @return $this
*/
public function setConfirmation($confirmation)
Expand Down
4 changes: 4 additions & 0 deletions src/DefaultLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public function getControllerLink($action, array $params = null)

/**
* Get the value of link
* @return string
*/
public function getLink()
{
Expand All @@ -102,6 +103,7 @@ public function getLink()
/**
* Set the value of link
*
* @param string $link
* @return $this
*/
public function setLink($link)
Expand All @@ -113,6 +115,7 @@ public function setLink($link)

/**
* Get the value of newWindow
* @return bool
*/
public function getNewWindow()
{
Expand All @@ -122,6 +125,7 @@ public function getNewWindow()
/**
* Set the value of newWindow
*
* @param bool $newWindow
* @return $this
*/
public function setNewWindow($newWindow)
Expand Down
23 changes: 20 additions & 3 deletions src/GridFieldSaveAllButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use Exception;
use SilverStripe\Control\HTTPResponse;

/**
* When using inline editing on a ModelAdmin, there is no save button
Expand All @@ -25,12 +28,23 @@ class GridFieldSaveAllButton extends GridFieldTableButton
protected $allowEmptyResponse = true;
protected bool $shouldReload = false;

/**
* @param string $targetFragment
* @param mixed $buttonLabel
*/
public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null)
{
parent::__construct($targetFragment, $buttonLabel);
$this->buttonLabel = $buttonLabel ?? _t('GridFieldSaveAllButton.SaveAll', 'Save all');
}

/**
* @param GridField $gridField
* @param Controller $controller
* @param array $arguments
* @param array $data
* @return ?HTTPResponse
*/
public function handle(GridField $gridField, Controller $controller, $arguments = [], $data = [])
{
$fieldName = $gridField->getName();
Expand All @@ -40,9 +54,12 @@ public function handle(GridField $gridField, Controller $controller, $arguments
// Without this, handleSave does not work
$gridField->setSubmittedValue($data[$fieldName]);

if (!($list instanceof DataList)) {
throw new Exception("Requires a DataList");
}

$updatedData = $data[$fieldName]['GridFieldEditableColumns'] ?? [];
foreach ($updatedData as $id => $values) {
/** @var DataObject $record */
$record = $list->byID($id);
if (!$record) {
continue;
Expand All @@ -61,17 +78,17 @@ public function handle(GridField $gridField, Controller $controller, $arguments
}
$newData = $data[$fieldName]['GridFieldAddNewInlineButton'] ?? [];
foreach ($newData as $idx => $values) {
$record = new $model;
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();
Expand Down
3 changes: 3 additions & 0 deletions src/ProgressiveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

trait ProgressiveAction
{
/**
* @var bool
*/
protected $progressive = false;

/**
Expand Down

0 comments on commit ce79a57

Please sign in to comment.