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