~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/ecosystem/workspace/ecosystem/uwa-server-original/server/lib/Exposition/Compiler/Uwa.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 'Compiler.php';
 
22
 
 
23
/**
 
24
 * UWA compilation utilities.
 
25
 */
 
26
class Compiler_Uwa extends Compiler
 
27
{
 
28
    /**
 
29
     * Main rendering function.
 
30
     *
 
31
     * @return string
 
32
     */
 
33
    public function render()
 
34
    {
 
35
        $style = $this->_widget->getStyle();
 
36
        $script = $this->_widget->getCompressedScript();
 
37
        $preferences = $this->_widget->getPreferences();
 
38
 
 
39
        $l = array();
 
40
 
 
41
        $l[] = '<?xml version="1.0" encoding="utf-8"?>';
 
42
        $l[] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' .
 
43
            ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
 
44
        $l[] = '<html xmlns="http://www.w3.org/1999/xhtml"'.
 
45
            ' xmlns:widget="http://www.netvibes.com/ns/">';
 
46
        $l[] = '<head>';
 
47
        $l[] = '<title>' . $this->_widget->getTitle() . '</title>';
 
48
        $l[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
 
49
 
 
50
        foreach ($this->_widget->getMetadata() as $key => $value) {
 
51
            $l[] = '<meta name="' . $key . '" content="' . $value . '" />';
 
52
        }
 
53
 
 
54
        $l[] = '<link rel="stylesheet" type="text/css"'.
 
55
            ' href="http://' . NV_HOST . '/themes/uwa/style.css"/>';
 
56
 
 
57
        $coreLibrary = $this->_widget->getCoreLibrary();
 
58
        $externalScripts = $this->_widget->getExternalScripts();
 
59
 
 
60
        if (empty($externalScripts) ) {
 
61
            $library = 'http://' . NV_HOST . '/js/UWA/load.js.php?env=Standalone';
 
62
        } else if ($coreLibrary == 'uwa') {
 
63
            $library = 'http://' . NV_HOST . '/js/c/UWA_Standalone.js';
 
64
        } else if ($coreLibrary == 'uwa-mootools') {
 
65
            $library = 'http://' . NV_HOST . '/js/c/UWA_Standalone_Mootools.js';
 
66
        }
 
67
 
 
68
        $l[] = '<script type="text/javascript" src="' . $library .'"></script>';
 
69
 
 
70
        foreach ($externalScripts as $javascript) {
 
71
            $l[] = '<script type="text/javascript" src="' . $javascript . '"></script>';
 
72
        }
 
73
 
 
74
        if (isset($preferences) && count($preferences) > 0) {
 
75
            $l[] = '<widget:preferences>';
 
76
            foreach ($preferences as $pref) {
 
77
                $l[] = $this->_getPreferenceXml($pref);
 
78
            }
 
79
            $l[] = '</widget:preferences>';
 
80
        }
 
81
        if (isset($style) && strlen($style) > 0) {
 
82
            $l[] = '<style type="text/css">'. "\n" . $style . '</style>';
 
83
        }
 
84
        if (isset($script) && strlen($script) > 0) {
 
85
            $l[] = '<script type="text/javascript"><![CDATA[';
 
86
            $l[] = $script;
 
87
            $l[] = ']]></script>';
 
88
        }
 
89
        $l[] = '</head>';
 
90
        $l[] = '<body>';
 
91
        $l[] = $this->_widget->getBody();
 
92
        $l[] = '</body>';
 
93
        $l[] = '</html>';
 
94
 
 
95
        return implode("\n", $l);
 
96
    }
 
97
 
 
98
    public function _getPreferenceXml($preference)
 
99
    {
 
100
        $preference = $preference->toArray();
 
101
        $xml = "<preference";
 
102
        foreach($preference as $key => $value) {
 
103
            if ($key != "options") { 
 
104
                $k = htmlspecialchars($key); 
 
105
                $v = htmlspecialchars($value); 
 
106
                $xml .= " $k=\"$v\""; 
 
107
            }
 
108
        }
 
109
        switch ($preference['type']) {
 
110
            case 'list':
 
111
                $xml .= ">\n";
 
112
                if (isset($preference['options']) && count($preference['options']) > 0) {
 
113
                    foreach ($preference['options'] as $opt) {
 
114
                        $xml .= sprintf("  <option value=\"%s\" label=\"%s\" />\n", $opt['value'], $opt['label']);
 
115
                    }
 
116
                }
 
117
                $xml .= "</preference>";
 
118
                break;
 
119
            default:
 
120
                $xml .= "/>";
 
121
                break;
 
122
 
 
123
        }
 
124
        return $xml;
 
125
    }
 
126
 
 
127
    /**
 
128
     * Renders the controller within a JavaScript closure.
 
129
     */
 
130
    public function renderJavaScriptRaw()
 
131
    {
 
132
        $l = array();
 
133
 
 
134
        $l[] = "(function(){";
 
135
        $l[] = $this->_widget->getCompressedScript();
 
136
        $l[] = "widget.setMetas(" . $this->_widget->getMetadataJson() . ");";
 
137
        $l[] = "widget.setPreferences(" . $this->_widget->getPreferencesJson() . ");";
 
138
        $l[] = "})();";
 
139
 
 
140
        return implode("\n", $l);
 
141
    }
 
142
 
 
143
    /**
 
144
     * Renders the controller as a JavaScript class.
 
145
     */
 
146
    public function renderJavaScriptFunction()
 
147
    {
 
148
        $l = array();
 
149
 
 
150
        $l[] = 'if (typeof UWA == "undefined") var UWA = {};';
 
151
 
 
152
        if (isset($this->options['uwaId'])) {
 
153
            $l[] = 'if (typeof UWA.Scripts == "undefined") UWA.Scripts = [];';
 
154
            $l[] = sprintf("UWA.Scripts['%s']=UWA.script=function(widget){", $this->options['uwaId']);
 
155
        } else {
 
156
            $l[] = "UWA.script=function(widget){";
 
157
        }
 
158
 
 
159
        $l[] = sprintf('widget.uwaUrl = %s;', Zend_Json::encode($this->_widget->getUrl()));
 
160
 
 
161
        $l[] = $this->_widget->getCompressedScript();
 
162
 
 
163
        $metas = $this->_widget->getMetas();
 
164
        $l[] = sprintf("widget.setMetas(%s);", Zend_Json::encode($metas));
 
165
 
 
166
        $preferences = $this->_widget->getPreferencesArray();
 
167
        if (count($preferences) > 0) {
 
168
            $l[] = sprintf("widget.setPreferences(%s);", Zend_Json::encode($preferences));
 
169
        }
 
170
 
 
171
        if (isset($this->options['platform']) && in_array($this->options['platform'], array('live', 'opera', 'dashboard'))) {
 
172
            $body = $this->_widget->getBody();
 
173
            if (!empty($body) && $body != '<p>Loading...</p>') {
 
174
                $l[] = sprintf('widget.setBody(%s);', Zend_Json::encode($body));
 
175
            }
 
176
        }
 
177
 
 
178
        $l[] = "return widget;";
 
179
 
 
180
        $l[] = "}";
 
181
 
 
182
        return implode("\n", $l);
 
183
    }
 
184
 
 
185
    /**
 
186
     * Renders the controller as a JavaScript closure.
 
187
     */
 
188
    public function renderJs()
 
189
    {
 
190
        return $this->renderJavaScriptFunction();
 
191
    }
 
192
 
 
193
    /**
 
194
     * Renders the styles information.
 
195
     */
 
196
    public function renderCss()
 
197
    {
 
198
        return $this->_widget->getStyle();
 
199
    }
 
200
}