|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\Control\Director; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
8
|
|
|
use SilverStripe\ORM\DataObject; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* When using inline editing on a ModelAdmin, there is no save button |
|
12
|
|
|
* This allows saving the records |
|
13
|
|
|
* It needs a custom endpoint because somehow, new records are not sent along |
|
14
|
|
|
*/ |
|
15
|
|
|
class GridFieldSaveAllButton extends GridFieldTableButton |
|
16
|
|
|
{ |
|
17
|
|
|
protected $fontIcon = 'save'; |
|
18
|
|
|
public bool $submitData = true; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $noAjax = false; |
|
23
|
|
|
protected ?string $completeMessage = null; |
|
24
|
|
|
protected ?bool $useHandleSave = true; |
|
25
|
|
|
protected $allowEmptyResponse = true; |
|
26
|
|
|
protected bool $shouldReload = false; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($targetFragment = 'buttons-before-left', $buttonLabel = null) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($targetFragment, $buttonLabel); |
|
31
|
|
|
$this->buttonLabel = $buttonLabel ?? _t('GridFieldSaveAllButton.SaveAll', 'Save all'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function handle(GridField $gridField, Controller $controller, $arguments = [], $data = []) |
|
35
|
|
|
{ |
|
36
|
|
|
$fieldName = $gridField->getName(); |
|
37
|
|
|
$list = $gridField->getList(); |
|
38
|
|
|
$model = $gridField->getModelClass(); |
|
39
|
|
|
|
|
40
|
|
|
// Without this, handleSave does not work |
|
41
|
|
|
$gridField->setSubmittedValue($data[$fieldName]); |
|
42
|
|
|
|
|
43
|
|
|
$updatedData = $data[$fieldName]['GridFieldEditableColumns'] ?? []; |
|
44
|
|
|
foreach ($updatedData as $id => $values) { |
|
45
|
|
|
/** @var DataObject $record */ |
|
46
|
|
|
$record = $list->byID($id); |
|
47
|
|
|
if (!$record) { |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
// You can use the grid field component or a simple loop with write |
|
51
|
|
|
if ($this->useHandleSave) { |
|
52
|
|
|
/** @var \Symbiote\GridFieldExtensions\GridFieldEditableColumns $component */ |
|
53
|
|
|
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldEditableColumns::class); |
|
|
|
|
|
|
54
|
|
|
$component->handleSave($gridField, $record); |
|
55
|
|
|
} else { |
|
56
|
|
|
foreach ($values as $k => $v) { |
|
57
|
|
|
$record->$k = $v; |
|
58
|
|
|
} |
|
59
|
|
|
$record->write(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
$newData = $data[$fieldName]['GridFieldAddNewInlineButton'] ?? []; |
|
63
|
|
|
foreach ($newData as $idx => $values) { |
|
64
|
|
|
if ($this->useHandleSave) { |
|
65
|
|
|
/** @var \Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton $component */ |
|
66
|
|
|
$component = $gridField->getConfig()->getComponentByType(\Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton::class); |
|
|
|
|
|
|
67
|
|
|
$component->handleSave($gridField, $record); |
|
|
|
|
|
|
68
|
|
|
} else { |
|
69
|
|
|
$record = new $model; |
|
70
|
|
|
foreach ($values as $k => $v) { |
|
71
|
|
|
$record->$k = $v; |
|
72
|
|
|
} |
|
73
|
|
|
$record->write(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$response = $controller->getResponse(); |
|
78
|
|
|
|
|
79
|
|
|
if (Director::is_ajax()) { |
|
80
|
|
|
if (!$this->completeMessage) { |
|
81
|
|
|
$this->completeMessage = _t('GridFieldSaveAllButton.DONE', 'All saved'); |
|
82
|
|
|
} |
|
83
|
|
|
if ($this->shouldReload) { |
|
84
|
|
|
ActionsGridFieldItemRequest::addXReload($controller); |
|
85
|
|
|
} |
|
86
|
|
|
$response->addHeader('X-Status', rawurlencode($this->completeMessage)); |
|
87
|
|
|
return null; |
|
88
|
|
|
} else { |
|
89
|
|
|
return $controller->redirectBack(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the value of completeMessage |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getCompleteMessage(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->completeMessage; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set the value of completeMessage |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $completeMessage |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setCompleteMessage($completeMessage): self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->completeMessage = $completeMessage; |
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Get the value of useHandleSave |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getUseHandleSave(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->useHandleSave; |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set the value of useHandleSave |
|
122
|
|
|
* |
|
123
|
|
|
* @param bool $useHandleSave |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setUseHandleSave($useHandleSave): self |
|
126
|
|
|
{ |
|
127
|
|
|
$this->useHandleSave = $useHandleSave; |
|
128
|
|
|
return $this; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get the value of shouldReload |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getShouldReload(): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->shouldReload; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set the value of shouldReload |
|
141
|
|
|
* |
|
142
|
|
|
* @param bool $shouldReload |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setShouldReload($shouldReload): self |
|
145
|
|
|
{ |
|
146
|
|
|
$this->shouldReload = $shouldReload; |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
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