~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/ForecastIo.php

  • Committer: GitHub
  • Author(s): Dan Garner
  • Date: 2017-02-14 08:53:21 UTC
  • mfrom: (552.1.1)
  • Revision ID: git-v1:28b5a08b8293d0566da12863dbf2bde244f28804
Merge pull request #237 from maurofmferrao/develop

Complete conflict resolution for https://github.com/xibosignage/xibo-cms/pull/234

Show diffs side-by-side

added added

removed removed

Lines of Context:
81
81
    {
82
82
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/vendor/jquery-1.11.1.min.js')->save();
83
83
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/xibo-layout-scaler.js')->save();
 
84
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/xibo-image-render.js')->save();
84
85
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/vendor/bootstrap.min.css')->save();
85
86
 
86
87
        foreach ($this->mediaFactory->createModuleFileFromFolder($this->resourceFolder) as $media) {
112
113
        $this->module->settings['cachePeriod'] = $this->getSanitizer()->getInt('cachePeriod', 300);
113
114
    }
114
115
 
115
 
    /**
116
 
     * Loads templates for this module
117
 
     */
118
 
    private function loadTemplates()
119
 
    {
120
 
        // Scan the folder for template files
121
 
        foreach (glob(PROJECT_ROOT . '/modules/forecastio/*.template.json') as $template) {
122
 
            // Read the contents, json_decode and add to the array
123
 
            $this->module->settings['templates'][] = json_decode(file_get_contents($template), true);
124
 
        }
125
 
 
126
 
        $this->getLog()->debug(count($this->module->settings['templates']));
127
 
    }
128
 
 
129
 
    /**
130
 
     * Templates available
131
 
     * @return array
132
 
     */
133
 
    public function templatesAvailable()
134
 
    {
135
 
        if (!isset($this->module->settings['templates']))
136
 
            $this->loadTemplates();
137
 
 
138
 
        return $this->module->settings['templates'];
139
 
    }
140
 
 
141
116
    public function validate()
142
117
    {
 
118
        
 
119
        if($this->getOption('overrideTemplate') == 0 && ( $this->getOption('templateId') == '' || $this->getOption('templateId') == null) )
 
120
            throw new \InvalidArgumentException(__('Please choose a template'));
 
121
            
143
122
        if ($this->getUseDuration() == 1 && $this->getDuration() == 0)
144
123
            throw new \InvalidArgumentException(__('Please enter a duration'));
145
124
 
165
144
        $this->setOption('longitude', $this->getSanitizer()->getDouble('longitude'));
166
145
        $this->setOption('latitude', $this->getSanitizer()->getDouble('latitude'));
167
146
        $this->setOption('templateId', $this->getSanitizer()->getString('templateId'));
168
 
        $this->setOption('icons', $this->getSanitizer()->getString('icons'));
169
147
        $this->setOption('overrideTemplate', $this->getSanitizer()->getCheckbox('overrideTemplate'));
170
148
        $this->setOption('units', $this->getSanitizer()->getString('units'));
171
149
        $this->setOption('updateInterval', $this->getSanitizer()->getInt('updateInterval', 60));
172
150
        $this->setOption('lang', $this->getSanitizer()->getString('lang'));
173
151
        $this->setOption('dayConditionsOnly', $this->getSanitizer()->getCheckbox('dayConditionsOnly'));
174
152
        
175
 
        $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
176
 
        $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
177
 
        $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', null));
178
 
        $this->setRawNode('currentTemplate', $this->getSanitizer()->getParam('currentTemplate', null));
179
 
        $this->setRawNode('dailyTemplate', $this->getSanitizer()->getParam('dailyTemplate', null));
 
153
        if( $this->getOption('overrideTemplate') == 1 ){
 
154
            $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', null));
 
155
            $this->setRawNode('currentTemplate', $this->getSanitizer()->getParam('currentTemplate', null));
 
156
            $this->setRawNode('dailyTemplate', $this->getSanitizer()->getParam('dailyTemplate', null));
 
157
            $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
 
158
            $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
 
159
        }
 
160
        
180
161
        $this->setRawNode('javaScript', $this->getSanitizer()->getParam('javaScript', ''));
181
 
 
 
162
        
182
163
        // Save the widget
183
164
        $this->validate();
184
165
        $this->saveWidget();
196
177
        $this->setOption('longitude', $this->getSanitizer()->getDouble('longitude'));
197
178
        $this->setOption('latitude', $this->getSanitizer()->getDouble('latitude'));
198
179
        $this->setOption('templateId', $this->getSanitizer()->getString('templateId'));
199
 
        $this->setOption('icons', $this->getSanitizer()->getString('icons'));
200
180
        $this->setOption('overrideTemplate', $this->getSanitizer()->getCheckbox('overrideTemplate'));
201
181
        $this->setOption('units', $this->getSanitizer()->getString('units'));
202
182
        $this->setOption('updateInterval', $this->getSanitizer()->getInt('updateInterval', 60));
203
183
        $this->setOption('lang', $this->getSanitizer()->getString('lang'));
204
184
        $this->setOption('dayConditionsOnly', $this->getSanitizer()->getCheckbox('dayConditionsOnly'));
 
185
        
 
186
        if( $this->getOption('overrideTemplate') == 1 ){
 
187
            $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', null));
 
188
            $this->setRawNode('currentTemplate', $this->getSanitizer()->getParam('currentTemplate', null));
 
189
            $this->setRawNode('dailyTemplate', $this->getSanitizer()->getParam('dailyTemplate', null));
 
190
            $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
 
191
            $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
 
192
        }
205
193
 
206
 
        $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
207
 
        $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
208
 
        $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', null));
209
 
        $this->setRawNode('currentTemplate', $this->getSanitizer()->getParam('currentTemplate', null));
210
 
        $this->setRawNode('dailyTemplate', $this->getSanitizer()->getParam('dailyTemplate', null));
211
194
        $this->setRawNode('javaScript', $this->getSanitizer()->getParam('javaScript', ''));
212
195
 
213
196
        // Save the widget
215
198
        $this->saveWidget();
216
199
    }
217
200
 
218
 
    public function iconsAvailable()
219
 
    {
220
 
        // Scan the forecast io folder for icons
221
 
        $icons = array();
222
 
 
223
 
        foreach (array_diff(scandir($this->resourceFolder), array('..', '.')) as $file) {
224
 
            if (stripos($file, '-icons.png'))
225
 
                $icons[] = array('id' => $file, 'value' => ucfirst(str_replace('-', ' ', str_replace('.png', '', $file))));
226
 
        }
227
 
 
228
 
        return $icons;
229
 
    }
230
 
 
231
201
    /**
232
202
     * Units supported by Forecast.IO API
233
203
     * @return array The Units Available (temperature, wind speed and visible distance)
552
522
 
553
523
        // Replace the View Port Width?
554
524
        $data['viewPortWidth'] = ($isPreview) ? $this->region->width : '[[ViewPortWidth]]';
 
525
        
 
526
        if( $this->getOption('overrideTemplate') == 0 ) {
 
527
            
 
528
            // Get CSS and HTML from the default templates
555
529
 
 
530
            $template = $this->getTemplateById($this->getOption('templateId'));
 
531
            
 
532
            if (isset($template)) {
 
533
                $body = $template['main'];
 
534
                $dailyTemplate = $template['daily'];
 
535
                $styleSheet = $template['css'];
 
536
                $widgetOriginalWidth = $template['widgetOriginalWidth'];
 
537
                $widgetOriginalHeight = $template['widgetOriginalHeight'];
 
538
            }
 
539
            
 
540
        } else {
 
541
            // Get CSS and HTML from the override input fields
 
542
            
 
543
            $body = $this->parseLibraryReferences($isPreview, $this->getRawNode('currentTemplate', ''));
 
544
            $dailyTemplate = $this->parseLibraryReferences($isPreview, $this->getRawNode('dailyTemplate', ''));
 
545
            $styleSheet = $this->getRawNode('styleSheet', '');
 
546
            $widgetOriginalWidth = $this->getSanitizer()->int($this->getOption('widgetOriginalWidth'));
 
547
            $widgetOriginalHeight = $this->getSanitizer()->int($this->getOption('widgetOriginalHeight'));
 
548
        }
 
549
        
 
550
        // Parse library references
 
551
        $body = $this->parseLibraryReferences($isPreview, $body);
 
552
        $dailyTemplate = $this->parseLibraryReferences($isPreview, $dailyTemplate);
 
553
        
556
554
        // Provide the background images to the templates styleSheet
557
555
        $styleSheet = $this->makeSubstitutions([
558
556
            'cloudy-image' => $this->getResourceUrl('forecastio/wi-cloudy.jpg'),
565
563
            'rain-image' => $this->getResourceUrl('forecastio/wi-rain.jpg'),
566
564
            'snow-image' => $this->getResourceUrl('forecastio/wi-snow.jpg'),
567
565
            'windy' => $this->getResourceUrl('forecastio/wi-windy.jpg'),
568
 
          ], $this->getRawNode('styleSheet', null)
 
566
          ], $styleSheet
569
567
        );
570
568
 
571
569
        $headContent = '
586
584
        // Replace any icon sets
587
585
        $data['head'] = str_replace('[[ICONS]]', $this->getResourceUrl('forecastio/' . $this->getOption('icons')), $headContent);
588
586
 
589
 
        // Make some body content
590
 
        $body = $this->parseLibraryReferences($isPreview, $this->getRawNode('currentTemplate', null));
591
 
        $dailyTemplate = $this->parseLibraryReferences($isPreview, $this->getRawNode('dailyTemplate', null));
592
 
 
593
587
        // Get the JavaScript node
594
588
        $javaScript = $this->parseLibraryReferences($isPreview, $this->getRawNode('javaScript', ''));
595
589
 
636
630
            'originalWidth' => $this->region->width,
637
631
            'originalHeight' => $this->region->height,
638
632
            'scaleOverride' => $this->getSanitizer()->getDouble('scale_override', 0),
639
 
            'widgetDesignWidth' => $this->getSanitizer()->int($this->getOption('widgetOriginalWidth')),
640
 
            'widgetDesignHeight'=> $this->getSanitizer()->int($this->getOption('widgetOriginalHeight'))
 
633
            'widgetDesignWidth' => $widgetOriginalWidth,
 
634
            'widgetDesignHeight'=> $widgetOriginalHeight
641
635
        );
642
636
 
643
637
        $javaScriptContent = '<script type="text/javascript" src="' . $this->getResourceUrl('vendor/jquery-1.11.1.min.js') . '"></script>';
644
638
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-layout-scaler.js') . '"></script>';
 
639
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-image-render.js') . '"></script>';
645
640
        $javaScriptContent .= '<script>
646
641
 
647
642
            var options = ' . json_encode($options) . '
648
643
 
649
644
            $(document).ready(function() {
650
645
                $("body").xiboLayoutScaler(options);
 
646
                $("#content").find("img").xiboImageRender(options);
651
647
            });
652
648
        </script>';
653
649
        $javaScriptContent .= $javaScript;