|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\PureModal; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\View\SSViewer; |
|
6
|
|
|
use SilverStripe\Forms\FormField; |
|
7
|
|
|
use SilverStripe\Admin\ModelAdmin; |
|
|
|
|
|
|
8
|
|
|
use SilverStripe\Control\Controller; |
|
9
|
|
|
use SilverStripe\Forms\DatalessField; |
|
10
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A simple pure css modal for usage in the cms |
|
14
|
|
|
* |
|
15
|
|
|
* If your content contains a form, it should be loaded through an iframe |
|
16
|
|
|
* because you cannot nest forms (warning, this gets tricky with "move_modal_to_body") |
|
17
|
|
|
* |
|
18
|
|
|
* @author lekoala |
|
19
|
|
|
*/ |
|
20
|
|
|
class PureModal extends DatalessField |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $title; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $content; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $iframe; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $iframeTop; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string|null |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $buttonIcon = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param string $title |
|
50
|
|
|
* @param string $content |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct($name, $title, $content) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setContent($content); |
|
55
|
|
|
|
|
56
|
|
|
parent::__construct($name, $title); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function getMoveModalScript() |
|
63
|
|
|
{ |
|
64
|
|
|
if (!self::config()->move_modal_to_body) { |
|
65
|
|
|
return ''; |
|
66
|
|
|
} |
|
67
|
|
|
return "document.body.appendChild(this.parentElement.querySelector('.pure-modal'));this.onclick=null;"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getOverlayTriggersCloseConfig() |
|
74
|
|
|
{ |
|
75
|
|
|
return boolval(self::config()->overlay_triggers_close); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* For template usage |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getOverlayTriggersClose() |
|
83
|
|
|
{ |
|
84
|
|
|
return PureModal::getOverlayTriggersCloseConfig(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return array<string,mixed> |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getAttributes() |
|
91
|
|
|
{ |
|
92
|
|
|
$attrs = []; |
|
93
|
|
|
// Move modal to body to avoid nesting issues |
|
94
|
|
|
$attrs['onclick'] = self::getMoveModalScript(); |
|
95
|
|
|
// Since the frame is hidden, we need to compute size on click |
|
96
|
|
|
// TODO: fix this as it may report incorrect height |
|
97
|
|
|
if ($this->getIframe() && self::config()->compute_iframe_height) { |
|
98
|
|
|
$attrs['onclick'] .= "var i=document.querySelector('#" . $this->getIframeID() . "');i.style.height = 0; setTimeout(function() {i.style.height = i.contentWindow.document.body.scrollHeight + 'px';},100);"; |
|
99
|
|
|
} |
|
100
|
|
|
return $attrs; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Render a dialog |
|
105
|
|
|
* |
|
106
|
|
|
* @param Controller $controller |
|
107
|
|
|
* @param array<string,mixed> $customFields |
|
108
|
|
|
* @return string|DBHTMLText |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function renderDialog($controller, $customFields = null) |
|
111
|
|
|
{ |
|
112
|
|
|
// Set empty content by default otherwise it will render the full page |
|
113
|
|
|
if (empty($customFields['Content'])) { |
|
114
|
|
|
$customFields['Content'] = ''; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$templates = SSViewer::get_templates_by_class(static::class, '_Dialog', __CLASS__); |
|
118
|
|
|
$templatesWithSuffix = SSViewer::chooseTemplate($templates); |
|
119
|
|
|
|
|
120
|
|
|
return $controller->renderWith($templatesWithSuffix, $customFields); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets the content of this field to a new value. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string|FormField $content |
|
127
|
|
|
* @return $this |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setContent($content) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->content = $content; |
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getContent() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->content; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Build an url for the current controller and pass along some parameters |
|
145
|
|
|
* |
|
146
|
|
|
* If you want to call actions on a model, use getModelLink |
|
147
|
|
|
* |
|
148
|
|
|
* @param string $action |
|
149
|
|
|
* @param array<string,mixed> $params |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getControllerLink($action, array $params = null) |
|
153
|
|
|
{ |
|
154
|
|
|
if ($params === null) { |
|
155
|
|
|
$params = []; |
|
156
|
|
|
} |
|
157
|
|
|
$ctrl = Controller::curr(); |
|
158
|
|
|
if ($ctrl instanceof ModelAdmin) { |
|
159
|
|
|
$allParams = $ctrl->getRequest()->allParams(); |
|
160
|
|
|
$modelClass = $ctrl->getRequest()->param('ModelClass'); |
|
161
|
|
|
$action = $modelClass . '/' . $action; |
|
162
|
|
|
$params = array_merge($allParams, $params); |
|
163
|
|
|
} |
|
164
|
|
|
if (!empty($params)) { |
|
165
|
|
|
$action .= '?' . http_build_query($params); |
|
166
|
|
|
} |
|
167
|
|
|
return $ctrl->Link($action); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array<string,mixed> $properties |
|
172
|
|
|
* @return DBHTMLText |
|
173
|
|
|
*/ |
|
174
|
|
|
public function Field($properties = []) |
|
175
|
|
|
{ |
|
176
|
|
|
$icon = $this->buttonIcon; |
|
177
|
|
|
if ($icon) { |
|
178
|
|
|
$this->addExtraClass('font-icon'); |
|
179
|
|
|
$this->addExtraClass('font-icon-' . $icon); |
|
180
|
|
|
} |
|
181
|
|
|
return parent::Field($properties); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return string |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getTitle() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->title; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $title |
|
194
|
|
|
* @return $this |
|
195
|
|
|
*/ |
|
196
|
|
|
public function setTitle($title) |
|
197
|
|
|
{ |
|
198
|
|
|
$this->title = $title; |
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getIframe() |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->iframe; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Link to iframe src |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $iframe |
|
214
|
|
|
* @return $this |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setIframe($iframe) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->iframe = $iframe; |
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Helper for setIframe link using conventions |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $action |
|
226
|
|
|
* @return $this |
|
227
|
|
|
*/ |
|
228
|
|
|
public function setIframeAction($action) |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->setIframe($this->getControllerLink($action)); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return bool |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getIframeTop() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->iframeTop; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Should the iframe be above the content |
|
243
|
|
|
* |
|
244
|
|
|
* @param bool $iframeTop |
|
245
|
|
|
* @return $this |
|
246
|
|
|
*/ |
|
247
|
|
|
public function setIframeTop($iframeTop) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->iframeTop = $iframeTop; |
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getModalID() |
|
257
|
|
|
{ |
|
258
|
|
|
return 'modal_' . $this->name; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @return string |
|
263
|
|
|
*/ |
|
264
|
|
|
public function getIframeID() |
|
265
|
|
|
{ |
|
266
|
|
|
return $this->getModalID() . '_iframe'; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Get an icon for this button |
|
271
|
|
|
* |
|
272
|
|
|
* @return string|null |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getButtonIcon() |
|
275
|
|
|
{ |
|
276
|
|
|
return $this->buttonIcon; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Set an icon for this button |
|
281
|
|
|
* |
|
282
|
|
|
* Feel free to use SilverStripeIcons constants |
|
283
|
|
|
* |
|
284
|
|
|
* @param string|null $buttonIcon An icon for this button |
|
285
|
|
|
* @return $this |
|
286
|
|
|
*/ |
|
287
|
|
|
public function setButtonIcon(string $buttonIcon = null) |
|
288
|
|
|
{ |
|
289
|
|
|
$this->buttonIcon = $buttonIcon; |
|
290
|
|
|
return $this; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
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