1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\PureModal; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\Forms\DatalessField; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Custom modal action |
10
|
|
|
* Requires cms-actions to work out of the box |
11
|
|
|
*/ |
12
|
|
|
class PureModalAction extends DatalessField |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Default classes applied in constructor by the FormField |
16
|
|
|
* @config |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $default_classes = ["btn", "btn-info"]; |
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FieldList |
23
|
|
|
*/ |
24
|
|
|
protected $fieldList; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A custom title for the dialog button |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $dialogButtonTitle; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Should it show the dialog button |
34
|
|
|
* @var boolean |
35
|
|
|
*/ |
36
|
|
|
protected $showDialogButton = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* An icon for this button |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $buttonIcon; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
protected $shouldRefresh = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Whether to place the button in a dot-menu. |
51
|
|
|
* @see https://github.com/lekoala/silverstripe-cms-actions |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $dropUp = false; |
55
|
|
|
|
56
|
|
|
public function __construct($name, $title) |
57
|
|
|
{ |
58
|
|
|
$name = 'doCustomAction[' . $name . ']'; |
59
|
|
|
$this->title = $title; |
60
|
|
|
$this->name = $name; |
61
|
|
|
|
62
|
|
|
parent::__construct($name, $title); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the title with icon if set |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function getButtonTitle() |
71
|
|
|
{ |
72
|
|
|
$title = $this->title; |
73
|
|
|
if ($this->buttonIcon) { |
74
|
|
|
$title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title; |
75
|
|
|
} |
76
|
|
|
return $title; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the dialog button title with icon if set |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
protected function getDialogButtonTitle() |
85
|
|
|
{ |
86
|
|
|
$title = $this->buttonTitle ?: $this->title; |
87
|
|
|
if ($this->buttonIcon) { |
88
|
|
|
$title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title; |
89
|
|
|
} |
90
|
|
|
return $title; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set dialog button customised button title |
95
|
|
|
* |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
|
|
public function setDialogButtonTitle($value) |
99
|
|
|
{ |
100
|
|
|
$this->buttonTitle = $value; |
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get an icon for this button |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getButtonIcon() |
110
|
|
|
{ |
111
|
|
|
return $this->buttonIcon; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set an icon for this button |
116
|
|
|
* |
117
|
|
|
* Feel free to use SilverStripeIcons constants |
118
|
|
|
* |
119
|
|
|
* @param string $buttonIcon An icon for this button |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setButtonIcon(string $buttonIcon) |
123
|
|
|
{ |
124
|
|
|
$this->buttonIcon = $buttonIcon; |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Set a new type of btn-something. It will remove any existing btn- class |
130
|
|
|
* @param string $type Leave blank to simply remove default button type |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setButtonType($type = null) |
134
|
|
|
{ |
135
|
|
|
if ($this->extraClasses) { |
|
|
|
|
136
|
|
|
foreach ($this->extraClasses as $k => $v) { |
137
|
|
|
if (strpos($k, 'btn-') !== false) { |
138
|
|
|
unset($this->extraClasses[$k]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
if ($type) { |
143
|
|
|
$btn = "btn-$type"; |
144
|
|
|
$this->extraClasses[$btn] = $btn; |
145
|
|
|
} |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get whether it must display the dialog button |
151
|
|
|
* |
152
|
|
|
* @return boolean |
153
|
|
|
*/ |
154
|
|
|
protected function getShowDialogButton() |
155
|
|
|
{ |
156
|
|
|
return $this->showDialogButton; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Set whether it must display the dialog button |
161
|
|
|
* |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
|
|
public function setShowDialogButton($value) |
165
|
|
|
{ |
166
|
|
|
$this->showDialogButton = !!$value; |
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get the value of fieldList |
172
|
|
|
* @return FieldList |
173
|
|
|
*/ |
174
|
|
|
public function getFieldList() |
175
|
|
|
{ |
176
|
|
|
return $this->fieldList; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set the value of fieldList |
181
|
|
|
* |
182
|
|
|
* @param FieldList $fieldList |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
|
|
public function setFieldList(FieldList $fieldList) |
186
|
|
|
{ |
187
|
|
|
$this->fieldList = $fieldList; |
188
|
|
|
foreach ($fieldList->dataFields() as $f) { |
189
|
|
|
$f->addExtraClass('no-change-track'); |
190
|
|
|
} |
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get the dropUp value |
196
|
|
|
* Called by ActionsGridFieldItemRequest to determine placement |
197
|
|
|
* |
198
|
|
|
* @see https://github.com/lekoala/silverstripe-cms-actions |
199
|
|
|
* @return bool |
200
|
|
|
*/ |
201
|
|
|
public function getDropUp() |
202
|
|
|
{ |
203
|
|
|
return $this->dropUp; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set the value of dropUp |
208
|
|
|
* You might want to call also setButtonType(null) for better styles |
209
|
|
|
* |
210
|
|
|
* @see https://github.com/lekoala/silverstripe-cms-actions |
211
|
|
|
* @param bool $is |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
public function setDropUp($is) |
215
|
|
|
{ |
216
|
|
|
$this->dropUp = !!$is; |
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Required for cms-actions |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function actionName() |
225
|
|
|
{ |
226
|
|
|
return rtrim(str_replace('doCustomAction[', '', $this->name), ']'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get the value of shouldRefresh |
231
|
|
|
* @return mixed |
232
|
|
|
*/ |
233
|
|
|
public function getShouldRefresh() |
234
|
|
|
{ |
235
|
|
|
return $this->shouldRefresh; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the value of shouldRefresh |
240
|
|
|
* |
241
|
|
|
* @param mixed $shouldRefresh |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
public function setShouldRefresh($shouldRefresh) |
245
|
|
|
{ |
246
|
|
|
$this->shouldRefresh = $shouldRefresh; |
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getModalID() |
251
|
|
|
{ |
252
|
|
|
return 'modal_' . $this->name; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function getTitle() |
256
|
|
|
{ |
257
|
|
|
return $this->title; |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|