~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/Clock.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
namespace Xibo\Widget;
23
23
 
24
24
use Respect\Validation\Validator as v;
 
25
use Xibo\Factory\MediaFactory;
 
26
use Xibo\Helper\Sanitize;
 
27
use Xibo\Helper\Theme;
25
28
use Xibo\Helper\Translate;
26
29
 
27
 
class Clock extends ModuleWidget
 
30
class Clock extends Module
28
31
{
29
32
    public $codeSchemaVersion = 1;
30
33
 
31
34
    public function installFiles()
32
35
    {
33
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/jquery-1.11.1.min.js')->save();
34
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/jquery-cycle-2.1.6.min.js')->save();
35
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/moment.js')->save();
36
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/flipclock.min.js')->save();
37
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/xibo-layout-scaler.js')->save();
 
36
        MediaFactory::createModuleFile('modules/vendor/jquery-1.11.1.min.js')->save();
 
37
        MediaFactory::createModuleFile('modules/vendor/jquery-cycle-2.1.6.min.js')->save();
 
38
        MediaFactory::createModuleFile('modules/vendor/moment.js')->save();
 
39
        MediaFactory::createModuleFile('modules/vendor/flipclock.min.js')->save();
 
40
        MediaFactory::createModuleFile('modules/xibo-layout-scaler.js')->save();
38
41
    }
39
42
 
40
43
    /**
43
46
    public function validate()
44
47
    {
45
48
        // Validate
46
 
        if ($this->getUseDuration() == 1 && !v::intType()->min(1)->validate($this->getDuration()))
47
 
            throw new \InvalidArgumentException(__('Please enter a duration.'));
 
49
        if (!v::int()->min(1)->validate($this->getDuration()))
 
50
            throw new \InvalidArgumentException(__('You must enter a duration.'));
48
51
    }
49
52
 
50
53
    /**
51
 
     * Adds a Clock Widget
52
 
     * @SWG\Post(
53
 
     *  path="/playlist/widget/clock/{playlistId}",
54
 
     *  operationId="WidgetClockAdd",
55
 
     *  tags={"widget"},
56
 
     *  summary="Add a Clock Widget",
57
 
     *  description="Add a new Clock Widget to the specified playlist",
58
 
     *  @SWG\Parameter(
59
 
     *      name="playlistId",
60
 
     *      in="path",
61
 
     *      description="The playlist ID to add a Clock widget to",
62
 
     *      type="integer",
63
 
     *      required=true
64
 
     *   ),
65
 
     *  @SWG\Parameter(
66
 
     *      name="name",
67
 
     *      in="formData",
68
 
     *      description="Optional Widget Name",
69
 
     *      type="string",
70
 
     *      required=false
71
 
     *  ),
72
 
     *  @SWG\Parameter(
73
 
     *      name="duration",
74
 
     *      in="formData",
75
 
     *      description="The Widget Duration",
76
 
     *      type="integer",
77
 
     *      required=false
78
 
     *  ),
79
 
     *  @SWG\Parameter(
80
 
     *      name="useDuration",
81
 
     *      in="formData",
82
 
     *      description="(0, 1) Select 1 only if you will provide duration parameter as well",
83
 
     *      type="integer",
84
 
     *      required=false
85
 
     *  ),
86
 
     *  @SWG\Parameter(
87
 
     *      name="themeId",
88
 
     *      in="formData",
89
 
     *      description="Flag (0 , 1) for Analogue clock the light and dark theme",
90
 
     *      type="integer",
91
 
     *      required=false
92
 
     *   ),
93
 
     *  @SWG\Parameter(
94
 
     *      name="clockTypeId",
95
 
     *      in="formData",
96
 
     *      description="Type of a clock widget 1-Analogue, 2-Digital, 3-Flip clock",
97
 
     *      type="integer",
98
 
     *      required=false
99
 
     *   ),
100
 
     *  @SWG\Parameter(
101
 
     *      name="offset",
102
 
     *      in="formData",
103
 
     *      description="The offset in minutes that should be applied to the current time, if a counter is selected then date/time to run from in the format Y-m-d H:i:s",
104
 
     *      type="string",
105
 
     *      required=false
106
 
     *   ),
107
 
     *  @SWG\Parameter(
108
 
     *      name="format",
109
 
     *      in="formData",
110
 
     *      description="For digital clock, format in which the time should be displayed example [HH:mm]",
111
 
     *      type="string",
112
 
     *      required=false
113
 
     *   ),
114
 
     *  @SWG\Parameter(
115
 
     *      name="showSeconds",
116
 
     *      in="formData",
117
 
     *      description="For Flip Clock, should the clock show seconds or not",
118
 
     *      type="integer",
119
 
     *      required=false
120
 
     *   ),
121
 
     *  @SWG\Parameter(
122
 
     *      name="ClockFace",
123
 
     *      in="formData",
124
 
     *      description="For Flip Clock, supported options: TwelveHourClock TwentyFourHourClock HourlyCounter MinuteCounter DailyCounter",
125
 
     *      type="string",
126
 
     *      required=false
127
 
     *   ),
128
 
     *  @SWG\Response(
129
 
     *      response=201,
130
 
     *      description="successful operation",
131
 
     *      @SWG\Schema(ref="#/definitions/Widget"),
132
 
     *      @SWG\Header(
133
 
     *          header="Location",
134
 
     *          description="Location of the new widget",
135
 
     *          type="string"
136
 
     *      )
137
 
     *  )
138
 
     * )
 
54
     * Add Media to the Database
139
55
     */
140
56
    public function add()
141
57
    {
142
58
        // You must also provide a duration (all media items must provide this field)
143
 
        $this->setOption('name', $this->getSanitizer()->getString('name'));
144
 
        $this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
145
 
        $this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
146
 
        $this->setOption('theme', $this->getSanitizer()->getInt('themeId', 0));
147
 
        $this->setOption('clockTypeId', $this->getSanitizer()->getInt('clockTypeId', 1));
148
 
        $this->setOption('offset', $this->getSanitizer()->getString('offset', 0));
149
 
        $this->setRawNode('format', $this->getSanitizer()->getParam('ta_text', $this->getSanitizer()->getParam('format', '')));
150
 
        $this->setOption('showSeconds', $this->getSanitizer()->getCheckbox('showSeconds', 1));
151
 
        $this->setOption('clockFace', $this->getSanitizer()->getString('clockFace', 'TwentyFourHourClock'));
 
59
        $this->setDuration(Sanitize::getInt('duration'));
 
60
        $this->setOption('theme', Sanitize::getInt('themeId', 0));
 
61
        $this->setOption('clockTypeId', Sanitize::getInt('clockTypeId', 1));
 
62
        $this->setOption('offset', Sanitize::getInt('offset', 0));
 
63
        $this->setRawNode('format', Sanitize::getParam('ta_text', ''));
152
64
 
153
65
        $this->validate();
154
66
 
156
68
        $this->saveWidget();
157
69
    }
158
70
 
159
 
     /**
160
 
     * Edit Clock
 
71
    /**
 
72
     * Edit Media in the Database
161
73
     */
162
74
    public function edit()
163
75
    {
164
76
        // You must also provide a duration (all media items must provide this field)
165
 
        $this->setOption('name', $this->getSanitizer()->getString('name'));
166
 
        $this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
167
 
        $this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
168
 
        $this->setOption('theme', $this->getSanitizer()->getInt('themeId', 0));
169
 
        $this->setOption('clockTypeId', $this->getSanitizer()->getInt('clockTypeId', 1));
170
 
        $this->setOption('offset', $this->getSanitizer()->getString('offset', 0));
171
 
        $this->setRawNode('format', $this->getSanitizer()->getParam('ta_text', $this->getSanitizer()->getParam('format', '')));
172
 
        $this->setOption('showSeconds', $this->getSanitizer()->getCheckbox('showSeconds'));
173
 
        $this->setOption('clockFace', $this->getSanitizer()->getString('clockFace'));
 
77
        $this->setDuration(Sanitize::getInt('duration'));
 
78
        $this->setOption('theme', Sanitize::getInt('themeId', 0));
 
79
        $this->setOption('clockTypeId', Sanitize::getInt('clockTypeId', 1));
 
80
        $this->setOption('offset', Sanitize::getInt('offset', 0));
 
81
        $this->setRawNode('format', Sanitize::getParam('ta_text', ''));
174
82
 
175
83
        $this->validate();
176
84
 
179
87
    }
180
88
 
181
89
    /**
182
 
     * Supported Clock Faces
183
 
     * @return array
184
 
     */
185
 
    public function clockFaces()
186
 
    {
187
 
        return [
188
 
            ['id' => 'TwelveHourClock', 'value' => __('12h Clock')],
189
 
            ['id' => 'TwentyFourHourClock', 'value' => __('24h Clock')],
190
 
            ['id' => 'HourlyCounter', 'value' => __('Hourly Counter')],
191
 
            ['id' => 'MinuteCounter', 'value' => __('Minute Counter')],
192
 
            ['id' => 'DailyCounter', 'value' => __('Daily Counter')]
193
 
        ];
194
 
    }
195
 
 
196
 
    /**
197
90
     * GetResource
198
91
     * Return the rendered resource to be used by the client (or a preview) for displaying this content.
199
92
     * @param integer $displayId If this comes from a real client, this will be the display id.
203
96
    {
204
97
        $template = null;
205
98
        $data = [];
206
 
        $isPreview = ($this->getSanitizer()->getCheckbox('preview') == 1);
 
99
        $isPreview = (Sanitize::getCheckbox('preview') == 1);
207
100
 
208
101
        // Clock Type
209
102
        switch ($this->getOption('clockTypeId', 1)) {
216
109
                $theme = ($this->getOption('theme') == 1 ? 'light' : 'dark');
217
110
                $theme_face = ($this->getOption('theme') == 1 ? 'clock_bg_modern_light.png' : 'clock_bg_modern_dark.png');
218
111
 
219
 
                $data['clockFace'] = base64_encode(file_get_contents(PROJECT_ROOT . '/modules/clock/' . $theme_face));
 
112
                $data['clockFace'] = base64_encode(file_get_contents('modules/clock/' . $theme_face));
220
113
 
221
114
                // Light or dark?
222
115
                $data['clockTheme'] = $theme;
251
144
 
252
145
                // After body content
253
146
                $options = array(
254
 
                    'previewWidth' => $this->getSanitizer()->getDouble('width', 0),
255
 
                    'previewHeight' => $this->getSanitizer()->getDouble('height', 0),
 
147
                    'previewWidth' => Sanitize::getDouble('width', 0),
 
148
                    'previewHeight' => Sanitize::getDouble('height', 0),
256
149
                    'originalWidth' => $this->region->width,
257
150
                    'originalHeight' => $this->region->height,
258
 
                    'scaleOverride' => $this->getSanitizer()->getDouble('scale_override', 0)
 
151
                    'scaleOverride' => Sanitize::getDouble('scale_override', 0)
259
152
                );
260
153
 
261
154
                $javaScriptContent = '<script type="text/javascript" src="' . $this->getResourceUrl('vendor/jquery-1.11.1.min.js') . '"></script>';
283
176
                $data['javaScript'] = $javaScriptContent;
284
177
 
285
178
                // Add our fonts.css file
286
 
                $headContent  = '<link href = "' . (($isPreview) ? $this->getApp()->urlFor('library.font.css') : 'fonts.css') . '" rel="stylesheet" media="screen">';
287
 
                $headContent .= '<style type = "text/css" > ' . file_get_contents($this->getConfig()->uri('css/client.css', true)) . '</style>';
 
179
                $headContent  = '<link href = "' . $this->getResourceUrl('fonts.css') . '" rel="stylesheet" media="screen">';
 
180
                $headContent .= '<style type = "text/css" > ' . file_get_contents(Theme::uri('css/client.css', true)) . '</style>';
288
181
 
289
182
                $data['head'] = $headContent;
290
183
 
295
188
                $template = 'clock-get-resource-flip';
296
189
 
297
190
                // Head Content (CSS for flip clock)
298
 
                $data['head'] = '<style type="text/css">' . file_get_contents(PROJECT_ROOT . '/modules/vendor/flipclock.css') . '</style>';
299
 
                $data['offset'] = $this->getOption('offset', '0');
300
 
                $data['duration'] = $this->getDuration();
301
 
                $data['clockFace'] = $this->getOption('clockFace', 'TwentyFourHourClock');
302
 
                $data['showSeconds'] = $this->getOption('showSeconds', 1);
 
191
                $data['head'] = '<style type="text/css">' . file_get_contents('modules/vendor/flipclock.css') . ' </style>';
 
192
                $data['offset'] = $this->GetOption('offset', 0);
303
193
 
304
194
                // After body content
305
195
                $javaScriptContent  = '<script type = "text/javascript" src = "' . $this->getResourceUrl('vendor/jquery-1.11.1.min.js') . '" ></script > ';
312
202
        }
313
203
 
314
204
        // If we are a preview, then pass in the width and height
315
 
        $data['previewWidth'] = $this->getSanitizer()->getDouble('width', 0);
316
 
        $data['previewHeight'] = $this->getSanitizer()->getDouble('height', 0);
 
205
        $data['previewWidth'] = Sanitize::getDouble('width');
 
206
        $data['previewHeight'] = Sanitize::getDouble('height');
317
207
 
318
208
        // Replace the View Port Width?
319
209
        $data['viewPortWidth'] = ($isPreview) ? $this->region->width : '[[ViewPortWidth]]';