1 |
joko |
1.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 |
joko |
1.2 |
// Id: TreeMenu.php,v 1.18 2003/12/20 13:10:59 richard Exp |
37 |
|
|
// $Id: TreeMenu.php,v 1.18 2003/12/20 13:10:59 richard Exp $ |
38 |
joko |
1.1 |
|
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 |
joko |
1.2 |
* $treeMenu = &HTML_TreeMenu::createFromStructure($myTreeStructureObj); |
105 |
joko |
1.1 |
* |
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 |
joko |
1.2 |
* Richard Heyes' (me!) second (array based) Tree class |
176 |
|
|
*/ |
177 |
|
|
case 'heyes_array': |
178 |
|
|
// Need to create a HTML_TreeMenu object ? |
179 |
|
|
if (!isset($params['treeMenu'])) { |
180 |
|
|
$treeMenu = &new HTML_TreeMenu(); |
181 |
|
|
$parentID = 0; |
182 |
|
|
} else { |
183 |
|
|
$treeMenu = &$params['treeMenu']; |
184 |
|
|
$parentID = $params['parentID']; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
// Loop thru the trees nodes |
188 |
|
|
foreach ($params['structure']->getChildren($parentID) as $nodeID) { |
189 |
|
|
$data = $params['structure']->getData($nodeID); |
190 |
|
|
$parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $data))); |
191 |
|
|
|
192 |
|
|
// Recurse ? |
193 |
|
|
if ($params['structure']->hasChildren($nodeID)) { |
194 |
|
|
$recurseParams['type'] = 'heyes_array'; |
195 |
|
|
$recurseParams['parentID'] = $nodeID; |
196 |
|
|
$recurseParams['nodeOptions'] = $params['nodeOptions']; |
197 |
|
|
$recurseParams['structure'] = &$params['structure']; |
198 |
|
|
$recurseParams['treeMenu'] = &$parentNode; |
199 |
|
|
HTML_TreeMenu::createFromStructure($recurseParams); |
200 |
|
|
} |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
break; |
204 |
|
|
|
205 |
|
|
/** |
206 |
|
|
* Richard Heyes' (me!) original OO based Tree class |
207 |
joko |
1.1 |
*/ |
208 |
|
|
case 'heyes': |
209 |
|
|
default: |
210 |
|
|
// Need to create a HTML_TreeMenu object ? |
211 |
|
|
if (!isset($params['treeMenu'])) { |
212 |
|
|
$treeMenu = &new HTML_TreeMenu(); |
213 |
|
|
} else { |
214 |
|
|
$treeMenu = &$params['treeMenu']; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
// Loop thru the trees nodes |
218 |
|
|
foreach ($params['structure']->nodes->nodes as $node) { |
219 |
|
|
$tag = $node->getTag(); |
220 |
|
|
$parentNode = &$treeMenu->addItem(new HTML_TreeNode(array_merge($params['nodeOptions'], $tag))); |
221 |
|
|
|
222 |
|
|
// Recurse ? |
223 |
|
|
if (!empty($node->nodes->nodes)) { |
224 |
|
|
$recurseParams['structure'] = $node; |
225 |
|
|
$recurseParams['nodeOptions'] = $params['nodeOptions']; |
226 |
|
|
$recurseParams['treeMenu'] = &$parentNode; |
227 |
|
|
HTML_TreeMenu::createFromStructure($recurseParams); |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
break; |
231 |
|
|
|
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
return $treeMenu; |
235 |
|
|
} |
236 |
joko |
1.2 |
|
237 |
|
|
/** |
238 |
joko |
1.1 |
* Creates a treeMenu from XML. The structure of your XML should be |
239 |
joko |
1.2 |
* like so: |
240 |
|
|
* |
241 |
|
|
* <treemenu> |
242 |
|
|
* <node text="First node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
243 |
|
|
* <node text="Second node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
244 |
|
|
* <node text="Sub node" icon="folder.gif" expandedIcon="folder-expanded.gif" /> |
245 |
|
|
* </node> |
246 |
|
|
* <node text="Third node" icon="folder.gif" expandedIcon="folder-expanded.gif"> |
247 |
|
|
* </treemenu> |
248 |
|
|
* |
249 |
|
|
* Any of the options you can supply to the HTML_TreeNode constructor can be supplied as |
250 |
|
|
* attributes to the <node> tag. If there are no subnodes for a particular node, you can |
251 |
|
|
* use the XML shortcut <node ... /> instead of <node ... ></node>. The $xml argument can |
252 |
|
|
* be either the XML as a string, or an pre-created XML_Tree object. Also, this method |
253 |
|
|
* REQUIRES my own Tree class to work (http://phpguru.org/tree.html). If this has not |
254 |
|
|
* been include()ed or require()ed this method will die(). |
255 |
|
|
* |
256 |
|
|
* @param mixed $xml This can be either a string containing the XML, or an XML_Tree object |
257 |
|
|
* (the PEAR::XML_Tree package). |
258 |
|
|
* @return object The HTML_TreeMenu object |
259 |
|
|
*/ |
260 |
|
|
function createFromXML($xml) |
261 |
|
|
{ |
262 |
|
|
if (!class_exists('Tree')) { |
263 |
|
|
die('Could not find Tree class'); |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
// Supplied $xml is a string |
267 |
|
|
if (is_string($xml)) { |
268 |
|
|
require_once('XML/Tree.php'); |
269 |
|
|
$xmlTree = &new XML_Tree(); |
270 |
|
|
$xmlTree->getTreeFromString($xml); |
271 |
|
|
|
272 |
|
|
// Supplied $xml is an XML_Tree object |
273 |
|
|
} else { |
274 |
|
|
$xmlTree = $xml; |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
// Now process the XML_Tree object, setting the XML attributes |
278 |
|
|
// to be the tag data (with out the XML tag name or contents). |
279 |
|
|
$treeStructure = Tree::createFromXMLTree($xmlTree, true); |
280 |
|
|
$treeStructure->nodes->traverse(create_function('&$node', '$tagData = $node->getTag(); $node->setTag($tagData["attributes"]);')); |
281 |
|
|
|
282 |
|
|
|
283 |
|
|
return HTML_TreeMenu::createFromStructure(array('structure' => $treeStructure)); |
284 |
|
|
} |
285 |
joko |
1.1 |
} // HTML_TreeMenu |
286 |
|
|
|
287 |
|
|
|
288 |
|
|
/** |
289 |
|
|
* HTML_TreeNode class |
290 |
|
|
* |
291 |
|
|
* This class is supplementary to the above and provides a way to |
292 |
|
|
* add nodes to the tree. A node can have other nodes added to it. |
293 |
|
|
* |
294 |
|
|
* @author Richard Heyes <richard@php.net> |
295 |
|
|
* @author Harald Radi <harald.radi@nme.at> |
296 |
|
|
* @access public |
297 |
|
|
* @package HTML_TreeMenu |
298 |
|
|
*/ |
299 |
|
|
class HTML_TreeNode |
300 |
|
|
{ |
301 |
|
|
/** |
302 |
|
|
* The text for this node. |
303 |
|
|
* @var string |
304 |
|
|
*/ |
305 |
|
|
var $text; |
306 |
|
|
|
307 |
|
|
/** |
308 |
|
|
* The link for this node. |
309 |
|
|
* @var string |
310 |
|
|
*/ |
311 |
|
|
var $link; |
312 |
|
|
|
313 |
|
|
/** |
314 |
|
|
* The icon for this node. |
315 |
|
|
* @var string |
316 |
|
|
*/ |
317 |
|
|
var $icon; |
318 |
|
|
|
319 |
|
|
/** |
320 |
|
|
* The icon to show when expanded for this node. |
321 |
|
|
* @var string |
322 |
|
|
*/ |
323 |
|
|
var $expandedIcon; |
324 |
|
|
|
325 |
|
|
/** |
326 |
|
|
* The css class for this node |
327 |
|
|
* @var string |
328 |
|
|
*/ |
329 |
|
|
var $cssClass; |
330 |
|
|
|
331 |
|
|
/** |
332 |
|
|
* The link target for this node |
333 |
|
|
* @var string |
334 |
|
|
*/ |
335 |
|
|
var $linkTarget; |
336 |
|
|
|
337 |
|
|
/** |
338 |
|
|
* Indexed array of subnodes |
339 |
|
|
* @var array |
340 |
|
|
*/ |
341 |
|
|
var $items; |
342 |
|
|
|
343 |
|
|
/** |
344 |
|
|
* Whether this node is expanded or not |
345 |
|
|
* @var bool |
346 |
|
|
*/ |
347 |
|
|
var $expanded; |
348 |
|
|
|
349 |
|
|
/** |
350 |
|
|
* Whether this node is dynamic or not |
351 |
|
|
* @var bool |
352 |
|
|
*/ |
353 |
|
|
var $isDynamic; |
354 |
|
|
|
355 |
|
|
/** |
356 |
|
|
* Should this node be made visible? |
357 |
|
|
* @var bool |
358 |
|
|
*/ |
359 |
|
|
var $ensureVisible; |
360 |
|
|
|
361 |
|
|
/** |
362 |
|
|
* The parent node. Null if top level |
363 |
|
|
* @var object |
364 |
|
|
*/ |
365 |
|
|
var $parent; |
366 |
|
|
|
367 |
|
|
/** |
368 |
|
|
* Javascript event handlers; |
369 |
|
|
* @var array |
370 |
|
|
*/ |
371 |
|
|
var $events; |
372 |
|
|
|
373 |
|
|
/** |
374 |
|
|
* Constructor |
375 |
|
|
* |
376 |
|
|
* @access public |
377 |
|
|
* @param array $options An array of options which you can pass to change |
378 |
|
|
* the way this node looks/acts. This can consist of: |
379 |
|
|
* o text The title of the node, defaults to blank |
380 |
|
|
* o link The link for the node, defaults to blank |
381 |
|
|
* o icon The icon for the node, defaults to blank |
382 |
|
|
* o expandedIcon The icon to show when the node is expanded |
383 |
joko |
1.2 |
* o cssClass The CSS class for this node, defaults to blank |
384 |
joko |
1.1 |
* o expanded The default expanded status of this node, defaults to false |
385 |
|
|
* This doesn't affect non dynamic presentation types |
386 |
|
|
* o linkTarget Target for the links. Defaults to linkTarget of the |
387 |
|
|
* HTML_TreeMenu_Presentation. |
388 |
|
|
* o isDynamic If this node is dynamic or not. Only affects |
389 |
|
|
* certain presentation types. |
390 |
|
|
* o ensureVisible If true this node will be made visible despite the expanded |
391 |
|
|
* settings, and client side persistence. Will not affect |
392 |
|
|
* some presentation styles, such as Listbox. Default is false |
393 |
|
|
* @param array $events An array of javascript events and the corresponding event handlers. |
394 |
|
|
* Additionally to the standard javascript events you can specify handlers |
395 |
|
|
* for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired |
396 |
|
|
* whenever a node is collapsed and/or expanded. |
397 |
|
|
*/ |
398 |
|
|
function HTML_TreeNode($options = array(), $events = array()) |
399 |
|
|
{ |
400 |
|
|
$this->text = ''; |
401 |
|
|
$this->link = ''; |
402 |
|
|
$this->icon = ''; |
403 |
|
|
$this->expandedIcon = ''; |
404 |
|
|
$this->cssClass = ''; |
405 |
|
|
$this->expanded = false; |
406 |
|
|
$this->isDynamic = true; |
407 |
|
|
$this->ensureVisible = false; |
408 |
|
|
$this->linkTarget = null; |
409 |
|
|
|
410 |
|
|
$this->parent = null; |
411 |
|
|
$this->events = $events; |
412 |
|
|
|
413 |
|
|
foreach ($options as $option => $value) { |
414 |
|
|
$this->$option = $value; |
415 |
|
|
} |
416 |
|
|
} |
417 |
|
|
|
418 |
|
|
/** |
419 |
|
|
* Allows setting of various parameters after the initial |
420 |
|
|
* constructor call. Possible options you can set are: |
421 |
|
|
* o text |
422 |
|
|
* o link |
423 |
|
|
* o icon |
424 |
|
|
* o cssClass |
425 |
|
|
* o expanded |
426 |
|
|
* o isDynamic |
427 |
|
|
* o ensureVisible |
428 |
|
|
* ie The same options as in the constructor |
429 |
|
|
* |
430 |
|
|
* @access public |
431 |
|
|
* @param string $option Option to set |
432 |
|
|
* @param string $value Value to set the option to |
433 |
|
|
*/ |
434 |
|
|
function setOption($option, $value) |
435 |
|
|
{ |
436 |
|
|
$this->$option = $value; |
437 |
|
|
} |
438 |
|
|
|
439 |
|
|
/** |
440 |
|
|
* Adds a new subnode to this node. |
441 |
|
|
* |
442 |
|
|
* @access public |
443 |
|
|
* @param object $node The new node |
444 |
|
|
*/ |
445 |
|
|
function &addItem(&$node) |
446 |
|
|
{ |
447 |
|
|
$node->parent = &$this; |
448 |
|
|
$this->items[] = &$node; |
449 |
|
|
|
450 |
|
|
/** |
451 |
|
|
* If the subnode has ensureVisible set it needs |
452 |
|
|
* to be handled, and all parents set accordingly. |
453 |
|
|
*/ |
454 |
|
|
if ($node->ensureVisible) { |
455 |
|
|
$this->_ensureVisible(); |
456 |
|
|
} |
457 |
|
|
|
458 |
|
|
return $this->items[count($this->items) - 1]; |
459 |
|
|
} |
460 |
|
|
|
461 |
|
|
/** |
462 |
|
|
* Private function to handle ensureVisible stuff |
463 |
|
|
* |
464 |
|
|
* @access private |
465 |
|
|
*/ |
466 |
|
|
function _ensureVisible() |
467 |
|
|
{ |
468 |
|
|
$this->ensureVisible = true; |
469 |
|
|
$this->expanded = true; |
470 |
|
|
|
471 |
|
|
if (!is_null($this->parent)) { |
472 |
|
|
$this->parent->_ensureVisible(); |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
} // HTML_TreeNode |
476 |
|
|
|
477 |
|
|
|
478 |
|
|
/** |
479 |
|
|
* HTML_TreeMenu_Presentation class |
480 |
|
|
* |
481 |
|
|
* Base class for other presentation classes to |
482 |
|
|
* inherit from. |
483 |
|
|
*/ |
484 |
|
|
class HTML_TreeMenu_Presentation |
485 |
|
|
{ |
486 |
|
|
/** |
487 |
|
|
* The TreeMenu structure |
488 |
|
|
* @var object |
489 |
|
|
*/ |
490 |
|
|
var $menu; |
491 |
|
|
|
492 |
|
|
/** |
493 |
|
|
* Base constructor simply sets the menu object |
494 |
|
|
* |
495 |
|
|
* @param object $structure The menu structure |
496 |
|
|
*/ |
497 |
|
|
function HTML_TreeMenu_Presentation(&$structure) |
498 |
|
|
{ |
499 |
|
|
$this->menu = &$structure; |
500 |
|
|
} |
501 |
|
|
|
502 |
|
|
/** |
503 |
|
|
* Prints the HTML generated by the toHTML() method. |
504 |
|
|
* toHTML() must therefore be defined by the derived |
505 |
|
|
* class. |
506 |
|
|
* |
507 |
|
|
* @access public |
508 |
|
|
* @param array Options to set. Any options taken by |
509 |
|
|
* the presentation class can be specified |
510 |
|
|
* here. |
511 |
|
|
*/ |
512 |
|
|
function printMenu($options = array()) |
513 |
|
|
{ |
514 |
|
|
foreach ($options as $option => $value) { |
515 |
|
|
$this->$option = $value; |
516 |
|
|
} |
517 |
|
|
|
518 |
|
|
echo $this->toHTML(); |
519 |
|
|
} |
520 |
|
|
} |
521 |
|
|
|
522 |
|
|
|
523 |
|
|
/** |
524 |
|
|
* HTML_TreeMenu_DHTML class |
525 |
|
|
* |
526 |
|
|
* This class is a presentation class for the tree structure |
527 |
|
|
* created using the TreeMenu/TreeNode. It presents the |
528 |
|
|
* traditional tree, static for browsers that can't handle |
529 |
|
|
* the DHTML. |
530 |
|
|
*/ |
531 |
|
|
class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation |
532 |
|
|
{ |
533 |
|
|
/** |
534 |
|
|
* Dynamic status of the treemenu. If true (default) this has no effect. If |
535 |
|
|
* false it will override all dynamic status vars and set the menu to be |
536 |
|
|
* fully expanded an non-dynamic. |
537 |
|
|
*/ |
538 |
|
|
var $isDynamic; |
539 |
|
|
|
540 |
|
|
/** |
541 |
|
|
* Path to the images |
542 |
|
|
* @var string |
543 |
|
|
*/ |
544 |
|
|
var $images; |
545 |
|
|
|
546 |
|
|
/** |
547 |
|
|
* Target for the links generated |
548 |
|
|
* @var string |
549 |
|
|
*/ |
550 |
|
|
var $linkTarget; |
551 |
|
|
|
552 |
|
|
/** |
553 |
|
|
* Whether to use clientside persistence or not |
554 |
|
|
* @var bool |
555 |
|
|
*/ |
556 |
|
|
var $userPersistence; |
557 |
|
|
|
558 |
|
|
/** |
559 |
|
|
* The default CSS class for the nodes |
560 |
|
|
*/ |
561 |
|
|
var $defaultClass; |
562 |
|
|
|
563 |
|
|
/** |
564 |
|
|
* Whether to skip first level branch images |
565 |
|
|
* @var bool |
566 |
|
|
*/ |
567 |
|
|
var $noTopLevelImages; |
568 |
|
|
|
569 |
|
|
/** |
570 |
|
|
* Constructor, takes the tree structure as |
571 |
|
|
* an argument and an array of options which |
572 |
|
|
* can consist of: |
573 |
|
|
* o images - The path to the images folder. Defaults to "images" |
574 |
|
|
* o linkTarget - The target for the link. Defaults to "_self" |
575 |
|
|
* o defaultClass - The default CSS class to apply to a node. Default is none. |
576 |
|
|
* o usePersistence - Whether to use clientside persistence. This persistence |
577 |
|
|
* is achieved using cookies. Default is true. |
578 |
|
|
* o noTopLevelImages - Whether to skip displaying the first level of images if |
579 |
|
|
* there is multiple top level branches. |
580 |
joko |
1.2 |
* o maxDepth - The maximum depth of indentation. Useful for ensuring |
581 |
|
|
* deeply nested trees don't go way off to the right of your |
582 |
|
|
* page etc. Defaults to no limit. |
583 |
joko |
1.1 |
* |
584 |
|
|
* And also a boolean for whether the entire tree is dynamic or not. |
585 |
|
|
* This overrides any perNode dynamic settings. |
586 |
|
|
* |
587 |
|
|
* @param object $structure The menu structure |
588 |
|
|
* @param array $options Array of options |
589 |
|
|
* @param bool $isDynamic Whether the tree is dynamic or not |
590 |
|
|
*/ |
591 |
|
|
function HTML_TreeMenu_DHTML(&$structure, $options = array(), $isDynamic = true) |
592 |
|
|
{ |
593 |
|
|
$this->HTML_TreeMenu_Presentation($structure); |
594 |
|
|
$this->isDynamic = $isDynamic; |
595 |
|
|
|
596 |
|
|
// Defaults |
597 |
|
|
$this->images = 'images'; |
598 |
joko |
1.2 |
$this->maxDepth = 0; // No limit |
599 |
joko |
1.1 |
$this->linkTarget = '_self'; |
600 |
|
|
$this->defaultClass = ''; |
601 |
|
|
$this->usePersistence = true; |
602 |
|
|
$this->noTopLevelImages = false; |
603 |
|
|
|
604 |
|
|
foreach ($options as $option => $value) { |
605 |
|
|
$this->$option = $value; |
606 |
|
|
} |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
|
|
* Returns the HTML for the menu. This method can be |
611 |
|
|
* used instead of printMenu() to use the menu system |
612 |
|
|
* with a template system. |
613 |
|
|
* |
614 |
|
|
* @access public |
615 |
|
|
* @return string The HTML for the menu |
616 |
|
|
*/ |
617 |
|
|
function toHTML() |
618 |
|
|
{ |
619 |
|
|
static $count = 0; |
620 |
joko |
1.2 |
$menuObj = 'objTreeMenu_' . ++$count; |
621 |
joko |
1.1 |
|
622 |
|
|
$html = "\n"; |
623 |
|
|
$html .= '<script language="javascript" type="text/javascript">' . "\n\t"; |
624 |
|
|
$html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);', |
625 |
|
|
$menuObj, |
626 |
|
|
$this->images, |
627 |
|
|
$menuObj, |
628 |
|
|
$this->linkTarget, |
629 |
|
|
$this->defaultClass, |
630 |
|
|
$this->usePersistence ? 'true' : 'false', |
631 |
|
|
$this->noTopLevelImages ? 'true' : 'false'); |
632 |
|
|
|
633 |
|
|
$html .= "\n"; |
634 |
|
|
|
635 |
|
|
/** |
636 |
|
|
* Loop through subnodes |
637 |
|
|
*/ |
638 |
|
|
if (isset($this->menu->items)) { |
639 |
|
|
for ($i=0; $i<count($this->menu->items); $i++) { |
640 |
|
|
$html .= $this->_nodeToHTML($this->menu->items[$i], $menuObj); |
641 |
|
|
} |
642 |
|
|
} |
643 |
|
|
|
644 |
joko |
1.2 |
$html .= sprintf("\n\t%s.drawMenu();", $menuObj); |
645 |
|
|
$html .= sprintf("\n\t%s.writeOutput();", $menuObj); |
646 |
|
|
|
647 |
joko |
1.1 |
if ($this->usePersistence && $this->isDynamic) { |
648 |
|
|
$html .= sprintf("\n\t%s.resetBranches();", $menuObj); |
649 |
|
|
} |
650 |
|
|
$html .= "\n</script>"; |
651 |
|
|
|
652 |
|
|
return $html; |
653 |
|
|
} |
654 |
|
|
|
655 |
|
|
/** |
656 |
|
|
* Prints a node of the menu |
657 |
|
|
* |
658 |
|
|
* @access private |
659 |
|
|
*/ |
660 |
joko |
1.2 |
function _nodeToHTML($nodeObj, $prefix, $return = 'newNode', $currentDepth = 0, $maxDepthPrefix = null) |
661 |
joko |
1.1 |
{ |
662 |
joko |
1.2 |
$prefix = empty($maxDepthPrefix) ? $prefix : $maxDepthPrefix; |
663 |
|
|
|
664 |
joko |
1.1 |
$expanded = $this->isDynamic ? ($nodeObj->expanded ? 'true' : 'false') : 'true'; |
665 |
|
|
$isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false'; |
666 |
|
|
$html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s', '%s', %s));\n", |
667 |
|
|
$return, |
668 |
|
|
$prefix, |
669 |
joko |
1.2 |
str_replace("'", "\\'", $nodeObj->text), |
670 |
joko |
1.1 |
!empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null', |
671 |
|
|
!empty($nodeObj->link) ? "'" . $nodeObj->link . "'" : 'null', |
672 |
|
|
$expanded, |
673 |
|
|
$isDynamic, |
674 |
|
|
$nodeObj->cssClass, |
675 |
|
|
$nodeObj->linkTarget, |
676 |
|
|
!empty($nodeObj->expandedIcon) ? "'" . $nodeObj->expandedIcon . "'" : 'null'); |
677 |
|
|
|
678 |
|
|
foreach ($nodeObj->events as $event => $handler) { |
679 |
|
|
$html .= sprintf("\t %s.setEvent('%s', '%s');\n", |
680 |
|
|
$return, |
681 |
|
|
$event, |
682 |
|
|
str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler)); |
683 |
|
|
} |
684 |
|
|
|
685 |
joko |
1.2 |
if ($this->maxDepth > 0 AND $currentDepth == $this->maxDepth) { |
686 |
|
|
$maxDepthPrefix = $prefix; |
687 |
|
|
} |
688 |
|
|
|
689 |
joko |
1.1 |
/** |
690 |
|
|
* Loop through subnodes |
691 |
|
|
*/ |
692 |
|
|
if (!empty($nodeObj->items)) { |
693 |
|
|
for ($i=0; $i<count($nodeObj->items); $i++) { |
694 |
joko |
1.2 |
$html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1), $currentDepth + 1, $maxDepthPrefix); |
695 |
joko |
1.1 |
} |
696 |
|
|
} |
697 |
|
|
|
698 |
|
|
return $html; |
699 |
|
|
} |
700 |
|
|
} // End class HTML_TreeMenu_DHTML |
701 |
|
|
|
702 |
|
|
|
703 |
|
|
/** |
704 |
|
|
* HTML_TreeMenu_Listbox class |
705 |
|
|
* |
706 |
|
|
* This class presents the menu as a listbox |
707 |
|
|
*/ |
708 |
|
|
class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation |
709 |
|
|
{ |
710 |
|
|
/** |
711 |
|
|
* The text that is displayed in the first option |
712 |
|
|
* @var string |
713 |
|
|
*/ |
714 |
|
|
var $promoText; |
715 |
|
|
|
716 |
|
|
/** |
717 |
|
|
* The character used for indentation |
718 |
|
|
* @var string |
719 |
|
|
*/ |
720 |
|
|
var $indentChar; |
721 |
|
|
|
722 |
|
|
/** |
723 |
|
|
* How many of the indent chars to use |
724 |
|
|
* per indentation level |
725 |
|
|
* @var integer |
726 |
|
|
*/ |
727 |
|
|
var $indentNum; |
728 |
|
|
|
729 |
|
|
/** |
730 |
|
|
* Target for the links generated |
731 |
|
|
* @var string |
732 |
|
|
*/ |
733 |
|
|
var $linkTarget; |
734 |
|
|
|
735 |
|
|
/** |
736 |
|
|
* Constructor |
737 |
|
|
* |
738 |
|
|
* @param object $structure The menu structure |
739 |
|
|
* @param array $options Options whic affect the display of the listbox. |
740 |
|
|
* These can consist of: |
741 |
|
|
* o promoText The text that appears at the the top of the listbox |
742 |
|
|
* Defaults to "Select..." |
743 |
|
|
* o indentChar The character to use for indenting the nodes |
744 |
|
|
* Defaults to " " |
745 |
|
|
* o indentNum How many of the indentChars to use per indentation level |
746 |
|
|
* Defaults to 2 |
747 |
|
|
* o linkTarget Target for the links. Defaults to "_self" |
748 |
|
|
* o submitText Text for the submit button. Defaults to "Go" |
749 |
|
|
*/ |
750 |
|
|
function HTML_TreeMenu_Listbox($structure, $options = array()) |
751 |
|
|
{ |
752 |
|
|
$this->HTML_TreeMenu_Presentation($structure); |
753 |
|
|
|
754 |
|
|
$this->promoText = 'Select...'; |
755 |
|
|
$this->indentChar = ' '; |
756 |
|
|
$this->indentNum = 2; |
757 |
|
|
$this->linkTarget = '_self'; |
758 |
|
|
$this->submitText = 'Go'; |
759 |
|
|
|
760 |
|
|
foreach ($options as $option => $value) { |
761 |
|
|
$this->$option = $value; |
762 |
|
|
} |
763 |
|
|
} |
764 |
|
|
|
765 |
|
|
/** |
766 |
|
|
* Returns the HTML generated |
767 |
|
|
*/ |
768 |
|
|
function toHTML() |
769 |
|
|
{ |
770 |
|
|
static $count = 0; |
771 |
|
|
$nodeHTML = ''; |
772 |
|
|
|
773 |
|
|
/** |
774 |
|
|
* Loop through subnodes |
775 |
|
|
*/ |
776 |
|
|
if (isset($this->menu->items)) { |
777 |
|
|
for ($i=0; $i<count($this->menu->items); $i++) { |
778 |
|
|
$nodeHTML .= $this->_nodeToHTML($this->menu->items[$i]); |
779 |
|
|
} |
780 |
|
|
} |
781 |
|
|
|
782 |
|
|
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>', |
783 |
|
|
$this->linkTarget, |
784 |
|
|
'HTML_TreeMenu_Listbox_' . ++$count, |
785 |
|
|
'HTML_TreeMenu_Listbox_' . $count, |
786 |
|
|
'HTML_TreeMenu_Listbox_' . $count, |
787 |
|
|
$this->promoText, |
788 |
|
|
$nodeHTML, |
789 |
|
|
$this->submitText); |
790 |
|
|
} |
791 |
|
|
|
792 |
|
|
/** |
793 |
|
|
* Returns HTML for a single node |
794 |
|
|
* |
795 |
|
|
* @access private |
796 |
|
|
*/ |
797 |
|
|
function _nodeToHTML($node, $prefix = '') |
798 |
|
|
{ |
799 |
|
|
$html = sprintf('<option value="%s">%s%s</option>', $node->link, $prefix, $node->text); |
800 |
|
|
|
801 |
|
|
/** |
802 |
|
|
* Loop through subnodes |
803 |
|
|
*/ |
804 |
|
|
if (isset($node->items)) { |
805 |
|
|
for ($i=0; $i<count($node->items); $i++) { |
806 |
|
|
$html .= $this->_nodeToHTML($node->items[$i], $prefix . str_repeat($this->indentChar, $this->indentNum)); |
807 |
|
|
} |
808 |
|
|
} |
809 |
|
|
|
810 |
|
|
return $html; |
811 |
|
|
} |
812 |
|
|
} // End class HTML_TreeMenu_Listbox |
813 |
|
|
?> |