-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathWidgetController.php
374 lines (326 loc) · 10.9 KB
/
WidgetController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/**
* @package ImpressPages
*
*
*/
namespace Ip;
use Ip\Internal\Content\Model;
/**
* Controller for widgets
* @package Ip
*/
class WidgetController
{
protected $name;
protected $pluginName;
/**
* @var boolean - true if widget is installed by default
*/
protected $core;
const SKIN_DIR = 'skin';
protected $widgetDir;
protected $widgetAssetsDir;
public function __construct($name, $pluginName, $core = false)
{
$this->name = $name;
$this->pluginName = $pluginName;
$this->core = $core;
if ($this->core) {
$this->widgetDir = 'Ip/Internal/' . $pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/';
} else {
$this->widgetDir = 'Plugin/' . $pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/';
}
$this->widgetAssetsDir = $this->widgetDir . \Ip\Application::ASSETS_DIR . '/';
}
/**
* Gets widget title
*
* Override this method to set the widget name displayed in widget toolbar.
*
* @return string Widget's title
*/
public function getTitle()
{
return self::getName();
}
/**
* Return a name, which is unique widget identifier
*
* @return string Widget's name
*/
public function getName()
{
return $this->name;
}
/**
* Return a name of the plugin the widget belongs to.
*
* @return string Plugin name
*/
public function getPluginName()
{
return $this->pluginName;
}
/**
* Get widget's administration directory
*
* @return string Widget's directory path
*/
public function getWidgetDir()
{
return $this->widgetDir;
}
/**
* Check if the widget is native ImpressPages widget
*
* @return bool Returns false, if a widget is provided by installable plugin.
*/
public function isCore()
{
return $this->core;
}
/**
* Get widget icon URL
*
* Widget icon is displayed in widget toolbar of administration page.
*
* @return string Icon URL
*/
public function getIcon()
{
if ($this->core) {
if (file_exists(ipFile($this->widgetAssetsDir . 'icon.svg'))) {
return ipFileUrl($this->widgetAssetsDir . 'icon.svg');
}
if (file_exists(ipFile($this->widgetAssetsDir . 'icon.png'))) {
return ipFileUrl($this->widgetAssetsDir . 'icon.png');
}
} else {
if (file_exists(ipFile($this->widgetAssetsDir . 'icon.svg'))) {
return ipFileUrl($this->widgetAssetsDir . 'icon.svg');
}
if (file_exists(ipFile($this->widgetAssetsDir . 'icon.png'))) {
return ipFileUrl($this->widgetAssetsDir . 'icon.png');
}
}
return ipFileUrl('Ip/Internal/Content/assets/img/iconWidget.svg');
}
/**
* Override this method to set default data of the widget
*
* @return array Default data
*/
public function defaultData()
{
return array();
}
/**
* Get all widget skins
*
* @return array List of skins
* @throws \Ip\Exception\Content
*/
public function getSkins()
{
$views = array();
//collect default view files
$skinDir = ipFile($this->widgetDir . self::SKIN_DIR . '/');
if (!is_dir($skinDir)) {
throw new \Ip\Exception\Content('Skins directory does not exist. ' . esc($skinDir));
}
$availableViewFiles = scandir($skinDir);
foreach ($availableViewFiles as $viewFile) {
if (is_file($skinDir . $viewFile) && substr($viewFile, -4) == '.php') {
$views[substr($viewFile, 0, -4)] = 1;
}
}
//collect overridden theme view files
if ($this->isCore()) {
$themeViewsFolder = ipThemeFile(
\Ip\View::OVERRIDE_DIR . '/Ip/Internal/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR
);
} else {
$themeViewsFolder = ipThemeFile(
\Ip\View::OVERRIDE_DIR . '/Plugin/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR
);
}
if (is_dir($themeViewsFolder)) {
$availableViewFiles = scandir($themeViewsFolder);
foreach ($availableViewFiles as $viewFile) {
if (is_file($themeViewsFolder . '/' . $viewFile) && substr($viewFile, -4) == '.php') {
$views[substr($viewFile, 0, -4)] = 1;
}
}
}
$skins = array();
foreach ($views as $viewKey => $view) {
if ($this->isCore()) {
$translation = __(ucfirst($viewKey), 'Ip-admin', false);
} else {
$translation = __(ucfirst($viewKey), $this->pluginName, false);
}
$skins[] = array('name' => $viewKey, 'title' => $translation);
}
if (empty($skins)) {
throw new \Ip\Exception\Content('No skins');
}
return $skins;
}
/**
* Update widget data
*
* This method is executed each time the widget data is updated.
*
* @param int $widgetId Widget ID
* @param array $postData
* @param array $currentData
* @return array Data to be stored to the database
*/
public function update($widgetId, $postData, $currentData)
{
return $postData;
}
/**
* Process post data submitted in public mode
*
* You can make posts directly to your widget (e.g., when submitting HTML form in public page).
*
* If you pass the following parameters:
* sa=Content.widgetPost
* securityToken=actualSecurityToken
* widgetId=actualWidgetId
*
* then that post request will be redirected to the specified method.
*
* Use return new \Ip\Response\Json($jsonArray) to return JSON.
*
* Attention: this method is accessible for website visitors without admin login.
*
* @param int $widgetId Widget ID
* @param array $data Widget Data array
* @return mixed
*/
public function post($widgetId, $data)
{
}
/**
* Duplicate widget action
*
* This function is executed after the widget has been duplicated.
* All widget data is duplicated automatically. This method is used only in case a widget
* needs to do some maintenance tasks on duplication.
*
* @param int $oldId Old widget ID
* @param int $newId Duplicated widget ID
* @param array $data Data that has been duplicated from old widget to the new one
* @return array
*/
public function duplicate($oldId, $newId, $data)
{
return $data;
}
/**
* Delete a widget
*
* This method is executed before actual deletion of a widget.
* It is used to remove widget data (e.g., photos, files, additional database records and so on).
* Standard widget data is being deleted automatically. So you don't need to extend this method
* if your widget does not upload files or add new records to the database manually.
* @param int $widgetId Widget ID
* @param array $data Data that is being stored in the widget
*/
public function delete($widgetId, $data)
{
}
public function adminHtmlSnippet()
{
$snippetDir = ipFile($this->getWidgetDir() . \Ip\Internal\Content\Model::SNIPPET_DIR) . '/';
if (!is_dir($snippetDir)) {
return array();
}
$snippetFiles = scandir($snippetDir);
$snippet = '';
foreach ($snippetFiles as $snippetFile) {
if (is_file($snippetDir . $snippetFile) && substr($snippetFile, -4) == '.php') {
$snippet .= ipView($snippetDir . $snippetFile)->render();
}
}
return $snippet;
}
/**
* Renders widget's HTML output
*
* You can extend this method when generating widget's HTML.
*
* @param int $revisionId Widget revision ID
* @param int $widgetId Widget ID
* @param int $widgetId Widget instance ID
* @param array $data Widget data array
* @param string $skin Skin name
* @return string Widget's HTML code
*/
public function generateHtml($revisionId, $widgetId, $data, $skin)
{
try {
if ($this->core) {
$skinFile = 'Ip/Internal/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR . '/' . $skin . '.php';
} else {
$skinFile = 'Plugin/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR . '/' . $skin . '.php';
}
if (!is_file(ipFile($skinFile)) && !is_file(ipThemeFile(\Ip\View::OVERRIDE_DIR . '/' . $skinFile))) {
$skin = 'default';
if ($this->core) {
$skinFile = 'Ip/Internal/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR . '/' . $skin . '.php';
} else {
$skinFile = 'Plugin/' . $this->pluginName . '/' . Model::WIDGET_DIR . '/' . $this->name . '/' . self::SKIN_DIR . '/' . $skin . '.php';
}
}
$answer = ipView($skinFile, $data)->render();
} catch (\Ip\Exception $e) {
if (ipIsManagementState()) {
$answer = $e->getMessage() . "\n " . $e->getTraceAsString();
} else {
$answer = '';
}
}
return $answer;
}
/**
* Process data which is passed to widget's JavaScript file for processing
*
* @param int $revisionId Widget revision ID
* @param int $widgetId Widget ID
* @param int $widgetId Widget instance ID
* @param array $data Widget data array
* @param string $skin Widget skin name
* @return array Data array
*/
public function dataForJs($revisionId, $widgetId, $data, $skin)
{
return $data;
}
/**
* Array 0f menu items to be added to the widget's options menu. (gear box on the left top corner of the widget)
* @param $revisionId
* @param $widgetId
* @param $data
* @param $skin
* @return array
*/
public function optionsMenu($revisionId, $widgetId, $data, $skin)
{
return array();
// example with one menu item
// $answer = array();
// $answer[] = array(
// 'title' => 'Menu item title',
// 'attributes' => array(
// 'class' => 'ipsMyMenu', //class to be added. Could be used in JS to bind actions on this button
// 'data-somedata' => json_encode('lorem ipsum'), //data that later can be accessed in js. E.g. $('.ipsMyMenu').first().data('somedata');
// ...
// )
// );
// return $answer;
}
}