| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\CmsActions; |
||
| 4 | |||
| 5 | use Symbiote\GridFieldExtensions\GridFieldEditableColumns; |
||
|
0 ignored issues
–
show
|
|||
| 6 | use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton; |
||
|
0 ignored issues
–
show
The type
Symbiote\GridFieldExtens...FieldAddNewInlineButton was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 7 | use SilverStripe\Control\Controller; |
||
| 8 | use SilverStripe\Control\Director; |
||
| 9 | use SilverStripe\Forms\GridField\GridField; |
||
| 10 | use SilverStripe\ORM\DataList; |
||
| 11 | use Exception; |
||
| 12 | use SilverStripe\Control\HTTPResponse; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * When using inline editing on a ModelAdmin, there is no save button |
||
| 16 | * This allows saving the records |
||
| 17 | * It needs a custom endpoint because somehow, new records are not sent along |
||
| 18 | */ |
||
| 19 | class GridFieldSaveAllButton extends GridFieldTableButton |
||
| 20 | { |
||
| 21 | protected $fontIcon = 'save'; |
||
| 22 | public bool $submitData = true; |
||
| 23 | /** |
||
| 24 | * @var boolean |
||
| 25 | */ |
||
| 26 | protected $noAjax = false; |
||
| 27 | protected ?string $completeMessage = null; |
||
| 28 | protected ?bool $useHandleSave = true; |
||
| 29 | protected $allowEmptyResponse = true; |
||
| 30 | protected bool $shouldReload = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $targetFragment |
||
| 34 | * @param mixed $buttonLabel |
||
| 35 | */ |
||
| 36 | public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null) |
||
| 37 | { |
||
| 38 | parent::__construct($targetFragment, $buttonLabel); |
||
| 39 | $this->buttonLabel = $buttonLabel ?? _t('GridFieldSaveAllButton.SaveAll', 'Save all'); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param GridField $gridField |
||
| 44 | * @param Controller $controller |
||
| 45 | * @param array $arguments |
||
| 46 | * @param array $data |
||
| 47 | * @return ?HTTPResponse |
||
| 48 | */ |
||
| 49 | public function handle(GridField $gridField, Controller $controller, $arguments = [], $data = []) |
||
| 50 | { |
||
| 51 | $fieldName = $gridField->getName(); |
||
| 52 | $list = $gridField->getList(); |
||
| 53 | $model = $gridField->getModelClass(); |
||
| 54 | |||
| 55 | // Without this, handleSave does not work |
||
| 56 | $gridField->setSubmittedValue($data[$fieldName]); |
||
| 57 | |||
| 58 | if (!($list instanceof DataList)) { |
||
| 59 | throw new Exception("Requires a DataList"); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($this->useHandleSave) { |
||
| 63 | /** @var GridFieldEditableColumns $component */ |
||
| 64 | $component = $gridField->getConfig()->getComponentByType(GridFieldEditableColumns::class); |
||
| 65 | $component->handleSave($gridField, singleton($model)); |
||
| 66 | |||
| 67 | /** @var GridFieldAddNewInlineButton $component */ |
||
| 68 | $component = $gridField->getConfig()->getComponentByType(GridFieldAddNewInlineButton::class); |
||
| 69 | $component->handleSave($gridField, singleton($model)); |
||
| 70 | } else { |
||
| 71 | foreach ($data[$fieldName]['GridFieldEditableColumns'] ?? [] as $id => $values) { |
||
| 72 | $record = $list->byID($id); |
||
| 73 | |||
| 74 | if (!$record) { |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | |||
| 78 | foreach ($values as $key => $value) { |
||
| 79 | $record->$key = $value; |
||
| 80 | } |
||
| 81 | |||
| 82 | $record->write(); |
||
| 83 | } |
||
| 84 | |||
| 85 | foreach ($data[$fieldName]['GridFieldAddNewInlineButton'] ?? [] as $values) { |
||
| 86 | $record = new $model; |
||
| 87 | |||
| 88 | foreach ($values as $key => $value) { |
||
| 89 | $record->$key = $value; |
||
| 90 | } |
||
| 91 | |||
| 92 | $record->write(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $response = $controller->getResponse(); |
||
| 97 | |||
| 98 | if (Director::is_ajax()) { |
||
| 99 | if (!$this->completeMessage) { |
||
| 100 | $this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'All saved'); |
||
| 101 | } |
||
| 102 | |||
| 103 | if ($this->shouldReload) { |
||
| 104 | ActionsGridFieldItemRequest::addXReload($controller); |
||
| 105 | } |
||
| 106 | |||
| 107 | $response->addHeader('X-Status', rawurlencode($this->completeMessage)); |
||
| 108 | |||
| 109 | return null; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $controller->redirectBack(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get the value of completeMessage |
||
| 117 | */ |
||
| 118 | public function getCompleteMessage(): string |
||
| 119 | { |
||
| 120 | return $this->completeMessage; |
||
|
0 ignored issues
–
show
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set the value of completeMessage |
||
| 125 | * |
||
| 126 | * @param string $completeMessage |
||
| 127 | */ |
||
| 128 | public function setCompleteMessage($completeMessage): self |
||
| 129 | { |
||
| 130 | $this->completeMessage = $completeMessage; |
||
| 131 | return $this; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get the value of useHandleSave |
||
| 136 | */ |
||
| 137 | public function getUseHandleSave(): bool |
||
| 138 | { |
||
| 139 | return $this->useHandleSave; |
||
|
0 ignored issues
–
show
|
|||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the value of useHandleSave |
||
| 144 | * |
||
| 145 | * @param bool $useHandleSave |
||
| 146 | */ |
||
| 147 | public function setUseHandleSave($useHandleSave): self |
||
| 148 | { |
||
| 149 | $this->useHandleSave = $useHandleSave; |
||
| 150 | return $this; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get the value of shouldReload |
||
| 155 | */ |
||
| 156 | public function getShouldReload(): bool |
||
| 157 | { |
||
| 158 | return $this->shouldReload; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Set the value of shouldReload |
||
| 163 | * |
||
| 164 | * @param bool $shouldReload |
||
| 165 | */ |
||
| 166 | public function setShouldReload($shouldReload): self |
||
| 167 | { |
||
| 168 | $this->shouldReload = $shouldReload; |
||
| 169 | return $this; |
||
| 170 | } |
||
| 171 | } |
||
| 172 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths