Re-enabled the iframe generation for the metadata service
[shindig.git] / php / src / gadgets / render / GadgetHtmlRenderer.php
blob7d464b1f5c91b64a00e8e5a422a4946480d31022
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 class EmptyClass {
24 /**
25 * Renders a Gadget's Content type="html" view, inlining the content, feature javascript and javascript initialization
26 * into the gadget's content
29 class GadgetHtmlRenderer extends GadgetRenderer {
31 public function renderGadget(Gadget $gadget, $view) {
32 // Was a privacy policy header configured? if so set it
33 if (Config::get('P3P') != '') {
34 header("P3P: " . Config::get('P3P'));
37 $content = '';
38 // Set doctype if quirks = false or empty in the view
39 if (! empty($view['quirks']) || ! $view['quirks']) {
40 $content .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
42 $content .= "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><style type=\"text/css\">" . Config::get('gadget_css') . "</style></head><body>\n";
44 // Inject the OpenSocial feature javascripts
45 $forcedJsLibs = $this->getForcedJsLibs();
46 if (! empty($forcedJsLibs)) {
47 // if some of the feature libraries are externalized (through a browser cachable <script src="/gadgets/js/opensocial-0.9:settitle.js"> type url)
48 // we inject the tag and don't inline those libs (and their dependencies)
49 $forcedJsLibs = explode(':', $forcedJsLibs);
50 $content .= sprintf("<script src=\"%s\"></script>\n", Config::get('default_js_prefix') . $this->getJsUrl($forcedJsLibs, $gadget) . "&container=" . $this->context->getContainer()) . "\n";
51 $registry = $this->context->getRegistry();
52 $missing = array();
53 $registry->resolveFeatures($forcedJsLibs, $forcedJsLibs, $missing);
55 $content .= "<script>\n";
56 foreach ($gadget->features as $feature) {
57 if (! is_array($forcedJsLibs) || (is_array($forcedJsLibs) && ! in_array($feature, $forcedJsLibs))) {
58 $content .= $this->context->getRegistry()->getFeatureContent($feature, $this->context, true);
62 // Add the JavaScript initialization strings for the configuration, localization and preloads
63 $content .= "\n";
64 $content .= $this->appendJsConfig($gadget, count($forcedJsLibs));
65 $content .= $this->appendMessages($gadget);
66 $content .= $this->appendPreloads($gadget);
67 $content .= "</script>";
69 // Append the content from the view
70 $content .= $gadget->substitutions->substitute($view['content']);
72 // And add our runOnLoadHandlers() call
73 $content .= "\n<script>gadgets.util.runOnLoadHandlers();</script></body>\n</html>";
74 echo $content;
77 /**
78 * Retrieve the forced javascript libraries (if any), using either the &libs= from the query
79 * or if that's empty, from the config
81 * @return unknown
83 private function getForcedJsLibs() {
84 $forcedJsLibs = $this->context->getForcedJsLibs();
85 // allow the &libs=.. param to override our forced js libs configuration value
86 if (empty($forcedJsLibs)) {
87 $forcedJsLibs = Config::get('focedJsLibs');
89 return $forcedJsLibs;
92 /**
93 * Appends the javascript features configuration string
95 * @param Gadget $gadget
96 * @param unknown_type $hasForcedLibs
97 * @return string
99 private function appendJsConfig(Gadget $gadget, $hasForcedLibs) {
100 $container = $this->context->getContainer();
101 $containerConfig = $this->context->getContainerConfig();
102 //TODO some day we should parse the forcedLibs too, and include their config selectivly as well for now we just include everything if forced libs is set.
103 if ($hasForcedLibs) {
104 $gadgetConfig = $containerConfig->getConfig($container, 'gadgets.features');
105 } else {
106 $gadgetConfig = array();
107 $featureConfig = $containerConfig->getConfig($container, 'gadgets.features');
108 foreach ($gadget->getJsLibraries() as $library) {
109 $feature = $library->getFeatureName();
110 if (! isset($gadgetConfig[$feature]) && ! empty($featureConfig[$feature])) {
111 $gadgetConfig[$feature] = $featureConfig[$feature];
115 // Add gadgets.util support. This is calculated dynamically based on request inputs.
116 // See java/org/apache/shindig/gadgets/render/RenderingContentRewriter.java for reference.
117 $requires = array();
118 foreach ($gadget->features as $feature) {
119 $requires[$feature] = new EmptyClass();
121 $gadgetConfig['core.util'] = $requires;
122 return "gadgets.config.init(" . json_encode($gadgetConfig) . ");\n";
126 * Injects the relevant translation message bundle into the javascript api
128 * @param Gadget $gadget
129 * @return string
131 private function appendMessages(Gadget $gadget) {
132 $msgs = '';
133 if (! empty($gadget->gadgetSpec->locales)) {
134 $msgs = json_encode($gadget->gadgetSpec->locales);
136 return "gadgets.Prefs.setMessages_($msgs);\n";
140 * Injects the preloaded content into the javascript api
142 * @param Gadget $gadget
143 * @return string
145 private function appendPreloads(Gadget $gadget) {
146 return "gadgets.io.preloaded_ = " . (count($gadget->gadgetSpec->preloads) ? json_encode($gadget->gadgetSpec->preloads) : "{}") . ";\n";