| 1 |
<?php |
| 2 |
// +-----------------------------------------------------------------------+ |
| 3 |
// | Copyright (c) 2002-2003, Richard Heyes, Harald Radi | |
| 4 |
// | All rights reserved. | |
| 5 |
// | | |
| 6 |
// | Redistribution and use in source and binary forms, with or without | |
| 7 |
// | modification, are permitted provided that the following conditions | |
| 8 |
// | are met: | |
| 9 |
// | | |
| 10 |
// | o Redistributions of source code must retain the above copyright | |
| 11 |
// | notice, this list of conditions and the following disclaimer. | |
| 12 |
// | o Redistributions in binary form must reproduce the above copyright | |
| 13 |
// | notice, this list of conditions and the following disclaimer in the | |
| 14 |
// | documentation and/or other materials provided with the distribution.| |
| 15 |
// | o The names of the authors may not be used to endorse or promote | |
| 16 |
// | products derived from this software without specific prior written | |
| 17 |
// | permission. | |
| 18 |
// | | |
| 19 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 20 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 21 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 22 |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 23 |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 24 |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 25 |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 26 |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 27 |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 28 |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 29 |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 30 |
// | | |
| 31 |
// +-----------------------------------------------------------------------+ |
| 32 |
// | Author: Richard Heyes <richard@phpguru.org> | |
| 33 |
// | Harald Radi <harald.radi@nme.at> | |
| 34 |
// +-----------------------------------------------------------------------+ |
| 35 |
// |
| 36 |
// Id: TreeMenu.php,v 1.15 2003/02/21 18:19:39 richard Exp |
| 37 |
// $Id: TreeMenu.php,v 1.15 2003/02/21 18:19:39 richard Exp $ |
| 38 |
|
| 39 |
/** |
| 40 |
* HTML_TreeMenu Class |
| 41 |
* |
| 42 |
* A simple couple of PHP classes and some not so simple |
| 43 |
* Jabbascript which produces a tree menu. In IE this menu |
| 44 |
* is dynamic, with branches being collapsable. In IE5+ the |
| 45 |
* status of the collapsed/open branches persists across page |
| 46 |
* refreshes.In any other browser the tree is static. Code is |
| 47 |
* based on work of Harald Radi. |
| 48 |
* |
| 49 |
* Usage. |
| 50 |
* |
| 51 |
* After installing the package, copy the example php script to |
| 52 |
* your servers document root. Also place the TreeMenu.js and the |
| 53 |
* images folder in the same place. Running the script should |
| 54 |
* then produce the tree. |
| 55 |
* |
| 56 |
* Thanks go to Chip Chapin (http://www.chipchapin.com) for many |
| 57 |
* excellent ideas and improvements. |
| 58 |
* |
| 59 |
* @author Richard Heyes <richard@php.net> |
| 60 |
* @author Harald Radi <harald.radi@nme.at> |
| 61 |
* @access public |
| 62 |
* @package HTML_TreeMenu |
| 63 |
*/ |
| 64 |
|
| 65 |
class HTML_TreeMenu |
| 66 |
{ |
| 67 |
/** |
| 68 |
* Indexed array of subnodes |
| 69 |
* @var array |
| 70 |
*/ |
| 71 |
var $items; |
| 72 |
|
| 73 |
/** |
| 74 |
* Constructor |
| 75 |
* |
| 76 |
* @access public |
| 77 |
*/ |
| 78 |
function HTML_TreeMenu() |
| 79 |
{ |
| 80 |
// Not much to do here :( |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* This function adds an item to the the tree. |
| 85 |
* |
| 86 |
* @access public |
| 87 |
* @param object $node The node to add. This object should be |
| 88 |
* a HTML_TreeNode object. |
| 89 |
* @return object Returns a reference to the new node inside |
| 90 |
* the tree. |
| 91 |
*/ |
| 92 |
function &addItem(&$node) |
| 93 |
{ |
| 94 |
$this->items[] = &$node; |
| 95 |
return $this->items[count($this->items) - 1]; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Import method for creating HTML_TreeMenu objects/structures |
| 100 |
* out of existing tree objects/structures. Currently supported |
| 101 |
* are Wolfram Kriesings' PEAR Tree class, and Richard Heyes' (me!) |
| 102 |
* Tree class (available here: http://www.phpguru.org/). This |
| 103 |
* method is intended to be used statically, eg: |
| 104 |
* $treeMenu = &HTML_TreeMenu::import($myTreeStructureObj); |
| 105 |
* |
| 106 |
* @param array $params An array of parameters that determine |
| 107 |
* how the import happens. This can consist of: |
| 108 |
* structure => The tree structure |
| 109 |
* type => The type of the structure, currently |
| 110 |
* can be either 'heyes' or 'kriesing' |
| 111 |
* nodeOptions => Default options for each node |
| 112 |
* |
| 113 |
* @return object The resulting HTML_TreeMenu object |
| 114 |
*/ |
| 115 |
function createFromStructure($params) |
| 116 |
{ |
| 117 |
if (!isset($params['nodeOptions'])) { |
| 118 |
$params['nodeOptions'] = array(); |
| 119 |
} |
| 120 |
|
| 121 |
switch (@$params['type']) { |
| 122 |
|
| 123 |
/** |
| 124 |
* Wolfram Kriesings' PEAR Tree class |
| 125 |
*/ |
| 126 |
case 'kriesing': |
| 127 |
$className = strtolower(get_class($params['structure']->dataSourceClass)); |
| 128 |
$isXMLStruct = strpos($className,'_xml') !== false ? true : false; |
| 129 |
|
| 130 |
// Get the entire tree, the $nodes are sorted like in the tree view |
| 131 |
// from top to bottom, so we can easily put them in the nodes |
| 132 |
$nodes = $params['structure']->getNode(); |
| 133 |
|
| 134 |
// Make a new menu and fill it with the values from the tree |
| 135 |
$treeMenu = new HTML_TreeMenu(); |
| 136 |
$curNode[0] = &$treeMenu; // we need the current node as the reference to the |
| 137 |
|
| 138 |
foreach ( $nodes as $aNode ) { |
| 139 |
$events = array(); |
| 140 |
$data = array(); |
| 141 |
|
| 142 |
// In an XML, all the attributes are saved in an array, but since they might be |
| 143 |
// used as the parameters, we simply extract them here if we handle an XML-structure |
| 144 |
if ( $isXMLStruct && sizeof($aNode['attributes']) ){ |
| 145 |
foreach ( $aNode['attributes'] as $key=>$val ) { |
| 146 |
if ( !$aNode[$key] ) { // dont overwrite existing values |
| 147 |
$aNode[$key] = $val; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Process all the data that are saved in $aNode and put them in the data and/or events array |
| 153 |
foreach ( $aNode as $key=>$val ) { |
| 154 |
if ( !is_array($val) ) { |
| 155 |
// Dont get the recursive data in here! they are always arrays |
| 156 |
if ( substr($key,0,2) == 'on' ){ // get the events |
| 157 |
$events[$key] = $val; |
| 158 |
} |
| 159 |
|
| 160 |
// I put it in data too, so in case an options starts with 'on' its also passed to the node ... not too cool i know |
| 161 |
$data[$key] = $val; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Normally the text is in 'name' in the Tree class, so we check both but 'text' is used if found |
| 166 |
$data['text'] = $aNode['text'] ? $aNode['text'] : $aNode['name']; |
| 167 |
|
| 168 |
// Add the item to the proper node |
| 169 |
$thisNode = &$curNode[$aNode['level']]->addItem( new HTML_TreeNode( $data , $events ) ); |
| 170 |
$curNode[$aNode['level']+1] = &$thisNode; |
| 171 |
} |
| 172 |
break; |
| 173 |
|
| 174 |
/** |
| 175 |
* Richard Heyes' (me!) Tree class |
| 176 |
*/ |
| 177 |
case 'heyes': |
| 178 |
default: |
| 179 |
// Need to create a HTML_TreeMenu object ? |
| 180 |
if (!isset($params['treeMenu'])) { |
| 181 |
$treeMenu = &new HTML_TreeMenu(); |
| 182 |
} else { |
| 183 |
$treeMenu = &$params['treeMenu']; |
| 184 |
} |
| 185 |
|
| 186 |
// Loop thru the trees nodes |
| 187 |
foreach ($params['structure']->nodes->nodes as $node) { |
| 188 |
$tag = $node->getTag(); |
| 189 |
$parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $tag))); |
| 190 |
|
| 191 |
// Recurse ? |
| 192 |
if (!empty($node->nodes->nodes)) { |
| 193 |
$recurseParams['structure'] = $node; |
| 194 |
$recurseParams['nodeOptions'] = $params['nodeOptions']; |
| 195 |
$recurseParams['treeMenu'] = &$parentNode; |
| 196 |
HTML_TreeMenu::createFromStructure($recurseParams); |
| 197 |
} |
| 198 |
} |
| 199 |
break; |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
return $treeMenu; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Creates a treeMenu from XML. The structure of your XML should be |
| 208 |
* like so: |
| 209 |
* |
| 210 |
* <treemenu> |
| 211 |
* <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
| 212 |
* <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
| 213 |
* <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
| 214 |
* </node> |
| 215 |
* <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
| 216 |
* </treemenu> |
| 217 |
* |
| 218 |
* Any of the options you can supply to the HTML_TreeNode constructor can be supplied as |
| 219 |
* attributes to the <node> tag. If there are no subnodes for a particular node, you can |
| 220 |
* use the XML shortcut <node ... /> instead of <node ... ></node>. The $xml argument can |
| 221 |
* be either the XML as a string, or an pre-created XML_Tree object. Also, this method |
| 222 |
* REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not |
| 223 |
* been include()ed or require()ed this method will die(). |
| 224 |
* |
| 225 |
* @param mixed $xml This can be either a string containing the XML, or an XML_Tree object |
| 226 |
* (the PEAR::XML_Tree package). |
| 227 |
* @return object The HTML_TreeMenu object |
| 228 |
*/ |
| 229 |
function createFromXML($xml) |
| 230 |
{ |
| 231 |
if (!class_exists('Tree')) { |
| 232 |
die('Could not find Tree class'); |
| 233 |
} |
| 234 |
|
| 235 |
// Supplied $xml is a string |
| 236 |
if (is_string($xml)) { |
| 237 |
require_once('XML/Tree.php'); |
| 238 |
$xmlTree = &new XML_Tree(); |
| 239 |
$xmlTree->getTreeFromString($xml); |
| 240 |
|
| 241 |
// Supplied $xml is an XML_Tree object |
| 242 |
} else { |
| 243 |
$xmlTree = $xml; |
| 244 |
} |
| 245 |
|
| 246 |
// Now process the XML_Tree object, setting the XML attributes |
| 247 |
// to be the tag data (with out the XML tag name or contents). |
| 248 |
$treeStructure = Tree::createFromXMLTree($xmlTree, true); |
| 249 |
$treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); |
| 250 |
|
| 251 |
|
| 252 |
return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); |
| 253 |
} |
| 254 |
} // HTML_TreeMenu |
| 255 |
|
| 256 |
|
| 257 |
/** |
| 258 |
* HTML_TreeNode class |
| 259 |
* |
| 260 |
* This class is supplementary to the above and provides a way to |
| 261 |
* add nodes to the tree. A node can have other nodes added to it. |
| 262 |
* |
| 263 |
* @author Richard Heyes <richard@php.net> |
| 264 |
* @author Harald Radi <harald.radi@nme.at> |
| 265 |
* @access public |
| 266 |
* @package HTML_TreeMenu |
| 267 |
*/ |
| 268 |
class HTML_TreeNode |
| 269 |
{ |
| 270 |
/** |
| 271 |
* The text for this node. |
| 272 |
* @var string |
| 273 |
*/ |
| 274 |
var $text; |
| 275 |
|
| 276 |
/** |
| 277 |
* The link for this node. |
| 278 |
* @var string |
| 279 |
*/ |
| 280 |
var $link; |
| 281 |
|
| 282 |
/** |
| 283 |
* The icon for this node. |
| 284 |
* @var string |
| 285 |
*/ |
| 286 |
var $icon; |
| 287 |
|
| 288 |
/** |
| 289 |
* The icon to show when expanded for this node. |
| 290 |
* @var string |
| 291 |
*/ |
| 292 |
var $expandedIcon; |
| 293 |
|
| 294 |
/** |
| 295 |
* The css class for this node |
| 296 |
* @var string |
| 297 |
*/ |
| 298 |
var $cssClass; |
| 299 |
|
| 300 |
/** |
| 301 |
* The link target for this node |
| 302 |
* @var string |
| 303 |
*/ |
| 304 |
var $linkTarget; |
| 305 |
|
| 306 |
/** |
| 307 |
* Indexed array of subnodes |
| 308 |
* @var array |
| 309 |
*/ |
| 310 |
var $items; |
| 311 |
|
| 312 |
/** |
| 313 |
* Whether this node is expanded or not |
| 314 |
* @var bool |
| 315 |
*/ |
| 316 |
var $expanded; |
| 317 |
|
| 318 |
/** |
| 319 |
* Whether this node is dynamic or not |
| 320 |
* @var bool |
| 321 |
*/ |
| 322 |
var $isDynamic; |
| 323 |
|
| 324 |
/** |
| 325 |
* Should this node be made visible? |
| 326 |
* @var bool |
| 327 |
*/ |
| 328 |
var $ensureVisible; |
| 329 |
|
| 330 |
/** |
| 331 |
* The parent node. Null if top level |
| 332 |
* @var object |
| 333 |
*/ |
| 334 |
var $parent; |
| 335 |
|
| 336 |
/** |
| 337 |
* Javascript event handlers; |
| 338 |
* @var array |
| 339 |
*/ |
| 340 |
var $events; |
| 341 |
|
| 342 |
/** |
| 343 |
* Constructor |
| 344 |
* |
| 345 |
* @access public |
| 346 |
* @param array $options An array of options which you can pass to change |
| 347 |
* the way this node looks/acts. This can consist of: |
| 348 |
* o text The title of the node, defaults to blank |
| 349 |
* o link The link for the node, defaults to blank |
| 350 |
* o icon The icon for the node, defaults to blank |
| 351 |
* o expandedIcon The icon to show when the node is expanded |
| 352 |
* o class The CSS class for this node, defaults to blank |
| 353 |
* o expanded The default expanded status of this node, defaults to false |
| 354 |
* This doesn't affect non dynamic presentation types |
| 355 |
* o linkTarget Target for the links. Defaults to linkTarget of the |
| 356 |
* HTML_TreeMenu_Presentation. |
| 357 |
* o isDynamic If this node is dynamic or not. Only affects |
| 358 |
* certain presentation types. |
| 359 |
* o ensureVisible If true this node will be made visible despite the expanded |
| 360 |
* settings, and client side persistence. Will not affect |
| 361 |
* some presentation styles, such as Listbox. Default is false |
| 362 |
* @param array $events An array of javascript events and the corresponding event handlers. |
| 363 |
* Additionally to the standard javascript events you can specify handlers |
| 364 |
* for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired |
| 365 |
* whenever a node is collapsed and/or expanded. |
| 366 |
*/ |
| 367 |
function HTML_TreeNode($options = array(), $events = array()) |
| 368 |
{ |
| 369 |
$this->text = ''; |
| 370 |
$this->link = ''; |
| 371 |
$this->icon = ''; |
| 372 |
$this->expandedIcon = ''; |
| 373 |
$this->cssClass = ''; |
| 374 |
$this->expanded = false; |
| 375 |
$this->isDynamic = true; |
| 376 |
$this->ensureVisible = false; |
| 377 |
$this->linkTarget = null; |
| 378 |
|
| 379 |
$this->parent = null; |
| 380 |
$this->events = $events; |
| 381 |
|
| 382 |
foreach ($options as $option => $value) { |
| 383 |
$this->$option = $value; |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Allows setting of various parameters after the initial |
| 389 |
* constructor call. Possible options you can set are: |
| 390 |
* o text |
| 391 |
* o link |
| 392 |
* o icon |
| 393 |
* o cssClass |
| 394 |
* o expanded |
| 395 |
* o isDynamic |
| 396 |
* o ensureVisible |
| 397 |
* ie The same options as in the constructor |
| 398 |
* |
| 399 |
* @access public |
| 400 |
* @param string $option Option to set |
| 401 |
* @param string $value Value to set the option to |
| 402 |
*/ |
| 403 |
function setOption($option, $value) |
| 404 |
{ |
| 405 |
$this->$option = $value; |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Adds a new subnode to this node. |
| 410 |
* |
| 411 |
* @access public |
| 412 |
* @param object $node The new node |
| 413 |
*/ |
| 414 |
function &addItem(&$node) |
| 415 |
{ |
| 416 |
$node->parent = &$this; |
| 417 |
$this->items[] = &$node; |
| 418 |
|
| 419 |
/** |
| 420 |
* If the subnode has ensureVisible set it needs |
| 421 |
* to be handled, and all parents set accordingly. |
| 422 |
*/ |
| 423 |
if ($node->ensureVisible) { |
| 424 |
$this->_ensureVisible(); |
| 425 |
} |
| 426 |
|
| 427 |
return $this->items[count($this->items) - 1]; |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Private function to handle ensureVisible stuff |
| 432 |
* |
| 433 |
* @access private |
| 434 |
*/ |
| 435 |
function _ensureVisible() |
| 436 |
{ |
| 437 |
$this->ensureVisible = true; |
| 438 |
$this->expanded = true; |
| 439 |
|
| 440 |
if (!is_null($this->parent)) { |
| 441 |
$this->parent->_ensureVisible(); |
| 442 |
} |
| 443 |
} |
| 444 |
} // HTML_TreeNode |
| 445 |
|
| 446 |
|
| 447 |
/** |
| 448 |
* HTML_TreeMenu_Presentation class |
| 449 |
* |
| 450 |
* Base class for other presentation classes to |
| 451 |
* inherit from. |
| 452 |
*/ |
| 453 |
class HTML_TreeMenu_Presentation |
| 454 |
{ |
| 455 |
/** |
| 456 |
* The TreeMenu structure |
| 457 |
* @var object |
| 458 |
*/ |
| 459 |
var $menu; |
| 460 |
|
| 461 |
/** |
| 462 |
* Base constructor simply sets the menu object |
| 463 |
* |
| 464 |
* @param object $structure The menu structure |
| 465 |
*/ |
| 466 |
function HTML_TreeMenu_Presentation(&$structure) |
| 467 |
{ |
| 468 |
$this->menu = &$structure; |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* Prints the HTML generated by the toHTML() method. |
| 473 |
* toHTML() must therefore be defined by the derived |
| 474 |
* class. |
| 475 |
* |
| 476 |
* @access public |
| 477 |
* @param array Options to set. Any options taken by |
| 478 |
* the presentation class can be specified |
| 479 |
* here. |
| 480 |
*/ |
| 481 |
function printMenu($options = array()) |
| 482 |
{ |
| 483 |
foreach ($options as $option => $value) { |
| 484 |
$this->$option = $value; |
| 485 |
} |
| 486 |
|
| 487 |
echo $this->toHTML(); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
/** |
| 493 |
* HTML_TreeMenu_DHTML class |
| 494 |
* |
| 495 |
* This class is a presentation class for the tree structure |
| 496 |
* created using the TreeMenu/TreeNode. It presents the |
| 497 |
* traditional tree, static for browsers that can't handle |
| 498 |
* the DHTML. |
| 499 |
*/ |
| 500 |
class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation |
| 501 |
{ |
| 502 |
/** |
| 503 |
* Dynamic status of the treemenu. If true (default) this has no effect. If |
| 504 |
* false it will override all dynamic status vars and set the menu to be |
| 505 |
* fully expanded an non-dynamic. |
| 506 |
*/ |
| 507 |
var $isDynamic; |
| 508 |
|
| 509 |
/** |
| 510 |
* Path to the images |
| 511 |
* @var string |
| 512 |
*/ |
| 513 |
var $images; |
| 514 |
|
| 515 |
/** |
| 516 |
* Target for the links generated |
| 517 |
* @var string |
| 518 |
*/ |
| 519 |
var $linkTarget; |
| 520 |
|
| 521 |
/** |
| 522 |
* Whether to use clientside persistence or not |
| 523 |
* @var bool |
| 524 |
*/ |
| 525 |
var $userPersistence; |
| 526 |
|
| 527 |
/** |
| 528 |
* The default CSS class for the nodes |
| 529 |
*/ |
| 530 |
var $defaultClass; |
| 531 |
|
| 532 |
/** |
| 533 |
* Whether to skip first level branch images |
| 534 |
* @var bool |
| 535 |
*/ |
| 536 |
var $noTopLevelImages; |
| 537 |
|
| 538 |
/** |
| 539 |
* Constructor, takes the tree structure as |
| 540 |
* an argument and an array of options which |
| 541 |
* can consist of: |
| 542 |
* o images - The path to the images folder. Defaults to "images" |
| 543 |
* o linkTarget - The target for the link. Defaults to "_self" |
| 544 |
* o defaultClass - The default CSS class to apply to a node. Default is none. |
| 545 |
* o usePersistence - Whether to use clientside persistence. This persistence |
| 546 |
* is achieved using cookies. Default is true. |
| 547 |
* o noTopLevelImages - Whether to skip displaying the first level of images if |
| 548 |
* there is multiple top level branches. |
| 549 |
* |
| 550 |
* And also a boolean for whether the entire tree is dynamic or not. |
| 551 |
* This overrides any perNode dynamic settings. |
| 552 |
* |
| 553 |
* @param object $structure The menu structure |
| 554 |
* @param array $options Array of options |
| 555 |
* @param bool $isDynamic Whether the tree is dynamic or not |
| 556 |
*/ |
| 557 |
function HTML_TreeMenu_DHTML(&$structure, $options = array(), $isDynamic = true) |
| 558 |
{ |
| 559 |
$this->HTML_TreeMenu_Presentation($structure); |
| 560 |
$this->isDynamic = $isDynamic; |
| 561 |
|
| 562 |
// Defaults |
| 563 |
$this->images = 'images'; |
| 564 |
$this->linkTarget = '_self'; |
| 565 |
$this->defaultClass = ''; |
| 566 |
$this->usePersistence = true; |
| 567 |
$this->noTopLevelImages = false; |
| 568 |
|
| 569 |
foreach ($options as $option => $value) { |
| 570 |
$this->$option = $value; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* Returns the HTML for the menu. This method can be |
| 576 |
* used instead of printMenu() to use the menu system |
| 577 |
* with a template system. |
| 578 |
* |
| 579 |
* @access public |
| 580 |
* @return string The HTML for the menu |
| 581 |
*/ |
| 582 |
function toHTML() |
| 583 |
{ |
| 584 |
static $count = 0; |
| 585 |
$menuObj = 'objTreeMenu_' . ++$count; |
| 586 |
|
| 587 |
$html = "\n"; |
| 588 |
$html .= '<script language="javascript" type="text/javascript">' . "\n\t"; |
| 589 |
$html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);', |
| 590 |
$menuObj, |
| 591 |
$this->images, |
| 592 |
$menuObj, |
| 593 |
$this->linkTarget, |
| 594 |
$this->defaultClass, |
| 595 |
$this->usePersistence ? 'true' : 'false', |
| 596 |
$this->noTopLevelImages ? 'true' : 'false'); |
| 597 |
|
| 598 |
$html .= "\n"; |
| 599 |
|
| 600 |
/** |
| 601 |
* Loop through subnodes |
| 602 |
*/ |
| 603 |
if (isset($this->menu->items)) { |
| 604 |
for ($i=0; $i<count($this->menu->items); $i++) { |
| 605 |
$html .= $this->_nodeToHTML($this->menu->items[$i], $menuObj); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
$html .= sprintf("\n\t%s.drawMenu();", $menuObj); |
| 610 |
if ($this->usePersistence && $this->isDynamic) { |
| 611 |
$html .= sprintf("\n\t%s.resetBranches();", $menuObj); |
| 612 |
} |
| 613 |
$html .= "\n</script>"; |
| 614 |
|
| 615 |
return $html; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* Prints a node of the menu |
| 620 |
* |
| 621 |
* @access private |
| 622 |
*/ |
| 623 |
function _nodeToHTML($nodeObj, $prefix, $return = 'newNode') |
| 624 |
{ |
| 625 |
$expanded = $this->isDynamic ? ($nodeObj->expanded ? 'true' : 'false') : 'true'; |
| 626 |
$isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false'; |
| 627 |
$html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n", |
| 628 |
$return, |
| 629 |
$prefix, |
| 630 |
$nodeObj->text, |
| 631 |
!empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null', |
| 632 |
!empty($nodeObj->link) ? "'" . $nodeObj->link . "'" : 'null', |
| 633 |
$expanded, |
| 634 |
$isDynamic, |
| 635 |
$nodeObj->cssClass, |
| 636 |
$nodeObj->linkTarget, |
| 637 |
!empty($nodeObj->expandedIcon) ? "'" . $nodeObj->expandedIcon . "'" : 'null'); |
| 638 |
|
| 639 |
foreach ($nodeObj->events as $event => $handler) { |
| 640 |
$html .= sprintf("\t %s.setEvent('%s', '%s');\n", |
| 641 |
$return, |
| 642 |
$event, |
| 643 |
str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler)); |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Loop through subnodes |
| 648 |
*/ |
| 649 |
if (!empty($nodeObj->items)) { |
| 650 |
for ($i=0; $i<count($nodeObj->items); $i++) { |
| 651 |
$html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1)); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
return $html; |
| 656 |
} |
| 657 |
} // End class HTML_TreeMenu_DHTML |
| 658 |
|
| 659 |
|
| 660 |
/** |
| 661 |
* HTML_TreeMenu_Listbox class |
| 662 |
* |
| 663 |
* This class presents the menu as a listbox |
| 664 |
*/ |
| 665 |
class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation |
| 666 |
{ |
| 667 |
/** |
| 668 |
* The text that is displayed in the first option |
| 669 |
* @var string |
| 670 |
*/ |
| 671 |
var $promoText; |
| 672 |
|
| 673 |
/** |
| 674 |
* The character used for indentation |
| 675 |
* @var string |
| 676 |
*/ |
| 677 |
var $indentChar; |
| 678 |
|
| 679 |
/** |
| 680 |
* How many of the indent chars to use |
| 681 |
* per indentation level |
| 682 |
* @var integer |
| 683 |
*/ |
| 684 |
var $indentNum; |
| 685 |
|
| 686 |
/** |
| 687 |
* Target for the links generated |
| 688 |
* @var string |
| 689 |
*/ |
| 690 |
var $linkTarget; |
| 691 |
|
| 692 |
/** |
| 693 |
* Constructor |
| 694 |
* |
| 695 |
* @param object $structure The menu structure |
| 696 |
* @param array $options Options whic affect the display of the listbox. |
| 697 |
* These can consist of: |
| 698 |
* o promoText The text that appears at the the top of the listbox |
| 699 |
* Defaults to "Select..." |
| 700 |
* o indentChar The character to use for indenting the nodes |
| 701 |
* Defaults to " " |
| 702 |
* o indentNum How many of the indentChars to use per indentation level |
| 703 |
* Defaults to 2 |
| 704 |
* o linkTarget Target for the links. Defaults to "_self" |
| 705 |
* o submitText Text for the submit button. Defaults to "Go" |
| 706 |
*/ |
| 707 |
function HTML_TreeMenu_Listbox($structure, $options = array()) |
| 708 |
{ |
| 709 |
$this->HTML_TreeMenu_Presentation($structure); |
| 710 |
|
| 711 |
$this->promoText = 'Select...'; |
| 712 |
$this->indentChar = ' '; |
| 713 |
$this->indentNum = 2; |
| 714 |
$this->linkTarget = '_self'; |
| 715 |
$this->submitText = 'Go'; |
| 716 |
|
| 717 |
foreach ($options as $option => $value) { |
| 718 |
$this->$option = $value; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Returns the HTML generated |
| 724 |
*/ |
| 725 |
function toHTML() |
| 726 |
{ |
| 727 |
static $count = 0; |
| 728 |
$nodeHTML = ''; |
| 729 |
|
| 730 |
/** |
| 731 |
* Loop through subnodes |
| 732 |
*/ |
| 733 |
if (isset($this->menu->items)) { |
| 734 |
for ($i=0; $i<count($this->menu->items); $i++) { |
| 735 |
$nodeHTML .= $this->_nodeToHTML($this->menu->items[$i]); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
return sprintf('<form target="%s" action="" onsubmit="var link = this.%s.options[this.%s.selectedIndex].value; if (link) {this.action = link; return true} else return false"><select name="%s"><option value="">%s</option>%s</select> <input type="submit" value="%s" /></form>', |
| 740 |
$this->linkTarget, |
| 741 |
'HTML_TreeMenu_Listbox_' . ++$count, |
| 742 |
'HTML_TreeMenu_Listbox_' . $count, |
| 743 |
'HTML_TreeMenu_Listbox_' . $count, |
| 744 |
$this->promoText, |
| 745 |
$nodeHTML, |
| 746 |
$this->submitText); |
| 747 |
} |
| 748 |
|
| 749 |
/** |
| 750 |
* Returns HTML for a single node |
| 751 |
* |
| 752 |
* @access private |
| 753 |
*/ |
| 754 |
function _nodeToHTML($node, $prefix = '') |
| 755 |
{ |
| 756 |
$html = sprintf('<option value="%s">%s%s</option>', $node->link, $prefix, $node->text); |
| 757 |
|
| 758 |
/** |
| 759 |
* Loop through subnodes |
| 760 |
*/ |
| 761 |
if (isset($node->items)) { |
| 762 |
for ($i=0; $i<count($node->items); $i++) { |
| 763 |
$html .= $this->_nodeToHTML($node->items[$i], $prefix . str_repeat($this->indentChar, $this->indentNum)); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
return $html; |
| 768 |
} |
| 769 |
} // End class HTML_TreeMenu_Listbox |
| 770 |
?> |