~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/Currencies.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:
73
73
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/vendor/jquery-1.11.1.min.js')->save();
74
74
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/xibo-finance-render.js')->save();
75
75
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/xibo-layout-scaler.js')->save();
 
76
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/xibo-image-render.js')->save();
76
77
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/web/modules/vendor/bootstrap.min.css')->save();
77
78
    }
78
79
 
79
80
    /**
80
 
     * Loads templates for this module
81
 
     */
82
 
    private function loadTemplates()
83
 
    {
84
 
        $this->module->settings['templates'] = [];
85
 
 
86
 
        // Scan the folder for template files
87
 
        foreach (glob(PROJECT_ROOT . '/modules/currencies/*.template.json') as $template) {
88
 
            // Read the contents, json_decode and add to the array
89
 
            $this->module->settings['templates'][] = json_decode(file_get_contents($template), true);
90
 
        }
91
 
    }
92
 
 
93
 
    /**
94
 
     * Templates available
95
 
     * @return array
96
 
     */
97
 
    public function templatesAvailable()
98
 
    {
99
 
        if (!isset($this->module->settings['templates']))
100
 
            $this->loadTemplates();
101
 
 
102
 
        return $this->module->settings['templates'];
103
 
    }
104
 
 
105
 
    /**
106
81
     * Form for updating the module settings
107
82
     */
108
83
    public function settingsForm()
123
98
 
124
99
    public function validate()
125
100
    {
 
101
        if($this->getOption('overrideTemplate') == 0 && ( $this->getOption('templateId') == '' || $this->getOption('templateId') == null) )
 
102
            throw new \InvalidArgumentException(__('Please choose a template'));
 
103
            
126
104
        if ($this->getUseDuration() == 1 && $this->getDuration() == 0)
127
105
            throw new \InvalidArgumentException(__('Please enter a duration'));
128
106
    }
163
141
        $this->setOption('backgroundColor', $this->getSanitizer()->getString('backgroundColor'));
164
142
        $this->setOption('noRecordsMessage', $this->getSanitizer()->getString('noRecordsMessage'));
165
143
        $this->setOption('dateFormat', $this->getSanitizer()->getString('dateFormat'));
166
 
        $this->setOption('overrideTemplate', $this->getSanitizer()->getCheckbox('overrideTemplate'));
 
144
        $this->setOption('reverseConversion', $this->getSanitizer()->getCheckbox('reverseConversion'));
167
145
        $this->setOption('updateInterval', $this->getSanitizer()->getInt('updateInterval', 60));
168
146
        $this->setOption('templateId', $this->getSanitizer()->getString('templateId'));
169
147
        $this->setOption('durationIsPerPage', $this->getSanitizer()->getCheckbox('durationIsPerPage'));
170
 
        $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
171
 
        $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
172
 
        $this->setOption('maxItemsPerPage', $this->getSanitizer()->getInt('maxItemsPerPage', 4));
173
 
        $this->setRawNode('mainTemplate', $this->getSanitizer()->getParam('mainTemplate', $this->getSanitizer()->getParam('mainTemplate', null)));
174
 
        $this->setRawNode('itemTemplate', $this->getSanitizer()->getParam('itemTemplate', $this->getSanitizer()->getParam('itemTemplate', null)));
175
 
        $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', $this->getSanitizer()->getParam('styleSheet', null)));
176
148
        $this->setRawNode('javaScript', $this->getSanitizer()->getParam('javaScript', ''));
 
149
        $this->setOption('overrideTemplate', $this->getSanitizer()->getCheckbox('overrideTemplate'));
 
150
        
 
151
        if( $this->getOption('overrideTemplate') == 1 ){
 
152
            $this->setRawNode('mainTemplate', $this->getSanitizer()->getParam('mainTemplate', $this->getSanitizer()->getParam('mainTemplate', null)));
 
153
            $this->setRawNode('itemTemplate', $this->getSanitizer()->getParam('itemTemplate', $this->getSanitizer()->getParam('itemTemplate', null)));
 
154
            $this->setRawNode('styleSheet', $this->getSanitizer()->getParam('styleSheet', $this->getSanitizer()->getParam('styleSheet', null)));
 
155
            $this->setOption('widgetOriginalWidth', $this->getSanitizer()->getInt('widgetOriginalWidth'));
 
156
            $this->setOption('widgetOriginalHeight', $this->getSanitizer()->getInt('widgetOriginalHeight'));
 
157
            $this->setOption('maxItemsPerPage', $this->getSanitizer()->getInt('maxItemsPerPage', 4));
 
158
        }
177
159
    }
178
160
 
179
161
    /**
182
164
     */
183
165
    protected function getYql()
184
166
    {
 
167
        
 
168
        $reverseConversion = ($this->getOption('reverseConversion', 0) == 1);
 
169
 
185
170
        $items = $this->getOption('items');
186
171
        $base = $this->getOption('base');
187
172
        
209
194
            
210
195
        // quote each item
211
196
        $itemsJoined = array();
 
197
        
212
198
        foreach ($items as $key => $item) {
 
199
            
 
200
            // Remove the multiplier if there's one
 
201
            $item = explode('|', $item)[0];
 
202
            
 
203
            $baseItemPair = ( $reverseConversion ) ? ( trim($item) . trim($base) ) : ( trim($base) . trim($item) );
 
204
            
213
205
            array_push(
214
206
                $itemsJoined, 
215
 
                ( $useVariation ) ? ('\'' . trim($base) . trim($item) . '=X' . '\'') : ('\'' . trim($base) . trim($item) . '\'')
 
207
                ( $useVariation ) ? ('\'' . $baseItemPair . '=X' . '\'') : ('\'' . $baseItemPair . '\'')
216
208
            );
217
209
        }
218
210
 
260
252
        // Replace all matches.
261
253
        $matches = '';
262
254
        preg_match_all('/\[.*?\]/', $source, $matches);
 
255
        
 
256
        // Get the currencies' items
 
257
        $items = $this->getOption('items');
 
258
        
 
259
        if (strstr($items, ','))
 
260
            $items = explode(',', $items);
 
261
        else
 
262
            $items = [$items];
 
263
        
 
264
        $reverseConversion = ($this->getOption('reverseConversion', 0) == 1);
263
265
 
264
266
        // Substitute
265
267
        foreach ($matches[0] as $sub) {
299
301
                    // Replace the other tags
300
302
                    switch ($replace) {
301
303
                        case 'NameShort':
302
 
                            // Replace the name to have just the second currency
303
 
                            if (isset($data['Name']))
304
 
                                $replacement = str_replace(($baseCurrency . '/'),'',$data['Name']);
305
 
                                
 
304
                            // Replace the name to have just the second currency (or the first if the currency is reversed)
 
305
                            $replaceBase = ( $reverseConversion ) ? ('/' . $baseCurrency) : ($baseCurrency . '/');
 
306
                            
 
307
                            if (isset($data['Name']))
 
308
                                $replacement = trim(str_replace($replaceBase,'',$data['Name']));
 
309
                                    
 
310
                            break;
 
311
                            
 
312
                        case 'Multiplier':
 
313
                            
 
314
                            // Initialize replacement with empty string 
 
315
                            $replacement = '';
 
316
                            
 
317
                            // Get the current currency name/code
 
318
                            $pairName = ( $reverseConversion ) ? ('/' . $baseCurrency) : ($baseCurrency . '/');
 
319
                            
 
320
                            if (isset($data['Name']))
 
321
                                $currencyName = trim(str_replace($pairName,'',$data['Name']));
 
322
                            
 
323
                            // Search for the item that relates to the actual currency
 
324
                            foreach ($items as $item) {
 
325
                                
 
326
                                // Get the item name
 
327
                                $itemName = trim(explode('|', $item)[0]);
 
328
                                
 
329
                                // Compare the item name with the actual currency and test if the inputed value has a multiplier flag
 
330
                                if( sizeof(explode('|', $item)) > 1 && strcmp($itemName, $currencyName) == 0 ){
 
331
                                    
 
332
                                    // Get the multiplier
 
333
                                    $replacement = explode('|', $item)[1];
 
334
                                }
 
335
                            }
 
336
                            
306
337
                            break;
307
338
                            
308
339
                        case 'CurrencyFlag':
309
 
                            $currencyCode = str_replace(($baseCurrency . '/'),'',$data['Name']);
 
340
                            $replaceBase = ( $reverseConversion ) ? ('/' . $baseCurrency) : ($baseCurrency . '/');
 
341
                            $currencyCode = str_replace($replaceBase,'',$data['Name']);
310
342
                            
311
343
                            if (!file_exists(PROJECT_ROOT . '/web/modules/currencies/currency-flags/' . $currencyCode . '.svg')) 
312
344
                                $currencyCode = 'default';
337
369
                            
338
370
                            break;
339
371
                            
 
372
                        case 'LastTradePriceOnlyValue':
 
373
                        case 'BidValue':
 
374
                        case 'AskValue':
 
375
                            
 
376
                            // Get the converted currency name
 
377
                            $currencyName = ( $reverseConversion ) ? ('/' . $baseCurrency) : ($baseCurrency . '/');
 
378
                            
 
379
                            if (isset($data['Name']))
 
380
                                $currencyName = trim(str_replace($currencyName, '', $data['Name']));
 
381
                            
 
382
                            // Get the field's name and set the replacement as the default value from the API
 
383
                            $fieldName = str_replace('Value', '', $replace);
 
384
                            $replacement = $data[$fieldName];
 
385
                                
 
386
                            // Search for the item that relates to the actual currency
 
387
                            foreach ($items as $item) {
 
388
                                
 
389
                                // Get the item name
 
390
                                $itemName = trim(explode('|', $item)[0]);
 
391
                                
 
392
                                // Compare the item name with the actual currency and test if the inputed value has a multiplier flag
 
393
                                if( sizeof(explode('|', $item)) > 1 && strcmp($itemName, $currencyName) == 0 ){
 
394
                                    // Get the multiplier
 
395
                                    $multiplier = explode('|', $item)[1];
 
396
                                    
 
397
                                    // Set the replacement to be the API value times the multiplier
 
398
                                    $replacement = $data[$fieldName] * (float)$multiplier;
 
399
                                }
 
400
                            }        
 
401
                            
 
402
                            break;
 
403
                            
340
404
                        case 'ChangeStyle':
341
405
                            // Default value as no change
342
406
                            $replacement = 'value-equal';
375
439
                            $replacement = strtoupper($data['Currency']);
376
440
                            
377
441
                            break;
378
 
                            
379
 
                        case 'CurrencyUpper':
380
 
                            // Currency in uppercase
381
 
                            $replacement = strtoupper($data['Currency']);
382
 
                            
383
 
                            break;
384
442
                                
385
443
                        default:
386
444
                            $replacement = 'NULL';
430
488
            return '';
431
489
        }
432
490
 
 
491
        if( $this->getOption('overrideTemplate') == 0 ) {
 
492
            
 
493
            $template = $this->getTemplateById($this->getOption('templateId'));
 
494
            
 
495
            if (isset($template)) {
 
496
                $mainTemplate = $template['main'];
 
497
                $itemTemplate = $template['item'];
 
498
                $styleSheet = $template['css'];
 
499
                $widgetOriginalWidth = $template['widgetOriginalWidth'];
 
500
                $widgetOriginalHeight = $template['widgetOriginalHeight'];
 
501
                $maxItemsPerPage = $template['maxItemsPerPage'];
 
502
            }
 
503
            
 
504
        } else {
 
505
            
 
506
            $mainTemplate = $this->getRawNode('mainTemplate');
 
507
            $itemTemplate = $this->getRawNode('itemTemplate');
 
508
            $styleSheet = $this->getRawNode('styleSheet', '');
 
509
            $widgetOriginalWidth = $this->getSanitizer()->int($this->getOption('widgetOriginalWidth'));
 
510
            $widgetOriginalHeight = $this->getSanitizer()->int($this->getOption('widgetOriginalHeight'));
 
511
            $maxItemsPerPage = $this->getSanitizer()->int($this->getOption('maxItemsPerPage'));
 
512
        }
 
513
        
433
514
        // Run through each item and substitute with the template
434
 
        $mainTemplate = $this->parseLibraryReferences($isPreview, $this->getRawNode('mainTemplate'));
435
 
        $itemTemplate = $this->parseLibraryReferences($isPreview, $this->getRawNode('itemTemplate'));
 
515
        $mainTemplate = $this->parseLibraryReferences($isPreview, $mainTemplate);
 
516
        $itemTemplate = $this->parseLibraryReferences($isPreview, $itemTemplate);
436
517
        
437
518
        $renderedItems = [];
438
519
        
453
534
            'originalHeight' => $this->region->height,
454
535
            'previewWidth' => $this->getSanitizer()->getDouble('width', 0),
455
536
            'previewHeight' => $this->getSanitizer()->getDouble('height', 0),
456
 
            'widgetDesignWidth' => $this->getSanitizer()->int($this->getOption('widgetOriginalWidth')),
457
 
            'widgetDesignHeight'=> $this->getSanitizer()->int($this->getOption('widgetOriginalHeight')),
 
537
            'widgetDesignWidth' => $widgetOriginalWidth,
 
538
            'widgetDesignHeight'=> $widgetOriginalHeight,
458
539
            'scaleOverride' => $this->getSanitizer()->getDouble('scale_override', 0), 
459
 
            'maxItemsPerPage' => $this->getSanitizer()->int($this->getOption('maxItemsPerPage'))
 
540
            'maxItemsPerPage' => $maxItemsPerPage
460
541
        );
461
542
 
462
543
        $itemsPerPage = $options['maxItemsPerPage'];
483
564
        }
484
565
        
485
566
        // Add the CSS if it isn't empty, and replace the wallpaper
486
 
        $css = $this->makeSubstitutions([], $this->getRawNode('styleSheet', null), '');
 
567
        $css = $this->makeSubstitutions([], $styleSheet, '');
487
568
 
488
569
        if ($css != '') {
489
570
            $headContent .= '<style type="text/css">' . $this->parseLibraryReferences($isPreview, $css) . '</style>';
500
581
 
501
582
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-layout-scaler.js') . '"></script>';
502
583
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-finance-render.js') . '"></script>';
 
584
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-image-render.js') . '"></script>';
503
585
 
504
586
        $javaScriptContent .= '<script type="text/javascript">';
505
587
        $javaScriptContent .= '   var options = ' . json_encode($options) . ';';
506
588
        $javaScriptContent .= '   var items = ' . json_encode($renderedItems) . ';';
507
589
        $javaScriptContent .= '   var body = ' . json_encode($mainTemplate) . ';';
508
590
        $javaScriptContent .= '   $(document).ready(function() { ';
509
 
        $javaScriptContent .= '       $("body").xiboLayoutScaler(options); $("#content").xiboFinanceRender(options, items, body); ';
 
591
        $javaScriptContent .= '       $("body").xiboLayoutScaler(options); $("#content").xiboFinanceRender(options, items, body); $("#content").find("img").xiboImageRender(options); ';
510
592
        $javaScriptContent .= '   }); ';
511
593
        $javaScriptContent .= $javaScript;
512
594
        $javaScriptContent .= '</script>';