|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use SilverStripe\Control\Controller; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
8
|
|
|
use SilverStripe\Forms\GridField\GridField_FormAction; |
|
9
|
|
|
use SilverStripe\Forms\GridField\GridField_ActionProvider; |
|
10
|
|
|
use SilverStripe\Forms\GridField\GridField_ColumnProvider; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A boilerplate to create row level buttons |
|
14
|
|
|
* |
|
15
|
|
|
* It create the "Actions" (or custom) column if it doesn't exist yet |
|
16
|
|
|
* |
|
17
|
|
|
* @link https://docs.silverstripe.org/en/4/developer_guides/forms/how_tos/create_a_gridfield_actionprovider/ |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class GridFieldRowButton implements GridField_ColumnProvider, GridField_ActionProvider |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Column name |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $columnName = 'Actions'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* A silverstripe icon |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $fontIcon; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Adds class grid-field__icon-action--hidden-on-hover if set |
|
37
|
|
|
* |
|
38
|
|
|
* @var boolean |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $hiddenOnHover = true; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Adds class no-ajax if false |
|
44
|
|
|
* |
|
45
|
|
|
* @var boolean |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $ajax = false; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Adds Bootstrap style class if not $fontIcon (eg btn-primary / btn-dark / btn-warning etc) |
|
51
|
|
|
* |
|
52
|
|
|
* @var string one of the btn-XXX styles (Bootstrap) |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $btnStyleClass = 'dark'; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var int |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $parentID; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $columnName name of the column for this button (default null -> 'Actions') |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct($columnName = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if($columnName){ |
|
67
|
|
|
$this->columnName = $columnName; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param GridField $gridField |
|
73
|
|
|
* @param DataObject $record |
|
|
|
|
|
|
74
|
|
|
* @param string $columnName |
|
75
|
|
|
* @return string Label for the gridfield button |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract function getButtonLabel(GridField $gridField, $record, $columnName); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
abstract function doHandle(GridField $gridField, $actionName, $arguments, $data); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
public function getActionName() |
|
82
|
|
|
{ |
|
83
|
|
|
$class = (new ReflectionClass($this->record))->getShortName(); |
|
|
|
|
|
|
84
|
|
|
// ! without lowercase, in does not work |
|
85
|
|
|
return strtolower(str_replace('Button', '', $class)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param GridField $gridField |
|
90
|
|
|
* @param array $columns |
|
91
|
|
|
*/ |
|
92
|
|
|
public function augmentColumns($gridField, &$columns) |
|
93
|
|
|
{ |
|
94
|
|
|
if (!in_array($this->columnName, $columns)) { |
|
95
|
|
|
$columns[] = $this->columnName; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Return any special attributes that will be used for FormField::create_tag() |
|
101
|
|
|
* |
|
102
|
|
|
* @param GridField $gridField |
|
103
|
|
|
* @param DataObject $record |
|
104
|
|
|
* @param string $columnName |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getColumnAttributes($gridField, $record, $columnName) |
|
108
|
|
|
{ |
|
109
|
|
|
// right-align if this column contains icon-buttons |
|
110
|
|
|
return [ 'class' => $this->fontIcon ? 'grid-field__col-compact' : '' ]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Add the title |
|
115
|
|
|
* |
|
116
|
|
|
* @param GridField $gridField |
|
117
|
|
|
* @param string $columnName |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getColumnMetadata($gridField, $columnName) |
|
121
|
|
|
{ |
|
122
|
|
|
// No titles for action column IF icon button |
|
123
|
|
|
if ($columnName == $this->columnName && $this->fontIcon) { |
|
124
|
|
|
return [ 'title' => '' ]; |
|
125
|
|
|
} |
|
126
|
|
|
return [ 'title' => $columnName ]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Which columns are handled by this component |
|
131
|
|
|
* |
|
132
|
|
|
* @param GridField $gridField |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getColumnsHandled($gridField) |
|
136
|
|
|
{ |
|
137
|
|
|
return [ $this->columnName ]; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Which GridField actions are this component handling |
|
142
|
|
|
* |
|
143
|
|
|
* @param GridField $gridField |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getActions($gridField) |
|
147
|
|
|
{ |
|
148
|
|
|
return [ $this->getActionName() ]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* |
|
153
|
|
|
* @param GridField $gridField |
|
154
|
|
|
* @param DataObject $record |
|
155
|
|
|
* @param string $columnName |
|
156
|
|
|
* @return string - the HTML for the column |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getColumnContent($gridField, $record, $columnName) |
|
159
|
|
|
{ |
|
160
|
|
|
$actionName = $this->getActionName(); |
|
161
|
|
|
$actionLabel = $this->getButtonLabel($gridField, $record, $columnName); |
|
162
|
|
|
$field = GridField_FormAction::create( |
|
163
|
|
|
$gridField, |
|
164
|
|
|
$actionName . '_' . $record->ID, |
|
165
|
|
|
($this->fontIcon ? false : $actionLabel), |
|
166
|
|
|
$actionName, |
|
167
|
|
|
array( |
|
168
|
|
|
'RecordID' => $record->ID, |
|
169
|
|
|
'ParentID' => $this->parentID |
|
170
|
|
|
) |
|
171
|
|
|
) |
|
172
|
|
|
->addExtraClass('gridfield-button-' . $actionName) |
|
173
|
|
|
->setAttribute('title', $this->getButtonLabel($gridField, $record, $columnName)); |
|
174
|
|
|
|
|
175
|
|
|
if (!$this->ajax) { |
|
176
|
|
|
$field->addExtraClass('no-ajax'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if ($this->hiddenOnHover) { |
|
180
|
|
|
$field->addExtraClass('grid-field__icon-action--hidden-on-hover'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
if ($this->fontIcon) { |
|
184
|
|
|
$field->addExtraClass('grid-field__icon-action btn--icon-large font-icon-' . $this->fontIcon); |
|
185
|
|
|
} else { |
|
186
|
|
|
// Add a regular button |
|
187
|
|
|
$field->addExtraClass('btn btn-' . $this->btnStyleClass); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
return $field->Field(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Handle the actions and apply any changes to the GridField |
|
195
|
|
|
* |
|
196
|
|
|
* @param GridField $gridField |
|
197
|
|
|
* @param string $actionName |
|
198
|
|
|
* @param mixed $arguments |
|
199
|
|
|
* @param array $data - form data |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
203
|
|
|
{ |
|
204
|
|
|
if ($actionName == $this->getActionName()) { |
|
205
|
|
|
$result = $this->doHandle($gridField, $actionName, $arguments, $data); |
|
206
|
|
|
if ($result) { |
|
207
|
|
|
return $result; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Do something! |
|
211
|
|
|
$controller = Controller::curr(); |
|
212
|
|
|
return $controller->redirectBack(); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Get the parent record id |
|
218
|
|
|
* |
|
219
|
|
|
* @return int |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getParentID() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->parentID; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Set the parent record id |
|
228
|
|
|
* |
|
229
|
|
|
* This will be passed as ParentID along RecordID in the arguments parameter |
|
230
|
|
|
* |
|
231
|
|
|
* @param int $id |
|
232
|
|
|
* @return $this |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setParentID($id) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->parentID = $id; |
|
237
|
|
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Get a silverstripe icon |
|
242
|
|
|
* |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
|
|
public function getFontIcon() |
|
246
|
|
|
{ |
|
247
|
|
|
return $this->fontIcon; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Set a silverstripe icon |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $fontIcon A silverstripe icon |
|
254
|
|
|
* |
|
255
|
|
|
* @return $this |
|
256
|
|
|
*/ |
|
257
|
|
|
public function setFontIcon(string $fontIcon) |
|
258
|
|
|
{ |
|
259
|
|
|
$this->fontIcon = $fontIcon; |
|
260
|
|
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Get adds class grid-field__icon-action--hidden-on-hover if set |
|
265
|
|
|
* |
|
266
|
|
|
* @return boolean |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getHiddenOnHover() |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->hiddenOnHover; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Set adds class grid-field__icon-action--hidden-on-hover if set |
|
275
|
|
|
* |
|
276
|
|
|
* @param boolean $hiddenOnHover Adds class grid-field__icon-action--hidden-on-hover if set |
|
277
|
|
|
* |
|
278
|
|
|
* @return $this |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setHiddenOnHover(bool $hiddenOnHover) |
|
281
|
|
|
{ |
|
282
|
|
|
$this->hiddenOnHover = $hiddenOnHover; |
|
283
|
|
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
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