Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +-----------------------------------------------------------------------+
3
// | Copyright (c) 2002, 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 198623 2005-10-17 18:37:50Z jeichorn $
37
 
38
/**
39
* @package HTML_TreeMenu
40
*/
41
/**
42
* HTML_TreeMenu Class
43
*
44
* A simple couple of PHP classes and some not so simple
45
* Jabbascript which produces a tree menu. In IE this menu
46
* is dynamic, with branches being collapsable. In IE5+ the
47
* status of the collapsed/open branches persists across page
48
* refreshes.In any other browser the tree is static. Code is
49
* based on work of Harald Radi.
50
*
51
* Usage.
52
*
53
* After installing the package, copy the example php script to
54
* your servers document root. Also place the TreeMenu.js and the
55
* images folder in the same place. Running the script should
56
* then produce the tree.
57
*
58
* Thanks go to Chip Chapin (http://www.chipchapin.com) for many
59
* excellent ideas and improvements.
60
*
61
* @author  Richard Heyes <richard@php.net>
62
* @author  Harald Radi <harald.radi@nme.at>
63
* @access  public
64
* @package HTML_TreeMenu
65
*/
66
 
67
class HTML_TreeMenu
68
{
69
    /**
70
    * Indexed array of subnodes
71
    * @var array
72
    */
73
    var $items;
74
 
75
    /**
76
    * Constructor
77
    *
78
    * @access public
79
    */
80
    function HTML_TreeMenu()
81
    {
82
        // Not much to do here :(
83
    }
84
 
85
    /**
86
    * This function adds an item to the the tree.
87
    *
88
    * @access public
89
    * @param  object $node The node to add. This object should be
90
    *                      a HTML_TreeNode object.
91
    * @return object       Returns a reference to the new node inside
92
    *                      the tree.
93
    */
94
    function &addItem(&$node)
95
    {
96
        $this->items[] = &$node;
97
        return $this->items[count($this->items) - 1];
98
    }
99
} // HTML_TreeMenu
100
 
101
 
102
/**
103
* HTML_TreeNode class
104
*
105
* This class is supplementary to the above and provides a way to
106
* add nodes to the tree. A node can have other nodes added to it.
107
*
108
* @author  Richard Heyes <richard@php.net>
109
* @author  Harald Radi <harald.radi@nme.at>
110
* @access  public
111
* @package HTML_TreeMenu
112
*/
113
class HTML_TreeNode
114
{
115
    /**
116
    * The text for this node.
117
    * @var string
118
    */
119
    var $text;
120
 
121
    /**
122
    * The link for this node.
123
    * @var string
124
    */
125
    var $link;
126
 
127
    /**
128
    * The icon for this node.
129
    * @var string
130
    */
131
    var $icon;
132
 
133
    /**
134
    * The css class for this node
135
    * @var string
136
    */
137
    var $cssClass;
138
 
139
    /**
140
    * Indexed array of subnodes
141
    * @var array
142
    */
143
    var $items;
144
 
145
    /**
146
    * Whether this node is expanded or not
147
    * @var bool
148
    */
149
    var $expanded;
150
 
151
    /**
152
    * Whether this node is dynamic or not
153
    * @var bool
154
    */
155
    var $isDynamic;
156
 
157
    /**
158
    * Should this node be made visible?
159
    * @var bool
160
    */
161
    var $ensureVisible;
162
 
163
    /**
164
    * The parent node. Null if top level
165
    * @var object
166
    */
167
    var $parent;
168
 
169
    /**
170
    * Javascript event handlers;
171
    * @var array
172
    */
173
    var $events;
174
 
175
    /**
176
    * Constructor
177
    *
178
    * @access public
179
    * @param  array $options An array of options which you can pass to change
180
    *                        the way this node looks/acts. This can consist of:
181
    *                         o text          The title of the node, defaults to blank
182
    *                         o link          The link for the node, defaults to blank
183
    *                         o icon          The icon for the node, defaults to blank
184
    *                         o class         The CSS class for this node, defaults to blank
185
    *                         o expanded      The default expanded status of this node, defaults to false
186
    *                                         This doesn't affect non dynamic presentation types
187
    *                         o isDynamic     If this node is dynamic or not. Only affects
188
    *                                         certain presentation types.
189
    *                         o ensureVisible If true this node will be made visible despite the expanded
190
    *                                         settings, and client side persistence. Will not affect
191
    *                                         some presentation styles, such as Listbox. Default is false
192
    * @param  array $events An array of javascript events and the corresponding event handlers.
193
    *                       Additionally to the standard javascript events you can specify handlers
194
    *                       for the 'onexpand', 'oncollapse' and 'ontoggle' events which will be fired
195
    *                       whenever a node is collapsed and/or expanded.
196
    */
197
    function HTML_TreeNode($options = array(), $events = array())
198
    {
199
        $this->text          = '';
200
        $this->link          = '';
201
        $this->icon          = '';
202
        $this->cssClass      = '';
203
        $this->expanded      = false;
204
        $this->isDynamic     = true;
205
        $this->ensureVisible = false;
206
 
207
        $this->parent        = null;
208
        $this->events        = $events;
209
 
210
        foreach ($options as $option => $value) {
211
            $this->$option = $value;
212
        }
213
    }
214
 
215
    /**
216
    * Allows setting of various parameters after the initial
217
    * constructor call. Possible options you can set are:
218
    *  o text
219
    *  o link
220
    *  o icon
221
    *  o cssClass
222
    *  o expanded
223
    *  o isDynamic
224
    *  o ensureVisible
225
    * ie The same options as in the constructor
226
    *
227
    * @access public
228
    * @param  string $option Option to set
229
    * @param  string $value  Value to set the option to
230
    */
231
    function setOption($option, $value)
232
    {
233
        $this->$option = $value;
234
    }
235
 
236
    /**
237
    * Adds a new subnode to this node.
238
    *
239
    * @access public
240
    * @param  object $node The new node
241
    */
242
    function &addItem(&$node)
243
    {
244
        $node->parent  = &$this;
245
        $this->items[] = &$node;
246
 
247
        /**
248
        * If the subnode has ensureVisible set it needs
249
        * to be handled, and all parents set accordingly.
250
        */
251
        if ($node->ensureVisible) {
252
            $this->_ensureVisible();
253
        }
254
 
255
        return $this->items[count($this->items) - 1];
256
    }
257
 
258
    /**
259
    * Private function to handle ensureVisible stuff
260
    *
261
    * @access private
262
    */
263
    function _ensureVisible()
264
    {
265
        $this->ensureVisible = true;
266
        $this->expanded      = true;
267
 
268
        if (!is_null($this->parent)) {
269
            $this->parent->_ensureVisible();
270
        }
271
    }
272
} // HTML_TreeNode
273
 
274
 
275
/**
276
* HTML_TreeMenu_Presentation class
277
*
278
* Base class for other presentation classes to
279
* inherit from.
280
* @package HTML_TreeMenu
281
*/
282
class HTML_TreeMenu_Presentation
283
{
284
    /**
285
    * The TreeMenu structure
286
    * @var object
287
    */
288
    var $menu;
289
 
290
    /**
291
    * Base constructor simply sets the menu object
292
    *
293
    * @param object $structure The menu structure
294
    */
295
    function HTML_TreeMenu_Presentation(&$structure)
296
    {
297
        $this->menu = &$structure;
298
    }
299
 
300
    /**
301
    * Prints the HTML generated by the toHTML() method.
302
    * toHTML() must therefore be defined by the derived
303
    * class.
304
    *
305
    * @access public
306
    * @param  array  Options to set. Any options taken by
307
    *                the presentation class can be specified
308
    *                here.
309
    */
310
    function printMenu($options = array())
311
    {
312
        foreach ($options as $option => $value) {
313
            $this->$option = $value;
314
        }
315
 
316
        echo $this->toHTML();
317
    }
318
}
319
 
320
/**
321
* HTML_TreeMenu_DHTML class
322
*
323
* This class is a presentation class for the tree structure
324
* created using the TreeMenu/TreeNode. It presents the
325
* traditional tree, static for browsers that can't handle
326
* the DHTML.
327
* @package HTML_TreeMenu
328
*/
329
class HTML_TreeMenu_DHTML extends HTML_TreeMenu_Presentation
330
{
331
    /**
332
    * Dynamic status of the treemenu. If true (default) this has no effect. If
333
    * false it will override all dynamic status vars and set the menu to be
334
    * fully expanded an non-dynamic.
335
    */
336
    var $isDynamic;
337
 
338
    /**
339
    * Path to the images
340
    * @var string
341
    */
342
    var $images;
343
 
344
    /**
345
    * Target for the links generated
346
    * @var string
347
    */
348
    var $linkTarget;
349
 
350
    /**
351
    * Whether to use clientside persistence or not
352
    * @var bool
353
    */
354
    var $userPersistence;
355
 
356
    /**
357
    * The default CSS class for the nodes
358
    */
359
    var $defaultClass;
360
 
361
    /**
362
    * Whether to skip first level branch images
363
    * @var bool
364
    */
365
    var $noTopLevelImages;
366
 
367
    /**
368
    * Constructor, takes the tree structure as
369
    * an argument and an array of options which
370
    * can consist of:
371
    *  o images            -  The path to the images folder. Defaults to "images"
372
    *  o linkTarget        -  The target for the link. Defaults to "_self"
373
    *  o defaultClass      -  The default CSS class to apply to a node. Default is none.
374
    *  o usePersistence    -  Whether to use clientside persistence. This persistence
375
    *                         is achieved using cookies. Default is true.
376
    *  o noTopLevelImages  -  Whether to skip displaying the first level of images if
377
    *                         there is multiple top level branches.
378
    *
379
    * And also a boolean for whether the entire tree is dynamic or not.
380
    * This overrides any perNode dynamic settings.
381
    *
382
    * @param object $structure The menu structure
383
    * @param array  $options   Array of options
384
    * @param bool   $isDynamic Whether the tree is dynamic or not
385
    */
386
    function HTML_TreeMenu_DHTML(&$structure, $options = array(), $isDynamic = true)
387
    {
388
        $this->menu      = &$structure;
389
        $this->isDynamic = $isDynamic;
390
 
391
        // Defaults
392
        $this->images           = 'images';
393
        $this->linkTarget       = '_self';
394
        $this->defaultClass     = '';
395
        $this->usePersistence   = true;
396
        $this->noTopLevelImages = false;
397
 
398
        foreach ($options as $option => $value) {
399
            $this->$option = $value;
400
        }
401
    }
402
 
403
    /**
404
    * Returns the HTML for the menu. This method can be
405
    * used instead of printMenu() to use the menu system
406
    * with a template system.
407
    *
408
    * @access public
409
    * @return string The HTML for the menu
410
    */
411
    function toHTML()
412
    {
413
        static $count = 0;
414
        $menuObj = 'objTreeMenu_' . ++$count;
415
 
416
        $html  = "\n";
417
        $html .= '<script language="javascript" type="text/javascript">' . "\n\t";
418
        $html .= sprintf('%s = new TreeMenu("%s", "%s", "%s", "%s", %s, %s);',
419
                         $menuObj,
420
                         $this->images,
421
                         $menuObj,
422
                         $this->linkTarget,
423
                         $this->defaultClass,
424
                         $this->usePersistence ? 'true' : 'false',
425
                         $this->noTopLevelImages ? 'true' : 'false');
426
 
427
        $html .= "\n";
428
 
429
        /**
430
        * Loop through subnodes
431
        */
432
        if (isset($this->menu->items)) {
433
            for ($i=0; $i<count($this->menu->items); $i++) {
434
                $html .= $this->_nodeToHTML($this->menu->items[$i], $menuObj);
435
            }
436
        }
437
 
438
         $html .= sprintf("\n\t%s.drawMenu();", $menuObj);
439
        if ($this->usePersistence && $this->isDynamic) {
440
            $html .= sprintf("\n\t%s.resetBranches();", $menuObj);
441
        }
442
        $html .= "\n</script>";
443
 
444
        return $html;
445
    }
446
 
447
    /**
448
    * Prints a node of the menu
449
    *
450
    * @access private
451
    */
452
    function _nodeToHTML($nodeObj, $prefix, $return = 'newNode')
453
    {
454
        $expanded  = $this->isDynamic ? ($nodeObj->expanded  ? 'true' : 'false') : 'true';
455
        $isDynamic = $this->isDynamic ? ($nodeObj->isDynamic ? 'true' : 'false') : 'false';
456
        $html = sprintf("\t %s = %s.addItem(new TreeNode('%s', %s, %s, %s, %s, '%s'));\n",
457
                        $return,
458
                        $prefix,
459
                        $nodeObj->text,
460
                        !empty($nodeObj->icon) ? "'" . $nodeObj->icon . "'" : 'null',
461
                        !empty($nodeObj->link) ? "'" . addslashes($nodeObj->link) . "'" : 'null',
462
                        $expanded,
463
                        $isDynamic,
464
                        $nodeObj->cssClass);
465
 
466
        foreach ($nodeObj->events as $event => $handler) {
467
            $html .= sprintf("\t %s.setEvent('%s', '%s');\n",
468
                             $return,
469
                             $event,
470
                             str_replace(array("\r", "\n", "'"), array('\r', '\n', "\'"), $handler));
471
        }
472
 
473
        /**
474
        * Loop through subnodes
475
        */
476
        if (!empty($nodeObj->items)) {
477
            for ($i=0; $i<count($nodeObj->items); $i++) {
478
                $html .= $this->_nodeToHTML($nodeObj->items[$i], $return, $return . '_' . ($i + 1));
479
            }
480
        }
481
 
482
        return $html;
483
    }
484
}
485
 
486
 
487
/**
488
* HTML_TreeMenu_Listbox class
489
*
490
* This class presents the menu as a listbox
491
* @package HTML_TreeMenu
492
*/
493
class HTML_TreeMenu_Listbox extends HTML_TreeMenu_Presentation
494
{
495
    /**
496
    * The text that is displayed in the first option
497
    * @var string
498
    */
499
    var $promoText;
500
 
501
    /**
502
    * The character used for indentation
503
    * @var string
504
    */
505
    var $indentChar;
506
 
507
    /**
508
    * How many of the indent chars to use
509
    * per indentation level
510
    * @var integer
511
    */
512
    var $indentNum;
513
 
514
    /**
515
    * Target for the links generated
516
    * @var string
517
    */
518
    var $linkTarget;
519
 
520
    /**
521
    * Constructor
522
    *
523
    * @param object $structure The menu structure
524
    * @param array  $options   Options whic affect the display of the listbox.
525
    *                          These can consist of:
526
    *                           o promoText  The text that appears at the the top of the listbox
527
    *                                        Defaults to "Select..."
528
    *                           o indentChar The character to use for indenting the nodes
529
    *                                        Defaults to "&nbsp;"
530
    *                           o indentNum  How many of the indentChars to use per indentation level
531
    *                                        Defaults to 2
532
    *                           o linkTarget Target for the links. Defaults to "_self"
533
    *                           o submitText Text for the submit button. Defaults to "Go"
534
    */
535
    function HTML_TreeMenu_Listbox($structure, $options = array())
536
    {
537
        $this->menu       = $structure;
538
        $this->promoText  = 'Select...';
539
        $this->indentChar = '&nbsp;';
540
        $this->indentNum  = 2;
541
        $this->linkTarget = '_self';
542
        $this->submitText = 'Go';
543
 
544
        foreach ($options as $option => $value) {
545
            $this->$option = $value;
546
        }
547
    }
548
 
549
    /**
550
    * Returns the HTML generated
551
    */
552
    function toHTML()
553
    {
554
        static $count = 0;
555
        $nodeHTML = '';
556
 
557
        /**
558
        * Loop through subnodes
559
        */
560
        if (isset($this->menu->items)) {
561
            for ($i=0; $i<count($this->menu->items); $i++) {
562
                $nodeHTML .= $this->_nodeToHTML($this->menu->items[$i]);
563
            }
564
        }
565
 
566
        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>',
567
                       $this->linkTarget,
568
                       'HTML_TreeMenu_Listbox_' . ++$count,
569
                       'HTML_TreeMenu_Listbox_' . $count,
570
                       'HTML_TreeMenu_Listbox_' . $count,
571
                       $this->promoText,
572
                       $nodeHTML,
573
                       $this->submitText);
574
    }
575
 
576
    /**
577
    * Returns HTML for a single node
578
    *
579
    * @access private
580
    */
581
    function _nodeToHTML($node, $prefix = '')
582
    {
583
        $html = sprintf('<option value="%s">%s%s</option>', $node->link, $prefix, $node->text);
584
 
585
        /**
586
        * Loop through subnodes
587
        */
588
        if (isset($node->items)) {
589
            for ($i=0; $i<count($node->items); $i++) {
590
                $html .= $this->_nodeToHTML($node->items[$i], $prefix . str_repeat($this->indentChar, $this->indentNum));
591
            }
592
        }
593
 
594
        return $html;
595
    }
596
}
597
?>