/** * GetResource * Return the rendered resource to be used by the client (or a preview) * for displaying this content. * @param integer $displayId If this comes from a real client, this will be the display id. * @return string */ public function GetResource($displayId = 0) { // Make sure we are set up correctly if ($this->GetSetting('apiKey') == '' || $this->GetSetting('apiSecret') == '') { Debug::Error('Twitter Module not configured. Missing API Keys'); return ''; } // Load in the template $template = file_get_contents('modules/preview/HtmlTemplate.html'); $isPreview = Kit::GetParam('preview', _REQUEST, _WORD, 'false') == 'true'; // Replace the View Port Width? if ($isPreview) { $template = str_replace('[[ViewPortWidth]]', $this->width, $template); } // Information from the Module $duration = $this->duration; // Generate a JSON string of substituted items. $items = $this->getTwitterFeed($displayId, $isPreview); // Return empty string if there are no items to show. if (count($items) == 0) { return ''; } $options = array('type' => $this->type, 'fx' => $this->GetOption('effect', 'none'), 'speed' => $this->GetOption('speed', 500), 'duration' => $duration, 'durationIsPerItem' => $this->GetOption('durationIsPerItem', 0) == 1, 'numItems' => count($items), 'itemsPerPage' => 1, 'originalWidth' => $this->width, 'originalHeight' => $this->height, 'previewWidth' => Kit::GetParam('width', _GET, _DOUBLE, 0), 'previewHeight' => Kit::GetParam('height', _GET, _DOUBLE, 0), 'scaleOverride' => Kit::GetParam('scale_override', _GET, _DOUBLE, 0)); // Replace the control meta with our data from twitter $controlMeta = '<!-- NUMITEMS=' . count($items) . ' -->' . PHP_EOL . '<!-- DURATION=' . ($this->GetOption('durationIsPerItem', 0) == 0 ? $duration : $duration * count($items)) . ' -->'; $template = str_replace('<!--[[[CONTROLMETA]]]-->', $controlMeta, $template); // Replace the head content $headContent = ''; // Add the CSS if it isn't empty $css = $this->GetRawNode('styleSheet'); if ($css != '') { $headContent .= '<style type="text/css">' . $css . '</style>'; } $backgroundColor = $this->GetOption('backgroundColor'); if ($backgroundColor != '') { $headContent .= '<style type="text/css">body, .page, .item { background-color: ' . $backgroundColor . ' }</style>'; } // Add our fonts.css file $headContent .= '<link href="' . ($isPreview ? 'modules/preview/' : '') . 'fonts.css" rel="stylesheet" media="screen">'; $headContent .= '<link href="' . ($isPreview ? 'modules/theme/twitter/' : '') . 'emoji.css" rel="stylesheet" media="screen">'; $headContent .= '<style type="text/css">' . file_get_contents(Theme::ItemPath('css/client.css')) . '</style>'; // Replace the Head Content with our generated javascript $template = str_replace('<!--[[[HEADCONTENT]]]-->', $headContent, $template); // Add some scripts to the JavaScript Content $javaScriptContent = '<script type="text/javascript" src="' . ($isPreview ? 'modules/preview/vendor/' : '') . 'jquery-1.11.1.min.js"></script>'; // Need the cycle plugin? if ($this->GetSetting('effect') != 'none') { $javaScriptContent .= '<script type="text/javascript" src="' . ($isPreview ? 'modules/preview/vendor/' : '') . 'jquery-cycle-2.1.6.min.js"></script>'; } // Need the marquee plugin? if (stripos($this->GetSetting('effect'), 'marquee')) { $javaScriptContent .= '<script type="text/javascript" src="' . ($isPreview ? 'modules/preview/vendor/' : '') . 'jquery.marquee.min.js"></script>'; } $javaScriptContent .= '<script type="text/javascript" src="' . ($isPreview ? 'modules/preview/' : '') . 'xibo-layout-scaler.js"></script>'; $javaScriptContent .= '<script type="text/javascript" src="' . ($isPreview ? 'modules/preview/' : '') . 'xibo-text-render.js"></script>'; $javaScriptContent .= '<script type="text/javascript">'; $javaScriptContent .= ' var options = ' . json_encode($options) . ';'; $javaScriptContent .= ' var items = ' . Kit::jsonEncode($items) . ';'; $javaScriptContent .= ' $(document).ready(function() { '; $javaScriptContent .= ' $("body").xiboLayoutScaler(options); $("#content").xiboTextRender(options, items); '; $javaScriptContent .= ' }); '; $javaScriptContent .= '</script>'; // Replace the Head Content with our generated javascript $template = str_replace('<!--[[[JAVASCRIPTCONTENT]]]-->', $javaScriptContent, $template); // Replace the Body Content with our generated text $template = str_replace('<!--[[[BODYCONTENT]]]-->', '', $template); return $template; }
/** * Json Encode, handling and logging errors * http://stackoverflow.com/questions/10199017/how-to-solve-json-error-utf8-error-in-php-json-decode * @param mixed $mixed The item to encode * @return mixed The Encoded Item */ public static function jsonEncode($mixed) { if (version_compare(PHP_VERSION, '5.4.0') >= 0) { $encoded = json_encode($mixed, JSON_PRETTY_PRINT); } else { $encoded = json_encode($mixed); } switch (json_last_error()) { case JSON_ERROR_NONE: return $encoded; case JSON_ERROR_DEPTH: Debug::Audit('Maximum stack depth exceeded'); return false; case JSON_ERROR_STATE_MISMATCH: Debug::Audit('Underflow or the modes mismatch'); return false; case JSON_ERROR_CTRL_CHAR: Debug::Audit('Unexpected control character found'); return false; case JSON_ERROR_SYNTAX: Debug::Audit('Syntax error, malformed JSON'); return false; case JSON_ERROR_UTF8: $clean = Kit::utf8ize($mixed); return Kit::jsonEncode($clean); default: Debug::Audit('Unknown error'); return false; } }