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 'Compiler.php';
24
* UWA compilation utilities.
26
class Compiler_Uwa extends Compiler
29
* Main rendering function.
33
public function render()
35
$style = $this->_widget->getStyle();
36
$script = $this->_widget->getCompressedScript();
37
$preferences = $this->_widget->getPreferences();
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/">';
47
$l[] = '<title>' . $this->_widget->getTitle() . '</title>';
48
$l[] = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
50
foreach ($this->_widget->getMetadata() as $key => $value) {
51
$l[] = '<meta name="' . $key . '" content="' . $value . '" />';
54
$l[] = '<link rel="stylesheet" type="text/css"'.
55
' href="http://' . NV_HOST . '/themes/uwa/style.css"/>';
57
$coreLibrary = $this->_widget->getCoreLibrary();
58
$externalScripts = $this->_widget->getExternalScripts();
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';
68
$l[] = '<script type="text/javascript" src="' . $library .'"></script>';
70
foreach ($externalScripts as $javascript) {
71
$l[] = '<script type="text/javascript" src="' . $javascript . '"></script>';
74
if (isset($preferences) && count($preferences) > 0) {
75
$l[] = '<widget:preferences>';
76
foreach ($preferences as $pref) {
77
$l[] = $this->_getPreferenceXml($pref);
79
$l[] = '</widget:preferences>';
81
if (isset($style) && strlen($style) > 0) {
82
$l[] = '<style type="text/css">'. "\n" . $style . '</style>';
84
if (isset($script) && strlen($script) > 0) {
85
$l[] = '<script type="text/javascript"><![CDATA[';
87
$l[] = ']]></script>';
91
$l[] = $this->_widget->getBody();
95
return implode("\n", $l);
98
public function _getPreferenceXml($preference)
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\"";
109
switch ($preference['type']) {
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']);
117
$xml .= "</preference>";
128
* Renders the controller within a JavaScript closure.
130
public function renderJavaScriptRaw()
134
$l[] = "(function(){";
135
$l[] = $this->_widget->getCompressedScript();
136
$l[] = "widget.setMetas(" . $this->_widget->getMetadataJson() . ");";
137
$l[] = "widget.setPreferences(" . $this->_widget->getPreferencesJson() . ");";
140
return implode("\n", $l);
144
* Renders the controller as a JavaScript class.
146
public function renderJavaScriptFunction()
150
$l[] = 'if (typeof UWA == "undefined") var UWA = {};';
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']);
156
$l[] = "UWA.script=function(widget){";
159
$l[] = sprintf('widget.uwaUrl = %s;', Zend_Json::encode($this->_widget->getUrl()));
161
$l[] = $this->_widget->getCompressedScript();
163
$metas = $this->_widget->getMetas();
164
$l[] = sprintf("widget.setMetas(%s);", Zend_Json::encode($metas));
166
$preferences = $this->_widget->getPreferencesArray();
167
if (count($preferences) > 0) {
168
$l[] = sprintf("widget.setPreferences(%s);", Zend_Json::encode($preferences));
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));
178
$l[] = "return widget;";
182
return implode("\n", $l);
186
* Renders the controller as a JavaScript closure.
188
public function renderJs()
190
return $this->renderJavaScriptFunction();
194
* Renders the styles information.
196
public function renderCss()
198
return $this->_widget->getStyle();