| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* This module demonstrates a way to create Google Gadgets from within Drupal.
|
| 4 |
*
|
| 5 |
* It is expected that users will actually evolve it to a generalized gadget
|
| 6 |
* creation class, from which actual gadgets will be derived without having to
|
| 7 |
* create a module for each gadget.
|
| 8 |
*
|
| 9 |
* It requires PHP5 and the DOM extension.
|
| 10 |
*
|
| 11 |
* Licensed under the CeCILL 2.0
|
| 12 |
*
|
| 13 |
* @copyright 2007 Frederic G. MARAND fgm@riff.org
|
| 14 |
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
|
| 15 |
* @version CVS: $Id:$
|
| 16 |
* @link http://blog.riff.org/gadget
|
| 17 |
* @since n.a.
|
| 18 |
*/
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Base class for gadget creation
|
| 22 |
*/
|
| 23 |
class GoogleGadget
|
| 24 |
{
|
| 25 |
/**
|
| 26 |
* Content type attribute
|
| 27 |
*
|
| 28 |
* @var string
|
| 29 |
*/
|
| 30 |
public $contentType;
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Raw content. Data should be in the format described by $this->contentType
|
| 34 |
*/
|
| 35 |
public $content = '';
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Hold elements (#-prefixed) and attributes below ModulePrefs
|
| 39 |
*
|
| 40 |
* @var array
|
| 41 |
*/
|
| 42 |
public $modulePrefs;
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Hold attributes for UserPref elements
|
| 46 |
*
|
| 47 |
* @var array
|
| 48 |
*/
|
| 49 |
public $userPrefs;
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Constructor builds an empty HTML element by default
|
| 53 |
*
|
| 54 |
* @param mixed $body
|
| 55 |
*/
|
| 56 |
public function __construct($body = '', $type = 'html')
|
| 57 |
{
|
| 58 |
$this->contentType = $type;
|
| 59 |
$this->content = $body;
|
| 60 |
$this->modulePrefs = array();
|
| 61 |
$this->userPrefs = array();
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Shortcut to set up module preferences
|
| 66 |
*
|
| 67 |
* @param string $section
|
| 68 |
* @param string $name
|
| 69 |
* @param string $value
|
| 70 |
* @return void
|
| 71 |
*/
|
| 72 |
public function setModulePref($section, $name, $value)
|
| 73 |
{
|
| 74 |
$this->modulePrefs["#$section"][$name] = $value;
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Add user-defined preferences
|
| 79 |
*
|
| 80 |
* $attributes is an associative array of optional name/value attribute pairs
|
| 81 |
*
|
| 82 |
* @param string $name
|
| 83 |
* @param string $displayName
|
| 84 |
* @param array $attributes
|
| 85 |
*/
|
| 86 |
public function addUserPref($name, $displayName, $attributes = array())
|
| 87 |
{
|
| 88 |
$this->userPrefs[$name] = $attributes;
|
| 89 |
$this->userPrefs[$name]['name'] = $name;
|
| 90 |
$this->userPrefs[$name]['display_name'] = $displayName;
|
| 91 |
}
|
| 92 |
|
| 93 |
/**
|
| 94 |
* Return the rendered module and die() to avoid drupal theming on XML content
|
| 95 |
*
|
| 96 |
* @return void
|
| 97 |
*/
|
| 98 |
public function render()
|
| 99 |
{
|
| 100 |
/**
|
| 101 |
* We don't use drupal_set_header because we're not letting drupal do the
|
| 102 |
* rendering
|
| 103 |
*/
|
| 104 |
header('Content-type: text/xml; charset=utf-8');
|
| 105 |
|
| 106 |
$doc = new DOMDocument();
|
| 107 |
$module = $doc->createElement('Module');
|
| 108 |
$module = $doc->appendChild($module);
|
| 109 |
|
| 110 |
/**
|
| 111 |
* module preferences
|
| 112 |
*/
|
| 113 |
if (count($this->modulePrefs) > 0)
|
| 114 |
{
|
| 115 |
$prefsNode = $doc->createElement('ModulePrefs');
|
| 116 |
$prefsNode = $module->appendChild($prefsNode);
|
| 117 |
$sections = array();
|
| 118 |
|
| 119 |
foreach ($this->modulePrefs as $prefName => $prefValue)
|
| 120 |
{
|
| 121 |
if ($prefName[0] == '#')
|
| 122 |
{
|
| 123 |
$prefName = substr($prefName, 1);
|
| 124 |
if (!in_array($prefName, $sections)) // must create it
|
| 125 |
{
|
| 126 |
${$prefName} = $doc->createElement($prefName);
|
| 127 |
${$prefName} = $prefsNode->appendChild(${$prefName});
|
| 128 |
$sections[] = $prefName;
|
| 129 |
}
|
| 130 |
foreach ($prefValue as $attrName => $attrValue)
|
| 131 |
{
|
| 132 |
${$prefName}->setAttribute($attrName, $attrValue);
|
| 133 |
}
|
| 134 |
}
|
| 135 |
else
|
| 136 |
{
|
| 137 |
$prefsNode->setAttribute($prefName, $prefValue);
|
| 138 |
}
|
| 139 |
}
|
| 140 |
} // module preferences
|
| 141 |
|
| 142 |
/**
|
| 143 |
* user preferences
|
| 144 |
*/
|
| 145 |
if (count($this->userPrefs > 0))
|
| 146 |
{
|
| 147 |
foreach ($this->userPrefs as $prefArray)
|
| 148 |
{
|
| 149 |
$prefNode = $doc->createElement('UserPref');
|
| 150 |
$prefNode = $module->appendChild($prefNode);
|
| 151 |
foreach ($prefArray as $prefName => $prefValue)
|
| 152 |
{
|
| 153 |
$prefNode->setAttribute($prefName, $prefValue);
|
| 154 |
}
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
/**
|
| 159 |
* module contents
|
| 160 |
*/
|
| 161 |
$content = $doc->createElement('Content');
|
| 162 |
$content = $module->appendChild($content);
|
| 163 |
$content->setAttribute('type', $this->contentType);
|
| 164 |
$data = $doc->createCDATASection($this->content);
|
| 165 |
$data = $content->appendChild($data);
|
| 166 |
|
| 167 |
/**
|
| 168 |
* clean up
|
| 169 |
*/
|
| 170 |
echo $doc->saveXML();
|
| 171 |
die();
|
| 172 |
}
|
| 173 |
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Implement hook_menu
|
| 178 |
*
|
| 179 |
* @param boolean $may_cache
|
| 180 |
* @return array
|
| 181 |
*/
|
| 182 |
function google_gadget_menu($may_cache)
|
| 183 |
{
|
| 184 |
$items = array();
|
| 185 |
if ($may_cache)
|
| 186 |
{
|
| 187 |
$items[] = array
|
| 188 |
(
|
| 189 |
'path' => 'gadget',
|
| 190 |
'title' => t('Google Gadget sample'),
|
| 191 |
'access' => TRUE,
|
| 192 |
'callback' => 'google_gadget_main',
|
| 193 |
);
|
| 194 |
}
|
| 195 |
return $items;
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* Main entry point for gadget creation
|
| 200 |
*
|
| 201 |
* @return void
|
| 202 |
*/
|
| 203 |
function google_gadget_main()
|
| 204 |
{
|
| 205 |
$g = new GoogleGadget(
|
| 206 |
<<<EOT
|
| 207 |
<div style="padding: 0 0.5em; margin: 0">
|
| 208 |
<script type="text/javascript">
|
| 209 |
function drupalapisearch(q)
|
| 210 |
{
|
| 211 |
destination = "http://api.drupal.org/api/__UP_apiversion__/function/" + q;
|
| 212 |
top.location.href = destination;
|
| 213 |
}
|
| 214 |
</script>
|
| 215 |
<form name="drupalapiform" onsubmit="drupalapisearch(drupalapiform.q.value)">
|
| 216 |
<input type="text" maxlength="128" name="q" size="40" value="" />
|
| 217 |
<input type="submit" value="Lookup Drupal function" />
|
| 218 |
</form>
|
| 219 |
<script>_IG_AdjustIFrameHeight();</script>
|
| 220 |
</div>
|
| 221 |
EOT
|
| 222 |
);
|
| 223 |
|
| 224 |
$g->contentType = 'html'; // default value, just added here to show it exists
|
| 225 |
$g->modulePrefs['title'] = 'Drupal API reference';
|
| 226 |
$g->modulePrefs['author'] = 'Frederic G. MARAND';
|
| 227 |
$g->modulePrefs['description'] = 'A simple module to demonstrate how to build google gadgets within Drupal';
|
| 228 |
$g->modulePrefs['author_email'] = 'http://blog.riff.org/contact';
|
| 229 |
$g->modulePrefs['author_link'] = 'http://blog.riff.org/';
|
| 230 |
$g->setModulePref('Require', 'feature', 'dynamic-height');
|
| 231 |
$g->addUserPref('apiversion', 'Drupal API version', array
|
| 232 |
(
|
| 233 |
'default_value' => '5'
|
| 234 |
));
|
| 235 |
$g->render(); // includes die() to avoid drupal themeing
|
| 236 |
}
|