3
* Copyright Netvibes 2006-2009.
4
* This file is part of Exposition PHP Lib.
6
* Exposition PHP Lib is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Exposition PHP Lib is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with Exposition PHP Lib. If not, see <http://www.gnu.org/licenses/>.
21
require_once 'Parser/Factory.php';
22
require_once 'Widget.php';
24
require_once 'Zend/Json.php';
29
* This class maps some widgets descriptors retrieved from the
30
* Netvibes REST API with the associated widgets instances created
31
* by the UWA widget parser.
33
* It also provides a rendering functionality to embed either inline
39
* Widgets descriptors.
43
protected $_widgetDescriptors = array();
46
* Widgets instances indexed by their URL.
50
protected $_widgetInstances = array();
57
protected $_coreLibraries = array(
59
'Drivers/UWA-alone.js',
60
'Drivers/UWA-legacy.js',
74
* @param array $options
76
public function __construct($options = array())
79
$this->_chromeTemplate =
80
'<div class="{class}" id="widget-{id}">' . "\n" .
81
' <div class="moduleFrame">' . "\n" .
82
' <div class="moduleHeaderContainer">' . "\n" .
83
' <div class="moduleHeader">' . "\n" .
84
' <span class="title">{title}</span>' . "\n" .
91
$this->_inlineTemplate = '<div class="moduleContent">{body}</div>' . "\n" .
92
'<script type="text/javascript">{js}</script>';
94
$this->_iframeTemplate = '<div class="moduleContent" style="padding:0">' .
95
'<iframe id="frame_{id}" src="{iframeUrl}" ' .
96
'scrolling="no" frameborder="0" style="display:block" width="{width}" height="{height}">' .
101
$this->_widgetDescriptors = array();
102
$this->_widgetInstances = array();
106
* Returns the correct list of UWA core libraries
107
* according to the compressed JavaScript constant value.
111
public function getCoreLibraries()
113
$libraries = array();
114
$useCompressedJs = Zend_Registry::get('useCompressedJs');
115
if ($useCompressedJs) {
116
$libraries[] = Zend_Registry::get('uwaJsDir') . 'UWA_Core.js';
118
foreach ($this->_coreLibraries as $library) {
119
$libraries[] = Zend_Registry::get('uwaJsDir') . $library;
126
* Sets the widgets descriptors.
128
* @param array $widgetDescriptors
130
public function setWidgets(array $widgetDescriptors)
132
$this->_widgetDescriptors = $widgetDescriptors;
136
* Retrieves all the Netvibes Widgets JavaScript files URL.
140
public function getScripts()
143
foreach ($this->_widgetDescriptors as $widgetDescriptor) {
144
$widgetUrl = $widgetDescriptor->getUrl();
145
if (isset($widgetUrl) && $this->_isInlinedWidget($widgetDescriptor)) {
146
$widget = $this->_buildWidget($widgetDescriptor);
147
$scriptUrl = $widgetDescriptor->getScriptUrl();
148
$scripts[] = $scriptUrl;
149
$scripts = array_merge($scripts, $widget->getExternalScripts());
152
return array_unique($scripts);
156
* Retrieves all the Netvibes Widgets Stylesheets URL.
160
public function getStylesheets()
162
$stylesheets = array();
163
foreach ($this->_widgetDescriptors as $widgetDescriptor) {
164
$widgetUrl = $widgetDescriptor->getUrl();
165
if (isset($widgetUrl) && $this->_isInlinedWidget($widgetDescriptor)) {
166
$widget = $this->_buildWidget($widgetDescriptor);
167
$stylesheets[] = $widgetDescriptor->getStylesheetUrl();
168
$stylesheets = array_merge($stylesheets, $widget->getExternalStylesheets());
171
return array_unique($stylesheets);
175
* Renders the widgets for a given tab column number.
179
public function renderWidgetsChrome($column = null)
181
foreach ($this->_widgetDescriptors as $widgetDescriptor) {
182
if ((isset($column) && $widgetDescriptor->getCol() == $column) || !isset($column)) {
183
echo $this->renderWidgetChrome($widgetDescriptor);
189
* Builds the widget instance from a given Netvibes widget descriptor.
191
* @param Netvibes_Widget $widgetDescriptor
194
private function _buildWidget(Netvibes_Widget $widgetDescriptor)
197
$widgetUrl = $widgetDescriptor->getUrl();
198
if (!empty($widgetUrl)) {
199
if (!isset($this->_widgetInstances[$widgetUrl])) {
200
// Parse the UWA widget from the given URL
201
$parser = Parser_Factory::getParser('uwa', $widgetUrl);
202
$this->_widgetInstances[$widgetUrl] = $parser->buildWidget();
204
$widget = $this->_widgetInstances[$widgetUrl];
206
// Create an empty widget
207
$widget = new Widget();
208
$widget->setBody('<p>This widget cannot be displayed.</p>');
214
* Renders a widget from its descriptor, either in inline or iframe mode.
216
* @param Netvibes_Widget $widgetDescriptor
218
public function renderWidgetChrome(Netvibes_Widget $widgetDescriptor)
220
$widgetUrl = $widgetDescriptor->getUrl();
221
if ($this->_isInlinedWidget($widgetDescriptor) || empty($widgetUrl)) {
222
return $this->_renderWidgetChromeInlined($widgetDescriptor);
224
return $this->_renderWidgetChromeIframed($widgetDescriptor);
229
* Renders a widget in inline mode.
231
* @param Netvibes_Widget $widgetDescriptor
234
private function _renderWidgetChromeInlined(Netvibes_Widget $widgetDescriptor)
236
$id = $widgetDescriptor->getId();
237
$script = $widgetDescriptor->getScriptUrl();
238
$widget = $this->_buildWidget($widgetDescriptor);
240
$template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_inlineTemplate));
241
if (!empty($script)) {
244
'chromeId' => "widget-$id",
245
'skeletonId' => $widgetDescriptor->getSkeletonId(),
246
'data' => $widgetDescriptor->getData()
248
$js = sprintf("UWA.Widgets.push(%s);", Zend_Json::encode($jsOptions));
251
'id' => $widgetDescriptor->getId(),
252
'title' => $widgetDescriptor->getTitle(),
253
'class' => $this->_getWidgetClass($widgetDescriptor),
254
'body' => $widget->getBody(),
255
'js' => isset($js) ? $js : ''
257
return $this->_renderTemplate($template, $tplOptions);
261
* Renders a widget in iframe mode.
263
* @param Netvibes_Widget $widgetDescriptor
266
private function _renderWidgetChromeIframed(Netvibes_Widget $widgetDescriptor)
268
$height = $widgetDescriptor->getHeight();
269
$template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_iframeTemplate));
271
'id' => $widgetDescriptor->getId(),
272
'title' => $widgetDescriptor->getTitle(),
273
'class' => $this->_getWidgetClass($widgetDescriptor),
275
'height' => empty($height) ? '200' : $height,
276
'iframeUrl' => $this->_getIFrameUrl($widgetDescriptor)
278
return $this->_renderTemplate($template, $tplOptions);
282
* Returns the widget CSS class name value.
284
* @param Netvibes_Widget $widgetDescriptor
287
private function _getWidgetClass(Netvibes_Widget $widgetDescriptor)
289
$data = $widgetDescriptor->getData();
290
$class = 'uwa module';
291
if ($widgetDescriptor->getName() != 'UWA') {
292
$class .= ' ' . $widgetDescriptor->getName();
294
$class .= ' ' . $widgetDescriptor->getSkeletonId();
296
$color = $widgetDescriptor->getColor();
298
$class .= ' ' . $color . '-module';
304
* Replaces the variables within the template and return it.
306
* @param string $html
307
* @param array $infos
310
private function _renderTemplate($html, $infos)
312
foreach ($infos as $key => $value) {
313
$html = str_replace('{' . $key . '}', $value, $html);
319
* Returns the iframe URL for a widget.
321
* @param Netvibes_Widget $widgetDescriptor
324
private function _getIFrameUrl(Netvibes_Widget $widgetDescriptor)
326
$iframeUrl = $widgetDescriptor->getIframeUrl();
327
$parseIframeUrl = parse_url($iframeUrl);
328
if (empty($iframeUrl) || $parseIframeUrl['host'] != NV_MODULES) {
329
$iframeUrl = 'http://' . NV_MODULES . '/widget/frame?uwaUrl=' . urlencode($widgetDescriptor->getUrl());
330
$iframeUrl .= '&id=' . $widgetDescriptor->getId();
332
$iframeUrl .= '?id=' . $widgetDescriptor->getId();
335
$data = $widgetDescriptor->getData();
336
foreach ($data as $key => $value) {
337
if (!empty($value)) {
338
$iframeUrl .= '&' . $key . '=' . urlencode($value);
341
if (isset($this->themeUrl)) {
342
$iframeUrl .= '&NVthemeUrl=' . urlencode($this->themeUrl);
344
if (isset($this->ifproxyUrl)) {
345
$iframeUrl .= '&ifproxyUrl=' . urlencode($this->ifproxyUrl);
351
* Whether a Netvibes widget must be inlined or iframed.
353
* @param Netvibes_Widget $widgetDescriptor
356
private function _isInlinedWidget(Netvibes_Widget $widgetDescriptor)
358
$origin = $widgetDescriptor->getSkeletonOrigin();
359
if (Zend_Registry::get('inlineWidgets')) {
360
return ($origin == NV_HOST || $origin == 'www.netvibes.com');