. */ require_once 'Parser/Factory.php'; require_once 'Widget.php'; require_once 'Zend/Json.php'; /** * Widgets Container. * * This class maps some widgets descriptors retrieved from the * Netvibes REST API with the associated widgets instances created * by the UWA widget parser. * * It also provides a rendering functionality to embed either inline * or iframed widgets. */ class Container { /** * Widgets descriptors. * * @var array */ protected $_widgetDescriptors = array(); /** * Widgets instances indexed by their URL. * * @var array */ protected $_widgetInstances = array(); /** * UWA core libraries. * * @var array */ protected $_coreLibraries = array( 'UWA.js', 'Drivers/UWA-alone.js', 'Drivers/UWA-legacy.js', 'String.js', 'Array.js', 'Element.js', 'Data.js', 'Environment.js', 'Widget.js', 'Utils.js', 'Utils/Client.js', ); /** * Constructor * * @param array $options */ public function __construct($options = array()) { // Widget template $this->_chromeTemplate = '
This widget cannot be displayed.
'); } return $widget; } /** * Renders a widget from its descriptor, either in inline or iframe mode. * * @param Netvibes_Widget $widgetDescriptor */ public function renderWidgetChrome(Netvibes_Widget $widgetDescriptor) { $widgetUrl = $widgetDescriptor->getUrl(); if ($this->_isInlinedWidget($widgetDescriptor) || empty($widgetUrl)) { return $this->_renderWidgetChromeInlined($widgetDescriptor); } else { return $this->_renderWidgetChromeIframed($widgetDescriptor); } } /** * Renders a widget in inline mode. * * @param Netvibes_Widget $widgetDescriptor * @return string */ private function _renderWidgetChromeInlined(Netvibes_Widget $widgetDescriptor) { $id = $widgetDescriptor->getId(); $script = $widgetDescriptor->getScriptUrl(); $widget = $this->_buildWidget($widgetDescriptor); $template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_inlineTemplate)); if (!empty($script)) { $jsOptions = array( 'id' => $id, 'chromeId' => "widget-$id", 'skeletonId' => $widgetDescriptor->getSkeletonId(), 'data' => $widgetDescriptor->getData() ); $js = sprintf("UWA.Widgets.push(%s);", Zend_Json::encode($jsOptions)); } $tplOptions = array( 'id' => $widgetDescriptor->getId(), 'title' => $widgetDescriptor->getTitle(), 'class' => $this->_getWidgetClass($widgetDescriptor), 'body' => $widget->getBody(), 'js' => isset($js) ? $js : '' ); return $this->_renderTemplate($template, $tplOptions); } /** * Renders a widget in iframe mode. * * @param Netvibes_Widget $widgetDescriptor * @return string */ private function _renderWidgetChromeIframed(Netvibes_Widget $widgetDescriptor) { $height = $widgetDescriptor->getHeight(); $template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_iframeTemplate)); $tplOptions = array( 'id' => $widgetDescriptor->getId(), 'title' => $widgetDescriptor->getTitle(), 'class' => $this->_getWidgetClass($widgetDescriptor), 'width' => '100%', 'height' => empty($height) ? '200' : $height, 'iframeUrl' => $this->_getIFrameUrl($widgetDescriptor) ); return $this->_renderTemplate($template, $tplOptions); } /** * Returns the widget CSS class name value. * * @param Netvibes_Widget $widgetDescriptor * @return string */ private function _getWidgetClass(Netvibes_Widget $widgetDescriptor) { $data = $widgetDescriptor->getData(); $class = 'uwa module'; if ($widgetDescriptor->getName() != 'UWA') { $class .= ' ' . $widgetDescriptor->getName(); } else { $class .= ' ' . $widgetDescriptor->getSkeletonId(); } $color = $widgetDescriptor->getColor(); if (isset($color)) { $class .= ' ' . $color . '-module'; } return $class; } /** * Replaces the variables within the template and return it. * * @param string $html * @param array $infos * @return string */ private function _renderTemplate($html, $infos) { foreach ($infos as $key => $value) { $html = str_replace('{' . $key . '}', $value, $html); } return $html; } /** * Returns the iframe URL for a widget. * * @param Netvibes_Widget $widgetDescriptor * @return string */ private function _getIFrameUrl(Netvibes_Widget $widgetDescriptor) { $iframeUrl = $widgetDescriptor->getIframeUrl(); $parseIframeUrl = parse_url($iframeUrl); if (empty($iframeUrl) || $parseIframeUrl['host'] != NV_MODULES) { $iframeUrl = 'http://' . NV_MODULES . '/widget/frame?uwaUrl=' . urlencode($widgetDescriptor->getUrl()); $iframeUrl .= '&id=' . $widgetDescriptor->getId(); } else { $iframeUrl .= '?id=' . $widgetDescriptor->getId(); } $data = $widgetDescriptor->getData(); foreach ($data as $key => $value) { if (!empty($value)) { $iframeUrl .= '&' . $key . '=' . urlencode($value); } } if (isset($this->themeUrl)) { $iframeUrl .= '&NVthemeUrl=' . urlencode($this->themeUrl); } if (isset($this->ifproxyUrl)) { $iframeUrl .= '&ifproxyUrl=' . urlencode($this->ifproxyUrl); } return $iframeUrl; } /** * Whether a Netvibes widget must be inlined or iframed. * * @param Netvibes_Widget $widgetDescriptor * @return boolean */ private function _isInlinedWidget(Netvibes_Widget $widgetDescriptor) { $origin = $widgetDescriptor->getSkeletonOrigin(); if (Zend_Registry::get('inlineWidgets')) { return ($origin == NV_HOST || $origin == 'www.netvibes.com'); } return false; } }