3
* Xibo - Digital Signage - http://www.xibo.org.uk
4
* Copyright (C) 2014-15 Daniel Garner
6
* This file is part of Xibo.
8
* Xibo is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Affero General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
13
* Xibo is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Affero General Public License for more details.
18
* You should have received a copy of the GNU Affero General Public License
19
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
23
use Xibo\Helper\ApplicationState;
26
use Xibo\Helper\Theme;
28
class clock extends Module
30
public $codeSchemaVersion = 1;
32
public function InstallFiles()
35
$media->addModuleFile('modules/preview/vendor/jquery-1.11.1.min.js');
36
$media->addModuleFile('modules/preview/vendor/jquery-cycle-2.1.6.min.js');
37
$media->addModuleFile('modules/preview/vendor/moment.js');
38
$media->addModuleFile('modules/preview/vendor/flipclock.min.js');
39
$media->addModuleFile('modules/preview/xibo-layout-scaler.js');
43
* Return the Add Form as HTML
45
public function AddForm()
47
$response = $this->getState();
50
$this->configureForm('AddMedia');
52
$formFields = array();
54
// Offer a choice of clock type
55
$formFields[] = Form::AddCombo(
60
array('clockTypeId' => '1', 'clockType' => 'Analogue'),
61
array('clockTypeId' => '2', 'clockType' => 'Digital'),
62
array('clockTypeId' => '3', 'clockType' => 'Flip Clock')
66
__('Please select the type of clock to display.'),
69
$formFields[] = Form::AddNumber('duration', __('Duration'), NULL,
70
__('The duration in seconds this item should be displayed.'), 'd', 'required');
72
$formFields[] = Form::AddNumber('offset', __('Offset'), NULL,
73
__('The offset in minutes that should be applied to the current time.'), 'o', NULL, 'offset-control-group');
75
// Offer a choice of theme
76
$formFields[] = Form::AddCombo(
80
array(array('themeid' => '1', 'theme' => 'Light'), array('themeid' => '2', 'theme' => 'Dark')),
83
__('Please select a theme for the clock.'),
85
'analogue-control-group');
87
$formFields[] = Form::AddMessage(sprintf(__('Enter a format for the Digital Clock below. e.g. [HH:mm] or [DD/MM/YYYY]. See the <a href="%s" target="_blank">format guide</a> for more information.'), Help::Link('Widget', 'ClockFormat')), 'digital-control-group');
89
$formFields[] = Form::AddMultiText('ta_text', NULL, '[HH:mm]',
90
__('Enter a format for the clock'), 'f', 10, '', 'digital-control-group');
92
Theme::Set('form_fields', $formFields);
94
// Dependencies (some fields should be shown / hidden)
95
$this->SetFieldDependencies($response);
97
// Modules should be rendered using the theme engine.
98
$response->html = Theme::RenderReturn('form_render');
100
$this->configureFormButtons($response);
101
$response->dialogTitle = __('Add Clock');
102
$response->callBack = 'text_callback';
104
// The response must be returned.
109
* Add Media to the Database
111
public function AddMedia()
113
$response = $this->getState();
115
// You must also provide a duration (all media items must provide this field)
116
$this->setDuration(Kit::GetParam('duration', _POST, _INT, $this->getDuration(), false));
117
$this->SetOption('theme', \Kit::GetParam('themeid', _POST, _INT, 0));
118
$this->SetOption('clockTypeId', \Kit::GetParam('clockTypeId', _POST, _INT, 1));
119
$this->SetOption('offset', \Kit::GetParam('offset', _POST, _INT, 0));
120
$this->setRawNode('ta_text', \Kit::GetParam('ta_text', _POST, _HTMLSTRING));
126
$response->loadForm = true;
127
$response->loadFormUri = $this->getTimelineLink();
133
* Return the Edit Form as HTML
135
public function EditForm()
137
$response = $this->getState();
139
// Edit calls are the same as add calls, except you will to check the user has permissions to do the edit
140
if (!$this->auth->edit)
141
throw new Exception(__('You do not have permission to edit this widget.'));
143
// Configure the form
144
$this->configureForm('EditMedia');
147
$formFields = array();
149
// Offer a choice of clock type
150
$formFields[] = Form::AddCombo(
153
$this->GetOption('clockTypeId'),
155
array('clockTypeId' => '1', 'clockType' => 'Analogue'),
156
array('clockTypeId' => '2', 'clockType' => 'Digital'),
157
array('clockTypeId' => '3', 'clockType' => 'Flip Clock')
161
__('Please select the type of clock to display.'),
164
$formFields[] = Form::AddNumber('duration', __('Duration'), $this->getDuration(),
165
__('The duration in seconds this item should be displayed'), 'd', 'required');
168
$formFields[] = Form::AddNumber('offset', __('Offset'), $this->GetOption('offset'),
169
__('The offset in minutes that should be applied to the current time.'), 'o', NULL, 'offset-control-group');
171
// Offer a choice of theme
172
$formFields[] = Form::AddCombo(
175
$this->GetOption('theme'),
176
array(array('themeid' => '1', 'theme' => 'Light'), array('themeid' => '2', 'theme' => 'Dark')),
179
__('Please select a theme for the clock.'),
181
'analogue-control-group');
183
$formFields[] = Form::AddMessage(sprintf(__('Enter a format for the Digital Clock below. e.g. [HH:mm] or [DD/MM/YYYY]. See the <a href="%s" target="_blank">format guide</a> for more information.'), Help::Link('Widget', 'ClockFormat')), 'digital-control-group');
185
$formFields[] = Form::AddMultiText('ta_text', NULL, $this->getRawNode('format', null),
186
__('Enter a format for the clock'), 'f', 10, '', 'digital-control-group');
188
Theme::Set('form_fields', $formFields);
190
// Dependencies (some fields should be shown / hidden)
191
$this->SetFieldDependencies($response);
193
// Modules should be rendered using the theme engine.
194
$response->html = Theme::RenderReturn('form_render');
196
$this->configureFormButtons($response);
197
$response->dialogTitle = __('Edit Clock');
198
$response->callBack = 'text_callback';
200
$this->response->AddButton(__('Apply'), 'XiboDialogApply("#ModuleForm")');
204
* Edit Media in the Database
206
public function EditMedia()
208
$response = $this->getState();
210
if (!$this->auth->edit)
211
throw new Exception(__('You do not have permission to edit this widget.'));
213
// You must also provide a duration (all media items must provide this field)
214
$this->setDuration(Kit::GetParam('duration', _POST, _INT, $this->getDuration(), false));
215
$this->SetOption('theme', \Kit::GetParam('themeid', _POST, _INT, 0));
216
$this->SetOption('clockTypeId', \Kit::GetParam('clockTypeId', _POST, _INT, 1));
217
$this->SetOption('offset', \Kit::GetParam('offset', _POST, _INT, 0));
218
$this->setRawNode('ta_text', \Kit::GetParam('ta_text', _POST, _HTMLSTRING));
224
$response->loadForm = true;
225
$response->loadFormUri = $this->getTimelineLink();
226
$this->response->callBack = 'refreshPreview("' . $this->regionid . '")';
232
* @param ApplicationState $response
234
private function SetFieldDependencies(&$response)
236
$clockTypeId_1 = array(
237
'.analogue-control-group' => array('display' => 'block'),
238
'.digital-control-group' => array('display' => 'none'),
239
'.flip-control-group' => array('display' => 'none'),
240
'.offset-control-group' => array('display' => 'block')
243
$clockTypeId_2 = array(
244
'.analogue-control-group' => array('display' => 'none'),
245
'.digital-control-group' => array('display' => 'block'),
246
'.flip-control-group' => array('display' => 'none'),
247
'.offset-control-group' => array('display' => 'block')
250
$clockTypeId_3 = array(
251
'.analogue-control-group' => array('display' => 'none'),
252
'.digital-control-group' => array('display' => 'none'),
253
'.flip-control-group' => array('display' => 'block'),
254
'.offset-control-group' => array('display' => 'none')
257
$response->AddFieldAction('clockTypeId', 'init', 1, $clockTypeId_1);
258
$response->AddFieldAction('clockTypeId', 'change', 1, $clockTypeId_1);
259
$response->AddFieldAction('clockTypeId', 'init', 2, $clockTypeId_2);
260
$response->AddFieldAction('clockTypeId', 'change', 2, $clockTypeId_2);
261
$response->AddFieldAction('clockTypeId', 'init', 3, $clockTypeId_3);
262
$response->AddFieldAction('clockTypeId', 'change', 3, $clockTypeId_3);
267
* Return the rendered resource to be used by the client (or a preview) for displaying this content.
268
* @param integer $displayId If this comes from a real client, this will be the display id.
271
public function GetResource($displayId = 0)
276
switch ($this->GetOption('clockTypeId', 1)) {
280
$template = file_get_contents('modules/theme/HtmlTemplateForClock.html');
282
// Render our clock face
283
$theme = ($this->GetOption('theme') == 1 ? 'light' : 'dark');
284
$theme_face = ($this->GetOption('theme') == 1 ? 'clock_bg_modern_light.png' : 'clock_bg_modern_dark.png');
286
$template = str_replace('<!--[[[CLOCK_FACE]]]-->', base64_encode(file_get_contents('modules/theme/' . $theme_face)), $template);
289
$template = str_replace('<!--[[[CLOCK_THEME]]]-->', $theme, $template);
290
$template = str_replace('<!--[[[OFFSET]]]-->', $this->GetOption('offset', 0), $template);
292
// After body content
293
$isPreview = (\Kit::GetParam('preview', _REQUEST, _WORD, 'false') == 'true');
294
$javaScriptContent = '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'jquery-1.11.1.min.js"></script>';
295
$javaScriptContent .= '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'moment.js"></script>';
297
// Replace the After body Content
298
$template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
303
// Digital clock is essentially a cut down text module which always fits to the region
304
$template = file_get_contents('modules/preview/HtmlTemplate.html');
306
// Extract the format from the raw node in the XLF
307
$format = $this->getRawNode('format', null);
309
// Strip out the bit between the [] brackets and use that as the format mask for moment.
311
preg_match_all('/\[.*?\]/', $format, $matches);
313
foreach($matches[0] as $subs) {
314
$format = str_replace($subs, '<span class="clock" format="' . str_replace('[', '', str_replace(']', '', $subs)) . '"></span>', $format);
317
// Replace all the subs
318
$template = str_replace('<!--[[[BODYCONTENT]]]-->', $format, $template);
320
// After body content
322
'previewWidth' => \Kit::GetParam('width', _GET, _DOUBLE, 0),
323
'previewHeight' => \Kit::GetParam('height', _GET, _DOUBLE, 0),
324
'originalWidth' => $this->region->width,
325
'originalHeight' => $this->region->height,
326
'scaleOverride' => \Kit::GetParam('scale_override', _GET, _DOUBLE, 0)
329
$isPreview = (\Kit::GetParam('preview', _REQUEST, _WORD, 'false') == 'true');
330
$javaScriptContent = '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'jquery-1.11.1.min.js"></script>';
331
$javaScriptContent .= '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'moment.js"></script>';
332
$javaScriptContent .= '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/' : '') . 'xibo-layout-scaler.js"></script>';
333
$javaScriptContent .= '<script type="text/javascript">
334
var locale = "' . TranslationEngine::GetJsLocale() . '";
335
var options = ' . json_encode($options) . ';
337
function updateClock() {
338
$(".clock").each(function() {
339
$(this).html(moment().add(' . {$this->GetOption('offset', 0)} . ', "m").format($(this).attr("format")));
343
$(document).ready(function() {
344
moment.locale(locale);
346
setInterval(updateClock, 1000);
347
$("body").xiboLayoutScaler(options);
352
// Replace the After body Content
353
$template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
355
// Add our fonts.css file
356
$headContent = '<link href="' . (($isPreview) ? 'modules/preview/' : '') . 'fonts.css" rel="stylesheet" media="screen">';
357
$headContent .= '<style type="text/css">' . file_get_contents(Theme::ItemPath('css/client.css')) . '</style>';
359
$template = str_replace('<!--[[[HEADCONTENT]]]-->', $headContent, $template);
365
$template = file_get_contents('modules/theme/HtmlTemplateForFlipClock.html');
367
// Head Content (CSS for flip clock)
368
$template = str_replace('<!--[[[HEADCONTENT]]]-->', '<style type="text/css">' . file_get_contents('modules/preview/vendor/flipclock.css') . '</style>', $template);
369
$template = str_replace('<!--[[[OFFSET]]]-->', $this->GetOption('offset', 0), $template);
371
// After body content
372
$isPreview = (\Kit::GetParam('preview', _REQUEST, _WORD, 'false') == 'true');
373
$javaScriptContent = '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'jquery-1.11.1.min.js"></script>';
374
$javaScriptContent .= '<script type="text/javascript" src="' . (($isPreview) ? 'modules/preview/vendor/' : '') . 'flipclock.min.js"></script>';
376
// Replace the After body Content
377
$template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
382
// If we are a preview, then pass in the width and height
383
$template = str_replace('<!--[[[PREVIEW_WIDTH]]]-->', \Kit::GetParam('width', _GET, _DOUBLE, 0), $template);
384
$template = str_replace('<!--[[[PREVIEW_HEIGHT]]]-->', \Kit::GetParam('height', _GET, _DOUBLE, 0), $template);
386
// Replace the View Port Width?
387
if (isset($_GET['preview']))
388
$template = str_replace('[[ViewPortWidth]]', $this->region->width, $template);
390
// Return that content.
398
public function IsValid()
400
// Using the information you have in your module calculate whether it is valid or not.