-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessHelpVideos.module.php
391 lines (350 loc) · 12.4 KB
/
ProcessHelpVideos.module.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
namespace ProcessWire;
class ProcessHelpVideos extends Process implements ConfigurableModule
{
/**
* Page edit form
*
* @var InputfieldForm
*
*/
protected $form;
/**
* Page being edited
*
* @var Page
*
*/
protected $page;
/**
* description field id
*
* @var int
*
*/
protected $descriptionField;
/**
* description field name
*
* @var string
*
*/
protected $descfieldName;
/**
* file field id
*
* @var int
*
*/
protected $fileField;
/**
* file field name
*
* @var string
*
*/
protected $filefieldName;
/**
* parent template name
*
* @var string
*
*/
protected $parentTemplate;
/**
* parent page id
*
* @var int
*
*/
protected $parentPage;
/**
* child template name
*
* @var string
*
*/
protected $childTemplate;
/**
* This is an optional initialization function called before any execute functions.
*
* If you don't need to do any initialization common to every execution of this module,
* you can simply remove this init() method.
*
*/
public function init()
{
// populate properties from module config data
$moduleConfig = new \ProcessWire\ProcessHelpVideosConfig;
$data = array_merge($moduleConfig->getDefaults(), $this->data); // merge with default data
foreach ($data as $key => $value) $this->$key = $value; // populate final data to class properties
$this->addScriptsAndStyles();
parent::init(); // always remember to call the parent init
}
/**
* This function is executed when a page with your Process assigned is accessed.
*
* This can be seen as your main or index function. You'll probably want to replace
* everything in this function.
*
*/
public function ___execute()
{
$this->headline($this->wire('pages')->get($this->parentPage)->title);
return $this->renderHelpDashboard();
}
/**
* renders the help video section
*
* @return string
*/
public function renderHelpDashboard()
{
return $this->renderItems();
}
/**
* renders help videos
*
* @return string
*/
public function renderItems()
{
$helpItems = $this->wire('pages')->find("template={$this->childTemplate}, sort=sort");
if (!$helpItems->count) {
$parentPage = $this->wire('pages')->get($this->parentPage);
$out = "<strong>" . $this->_('There are no help videos yet.') . "</strong><br>";
$out .=
sprintf($this->_('To add help videos, add child pages to the page %s in the page tree.'), "<strong>{$parentPage->title}</strong>");
return $out;
}
$out = '<div class="uk-grid-match" uk-grid>';
foreach ($helpItems as $item) {
$description = $item->get($this->descriptionField);
$pageEditlink = ($item->editable) ? "<br><a href='{$item->editUrl}' title='" . $this->_('Edit page') . "'>"
. $this->_('Edit') . " {$item->title}</a>" : '';
$video = $this->renderVideo($item, $pageEditlink);
$out .= "
<div class='uk-width-1-3'>
<div class='uk-card uk-card-default uk-card-body uk-flex uk-flex-column uk-flex-between'>
<h3 class='uk-card-title'>{$item->title}</h3>
<div class='videocontent'>
{$description}
{$video}
{$pageEditlink}
</div>
</div>
</div>";
}
$out .= '</div>';
return $out;
}
/**
* Render video tag
*
* @param \ProcessWire\Page $page
* @param string $editLink
* @return string
*/
public function renderVideo($page, $editLink)
{
// if ($page->of()) $page->of(false);
$out = '';
$source = '';
foreach ($page->{$this->filefieldName} as $file) {
$id = $file->created;
$source .= "<source src='{$file->httpUrl()}' type='video/{$file->ext()}'>";
}
if (empty($source)) {
$pageEditlink = ($page->editable) ? "<a href='{$page->editUrl}' title='Edit page'>{$page->title}</a>" : $page->title;
$out = '<strong>' . $this->_('No video found.') . '</strong>';
if (empty($editLink) && $page->editable) $out .= '<br>' . sprintf($this->_('Please upload a video to page %s'), "<br>{$pageEditlink}");
return $out;
}
$vjsOptions = json_encode(array(
'fluid' => true,
"liveui" => true
));
$out .= "<video uk-responsive-width id='id-{$id}' class='video-js vjs-big-play-centered' controls preload='auto' data-vjsoptions='{$vjsOptions}'>
{$source}
<p class='vjs-no-js'>
To view this video please enable JavaScript, and consider upgrading to a web browser that
<a href='http://videojs.com/html5-video-support/' target='_blank'>supports HTML5 video</a>
</p>
</video>
";
return $out;
}
/**
* loads scripts and styles
*
* @return void
*/
public function addScriptsAndStyles()
{
$this->wire('config')->styles->add("https://vjs.zencdn.net/5.19.2/video-js.css");
$this->wire('config')->scripts->add("https://vjs.zencdn.net/5.19.2/video.js");
if ($this->wire('user')->admin_theme != 'AdminThemeUikit') {
$this->wire('config')->styles->add($this->wire('config')->urls->{$this->className} . 'assets/nouikit.css');
}
}
/**
* installs all necessary fields
*
* @return void
*/
public function installFields()
{
$fields = $this->wire('fields');
$ckeFields = array();
$fileFields = array();
// check for CKEditor fields that we can use for help video template
foreach ($fields->find('type=FieldtypeTextarea') as $f) {
if ($f->inputfieldClass != 'InputfieldCKEditor') continue;
$ckeFields[] = ['id' => $f->id, 'name' => $f->name];
}
// check for file fields that we can use for help video template
foreach ($fields->find('type=FieldtypeFile') as $f) {
if (strpos($f->extensions, 'mp4') === false) continue;
$fileFields[] = ['id' => $f->id, 'name' => $f->name];
}
// no ckeditor fields found, create one
if (!count($ckeFields) && !$fields->get($this->descfieldName)) {
$field = new \ProcessWire\Field;
$field->type = $this->wire('modules')->get('FieldtypeTextarea');
$field->name = $this->descfieldName;
$field->label = "Description";
$field->inputfield = "InputfieldCKEditor";
$field->inputfieldClass = "InputfieldCKEditor";
$field->contentType = 1; // contentTypeHTML
$fields->save($field);
$field = $fields->get($this->descfieldName);
$this->descriptionField = $field->id;
$this->descfieldName = $field->name;
} else {
$this->descriptionField = $ckeFields[0]['id'];
$this->descfieldName = $ckeFields[0]['name'];
}
// no file fields found, create one
if (!count($fileFields) && !$fields->get($this->filefieldName)) {
$field = new \ProcessWire\Field;
$field->type = $this->wire('modules')->get('FieldtypeFile');
$field->name = $this->filefieldName;
$field->label = "Video";
$field->extensions = "mp4 webm";
$field->inputfield = "InputfieldFile";
$field->descriptionRows = 0;
$fields->save($field);
$field = $fields->get($this->filefieldName);
$this->fileField = $field->id;
$this->filefieldName = $field->name;
} else {
$this->fileField = $fileFields[0]['id'];
$this->filefieldName = $fileFields[0]['name'];
}
$configData = $this->wire('modules')->getModuleConfigData($this);
$configData['fileField'] = $this->fileField;
$configData['filefieldName'] = $this->filefieldName;
$configData['descriptionField'] = $this->descriptionField;
$configData['descfieldName'] = $this->descfieldName;
$this->wire('modules')->saveModuleConfigData($this, $configData);
}
/**
* installs all necessary templates
* also adjusts template family settings
*
* @return void
*/
public function installTemplates()
{
$fields = $this->wire('fields');
$templates = $this->wire('templates');
if (!$templates->get($this->parentTemplate)) {
$fieldgroup = new \ProcessWire\Fieldgroup;
$fieldgroup->name = $this->parentTemplate;
$fieldgroup->add($fields->get('title'));
$fieldgroup->save();
$template = new \ProcessWire\Template;
$template->name = $this->parentTemplate;
$template->setFieldgroup($fieldgroup);
$parentTemplate = $template->save();
$this->parentTemplate = $parentTemplate->id;
}
if (!$templates->get($this->childTemplate)) {
$fieldgroup = new \ProcessWire\Fieldgroup;
$fieldgroup->name = $this->childTemplate;
$fieldgroup->add($fields->get('title'));
$fieldgroup->add($fields->get($this->descriptionField));
$fieldgroup->add($fields->get($this->fileField));
$fieldgroup->save();
$template = new \ProcessWire\Template;
$template->name = $this->childTemplate;
$template->setFieldgroup($fieldgroup);
// template family settings
$template->noChildren = 1;
$template->parentTemplates = array($templates->get($this->parentTemplate)->id);
$childTemplate = $template->save();
$this->childTemplate = $childTemplate->id;
}
// template family settings
$homeTemplate = $templates->get('home');
// set childTemlates and parentTemplate for parentTemplate
$parentTemplate->childTemplates = array($templates->get($this->childTemplate)->id);
$parentTemplate->parentTemplates = array($homeTemplate->id);
$parentTemplate->save();
// set parentTemplate as childTemplate for home template if home template has set allowed child page templates
$childTemplates = $homeTemplate->childTemplates;
if (count($childTemplates)) {
array_push($childTemplates, $parentTemplate->id);
$homeTemplate->childTemplates = $childTemplates;
$homeTemplate->save();
}
}
/**
* installs the parent page for holding the help video pages
*
* @return void
*/
public function installVideoParent()
{
$home = $this->wire('pages')->get(1);
$helpvideosParent = new \ProcessWire\Page;
$helpvideosParent->template = $this->parentTemplate;
$helpvideosParent->parent = $home;
$helpvideosParent->title = "Help Videos";
$helpvideosParent->name = 'processhelpvideos-help';
$helpvideosParent->addStatus('hidden');
$helpvideosParent->save();
$this->parentPage = $helpvideosParent->id;
$configData = $this->wire('modules')->getModuleConfigData($this);
$configData['parentPage'] = $helpvideosParent->id;
$this->wire('modules')->saveModuleConfigData($this, $configData);
}
/**
* Called only when your module is installed
*
* If you don't need anything here, you can simply remove this method.
*
*/
public function ___install()
{
$moduleConfig = new \ProcessWire\ProcessHelpVideosConfig;
$defaults = $moduleConfig->getDefaults();
foreach ($defaults as $key => $value) $this->$key = $value;
$this->installFields();
$this->installTemplates();
$this->installVideoParent();
parent::___install(); // always remember to call parent method
}
/**
* Called only when your module is uninstalled
*
* This should return the site to the same state it was in before the module was installed.
*
* If you don't need anything here, you can simply remove this method.
*
*/
public function ___uninstall()
{
parent::___uninstall(); // always remember to call parent method
}
}