1 | <?php |
||
2 | |||
3 | namespace LeKoala\PureModal; |
||
4 | |||
5 | use SilverStripe\View\SSViewer; |
||
6 | use SilverStripe\Forms\FormField; |
||
7 | use SilverStripe\Admin\ModelAdmin; |
||
0 ignored issues
–
show
|
|||
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 | // If you want, you can remove this and switch to another type |
||
57 | $this->addExtraClass('btn-primary'); |
||
58 | |||
59 | parent::__construct($name, $title); |
||
60 | } |
||
61 | |||
62 | public function Type() |
||
63 | { |
||
64 | return 'pure-modal'; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return string |
||
69 | */ |
||
70 | public static function getMoveModalScript() |
||
71 | { |
||
72 | if (!self::config()->move_modal_to_body) { |
||
73 | return ''; |
||
74 | } |
||
75 | return "document.body.appendChild(this.parentElement.querySelector('.pure-modal'));this.onclick=null;"; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return bool |
||
80 | */ |
||
81 | public static function getOverlayTriggersCloseConfig() |
||
82 | { |
||
83 | return boolval(self::config()->overlay_triggers_close); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * For template usage |
||
88 | * @return bool |
||
89 | */ |
||
90 | public function getOverlayTriggersClose() |
||
91 | { |
||
92 | return PureModal::getOverlayTriggersCloseConfig(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return array<string,mixed> |
||
97 | */ |
||
98 | public function getAttributes() |
||
99 | { |
||
100 | $attrs = []; |
||
101 | // Move modal to body to avoid nesting issues |
||
102 | $attrs['onclick'] = self::getMoveModalScript(); |
||
103 | // Since the frame is hidden, we need to compute size on click |
||
104 | // TODO: fix this as it may report incorrect height |
||
105 | if ($this->getIframe() && self::config()->compute_iframe_height) { |
||
106 | $attrs['onclick'] .= "var i=document.querySelector('#" . $this->getIframeID() . "');i.style.height = 0; setTimeout(function() {i.style.height = i.contentWindow.document.body.scrollHeight + 'px';},100);"; |
||
107 | } |
||
108 | return $attrs; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Render a dialog |
||
113 | * |
||
114 | * @param Controller $controller |
||
115 | * @param array<string,mixed> $customFields |
||
116 | * @return string|DBHTMLText |
||
117 | */ |
||
118 | public static function renderDialog($controller, $customFields = null) |
||
119 | { |
||
120 | // Set empty content by default otherwise it will render the full page |
||
121 | if (empty($customFields['Content'])) { |
||
122 | $customFields['Content'] = ''; |
||
123 | } |
||
124 | |||
125 | $templates = SSViewer::get_templates_by_class(static::class, '_Dialog', __CLASS__); |
||
126 | $templatesWithSuffix = SSViewer::chooseTemplate($templates); |
||
127 | |||
128 | return $controller->renderWith($templatesWithSuffix, $customFields); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Sets the content of this field to a new value. |
||
133 | * |
||
134 | * @param string|FormField $content |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function setContent($content) |
||
138 | { |
||
139 | $this->content = $content; |
||
140 | return $this; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getContent() |
||
147 | { |
||
148 | return $this->content; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Build an url for the current controller and pass along some parameters |
||
153 | * |
||
154 | * If you want to call actions on a model, use getModelLink |
||
155 | * |
||
156 | * @param string $action |
||
157 | * @param array<string,mixed> $params |
||
158 | * @return string |
||
159 | */ |
||
160 | public function getControllerLink($action, array $params = null) |
||
161 | { |
||
162 | if ($params === null) { |
||
163 | $params = []; |
||
164 | } |
||
165 | $ctrl = Controller::curr(); |
||
166 | if ($ctrl instanceof ModelAdmin) { |
||
167 | $allParams = $ctrl->getRequest()->allParams(); |
||
168 | $modelClass = $ctrl->getRequest()->param('ModelClass'); |
||
169 | $action = $modelClass . '/' . $action; |
||
170 | $params = array_merge($allParams, $params); |
||
171 | } |
||
172 | if (!empty($params)) { |
||
173 | $action .= '?' . http_build_query($params); |
||
174 | } |
||
175 | return $ctrl->Link($action); |
||
0 ignored issues
–
show
Are you sure the usage of
$ctrl->Link($action) targeting SilverStripe\Control\RequestHandler::Link() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param array<string,mixed> $properties |
||
180 | * @return DBHTMLText |
||
181 | */ |
||
182 | public function Field($properties = []) |
||
183 | { |
||
184 | $icon = $this->buttonIcon; |
||
185 | if ($icon) { |
||
186 | $this->addExtraClass('font-icon'); |
||
187 | $this->addExtraClass('font-icon-' . $icon); |
||
188 | } |
||
189 | return parent::Field($properties); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getTitle() |
||
196 | { |
||
197 | return $this->title; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param string $title |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function setTitle($title) |
||
205 | { |
||
206 | $this->title = $title; |
||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return string |
||
212 | */ |
||
213 | public function getIframe() |
||
214 | { |
||
215 | return $this->iframe; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Link to iframe src |
||
220 | * |
||
221 | * @param string $iframe |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function setIframe($iframe) |
||
225 | { |
||
226 | $this->iframe = $iframe; |
||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Helper for setIframe link using conventions |
||
232 | * |
||
233 | * @param string $action |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function setIframeAction($action) |
||
237 | { |
||
238 | return $this->setIframe($this->getControllerLink($action)); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function getIframeTop() |
||
245 | { |
||
246 | return $this->iframeTop; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Should the iframe be above the content |
||
251 | * |
||
252 | * @param bool $iframeTop |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setIframeTop($iframeTop) |
||
256 | { |
||
257 | $this->iframeTop = $iframeTop; |
||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getModalID() |
||
265 | { |
||
266 | return 'modal_' . $this->name; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getIframeID() |
||
273 | { |
||
274 | return $this->getModalID() . '_iframe'; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get an icon for this button |
||
279 | * |
||
280 | * @return string|null |
||
281 | */ |
||
282 | public function getButtonIcon() |
||
283 | { |
||
284 | return $this->buttonIcon; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Set an icon for this button |
||
289 | * |
||
290 | * Feel free to use SilverStripeIcons constants |
||
291 | * |
||
292 | * @param string|null $buttonIcon An icon for this button |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setButtonIcon(string $buttonIcon = null) |
||
296 | { |
||
297 | $this->buttonIcon = $buttonIcon; |
||
298 | return $this; |
||
299 | } |
||
300 | } |
||
301 |
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