~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to modules/clock.module.php

  • Committer: Dan Garner
  • Date: 2015-06-11 15:10:51 UTC
  • mto: This revision was merged to the branch mainline in revision 429.
  • Revision ID: git-v1:c4db8c301380f37bc63cb37fc0894c4bba0e1249
Namespaced Modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Xibo - Digital Signage - http://www.xibo.org.uk
4
 
 * Copyright (C) 2014-15 Daniel Garner
5
 
 *
6
 
 * This file is part of Xibo.
7
 
 *
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
11
 
 * any later version. 
12
 
 *
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.
17
 
 *
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/>.
20
 
 *
21
 
 */
22
 
use Widget\Module;
23
 
use Xibo\Helper\ApplicationState;
24
 
use Xibo\Helper\Form;
25
 
use Xibo\Helper\Help;
26
 
use Xibo\Helper\Theme;
27
 
 
28
 
class clock extends Module
29
 
{
30
 
    public $codeSchemaVersion = 1;
31
 
 
32
 
    public function InstallFiles()
33
 
    {
34
 
        $media = new Media();
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');
40
 
    }
41
 
 
42
 
    /**
43
 
     * Return the Add Form as HTML
44
 
     */
45
 
    public function AddForm()
46
 
    {
47
 
        $response = $this->getState();
48
 
 
49
 
        // Configure form
50
 
        $this->configureForm('AddMedia');
51
 
    
52
 
        $formFields = array();
53
 
 
54
 
        // Offer a choice of clock type
55
 
        $formFields[] = Form::AddCombo(
56
 
                    'clockTypeId', 
57
 
                    __('Clock Type'), 
58
 
                    NULL,
59
 
                    array(
60
 
                        array('clockTypeId' => '1', 'clockType' => 'Analogue'),
61
 
                        array('clockTypeId' => '2', 'clockType' => 'Digital'),
62
 
                        array('clockTypeId' => '3', 'clockType' => 'Flip Clock')
63
 
                    ),
64
 
                    'clockTypeId',
65
 
                    'clockType',
66
 
                    __('Please select the type of clock to display.'), 
67
 
                    'c');
68
 
 
69
 
        $formFields[] = Form::AddNumber('duration', __('Duration'), NULL,
70
 
            __('The duration in seconds this item should be displayed.'), 'd', 'required');
71
 
 
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');
74
 
 
75
 
        // Offer a choice of theme
76
 
        $formFields[] = Form::AddCombo(
77
 
                    'themeid', 
78
 
                    __('Theme'), 
79
 
                    NULL,
80
 
                    array(array('themeid' => '1', 'theme' => 'Light'), array('themeid' => '2', 'theme' => 'Dark')),
81
 
                    'themeid',
82
 
                    'theme',
83
 
                    __('Please select a theme for the clock.'), 
84
 
                    't',
85
 
                    'analogue-control-group');
86
 
 
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');
88
 
        
89
 
        $formFields[] = Form::AddMultiText('ta_text', NULL, '[HH:mm]',
90
 
            __('Enter a format for the clock'), 'f', 10, '', 'digital-control-group');
91
 
 
92
 
        Theme::Set('form_fields', $formFields);
93
 
 
94
 
        // Dependencies (some fields should be shown / hidden)
95
 
        $this->SetFieldDependencies($response);
96
 
 
97
 
        // Modules should be rendered using the theme engine.
98
 
        $response->html = Theme::RenderReturn('form_render');
99
 
 
100
 
        $this->configureFormButtons($response);
101
 
        $response->dialogTitle = __('Add Clock');
102
 
        $response->callBack = 'text_callback';
103
 
 
104
 
        // The response must be returned.
105
 
        return $response;
106
 
    }
107
 
 
108
 
    /**
109
 
     * Add Media to the Database
110
 
     */
111
 
    public function AddMedia()
112
 
    {
113
 
        $response = $this->getState();
114
 
 
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));
121
 
 
122
 
        // Save the widget
123
 
        $this->saveWidget();
124
 
 
125
 
        // Load form
126
 
        $response->loadForm = true;
127
 
        $response->loadFormUri = $this->getTimelineLink();
128
 
        
129
 
        return $response;
130
 
    }
131
 
 
132
 
    /**
133
 
     * Return the Edit Form as HTML
134
 
     */
135
 
    public function EditForm()
136
 
    {
137
 
        $response = $this->getState();
138
 
 
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.'));
142
 
 
143
 
        // Configure the form
144
 
        $this->configureForm('EditMedia');
145
 
 
146
 
        // Build the form
147
 
        $formFields = array();
148
 
 
149
 
        // Offer a choice of clock type
150
 
        $formFields[] = Form::AddCombo(
151
 
                    'clockTypeId', 
152
 
                    __('Clock Type'), 
153
 
                    $this->GetOption('clockTypeId'),
154
 
                    array(
155
 
                        array('clockTypeId' => '1', 'clockType' => 'Analogue'),
156
 
                        array('clockTypeId' => '2', 'clockType' => 'Digital'),
157
 
                        array('clockTypeId' => '3', 'clockType' => 'Flip Clock')
158
 
                    ),
159
 
                    'clockTypeId',
160
 
                    'clockType',
161
 
                    __('Please select the type of clock to display.'), 
162
 
                    'c');
163
 
 
164
 
        $formFields[] = Form::AddNumber('duration', __('Duration'), $this->getDuration(),
165
 
            __('The duration in seconds this item should be displayed'), 'd', 'required');
166
 
 
167
 
 
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');
170
 
 
171
 
        // Offer a choice of theme
172
 
        $formFields[] = Form::AddCombo(
173
 
                    'themeid', 
174
 
                    __('Theme'), 
175
 
                    $this->GetOption('theme'),
176
 
                    array(array('themeid' => '1', 'theme' => 'Light'), array('themeid' => '2', 'theme' => 'Dark')),
177
 
                    'themeid',
178
 
                    'theme',
179
 
                    __('Please select a theme for the clock.'), 
180
 
                    't',
181
 
                    'analogue-control-group');
182
 
 
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');
184
 
        
185
 
        $formFields[] = Form::AddMultiText('ta_text', NULL, $this->getRawNode('format', null),
186
 
            __('Enter a format for the clock'), 'f', 10, '', 'digital-control-group');
187
 
 
188
 
        Theme::Set('form_fields', $formFields);
189
 
 
190
 
        // Dependencies (some fields should be shown / hidden)
191
 
        $this->SetFieldDependencies($response);
192
 
 
193
 
        // Modules should be rendered using the theme engine.
194
 
        $response->html = Theme::RenderReturn('form_render');
195
 
 
196
 
        $this->configureFormButtons($response);
197
 
        $response->dialogTitle = __('Edit Clock');
198
 
        $response->callBack = 'text_callback';
199
 
        return $response;
200
 
        $this->response->AddButton(__('Apply'), 'XiboDialogApply("#ModuleForm")');
201
 
    }
202
 
 
203
 
    /**
204
 
     * Edit Media in the Database
205
 
     */
206
 
    public function EditMedia()
207
 
    {
208
 
        $response = $this->getState();
209
 
 
210
 
        if (!$this->auth->edit)
211
 
            throw new Exception(__('You do not have permission to edit this widget.'));
212
 
 
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));
219
 
 
220
 
        // Save the widget
221
 
        $this->saveWidget();
222
 
 
223
 
        // Load an edit form
224
 
        $response->loadForm = true;
225
 
        $response->loadFormUri = $this->getTimelineLink();
226
 
            $this->response->callBack = 'refreshPreview("' . $this->regionid . '")';
227
 
        
228
 
        return $response;
229
 
    }
230
 
 
231
 
    /**
232
 
     * @param ApplicationState $response
233
 
     */
234
 
    private function SetFieldDependencies(&$response)
235
 
    {
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')
241
 
            );
242
 
 
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')
248
 
            );
249
 
 
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')
255
 
            );
256
 
            
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);
263
 
    }
264
 
 
265
 
    /**
266
 
     * GetResource
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.
269
 
     * @return mixed
270
 
     */
271
 
    public function GetResource($displayId = 0)
272
 
    {
273
 
        $template = null;
274
 
 
275
 
        // Clock Type
276
 
        switch ($this->GetOption('clockTypeId', 1)) {
277
 
 
278
 
            case 1:
279
 
                // Analogue
280
 
                $template = file_get_contents('modules/theme/HtmlTemplateForClock.html');
281
 
                
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');
285
 
                 
286
 
                $template = str_replace('<!--[[[CLOCK_FACE]]]-->', base64_encode(file_get_contents('modules/theme/' . $theme_face)), $template);
287
 
                
288
 
                // Light or dark?
289
 
                $template = str_replace('<!--[[[CLOCK_THEME]]]-->', $theme, $template);
290
 
                $template = str_replace('<!--[[[OFFSET]]]-->', $this->GetOption('offset', 0), $template);
291
 
 
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>';
296
 
                
297
 
                // Replace the After body Content
298
 
                $template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
299
 
                break;
300
 
 
301
 
            case 2:
302
 
                // Digital
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');
305
 
 
306
 
                // Extract the format from the raw node in the XLF
307
 
                $format = $this->getRawNode('format', null);
308
 
 
309
 
                // Strip out the bit between the [] brackets and use that as the format mask for moment.
310
 
                $matches = '';
311
 
                preg_match_all('/\[.*?\]/', $format, $matches);
312
 
 
313
 
                foreach($matches[0] as $subs) {
314
 
                    $format = str_replace($subs, '<span class="clock" format="' . str_replace('[', '', str_replace(']', '', $subs)) . '"></span>', $format);
315
 
                }
316
 
 
317
 
                // Replace all the subs
318
 
                $template = str_replace('<!--[[[BODYCONTENT]]]-->', $format, $template);
319
 
 
320
 
                // After body content
321
 
                $options = array(
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)
327
 
                    );
328
 
 
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) . ';
336
 
 
337
 
    function updateClock() {
338
 
        $(".clock").each(function() {
339
 
            $(this).html(moment().add(' . {$this->GetOption('offset', 0)} . ', "m").format($(this).attr("format")));
340
 
        });
341
 
    }
342
 
 
343
 
    $(document).ready(function() {
344
 
                        moment.locale(locale);
345
 
        updateClock();
346
 
        setInterval(updateClock, 1000);
347
 
        $("body").xiboLayoutScaler(options);
348
 
    });
349
 
</script>
350
 
END;
351
 
 
352
 
                // Replace the After body Content
353
 
                $template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
354
 
 
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>';
358
 
 
359
 
                $template = str_replace('<!--[[[HEADCONTENT]]]-->', $headContent, $template);
360
 
 
361
 
                break;
362
 
 
363
 
            case 3:
364
 
                // Flip Clock
365
 
                $template = file_get_contents('modules/theme/HtmlTemplateForFlipClock.html');
366
 
 
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);
370
 
 
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>';
375
 
 
376
 
                // Replace the After body Content
377
 
                $template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template);
378
 
 
379
 
                break;
380
 
        }
381
 
 
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);
385
 
 
386
 
        // Replace the View Port Width?
387
 
        if (isset($_GET['preview']))
388
 
            $template = str_replace('[[ViewPortWidth]]', $this->region->width, $template);
389
 
 
390
 
        // Return that content.
391
 
        return $template;
392
 
    }
393
 
 
394
 
    /**
395
 
     * Is Valid
396
 
     * @return int
397
 
     */
398
 
    public function IsValid()
399
 
    {
400
 
        // Using the information you have in your module calculate whether it is valid or not.
401
 
        // 0 = Invalid
402
 
        // 1 = Valid
403
 
        // 2 = Unknown
404
 
        return 1;
405
 
    }
406
 
}