~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Exposition/Container.php

  • Committer: parra
  • Date: 2010-03-15 02:39:02 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1433
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Copyright Netvibes 2006-2009.
 
4
 * This file is part of Exposition PHP Lib.
 
5
 * 
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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/>.
 
18
 */
 
19
 
 
20
 
 
21
require_once 'Parser/Factory.php';
 
22
require_once 'Widget.php';
 
23
 
 
24
require_once 'Zend/Json.php';
 
25
 
 
26
/**
 
27
 * Widgets Container.
 
28
 *
 
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.
 
32
 *
 
33
 * It also provides a rendering functionality to embed either inline
 
34
 * or iframed widgets.
 
35
 */
 
36
class Container
 
37
{
 
38
    /**
 
39
     * Widgets descriptors.
 
40
     *
 
41
     * @var array
 
42
     */
 
43
    protected $_widgetDescriptors = array();
 
44
 
 
45
    /**
 
46
     * Widgets instances indexed by their URL.
 
47
     *
 
48
     * @var array
 
49
     */
 
50
    protected $_widgetInstances = array();
 
51
 
 
52
    /**
 
53
     * UWA core libraries.
 
54
     *
 
55
     * @var array
 
56
     */
 
57
    protected $_coreLibraries = array(
 
58
        'UWA.js',
 
59
        'Drivers/UWA-alone.js',
 
60
        'Drivers/UWA-legacy.js',
 
61
        'String.js',
 
62
        'Array.js',
 
63
        'Element.js',
 
64
        'Data.js',
 
65
        'Environment.js',
 
66
        'Widget.js',
 
67
        'Utils.js',
 
68
        'Utils/Client.js',
 
69
    );
 
70
 
 
71
    /**
 
72
     * Constructor
 
73
     *
 
74
     * @param array $options
 
75
     */
 
76
    public function __construct($options = array())
 
77
    {
 
78
        // Widget template
 
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" .
 
85
            '      </div>' . "\n" .
 
86
            '    </div>' . "\n" .
 
87
            '    {content}' . "\n" .
 
88
            '  </div>' . "\n" .
 
89
            '</div>' . "\n";
 
90
 
 
91
        $this->_inlineTemplate = '<div class="moduleContent">{body}</div>' . "\n" .
 
92
            '<script type="text/javascript">{js}</script>';
 
93
 
 
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}">' .
 
97
            '</iframe>' .
 
98
        '</div>';
 
99
 
 
100
        // Widgets arrays
 
101
        $this->_widgetDescriptors = array();
 
102
        $this->_widgetInstances = array();
 
103
    }
 
104
 
 
105
    /**
 
106
     * Returns the correct list of UWA core libraries
 
107
     * according to the compressed JavaScript constant value.
 
108
     *
 
109
     * @return array
 
110
     */
 
111
    public function getCoreLibraries()
 
112
    {
 
113
        $libraries = array();
 
114
        $useCompressedJs = Zend_Registry::get('useCompressedJs');
 
115
        if ($useCompressedJs) {
 
116
            $libraries[] = Zend_Registry::get('uwaJsDir') . 'UWA_Core.js';
 
117
        } else {
 
118
            foreach ($this->_coreLibraries as $library) {
 
119
                $libraries[] = Zend_Registry::get('uwaJsDir') . $library;
 
120
            }
 
121
        }
 
122
        return $libraries;
 
123
    }
 
124
 
 
125
    /**
 
126
     * Sets the widgets descriptors.
 
127
     *
 
128
     * @param array $widgetDescriptors
 
129
     */
 
130
    public function setWidgets(array $widgetDescriptors)
 
131
    {
 
132
        $this->_widgetDescriptors = $widgetDescriptors;
 
133
    }
 
134
 
 
135
    /**
 
136
     * Retrieves all the Netvibes Widgets JavaScript files URL.
 
137
     *
 
138
     * @return array
 
139
     */
 
140
    public function getScripts()
 
141
    {
 
142
        $scripts = array();
 
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());
 
150
            }
 
151
        }
 
152
        return array_unique($scripts);
 
153
    }
 
154
 
 
155
    /**
 
156
     * Retrieves all the Netvibes Widgets Stylesheets URL.
 
157
     *
 
158
     * @return array
 
159
     */
 
160
    public function getStylesheets()
 
161
    {
 
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());
 
169
            }
 
170
        }
 
171
        return array_unique($stylesheets);
 
172
    }
 
173
 
 
174
    /**
 
175
     * Renders the widgets for a given tab column number.
 
176
     *
 
177
     * @param int $column
 
178
     */
 
179
    public function renderWidgetsChrome($column = null)
 
180
    {
 
181
        foreach ($this->_widgetDescriptors as $widgetDescriptor) {
 
182
            if ((isset($column) && $widgetDescriptor->getCol() == $column) || !isset($column)) {
 
183
                echo $this->renderWidgetChrome($widgetDescriptor);
 
184
            }
 
185
        }
 
186
    }
 
187
 
 
188
    /**
 
189
     * Builds the widget instance from a given Netvibes widget descriptor.
 
190
     *
 
191
     * @param Netvibes_Widget $widgetDescriptor
 
192
     * @return Widget
 
193
     */
 
194
    private function _buildWidget(Netvibes_Widget $widgetDescriptor)
 
195
    {
 
196
        $widget = null;
 
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();
 
203
            }
 
204
            $widget = $this->_widgetInstances[$widgetUrl];
 
205
        } else {
 
206
            // Create an empty widget
 
207
            $widget = new Widget();
 
208
            $widget->setBody('<p>This widget cannot be displayed.</p>');
 
209
        }
 
210
        return $widget;
 
211
    }
 
212
 
 
213
    /**
 
214
     * Renders a widget from its descriptor, either in inline or iframe mode.
 
215
     *
 
216
     * @param Netvibes_Widget $widgetDescriptor
 
217
     */
 
218
    public function renderWidgetChrome(Netvibes_Widget $widgetDescriptor)
 
219
    {
 
220
        $widgetUrl = $widgetDescriptor->getUrl();
 
221
        if ($this->_isInlinedWidget($widgetDescriptor) || empty($widgetUrl)) {
 
222
            return $this->_renderWidgetChromeInlined($widgetDescriptor);
 
223
        } else {
 
224
            return $this->_renderWidgetChromeIframed($widgetDescriptor);
 
225
        }
 
226
    }
 
227
 
 
228
    /**
 
229
     * Renders a widget in inline mode.
 
230
     *
 
231
     * @param Netvibes_Widget $widgetDescriptor
 
232
     * @return string
 
233
     */
 
234
    private function _renderWidgetChromeInlined(Netvibes_Widget $widgetDescriptor)
 
235
    {
 
236
        $id = $widgetDescriptor->getId();
 
237
        $script = $widgetDescriptor->getScriptUrl();
 
238
        $widget = $this->_buildWidget($widgetDescriptor);
 
239
 
 
240
        $template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_inlineTemplate));
 
241
        if (!empty($script)) {
 
242
            $jsOptions = array(
 
243
                'id'         => $id,
 
244
                'chromeId'   => "widget-$id",
 
245
                'skeletonId' => $widgetDescriptor->getSkeletonId(),
 
246
                'data'       => $widgetDescriptor->getData()
 
247
            );
 
248
            $js = sprintf("UWA.Widgets.push(%s);", Zend_Json::encode($jsOptions));
 
249
        }
 
250
        $tplOptions = array(
 
251
            'id'    => $widgetDescriptor->getId(),
 
252
            'title' => $widgetDescriptor->getTitle(),
 
253
            'class' => $this->_getWidgetClass($widgetDescriptor),
 
254
            'body'  => $widget->getBody(),
 
255
            'js'    => isset($js) ? $js : ''
 
256
        );
 
257
        return $this->_renderTemplate($template, $tplOptions);
 
258
    }
 
259
 
 
260
    /**
 
261
     * Renders a widget in iframe mode.
 
262
     *
 
263
     * @param Netvibes_Widget $widgetDescriptor
 
264
     * @return string
 
265
     */
 
266
    private function _renderWidgetChromeIframed(Netvibes_Widget $widgetDescriptor)
 
267
    {
 
268
        $height = $widgetDescriptor->getHeight();
 
269
        $template = $this->_renderTemplate($this->_chromeTemplate, array('content' => $this->_iframeTemplate));
 
270
        $tplOptions = array(
 
271
            'id'        => $widgetDescriptor->getId(),
 
272
            'title'     => $widgetDescriptor->getTitle(),
 
273
            'class'     => $this->_getWidgetClass($widgetDescriptor),
 
274
            'width'     => '100%',
 
275
            'height'    => empty($height) ? '200' : $height,
 
276
            'iframeUrl' => $this->_getIFrameUrl($widgetDescriptor)
 
277
        );
 
278
        return $this->_renderTemplate($template, $tplOptions);
 
279
    }
 
280
 
 
281
    /**
 
282
     * Returns the widget CSS class name value.
 
283
     *
 
284
     * @param Netvibes_Widget $widgetDescriptor
 
285
     * @return string
 
286
     */
 
287
    private function _getWidgetClass(Netvibes_Widget $widgetDescriptor)
 
288
    {
 
289
        $data = $widgetDescriptor->getData();
 
290
        $class = 'uwa module';
 
291
        if ($widgetDescriptor->getName() != 'UWA') {
 
292
            $class .= ' ' . $widgetDescriptor->getName();
 
293
        } else {
 
294
            $class .= ' ' . $widgetDescriptor->getSkeletonId();
 
295
        }
 
296
        $color = $widgetDescriptor->getColor();
 
297
        if (isset($color)) {
 
298
            $class .= ' ' . $color . '-module';
 
299
        }
 
300
        return $class;
 
301
    }
 
302
 
 
303
    /**
 
304
     * Replaces the variables within the template and return it.
 
305
     *
 
306
     * @param  string $html
 
307
     * @param  array  $infos
 
308
     * @return string
 
309
     */
 
310
    private function _renderTemplate($html, $infos)
 
311
    {
 
312
        foreach ($infos as $key => $value) {
 
313
            $html = str_replace('{' . $key . '}', $value, $html);
 
314
        }
 
315
        return $html;
 
316
    }
 
317
 
 
318
    /**
 
319
     * Returns the iframe URL for a widget.
 
320
     *
 
321
     * @param Netvibes_Widget $widgetDescriptor
 
322
     * @return string
 
323
     */
 
324
    private function _getIFrameUrl(Netvibes_Widget $widgetDescriptor)
 
325
    {
 
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();
 
331
        } else {
 
332
            $iframeUrl .= '?id=' . $widgetDescriptor->getId();
 
333
        }
 
334
 
 
335
        $data = $widgetDescriptor->getData();
 
336
        foreach ($data as $key => $value) {
 
337
            if (!empty($value)) {
 
338
                $iframeUrl .= '&' . $key . '=' . urlencode($value);
 
339
            }
 
340
        }
 
341
        if (isset($this->themeUrl)) {
 
342
            $iframeUrl .= '&NVthemeUrl=' . urlencode($this->themeUrl);
 
343
        }
 
344
        if (isset($this->ifproxyUrl)) {
 
345
            $iframeUrl .= '&ifproxyUrl=' . urlencode($this->ifproxyUrl);
 
346
        }
 
347
        return $iframeUrl;
 
348
    }
 
349
 
 
350
    /**
 
351
     * Whether a Netvibes widget must be inlined or iframed.
 
352
     *
 
353
     * @param Netvibes_Widget $widgetDescriptor
 
354
     * @return boolean
 
355
     */
 
356
    private function _isInlinedWidget(Netvibes_Widget $widgetDescriptor)
 
357
    {
 
358
        $origin = $widgetDescriptor->getSkeletonOrigin();
 
359
        if (Zend_Registry::get('inlineWidgets')) {
 
360
            return ($origin == NV_HOST || $origin == 'www.netvibes.com');
 
361
        }
 
362
        return false;
 
363
    }
 
364
}