|
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_URLHandler; |
|
10
|
|
|
use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
|
11
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Provide a simple way to declare buttons that affects a whole GridField |
|
15
|
|
|
* |
|
16
|
|
|
* This implements a URL Handler that can be called by the button |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class GridFieldTableButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler |
|
19
|
|
|
{ |
|
20
|
|
|
use ProgressiveAction; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Fragment to write the button to |
|
24
|
|
|
* @string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $targetFragment; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var boolean |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $noAjax = true; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var boolean |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $allowEmptyResponse = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $buttonLabel; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $fontIcon; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $parentID; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $confirm; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $prompt; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $promptDefault; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected $attributes = []; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $targetFragment The HTML fragment to write the button into |
|
75
|
|
|
* @param string $buttonLabel |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct($targetFragment = "buttons-before-right", $buttonLabel = null) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->targetFragment = $targetFragment; |
|
80
|
|
|
if ($buttonLabel) { |
|
81
|
|
|
$this->buttonLabel = $buttonLabel; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getActionName() |
|
86
|
|
|
{ |
|
87
|
|
|
$class = (new ReflectionClass(get_called_class()))->getShortName(); |
|
88
|
|
|
// ! without lowercase, in does not work |
|
89
|
|
|
return strtolower(str_replace('Button', '', $class)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getButtonLabel() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->buttonLabel; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Place the export button in a <p> tag below the field |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getHTMLFragments($gridField) |
|
101
|
|
|
{ |
|
102
|
|
|
$action = $this->getActionName(); |
|
103
|
|
|
|
|
104
|
|
|
$button = new CustomGridField_FormAction( |
|
105
|
|
|
$gridField, |
|
106
|
|
|
$action, |
|
107
|
|
|
$this->getButtonLabel(), |
|
108
|
|
|
$action, |
|
109
|
|
|
null |
|
110
|
|
|
); |
|
111
|
|
|
$button->addExtraClass('btn btn-secondary action_' . $action); |
|
112
|
|
|
if ($this->noAjax) { |
|
113
|
|
|
$button->addExtraClass('no-ajax'); |
|
114
|
|
|
} |
|
115
|
|
|
if ($this->fontIcon) { |
|
116
|
|
|
$button->addExtraClass('font-icon-' . $this->fontIcon); |
|
117
|
|
|
} |
|
118
|
|
|
//TODO: replace prompt and confirm with inline js |
|
119
|
|
|
if ($this->prompt) { |
|
120
|
|
|
$button->setAttribute('data-prompt', $this->prompt); |
|
121
|
|
|
$promptDefault = $this->getPromptDefault(); |
|
122
|
|
|
if ($promptDefault) { |
|
123
|
|
|
$button->setAttribute('data-prompt-default', $promptDefault); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
if ($this->progressive) { |
|
127
|
|
|
$button->setProgressive(true); |
|
128
|
|
|
} |
|
129
|
|
|
if ($this->confirm) { |
|
130
|
|
|
$button->setAttribute('data-confirm', $this->confirm); |
|
131
|
|
|
} |
|
132
|
|
|
foreach ($this->attributes as $attributeName => $attributeValue) { |
|
133
|
|
|
$button->setAttribute($attributeName, $attributeValue); |
|
134
|
|
|
} |
|
135
|
|
|
$button->setForm($gridField->getForm()); |
|
136
|
|
|
return array( |
|
137
|
|
|
$this->targetFragment => $button->Field() |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $name |
|
143
|
|
|
* @param string $value |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setAttribute($name, $value) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->attributes[$name] = $value; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $name |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getAttribute($name) |
|
157
|
|
|
{ |
|
158
|
|
|
if (isset($this->attributes[$name])) { |
|
159
|
|
|
return $this->attributes[$name]; |
|
160
|
|
|
} |
|
161
|
|
|
return null; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function getActions($gridField) |
|
165
|
|
|
{ |
|
166
|
|
|
return array($this->getActionName()); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
170
|
|
|
{ |
|
171
|
|
|
if (in_array($actionName, $this->getActions($gridField))) { |
|
172
|
|
|
$controller = Controller::curr(); |
|
173
|
|
|
|
|
174
|
|
|
if ($this->progressive) { |
|
175
|
|
|
// Otherwise we would need some kind of UI |
|
176
|
|
|
if (!Director::is_ajax()) { |
|
177
|
|
|
return $controller->redirectBack(); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$result = $this->handle($gridField, $controller); |
|
182
|
|
|
if ((!$result || is_string($result)) && $this->progressive) { |
|
183
|
|
|
// simply increment counter and let's hope last action will return something |
|
184
|
|
|
$step = (int) $controller->getRequest()->postVar("progress_step"); |
|
185
|
|
|
$total = (int) $controller->getRequest()->postVar("progress_total"); |
|
186
|
|
|
$result = [ |
|
187
|
|
|
'progress_step' => $step + 1, |
|
188
|
|
|
'progress_total' => $total, |
|
189
|
|
|
'message' => $result, |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
if ($result) { |
|
193
|
|
|
// Send a json response this will be handled by cms-actions.js |
|
194
|
|
|
if ($this->progressive) { |
|
195
|
|
|
$response = $controller->getResponse(); |
|
196
|
|
|
$response->addHeader('Content-Type', 'application/json'); |
|
197
|
|
|
$response->setBody(json_encode($result)); |
|
198
|
|
|
return $response; |
|
199
|
|
|
} |
|
200
|
|
|
return $result; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if ($this->allowEmptyResponse) { |
|
204
|
|
|
return; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// Do something! |
|
208
|
|
|
if ($this->noAjax || !Director::is_ajax()) { |
|
209
|
|
|
return $controller->redirectBack(); |
|
210
|
|
|
} else { |
|
211
|
|
|
$response = $controller->getResponse(); |
|
212
|
|
|
$response->setBody($gridField->forTemplate()); |
|
213
|
|
|
$response |
|
214
|
|
|
->addHeader('X-Status', 'Action completed'); |
|
215
|
|
|
return $response; |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* it is also a URL |
|
222
|
|
|
*/ |
|
223
|
|
|
public function getURLHandlers($gridField) |
|
224
|
|
|
{ |
|
225
|
|
|
return array( |
|
226
|
|
|
$this->getActionName() => 'handle', |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
abstract public function handle(GridField $gridField, Controller $controller); |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Get the value of fontIcon |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getFontIcon() |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->fontIcon; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Set the value of fontIcon |
|
244
|
|
|
* |
|
245
|
|
|
* @param string $fontIcon |
|
246
|
|
|
* |
|
247
|
|
|
* @return $this |
|
248
|
|
|
*/ |
|
249
|
|
|
public function setFontIcon($fontIcon) |
|
250
|
|
|
{ |
|
251
|
|
|
$this->fontIcon = $fontIcon; |
|
252
|
|
|
|
|
253
|
|
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get the parent record id |
|
259
|
|
|
* |
|
260
|
|
|
* @return int |
|
261
|
|
|
*/ |
|
262
|
|
|
public function getParentID() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->parentID; |
|
|
|
|
|
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Set the parent record id |
|
269
|
|
|
* |
|
270
|
|
|
* @param int $id |
|
271
|
|
|
* @return $this |
|
272
|
|
|
*/ |
|
273
|
|
|
public function setParentID($id) |
|
274
|
|
|
{ |
|
275
|
|
|
$this->parentID = $id; |
|
276
|
|
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Get the value of confirm |
|
281
|
|
|
* |
|
282
|
|
|
* @return string |
|
283
|
|
|
*/ |
|
284
|
|
|
public function getConfirm() |
|
285
|
|
|
{ |
|
286
|
|
|
return $this->confirm; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Set the value of confirm |
|
291
|
|
|
* |
|
292
|
|
|
* @param string $confirm |
|
293
|
|
|
* @return $this |
|
294
|
|
|
*/ |
|
295
|
|
|
public function setConfirm($confirm) |
|
296
|
|
|
{ |
|
297
|
|
|
$this->confirm = $confirm; |
|
298
|
|
|
return $this; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Get the value of prompt |
|
303
|
|
|
* |
|
304
|
|
|
* @return string |
|
305
|
|
|
*/ |
|
306
|
|
|
public function getPrompt() |
|
307
|
|
|
{ |
|
308
|
|
|
return $this->prompt; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Set the value of prompt |
|
313
|
|
|
* |
|
314
|
|
|
* @param string $prompt |
|
315
|
|
|
* @return $this |
|
316
|
|
|
*/ |
|
317
|
|
|
public function setPrompt($prompt) |
|
318
|
|
|
{ |
|
319
|
|
|
$this->prompt = $prompt; |
|
320
|
|
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* Get the value of promptDefault |
|
325
|
|
|
* |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
public function getPromptDefault() |
|
329
|
|
|
{ |
|
330
|
|
|
return $this->promptDefault; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Set the value of promptDefault |
|
335
|
|
|
* |
|
336
|
|
|
* @param string $promptDefault |
|
337
|
|
|
* @return $this |
|
338
|
|
|
*/ |
|
339
|
|
|
public function setPromptDefault($promptDefault) |
|
340
|
|
|
{ |
|
341
|
|
|
$this->promptDefault = $promptDefault; |
|
342
|
|
|
return $this; |
|
343
|
|
|
} |
|
344
|
|
|
} |
|
345
|
|
|
|