~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Widget/TwitterMetro.php

  • Committer: Dan Garner
  • Date: 2015-09-29 15:16:59 UTC
  • mto: (454.2.11) (471.2.2)
  • mto: This revision was merged to the branch mainline in revision 468.
  • Revision ID: git-v1:ae24387a7b1397750b6ec86d0f286373da05eb16
Fixed Display Version Information Form (not showing media name)

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-2015 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
 
namespace Xibo\Widget;
23
 
 
24
 
use Emojione\Client;
25
 
use Emojione\Ruleset;
26
 
use Respect\Validation\Validator as v;
27
 
use Stash\Invalidation;
28
 
use Xibo\Exception\ConfigurationException;
29
 
use Xibo\Exception\XiboException;
30
 
use Xibo\Factory\ModuleFactory;
31
 
 
32
 
/**
33
 
 * Class TwitterMetro
34
 
 * @package Xibo\Widget
35
 
 */
36
 
class TwitterMetro extends TwitterBase
37
 
{
38
 
    private $parent;
39
 
    public $codeSchemaVersion = 1;
40
 
    private $resourceFolder;
41
 
 
42
 
    /**
43
 
     * TwitterMetro constructor.
44
 
     */
45
 
    public function init()
46
 
    {
47
 
        $this->resourceFolder = PROJECT_ROOT . '/modules/twittermetro';
48
 
 
49
 
        // Initialise extra validation rules
50
 
        v::with('Xibo\\Validation\\Rules\\');
51
 
    }
52
 
    
53
 
    /**
54
 
     * Install or Update this module
55
 
     * @param ModuleFactory $moduleFactory
56
 
     */
57
 
    public function installOrUpdate($moduleFactory)
58
 
    {
59
 
        if ($this->module == null) {
60
 
            // Install
61
 
            $module = $moduleFactory->createEmpty();
62
 
            $module->name = 'Twitter Metro';
63
 
            $module->type = 'twittermetro';
64
 
            $module->class = 'Xibo\Widget\TwitterMetro';
65
 
            $module->description = 'Twitter Metro Search Module';
66
 
            $module->imageUri = 'forms/library.gif';
67
 
            $module->enabled = 1;
68
 
            $module->previewEnabled = 1;
69
 
            $module->assignable = 1;
70
 
            $module->regionSpecific = 1;
71
 
            $module->renderAs = 'html';
72
 
            $module->schemaVersion = $this->codeSchemaVersion;
73
 
            $module->defaultDuration = 60;
74
 
            $module->settings = [];
75
 
 
76
 
            $this->setModule($module);
77
 
            $this->installModule();
78
 
        }
79
 
 
80
 
        // Check we are all installed
81
 
        $this->installFiles();
82
 
    }
83
 
 
84
 
    /**
85
 
     * Install Files
86
 
     */
87
 
    public function installFiles()
88
 
    {
89
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/jquery-1.11.1.min.js')->save();
90
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/xibo-metro-render.js')->save();
91
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/xibo-layout-scaler.js')->save();
92
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/xibo-image-render.js')->save();
93
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/emojione/emojione.sprites.svg')->save();
94
 
        $this->mediaFactory->createModuleSystemFile(PROJECT_ROOT . '/modules/vendor/bootstrap.min.css')->save();
95
 
        
96
 
        foreach ($this->mediaFactory->createModuleFileFromFolder($this->resourceFolder) as $media) {
97
 
            /* @var \Xibo\Entity\Media $media */
98
 
            $media->save();
99
 
        }
100
 
    }
101
 
 
102
 
    /**
103
 
     * Override the apikey/secret
104
 
     * @param string $setting
105
 
     * @param null $default
106
 
     * @return mixed|string
107
 
     */
108
 
    public function getSetting($setting, $default = NULL)
109
 
    {
110
 
        if ($setting == 'apiKey' || $setting == 'apiSecret' || $setting == 'cachePeriod') {
111
 
            // Create a Twitter Module as the source of this modules settings
112
 
            // only go to the stock twitter module
113
 
            if ($this->parent === null)
114
 
                $this->parent = $this->moduleFactory->createByClass('Xibo\Widget\Twitter');
115
 
 
116
 
            return $this->parent->getSetting($setting, $default);
117
 
        }
118
 
 
119
 
        return parent::getSetting($setting, $default);
120
 
    }
121
 
    
122
 
    /**
123
 
     * @return string
124
 
     */
125
 
    public function layoutDesignerJavaScript()
126
 
    {
127
 
        // We use the same javascript as the data set view designer
128
 
        return 'twittermetro-form-javascript';
129
 
    }
130
 
    
131
 
    /**
132
 
     * Get the template HTML, CSS, widgetOriginalWidth, widgetOriginalHeight giving its orientation (0:Landscape 1:Portrait)
133
 
     * @return array
134
 
     */
135
 
    public function getTemplateData() {
136
 
        
137
 
        $orientation = ($this->getSanitizer()->getDouble('width', $this->region->width) > $this->getSanitizer()->getDouble('height', $this->region->height)) ? 0 : 1; 
138
 
        
139
 
        $templateArray = array(
140
 
            array(  'template' => '<div class="cell-[itemType] [ShadowType] cell" id="item-[itemId]" style="[Photo]"> <div class="item-container [ShadowType]" style="[Color]"> <div class="item-text">[Tweet]</div> <div class="userData"> <div class="tweet-profilePic">[ProfileImage|normal]</div> <div class="tweet-userData"> <div class="user">[User]</div> <small>[Date]</small></div> </div> </div> </div>',
141
 
                    'styleSheet' => 'body { font-family: "Helvetica", "Arial", sans-serif; line-height: 1; margin: 0; } #content { width: 1920px !important; height: 1080px !important; background: rgba(255, 255, 255, 0.6); color: rgba(255, 255, 255, 1); } .row-1 { height: 360px; } .page { float: left; margin: 0; padding: 0; } .cell-1 { width: 310px; } .cell-2 { width: 630px; } .cell-3 { width: 950px; } .cell-1, .cell-2, .cell-3 { float: left; height: inherit; margin: 5px; background-repeat: no-repeat; background-size: cover; background-position-x: 50%; background-position-y: 50%; } .item-container { padding: 10px; color: #fff; height: 350px; } .userData { height: 50px; } .darken-container { background-color: rgba(0, 0, 0, 0.4); } .tweet-profilePic { width: 20%; float: left; } .tweet-profilePic img { width: 48px; } .tweet-userData { width: 80%; float: left; text-align: right; } .item-text { padding: 10px; color: #fff; } .emojione { width: 26px; height: 26px; } .cell-1 .item-text { line-height: 30px; font-size: 25px; height: 280px; } .cell-2 .item-text { line-height: 40px; font-size: 40px; height: 280px; } .cell-3 .item-text { line-height: 53px; font-size: 50px; height: 280px; } .user { font-size: 14px; font-weight: bold; padding-top: 20px; } .shadow { text-shadow: 1px 1px 2px rgba(0, 0, 3, 1); } .no-shadow { text-shadow: none !important; } small { font-size: 70%; }',
142
 
                    'originalWidth' => '1920',
143
 
                    'originalHeight' => '1080'
144
 
            ),
145
 
            array(  'template' => '<div class="cell-[itemType] [ShadowType] cell" id="item-[itemId]" style="[Photo]"> <div class="item-container [ShadowType]" style="[Color]"> <div class="item-text">[Tweet]</div> <div class="userData"> <div class="tweet-profilePic">[ProfileImage|normal]</div> <div class="tweet-userData"> <div class="user">[User]</div> <small>[Date]</small></div> </div> </div> </div>',
146
 
                    'styleSheet' => 'body { font-family: "Helvetica", "Arial", sans-serif; line-height: 1; margin: 0; } #content { width: 1080px !important; height: 1920px !important; background: rgba(255, 255, 255, 0.6); color: rgba(255, 255, 255, 1); } .row-1 { height: 320px; } .page { float: left; margin: 0; padding: 0; } .cell-1 { width: 350px; } .cell-2 { width: 710px; } .cell-3 { width: 1070px; } .cell-1, .cell-2, .cell-3 { float: left; height: inherit; margin: 5px; background-repeat: no-repeat; background-size: cover; background-position-x: 50%; background-position-y: 50%; } .item-container { padding: 10px; color: #fff; height: 310px; } .userData { height: 50px; } .darken-container { background-color: rgba(0, 0, 0, 0.4); } .tweet-profilePic { width: 20%; float: left; } .tweet-profilePic img { width: 48px; } .tweet-userData { width: 80%; float: left; text-align: right; } .item-text { padding: 10px; color: #fff; } .emojione { width: 26px; height: 26px; } .cell-1 .item-text { line-height: 30px; font-size: 25px; height: 240px; } .cell-2 .item-text { line-height: 40px; font-size: 40px; height: 240px; } .cell-3 .item-text { line-height: 53px; font-size: 50px; height: 240px; } .user { font-size: 14px; font-weight: bold; padding-top: 20px; } .shadow { text-shadow: 1px 1px 2px rgba(0, 0, 3, 1); } .no-shadow { text-shadow: none !important; } small { font-size: 70%; }',
147
 
                    'originalWidth' => '1080',
148
 
                    'originalHeight' => '1920'
149
 
            )
150
 
        );
151
 
        
152
 
        return $templateArray[$orientation];
153
 
    }
154
 
 
155
 
    /**
156
 
     * Form for updating the module settings
157
 
     */
158
 
    public function settingsForm()
159
 
    {
160
 
        return 'twittermetro-form-settings';
161
 
    }
162
 
 
163
 
    public function validate()
164
 
    {
165
 
        // If overrideColorTemplate is false we have to define a template Id 
166
 
        if($this->getOption('overrideColorTemplate') == 0 && ( $this->getOption('colorTemplateId') == '' || $this->getOption('colorTemplateId') == null) )
167
 
            throw new \InvalidArgumentException(__('Please choose a template'));
168
 
            
169
 
        if ($this->getUseDuration() == 1 && $this->getDuration() == 0)
170
 
            throw new \InvalidArgumentException(__('Please enter a duration'));
171
 
 
172
 
        if (!v::stringType()->notEmpty()->validate($this->getOption('searchTerm')))
173
 
            throw new \InvalidArgumentException(__('Please enter a search term'));
174
 
    }
175
 
 
176
 
    /**
177
 
     * Add Media
178
 
     */
179
 
    public function add()
180
 
    {
181
 
        $this->setCommonOptions();
182
 
 
183
 
        // Save the widget
184
 
        $this->validate();
185
 
        $this->saveWidget();
186
 
    }
187
 
 
188
 
    /**
189
 
     * Edit Media
190
 
     */
191
 
    public function edit()
192
 
    {
193
 
        $this->setCommonOptions();
194
 
 
195
 
        // Save the widget
196
 
        $this->validate();
197
 
        $this->saveWidget();
198
 
    }
199
 
 
200
 
    /**
201
 
     * Set common options from Request Params
202
 
     */
203
 
    private function setCommonOptions()
204
 
    {
205
 
        $this->setDuration($this->getSanitizer()->getInt('duration', $this->getDuration()));
206
 
        $this->setUseDuration($this->getSanitizer()->getCheckbox('useDuration'));
207
 
        $this->setOption('name', $this->getSanitizer()->getString('name'));
208
 
        $this->setOption('searchTerm', $this->getSanitizer()->getString('searchTerm'));
209
 
        $this->setOption('effect', $this->getSanitizer()->getString('effect'));
210
 
        $this->setOption('speed', $this->getSanitizer()->getInt('speed'));
211
 
        $this->setOption('backgroundColor', $this->getSanitizer()->getString('backgroundColor'));
212
 
        $this->setOption('noTweetsMessage', $this->getSanitizer()->getString('noTweetsMessage'));
213
 
        $this->setOption('dateFormat', $this->getSanitizer()->getString('dateFormat'));
214
 
        $this->setOption('resultType', $this->getSanitizer()->getString('resultType'));
215
 
        $this->setOption('tweetDistance', $this->getSanitizer()->getInt('tweetDistance'));
216
 
        $this->setOption('tweetCount', $this->getSanitizer()->getInt('tweetCount', 60));
217
 
        $this->setOption('removeUrls', $this->getSanitizer()->getCheckbox('removeUrls'));
218
 
        $this->setOption('removeMentions', $this->getSanitizer()->getCheckbox('removeMentions'));
219
 
        $this->setOption('removeHashtags', $this->getSanitizer()->getCheckbox('removeHashtags'));
220
 
        $this->setOption('overrideColorTemplate', $this->getSanitizer()->getCheckbox('overrideColorTemplate'));
221
 
        $this->setOption('updateInterval', $this->getSanitizer()->getInt('updateInterval', 60));
222
 
        $this->setOption('colorTemplateId', $this->getSanitizer()->getString('colorTemplateId'));
223
 
        $this->setOption('resultContent', $this->getSanitizer()->getString('resultContent'));
224
 
        $this->setOption('removeRetweets', $this->getSanitizer()->getCheckbox('removeRetweets'));
225
 
        
226
 
        if( $this->getOption('overrideColorTemplate') == 1 ){
227
 
            
228
 
            // Convert the colors array to string to be able to save it
229
 
            $stringColor = $this->getSanitizer()->getStringArray('color')[0];
230
 
            for ($i=1; $i < count($this->getSanitizer()->getStringArray('color')); $i++) {
231
 
                if(!empty($this->getSanitizer()->getStringArray('color')[$i]))
232
 
                    $stringColor .= "|" . $this->getSanitizer()->getStringArray('color')[$i];
233
 
            }
234
 
            $this->setOption('templateColours', $stringColor);
235
 
        }
236
 
    }
237
 
 
238
 
    /**
239
 
     * @param int $displayId
240
 
     * @param bool $isPreview
241
 
     * @return array|false
242
 
     * @throws XiboException
243
 
     */
244
 
    protected function getTwitterFeed($displayId = 0, $isPreview = true)
245
 
    {
246
 
        if (!extension_loaded('curl'))
247
 
            throw new ConfigurationException(__('cURL extension is required for Twitter'));
248
 
 
249
 
        // Do we need to add a geoCode?
250
 
        $geoCode = '';
251
 
        $distance = $this->getOption('tweetDistance');
252
 
        if ($distance != 0) {
253
 
            // Use the display ID or the default.
254
 
            if ($displayId != 0) {
255
 
                // Look up the lat/long
256
 
                $display = $this->displayFactory->getById($displayId);
257
 
                $defaultLat = $display->latitude;
258
 
                $defaultLong = $display->longitude;
259
 
            } else {
260
 
                $defaultLat = $this->getConfig()->GetSetting('DEFAULT_LAT');
261
 
                $defaultLong = $this->getConfig()->GetSetting('DEFAULT_LONG');
262
 
            }
263
 
 
264
 
            // Built the geoCode string.
265
 
            $geoCode = implode(',', array($defaultLat, $defaultLong, $distance)) . 'mi';
266
 
        }
267
 
        
268
 
        // Search content filtered by type of tweets  
269
 
        $searchTerm = $this->getOption('searchTerm');
270
 
        $resultContent = $this->getOption('resultContent');
271
 
        
272
 
        switch ($resultContent) {
273
 
          case 0:
274
 
            //Default
275
 
            $searchTerm .= '';
276
 
            break;
277
 
            
278
 
          case 1:
279
 
            // Remove media
280
 
            $searchTerm .= ' -filter:media';
281
 
            break;
282
 
            
283
 
          case 2:
284
 
            // Only tweets with native images
285
 
            $searchTerm .= ' filter:twimg';
286
 
            break; 
287
 
               
288
 
          default:
289
 
            $searchTerm .= '';
290
 
            break;
291
 
        }
292
 
        
293
 
        // Search term retweets filter
294
 
        $searchTerm .= ($this->getOption('removeRetweets')) ? ' -filter:retweets' : '';
295
 
        
296
 
        // Connect to twitter and get the twitter feed.
297
 
        /** @var \Stash\Item $cache */
298
 
        $cache = $this->getPool()->getItem($this->makeCacheKey(md5($searchTerm . $this->getOption('resultType') . $this->getOption('tweetCount', 60) . $geoCode)));
299
 
        $cache->setInvalidationMethod(Invalidation::SLEEP, 5000, 15);
300
 
 
301
 
        $data = $cache->get();
302
 
 
303
 
        if ($cache->isMiss()) {
304
 
 
305
 
            $this->getLog()->debug('Querying API for ' . $searchTerm);
306
 
 
307
 
            // Lock this cache item (for 30 seconds)
308
 
            $cache->lock();
309
 
 
310
 
            // We need to search for it
311
 
            if (!$token = $this->getToken())
312
 
                return false;
313
 
 
314
 
            // We have the token, make a tweet
315
 
            if (!$data = $this->searchApi($token, $searchTerm, $this->getOption('resultType'), $geoCode, $this->getOption('tweetCount', 60)))
316
 
                return false;
317
 
 
318
 
            // Cache it
319
 
            $cache->set($data);
320
 
            $cache->expiresAfter($this->getSetting('cachePeriod', 3600));
321
 
            $this->getPool()->saveDeferred($cache);
322
 
        }
323
 
 
324
 
        // Get the template data
325
 
        $templateData = $this->getTemplateData();
326
 
        $template = $this->parseLibraryReferences($isPreview, $templateData['template']);
327
 
 
328
 
        // Parse the text template
329
 
        $matches = '';
330
 
        preg_match_all('/\[.*?\]/', $template, $matches);
331
 
 
332
 
        // Build an array to return
333
 
        $return = array();
334
 
 
335
 
        // Expiry time for any media that is downloaded
336
 
        $expires = $this->getDate()->parse()->addHours($this->getSetting('cachePeriodImages', 24))->format('U');
337
 
 
338
 
        // Remove URL setting
339
 
        $removeUrls = $this->getOption('removeUrls', 1)  == 1;
340
 
        $removeMentions = $this->getOption('removeMentions', 1)  == 1;
341
 
        $removeHashTags = $this->getOption('removeHashTags', 1)  == 1;
342
 
 
343
 
        // If we have nothing to show, display a no tweets message.
344
 
        if (count($data->statuses) <= 0) {
345
 
            // Create ourselves an empty tweet so that the rest of the code can continue as normal
346
 
            $user = new \stdClass();
347
 
            $user->name = '';
348
 
            $user->screen_name = '';
349
 
            $user->profile_image_url = '';
350
 
            $user->location = '';
351
 
 
352
 
            $tweet = new \stdClass();
353
 
            $tweet->full_text = $this->getOption('noTweetsMessage', __('There are no tweets to display'));
354
 
            $tweet->created_at = '';
355
 
            $tweet->user = $user;
356
 
 
357
 
            // Append to our statuses
358
 
            $data->statuses[] = $tweet;
359
 
        }
360
 
 
361
 
        // Make an emojione client
362
 
        $emoji = new Client(new Ruleset());
363
 
        $emoji->imageType = 'svg';
364
 
        $emoji->sprites = true;
365
 
        $emoji->imagePathSVGSprites = $this->getResourceUrl('emojione/emojione.sprites.svg');
366
 
 
367
 
        // Get the date format to apply
368
 
        $dateFormat = $this->getOption('dateFormat', $this->getConfig()->GetSetting('DATE_FORMAT'));
369
 
 
370
 
        // This should return the formatted items.
371
 
        foreach ($data->statuses as $tweet) {
372
 
            // Substitute for all matches in the template
373
 
            $rowString = $template;
374
 
 
375
 
            foreach ($matches[0] as $sub) {
376
 
                // Always clear the stored template replacement
377
 
                $replace = '';
378
 
                $tagOptions = array();
379
 
                
380
 
                // Get the options from the tag and create an array
381
 
                $subClean = str_replace('[', '', str_replace(']', '', $sub));
382
 
                if (stripos($subClean, '|') > -1) {
383
 
                    $tagOptions = explode('|', $subClean);
384
 
                    
385
 
                    // Save the main tag 
386
 
                    $subClean = $tagOptions[0];
387
 
                    
388
 
                    // Remove the tag from the first position
389
 
                    array_shift($tagOptions);
390
 
                }
391
 
                
392
 
                // Maybe make this more generic?
393
 
                switch ($subClean) {
394
 
                    case 'Tweet':
395
 
                        // Get the tweet text to operate on
396
 
                        $tweetText = $tweet->full_text;
397
 
 
398
 
                        // Replace URLs with their display_url before removal
399
 
                        if (isset($tweet->entities->urls)) {
400
 
                            foreach ($tweet->entities->urls as $url) {
401
 
                                $tweetText = str_replace($url->url, $url->display_url, $tweetText);
402
 
                            }
403
 
                        }
404
 
 
405
 
                        // Clean up the tweet text
406
 
                        // thanks to https://github.com/solarbug (https://github.com/xibosignage/xibo/issues/703)
407
 
                        // Remove Mentions
408
 
                        if ($removeMentions)
409
 
                            $tweetText = preg_replace('/(\s+|^)@\S+/', '', $tweetText);
410
 
 
411
 
                        // Remove HashTags
412
 
                        if ($removeHashTags)
413
 
                            $tweetText = preg_replace('/(\s+|^)#\S+/', '', $tweetText);
414
 
 
415
 
                        if ($removeUrls)
416
 
                            // Regex taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
417
 
                            $tweetText  = preg_replace('~(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))~', '', $tweetText); // remove urls
418
 
 
419
 
                        $replace = $emoji->toImage($tweetText);
420
 
                        break;
421
 
 
422
 
                    case 'User':
423
 
                        $replace = $emoji->toImage($tweet->user->name);
424
 
                        break;
425
 
 
426
 
                    case 'ScreenName':
427
 
                        $replace = ($tweet->user->screen_name != '') ? ('@' . $tweet->user->screen_name) : '';
428
 
                        break;
429
 
 
430
 
                    case 'Date':
431
 
                        if($tweet->created_at != '')
432
 
                            $replace = $this->getDate()->getLocalDate(strtotime($tweet->created_at), $dateFormat);
433
 
                        break;
434
 
  
435
 
                    case 'Location':
436
 
                        $replace = $tweet->user->location;
437
 
                        break;
438
 
 
439
 
                    case 'ProfileImage':
440
 
                        // Grab the profile image
441
 
                        if ($tweet->user->profile_image_url != '') {
442
 
                            
443
 
                            // Original Default Image
444
 
                            $imageSizeType = "";
445
 
                            if( count($tagOptions) > 0 ) {
446
 
                              // Image options ( normal, bigger, mini )
447
 
                              $imageSizeType = '_' . $tagOptions[0];
448
 
                            }
449
 
                            
450
 
                            // Twitter image size
451
 
                            $tweet->user->profile_image_url = str_replace('_normal', $imageSizeType, $tweet->user->profile_image_url);
452
 
                            
453
 
                            // Grab the profile image
454
 
                            $file = $this->mediaFactory->queueDownload('twitter_' . $tweet->user->id, $tweet->user->profile_image_url, $expires);
455
 
 
456
 
                            $replace = ($isPreview)
457
 
                                ? '<img src="' . $this->getApp()->urlFor('library.download', ['id' => $file->mediaId, 'type' => 'image']) . '?preview=1" />'
458
 
                                : '<img src="' . $file->storedAs . '"  />';
459
 
                        }
460
 
                        break;
461
 
                        
462
 
                    case 'Color':
463
 
                        // See if there is a profile image
464
 
                        if (!$this->tweetHasPhoto($tweet)) {
465
 
                        
466
 
                            // Get the colors array
467
 
                            if ($this->getOption('overrideColorTemplate') == 0) {
468
 
                                $colorTemplate = $this->getTemplateById($this->getOption('colorTemplateId'));
469
 
 
470
 
                                if ($colorTemplate === null)
471
 
                                    $colorTemplate = $this->getTemplateById('default');
472
 
 
473
 
                                $colorArray = $colorTemplate['colors'];
474
 
                            } else {
475
 
                                $colorArray = explode("|", $this->getOption('templateColours'));
476
 
                            }
477
 
                            
478
 
                            // Find a random color
479
 
                            $randomNum = rand(0,count($colorArray)-1);
480
 
                            $randomColor = $colorArray[$randomNum];
481
 
                            
482
 
                            $replace = 'background-color:' . $randomColor;
483
 
                        }
484
 
                        break;
485
 
                        
486
 
                    case 'ShadowType':
487
 
                        // See if there is a profile image
488
 
                        $replace = ($this->tweetHasPhoto($tweet)) ? 'shadow darken-container' : '';
489
 
                        break;
490
 
 
491
 
                    case 'Photo':
492
 
                        // See if there are any photos associated with this tweet.
493
 
                        if ($this->tweetHasPhoto($tweet)) {
494
 
                            
495
 
                            // See if it's an image from a tweet or RT, and only take the first one
496
 
                            $mediaObject = (isset($tweet->entities->media))
497
 
                                ? $tweet->entities->media[0]
498
 
                                : $tweet->retweeted_status->entities->media[0];
499
 
                            
500
 
                            $photoUrl = $mediaObject->media_url;
501
 
                            
502
 
                            if ($photoUrl != '') {
503
 
                                $file = $this->mediaFactory->queueDownload('twitter_photo_' . $tweet->user->id . '_' . $mediaObject->id_str, $photoUrl, $expires);
504
 
 
505
 
                                $replace = "background-image: url(" 
506
 
                                    . (($isPreview) ? $this->getApp()->urlFor('library.download', ['id' => $file->mediaId, 'type' => 'image']) : $file->storedAs)
507
 
                                    . ")";
508
 
                            }
509
 
                        }
510
 
                        break;
511
 
                        
512
 
                    case 'TwitterLogoWhite':
513
 
                        //Get the Twitter logo image file path
514
 
                        $replace = $this->getResourceUrl('twitter/twitter_white.png');
515
 
                        break;
516
 
                        
517
 
                    case 'TwitterLogoBlue':
518
 
                        //Get the Twitter logo image file path
519
 
                        $replace = $this->getResourceUrl('twitter/twitter_blue.png');
520
 
                        break;
521
 
 
522
 
                    default:
523
 
                        $replace = '[' . $subClean . ']';
524
 
                }
525
 
 
526
 
                $rowString = str_replace($sub, $replace, $rowString);
527
 
            }
528
 
 
529
 
            // Substitute the replacement we have found (it might be '')
530
 
            $return[] = $rowString;
531
 
        }
532
 
 
533
 
        // Process queued downloads
534
 
        $this->mediaFactory->processDownloads(function($media) {
535
 
            // Success
536
 
            $this->getLog()->debug('Successfully downloaded ' . $media->mediaId);
537
 
 
538
 
            // Tag this layout with this file
539
 
            $this->assignMedia($media->mediaId);
540
 
        });
541
 
 
542
 
        // Return the data array
543
 
        return $return;
544
 
    }
545
 
 
546
 
    /**
547
 
     * Get Resource
548
 
     * @param int $displayId
549
 
     * @return mixed
550
 
     * @throws XiboException
551
 
     */
552
 
    public function getResource($displayId = 0)
553
 
    {
554
 
        // Make sure we are set up correctly
555
 
        if ($this->getSetting('apiKey') == '' || $this->getSetting('apiSecret') == '') {
556
 
            $this->getLog()->error('Twitter Module not configured. Missing API Keys');
557
 
            return '';
558
 
        }
559
 
 
560
 
        // Lock the request
561
 
        $this->concurrentRequestLock();
562
 
 
563
 
        $data = [];
564
 
        $isPreview = ($this->getSanitizer()->getCheckbox('preview') == 1);
565
 
        
566
 
        // Replace the View Port Width?
567
 
        $data['viewPortWidth'] = ($isPreview) ? $this->region->width : '[[ViewPortWidth]]';
568
 
 
569
 
        // Information from the Module
570
 
        $duration = $this->getCalculatedDurationForGetResource();
571
 
 
572
 
        // Generate a JSON string of substituted items.
573
 
        $items = $this->getTwitterFeed($displayId, $isPreview);
574
 
        
575
 
        // Get the template data
576
 
        $templateData = $this->getTemplateData();
577
 
        
578
 
        // Return empty string if there are no items to show.
579
 
        if (count($items) == 0)
580
 
            return '';
581
 
 
582
 
        $options = array(
583
 
            'type' => $this->getModuleType(),
584
 
            'fx' => $this->getOption('effect', 'noAnim'),
585
 
            'speed' => $this->getOption('speed', 500),
586
 
            'duration' => $duration,
587
 
            'numItems' => count($items),
588
 
            'originalWidth' => $this->region->width,
589
 
            'originalHeight' => $this->region->height,
590
 
            'previewWidth' => $this->getSanitizer()->getDouble('width', 0),
591
 
            'previewHeight' => $this->getSanitizer()->getDouble('height', 0),
592
 
            'scaleOverride' => $this->getSanitizer()->getDouble('scale_override', 0),
593
 
            'widgetDesignWidth' => $templateData['originalWidth'],
594
 
            'widgetDesignHeight'=> $templateData['originalHeight'],
595
 
            'resultContent'=> $this->getSanitizer()->string($this->getOption('resultContent'))            
596
 
        );
597
 
 
598
 
        // Replace the control meta with our data from twitter
599
 
        $data['controlMeta'] = '<!-- NUMITEMS=' . count($items) . ' -->' . PHP_EOL . '<!-- DURATION=' . $duration . ' -->';
600
 
 
601
 
        // Replace the head content
602
 
        $headContent = '';
603
 
 
604
 
        // Add our fonts.css file
605
 
        $headContent .= '<link href="' . (($isPreview) ? $this->getApp()->urlFor('library.font.css') : 'fonts.css') . '" rel="stylesheet" media="screen">
606
 
        <link href="' . $this->getResourceUrl('vendor/bootstrap.min.css')  . '" rel="stylesheet" media="screen">';
607
 
        
608
 
        $backgroundColor = $this->getOption('backgroundColor');
609
 
        if ($backgroundColor != '') {
610
 
            $headContent .= '<style type="text/css">body { background-color: ' . $backgroundColor . ' }</style>';
611
 
        } else {
612
 
          $headContent .= '<style type="text/css"> body { background-color: transparent }</style>';
613
 
        }
614
 
        
615
 
        // Add the CSS if it isn't empty
616
 
        $css = $templateData['styleSheet'];
617
 
        
618
 
        if ($css != '') {
619
 
            $headContent .= '<style type="text/css">' . $this->parseLibraryReferences($isPreview, $css) . '</style>';
620
 
        }
621
 
        $headContent .= '<style type="text/css">' . file_get_contents($this->getConfig()->uri('css/client.css', true)) . '</style>';
622
 
 
623
 
        // Replace the Head Content with our generated javascript
624
 
        $data['head'] = $headContent;
625
 
 
626
 
        // Add some scripts to the JavaScript Content
627
 
        $javaScriptContent = '<script type="text/javascript" src="' . $this->getResourceUrl('vendor/jquery-1.11.1.min.js') . '"></script>';
628
 
 
629
 
        // Get the colors array
630
 
        if ($this->getOption('overrideColorTemplate') == 0) {
631
 
            $template = $this->getTemplateById($this->getOption('colorTemplateId'));
632
 
 
633
 
            if ($template === null)
634
 
                $template = $this->getTemplateById('default');
635
 
 
636
 
            $colorArray = $template['colors'];
637
 
        } else {
638
 
            $colorArray = explode("|", $this->getOption('templateColours'));
639
 
        }
640
 
        
641
 
        // Need the cycle plugin?
642
 
        if ($this->getOption('effect') != 'none')
643
 
            $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('vendor/jquery-cycle-2.1.6.min.js') . '"></script>';
644
 
 
645
 
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-layout-scaler.js') . '"></script>';
646
 
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-metro-render.js') . '"></script>';
647
 
        $javaScriptContent .= '<script type="text/javascript" src="' . $this->getResourceUrl('xibo-image-render.js') . '"></script>';
648
 
 
649
 
        $javaScriptContent .= '<script type="text/javascript">';
650
 
        $javaScriptContent .= '   var options = ' . json_encode($options) . ';';
651
 
        $javaScriptContent .= '   var items = ' . json_encode($items) . ';';
652
 
        $javaScriptContent .= '   var colors = ' . json_encode($colorArray) . ';';
653
 
        $javaScriptContent .= '   $(document).ready(function() { ';
654
 
        $javaScriptContent .= '       $("body").xiboLayoutScaler(options); $("#content").xiboMetroRender(options, items, colors); $("#content").find("img").xiboImageRender(options); $("#content").find(".cell").xiboImageRender(options);';
655
 
        $javaScriptContent .= '   }); ';
656
 
        $javaScriptContent .= '</script>';
657
 
 
658
 
        // Replace the Head Content with our generated javascript
659
 
        $data['javaScript'] = $javaScriptContent;
660
 
 
661
 
        // Update and save widget if we've changed our assignments.
662
 
        if ($this->hasMediaChanged())
663
 
            $this->widget->save(['saveWidgetOptions' => false, 'notify' => false, 'notifyDisplays' => true, 'audit' => false]);
664
 
 
665
 
        $this->concurrentRequestRelease();
666
 
 
667
 
        return $this->renderTemplate($data);
668
 
    }
669
 
 
670
 
    /** @inheritdoc */
671
 
    public function isValid()
672
 
    {
673
 
        // Using the information you have in your module calculate whether it is valid or not.
674
 
        // 0 = Invalid
675
 
        // 1 = Valid
676
 
        // 2 = Unknown
677
 
        return 1;
678
 
    }
679
 
 
680
 
    /**
681
 
     * @param $tweet
682
 
     * @return bool
683
 
     */
684
 
    private function tweetHasPhoto($tweet)
685
 
    {
686
 
        return ((isset($tweet->entities->media)
687
 
                && count($tweet->entities->media) > 0)
688
 
            || (isset($tweet->retweeted_status->entities->media)
689
 
                && count($tweet->retweeted_status->entities->media) > 0));
690
 
    }
691
 
}