1 | <?php |
||
2 | |||
3 | namespace LeKoala\PureModal; |
||
4 | |||
5 | use SilverStripe\Forms\Form; |
||
6 | use SilverStripe\Forms\FieldList; |
||
7 | use SilverStripe\Control\Controller; |
||
8 | use SilverStripe\Forms\DatalessField; |
||
9 | |||
10 | /** |
||
11 | * Custom modal action |
||
12 | * Requires cms-actions to work out of the box |
||
13 | */ |
||
14 | class PureModalAction extends DatalessField |
||
15 | { |
||
16 | /** |
||
17 | * Default classes applied in constructor by the FormField |
||
18 | * @config |
||
19 | * @var array<string> |
||
20 | */ |
||
21 | private static $default_classes = ["btn", "btn-info"]; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
22 | |||
23 | /** |
||
24 | * @var FieldList |
||
25 | */ |
||
26 | protected $fieldList; |
||
27 | |||
28 | /** |
||
29 | * A custom title for the dialog button |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $dialogButtonTitle; |
||
33 | |||
34 | /** |
||
35 | * Should it show the dialog button |
||
36 | * @var boolean |
||
37 | */ |
||
38 | protected $showDialogButton = true; |
||
39 | |||
40 | /** |
||
41 | * An icon for this button |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $buttonIcon; |
||
45 | |||
46 | /** |
||
47 | * @var boolean |
||
48 | */ |
||
49 | protected $shouldRefresh = false; |
||
50 | |||
51 | /** |
||
52 | * Whether to place the button in a dot-menu. |
||
53 | * @see https://github.com/lekoala/silverstripe-cms-actions |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $dropUp = false; |
||
57 | |||
58 | /** |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $fillHeight = false; |
||
62 | |||
63 | /** |
||
64 | * @param string $name |
||
65 | * @param string $title |
||
66 | */ |
||
67 | public function __construct($name, $title) |
||
68 | { |
||
69 | $name = 'doCustomAction[' . $name . ']'; |
||
70 | $this->title = $title; |
||
71 | $this->name = $name; |
||
72 | |||
73 | parent::__construct($name, $title); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * For template usage |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function getOverlayTriggersClose() |
||
81 | { |
||
82 | return PureModal::getOverlayTriggersCloseConfig(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * If the modal is in the body, it needs to go back to the form to submit properly |
||
87 | * @return string |
||
88 | */ |
||
89 | public function SubmitOnClickScript() |
||
90 | { |
||
91 | if (!PureModal::getMoveModalScript()) { |
||
92 | return ''; |
||
93 | } |
||
94 | $formId = PureModal::config()->edit_form_id; |
||
95 | return "event.stopPropagation();var f=document.getElementById('$formId');var m=this.closest('.pure-modal');m.style.display='none';f.appendChild(m);f.submit();"; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Move modal when clicking on the open button. Trigger only once. |
||
100 | * @return string |
||
101 | */ |
||
102 | public static function getMoveModalScript() |
||
103 | { |
||
104 | if (!PureModal::config()->move_modal_to_body) { |
||
105 | return ''; |
||
106 | } |
||
107 | $formId = PureModal::config()->edit_form_id; |
||
108 | return "document.getElementById('$formId').appendChild(this.parentElement.querySelector('.pure-modal'));this.onclick=null;"; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return array<string,mixed> |
||
113 | */ |
||
114 | public function getAttributes() |
||
115 | { |
||
116 | $attrs = []; |
||
117 | // Move modal to form to avoid nesting issues |
||
118 | // We cannot append to body because this breaks form submission |
||
119 | $attrs['onclick'] = self::getMoveModalScript(); |
||
120 | return $attrs; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Get the title with icon if set |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | protected function getButtonTitle() |
||
129 | { |
||
130 | $title = $this->title; |
||
131 | if ($this->buttonIcon) { |
||
132 | $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title; |
||
133 | } |
||
134 | return $title; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get the dialog button title with icon if set |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function getDialogButtonTitle() |
||
143 | { |
||
144 | $title = $this->dialogButtonTitle ?: $this->title; |
||
145 | if ($this->buttonIcon) { |
||
146 | $title = '<span class="font-icon-' . $this->buttonIcon . '"></span> ' . $title; |
||
147 | } |
||
148 | return $title; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Set dialog button customised button title |
||
153 | * |
||
154 | * @param string $value |
||
155 | * @return self |
||
156 | */ |
||
157 | public function setDialogButtonTitle($value) |
||
158 | { |
||
159 | $this->dialogButtonTitle = $value; |
||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get an icon for this button |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getButtonIcon() |
||
169 | { |
||
170 | return $this->buttonIcon; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Set an icon for this button |
||
175 | * |
||
176 | * Feel free to use SilverStripeIcons constants |
||
177 | * |
||
178 | * @param string $buttonIcon An icon for this button |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setButtonIcon(string $buttonIcon) |
||
182 | { |
||
183 | $this->buttonIcon = $buttonIcon; |
||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Set a new type of btn-something. It will remove any existing btn- class |
||
189 | * @param string $type Leave blank to simply remove default button type |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function setButtonType($type = null) |
||
193 | { |
||
194 | if ($this->extraClasses) { |
||
0 ignored issues
–
show
The expression
$this->extraClasses of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
195 | foreach ($this->extraClasses as $k => $v) { |
||
196 | if (strpos($k, 'btn-') !== false) { |
||
197 | unset($this->extraClasses[$k]); |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | if ($type) { |
||
202 | $btn = "btn-$type"; |
||
203 | $this->extraClasses[$btn] = $btn; |
||
204 | } |
||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Get whether it must display the dialog button |
||
210 | * |
||
211 | * @return boolean |
||
212 | */ |
||
213 | protected function getShowDialogButton() |
||
214 | { |
||
215 | return $this->showDialogButton; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Set whether it must display the dialog button |
||
220 | * |
||
221 | * @param string $value |
||
222 | * @return self |
||
223 | */ |
||
224 | public function setShowDialogButton($value) |
||
225 | { |
||
226 | $this->showDialogButton = !!$value; |
||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Get the value of fieldList |
||
232 | * @return FieldList |
||
233 | */ |
||
234 | public function getFieldList() |
||
235 | { |
||
236 | return $this->fieldList; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Set the value of fieldList |
||
241 | * |
||
242 | * @param FieldList $fieldList |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setFieldList(FieldList $fieldList) |
||
246 | { |
||
247 | $this->fieldList = $fieldList; |
||
248 | foreach ($fieldList->dataFields() as $f) { |
||
249 | $f->addExtraClass('no-change-track'); |
||
250 | } |
||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Make sure our nested fieldlist is bound to form |
||
256 | * |
||
257 | * @param Form $form |
||
258 | * @return $this |
||
259 | */ |
||
260 | public function setForm($form) |
||
261 | { |
||
262 | //@link https://github.com/unclecheese/silverstripe-display-logic/pull/155#issuecomment-1540966157 |
||
263 | /** @var FieldList|null $fieldList */ |
||
264 | $fieldList = $this->fieldList; |
||
265 | if ($fieldList) { |
||
0 ignored issues
–
show
|
|||
266 | foreach ($fieldList as $field) { |
||
267 | $field->setForm($form); |
||
268 | } |
||
269 | } |
||
270 | return parent::setForm($form); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get the dropUp value |
||
275 | * Called by ActionsGridFieldItemRequest to determine placement |
||
276 | * |
||
277 | * @see https://github.com/lekoala/silverstripe-cms-actions |
||
278 | * @return bool |
||
279 | */ |
||
280 | public function getDropUp() |
||
281 | { |
||
282 | return $this->dropUp; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Set the value of dropUp |
||
287 | * You might want to call also setButtonType(null) for better styles |
||
288 | * |
||
289 | * @see https://github.com/lekoala/silverstripe-cms-actions |
||
290 | * @param bool $is |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function setDropUp($is) |
||
294 | { |
||
295 | $this->dropUp = !!$is; |
||
296 | return $this; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Required for cms-actions |
||
301 | * @return string |
||
302 | */ |
||
303 | public function actionName() |
||
304 | { |
||
305 | return rtrim(str_replace('doCustomAction[', '', $this->name), ']'); |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * Get the value of shouldRefresh |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function getShouldRefresh() |
||
313 | { |
||
314 | return $this->shouldRefresh; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Set the value of shouldRefresh |
||
319 | * |
||
320 | * @param mixed $shouldRefresh |
||
321 | * @return $this |
||
322 | */ |
||
323 | public function setShouldRefresh($shouldRefresh) |
||
324 | { |
||
325 | $this->shouldRefresh = $shouldRefresh; |
||
326 | return $this; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @return string |
||
331 | */ |
||
332 | public function getModalID() |
||
333 | { |
||
334 | return 'modal_' . $this->name; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return string |
||
339 | */ |
||
340 | public function getTitle() |
||
341 | { |
||
342 | return $this->title; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function getFillHeight() |
||
349 | { |
||
350 | return $this->fillHeight; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @param bool $fillHeight |
||
355 | * @return $this |
||
356 | */ |
||
357 | public function setFillHeight($fillHeight) |
||
358 | { |
||
359 | $this->fillHeight = $fillHeight; |
||
360 | return $this; |
||
361 | } |
||
362 | } |
||
363 |