1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\Director; |
8
|
|
|
use SilverStripe\Forms\GridField\GridField; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider; |
10
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
11
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler; |
12
|
|
|
use SilverStripe\Core\Injector\Injectable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Provide a simple way to declare buttons that affects a whole GridField |
16
|
|
|
* |
17
|
|
|
* This implements a URL Handler that can be called by the button |
18
|
|
|
*/ |
19
|
|
|
abstract class GridFieldTableButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler |
20
|
|
|
{ |
21
|
|
|
use ProgressiveAction; |
22
|
|
|
use Injectable; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Fragment to write the button to |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $targetFragment; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var boolean |
32
|
|
|
*/ |
33
|
|
|
protected $noAjax = true; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
protected $allowEmptyResponse = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $buttonLabel; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $fontIcon; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $parentID; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $confirm; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $prompt; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $promptDefault; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array<string,mixed> |
72
|
|
|
*/ |
73
|
|
|
protected $attributes = []; |
74
|
|
|
|
75
|
|
|
public bool $submitData = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $targetFragment The HTML fragment to write the button into |
79
|
|
|
* @param string $buttonLabel |
80
|
|
|
*/ |
81
|
|
|
public function __construct($targetFragment = "buttons-before-right", $buttonLabel = null) |
82
|
|
|
{ |
83
|
|
|
$this->targetFragment = $targetFragment; |
84
|
|
|
if ($buttonLabel) { |
85
|
|
|
$this->buttonLabel = $buttonLabel; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getActionName() |
93
|
|
|
{ |
94
|
|
|
$class = (new ReflectionClass(get_called_class()))->getShortName(); |
95
|
|
|
|
96
|
|
|
// ! without lowercase, it does not work |
97
|
|
|
return strtolower(str_replace('Button', '', $class)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getButtonLabel() |
104
|
|
|
{ |
105
|
|
|
return $this->buttonLabel; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Place the export button in a <p> tag below the field |
110
|
|
|
* @return array<string,mixed> |
111
|
|
|
*/ |
112
|
|
|
public function getHTMLFragments($gridField) |
113
|
|
|
{ |
114
|
|
|
$action = $this->getActionName(); |
115
|
|
|
|
116
|
|
|
$button = new CustomGridField_FormAction( |
117
|
|
|
$gridField, |
118
|
|
|
$action, |
119
|
|
|
$this->getButtonLabel(), |
120
|
|
|
$action, |
121
|
|
|
[] |
122
|
|
|
); |
123
|
|
|
if ($this->submitData) { |
124
|
|
|
$button->submitData = true; |
125
|
|
|
} |
126
|
|
|
$button->addExtraClass('btn btn-secondary action_' . $action); |
127
|
|
|
if ($this->noAjax) { |
128
|
|
|
$button->addExtraClass('no-ajax'); |
129
|
|
|
} |
130
|
|
|
if ($this->fontIcon) { |
131
|
|
|
$button->addExtraClass('font-icon-' . $this->fontIcon); |
132
|
|
|
} |
133
|
|
|
//TODO: replace prompt and confirm with inline js |
134
|
|
|
if ($this->prompt) { |
135
|
|
|
$button->setAttribute('data-prompt', $this->prompt); |
136
|
|
|
$promptDefault = $this->getPromptDefault(); |
137
|
|
|
if ($promptDefault) { |
138
|
|
|
$button->setAttribute('data-prompt-default', $promptDefault); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
if ($this->progressive) { |
142
|
|
|
$button->setProgressive(true); |
143
|
|
|
} |
144
|
|
|
if ($this->confirm) { |
145
|
|
|
$button->setAttribute('data-confirm', $this->confirm); |
146
|
|
|
} |
147
|
|
|
foreach ($this->attributes as $attributeName => $attributeValue) { |
148
|
|
|
$button->setAttribute($attributeName, $attributeValue); |
149
|
|
|
} |
150
|
|
|
$button->setForm($gridField->getForm()); |
151
|
|
|
|
152
|
|
|
return [$this->targetFragment => $button->Field()]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $name |
157
|
|
|
* @param string $value |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function setAttribute($name, $value) |
161
|
|
|
{ |
162
|
|
|
$this->attributes[$name] = $value; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $name |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getAttribute($name) |
172
|
|
|
{ |
173
|
|
|
return $this->attributes[$name] ?? null; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param GridField $gridField |
178
|
|
|
* @return array<string> |
179
|
|
|
*/ |
180
|
|
|
public function getActions($gridField) |
181
|
|
|
{ |
182
|
|
|
// $gridField is not used but required by parent class |
183
|
|
|
return [$this->getActionName()]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param GridField $gridField |
188
|
|
|
* @param string $actionName |
189
|
|
|
* @param array<mixed> $arguments |
190
|
|
|
* @param array<mixed> $data |
191
|
|
|
* @return array<mixed>|\SilverStripe\Control\HTTPResponse|void |
192
|
|
|
*/ |
193
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
194
|
|
|
{ |
195
|
|
|
if (in_array($actionName, $this->getActions($gridField))) { |
196
|
|
|
$controller = Controller::curr(); |
197
|
|
|
|
198
|
|
|
if ($this->progressive) { |
199
|
|
|
// Otherwise we would need some kind of UI |
200
|
|
|
if (!Director::is_ajax()) { |
201
|
|
|
return $controller->redirectBack(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Data should contain $_POST vars |
206
|
|
|
$result = $this->handle($gridField, $controller, $arguments, $data); |
|
|
|
|
207
|
|
|
if ((!$result || is_string($result)) && $this->progressive) { |
208
|
|
|
// simply increment counter and let's hope last action will return something |
209
|
|
|
$step = (int)$controller->getRequest()->postVar("progress_step"); |
210
|
|
|
$total = (int)$controller->getRequest()->postVar("progress_total"); |
211
|
|
|
$result = [ |
212
|
|
|
'progress_step' => $step + 1, |
213
|
|
|
'progress_total' => $total, |
214
|
|
|
'message' => $result, |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
if ($result) { |
218
|
|
|
// Send a json response this will be handled by cms-actions.js |
219
|
|
|
if ($this->progressive) { |
220
|
|
|
$response = $controller->getResponse(); |
221
|
|
|
$response->addHeader('Content-Type', 'application/json'); |
222
|
|
|
$encodedResult = json_encode($result); |
223
|
|
|
if ($encodedResult === false) { |
224
|
|
|
$encodedResult = json_last_error_msg(); |
225
|
|
|
} |
226
|
|
|
$response->setBody($encodedResult); |
227
|
|
|
|
228
|
|
|
return $response; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $result; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
// This can be helpful if you want to refresh the whole form for PJAX requests |
235
|
|
|
if ($this->allowEmptyResponse) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
// Do something! |
240
|
|
|
if ($this->noAjax || !Director::is_ajax()) { |
241
|
|
|
return $controller->redirectBack(); |
242
|
|
|
} else { |
243
|
|
|
$response = $controller->getResponse(); |
244
|
|
|
$response->setBody($gridField->forTemplate()); |
245
|
|
|
|
246
|
|
|
// Add default message if none set |
247
|
|
|
if (!$response->getHeader('X-Status')) { |
248
|
|
|
$response->addHeader('X-Status', 'Action completed'); |
249
|
|
|
} |
250
|
|
|
return $response; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* it is also a URL |
257
|
|
|
* @return array<string,string> |
258
|
|
|
*/ |
259
|
|
|
public function getURLHandlers($gridField) |
260
|
|
|
{ |
261
|
|
|
return [$this->getActionName() => 'handle']; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* TODO: update the actual method with the new arguments |
266
|
|
|
* @param GridField $gridField |
267
|
|
|
* @param Controller $controller |
268
|
|
|
* @param array<mixed> $arguments |
269
|
|
|
* @param array<mixed> $data |
270
|
|
|
* @return mixed |
271
|
|
|
*/ |
272
|
|
|
abstract public function handle(GridField $gridField, Controller $controller); |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get the value of fontIcon |
276
|
|
|
* |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
|
|
public function getFontIcon() |
280
|
|
|
{ |
281
|
|
|
return $this->fontIcon; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Set the value of fontIcon |
286
|
|
|
* |
287
|
|
|
* @param string $fontIcon |
288
|
|
|
* |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
public function setFontIcon($fontIcon) |
292
|
|
|
{ |
293
|
|
|
$this->fontIcon = $fontIcon; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Get the parent record id |
301
|
|
|
* |
302
|
|
|
* @return int |
303
|
|
|
*/ |
304
|
|
|
public function getParentID() |
305
|
|
|
{ |
306
|
|
|
return $this->parentID; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Set the parent record id |
311
|
|
|
* |
312
|
|
|
* @param int $id |
313
|
|
|
* @return $this |
314
|
|
|
*/ |
315
|
|
|
public function setParentID($id) |
316
|
|
|
{ |
317
|
|
|
$this->parentID = $id; |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Get the value of confirm |
324
|
|
|
* |
325
|
|
|
* @return string |
326
|
|
|
*/ |
327
|
|
|
public function getConfirm() |
328
|
|
|
{ |
329
|
|
|
return $this->confirm; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set the value of confirm |
334
|
|
|
* |
335
|
|
|
* @param string $confirm |
336
|
|
|
* @return $this |
337
|
|
|
*/ |
338
|
|
|
public function setConfirm($confirm) |
339
|
|
|
{ |
340
|
|
|
$this->confirm = $confirm; |
341
|
|
|
|
342
|
|
|
return $this; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Get the value of prompt |
347
|
|
|
* |
348
|
|
|
* @return string |
349
|
|
|
*/ |
350
|
|
|
public function getPrompt() |
351
|
|
|
{ |
352
|
|
|
return $this->prompt; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Set the value of prompt |
357
|
|
|
* |
358
|
|
|
* @param string $prompt |
359
|
|
|
* @return $this |
360
|
|
|
*/ |
361
|
|
|
public function setPrompt($prompt) |
362
|
|
|
{ |
363
|
|
|
$this->prompt = $prompt; |
364
|
|
|
|
365
|
|
|
return $this; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Get the value of promptDefault |
370
|
|
|
* |
371
|
|
|
* @return string |
372
|
|
|
*/ |
373
|
|
|
public function getPromptDefault() |
374
|
|
|
{ |
375
|
|
|
return $this->promptDefault; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Set the value of promptDefault |
380
|
|
|
* |
381
|
|
|
* @param string $promptDefault |
382
|
|
|
* @return $this |
383
|
|
|
*/ |
384
|
|
|
public function setPromptDefault($promptDefault) |
385
|
|
|
{ |
386
|
|
|
$this->promptDefault = $promptDefault; |
387
|
|
|
|
388
|
|
|
return $this; |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.