File: html.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (591 lines) | stat: -rw-r--r-- 17,493 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?php
/**
 * The Horde_Tree_html:: class extends the Horde_Tree class to provide
 * HTML specific rendering functions.
 *
 * Copyright 2003-2006 Marko Djukic <marko@oblo.com>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * $Horde: framework/Tree/Tree/html.php,v 1.51.2.12 2006/01/15 16:33:29 chuck Exp $
 *
 * @author  Marko Djukic <marko@oblo.com>
 * @package Horde_Tree
 * @since   Horde 3.0
 */
class Horde_Tree_html extends Horde_Tree {

    /**
     * Image directory location.
     *
     * @var string
     */
    var $_img_dir = '';

    /**
     * Default tree graphic for a line.
     *
     * @var string
     */
    var $_img_line = 'line.png';

    /**
     * Default tree graphic for a blank.
     *
     * @var string
     */
    var $_img_blank = 'blank.png';

    /**
     * Default tree graphic for a join.
     *
     * @var string
     */
    var $_img_join = 'join.png';

    /**
     * Default tree graphic for a bottom join.
     *
     * @var string
     */
    var $_img_join_bottom = 'joinbottom.png';

    /**
     * Default tree graphic for a plus.
     *
     * @var string
     */
    var $_img_plus = 'plus.png';

    /**
     * Default tree graphic for a bottom plus.
     *
     * @var string
     */
    var $_img_plus_bottom = 'plusbottom.png';

    /**
     * Default tree graphic for a plus only.
     *
     * @var string
     */
    var $_img_plus_only = 'plusonly.png';

    /**
     * Default tree graphic for a minus.
     *
     * @var string
     */
    var $_img_minus = 'minus.png';

    /**
     * Default tree graphic for a bottom minus.
     *
     * @var string
     */
    var $_img_minus_bottom = 'minusbottom.png';

    /**
     * Default tree graphic for a minus only.
     *
     * @var string
     */
    var $_img_minus_only = 'minusonly.png';

    /**
     * Default tree graphic for a null only.
     *
     * @var string
     */
    var $_img_null_only = 'nullonly.png';

    /**
     * Default tree graphic for a folder.
     *
     * @var string
     */
    var $_img_folder = 'folder.png';

    /**
     * Default tree graphic for an open folder.
     *
     * @var string
     */
    var $_img_folder_open = 'folderopen.png';

    /**
     * Default tree graphic for a leaf.
     *
     * @var string
     */
    var $_img_leaf = 'leaf.png';

    /**
     * TODO
     *
     * @var array
     */
    var $_nodes = array();

    /**
     * TODO
     *
     * @var array
     */
    var $_node_pos = array();

    /**
     * TODO
     *
     * @var array
     */
    var $_dropline = array();

    /**
     * Current value of the alt tag count.
     *
     * @var integer
     */
    var $_alt_count = 0;

    /**
     * Constructor
     */
    function Horde_Tree_html($tree_name, $params)
    {
        parent::Horde_Tree($tree_name, $params);

        $this->_img_dir = $GLOBALS['registry']->getImageDir('horde') . '/tree';
    }

    /**
     * Returns the tree.
     *
     * @param boolean $static  If true the tree nodes can't be expanded and
     *                         collapsed and the tree gets rendered expanded.
     *
     * @return string  The HTML code of the rendered tree.
     */
    function getTree($static = false)
    {
        $this->_static = $static;
        $this->_buildIndents($this->_root_nodes);

        $tree = $this->_buildHeader();
        foreach ($this->_root_nodes as $node_id) {
            $tree .= $this->_buildTree($node_id);
        }
        return $tree;
    }

    /**
     * Check the current environment to see if we can render the HTML
     * tree. HTML is always renderable, at least until we add a
     * php-gtk tree backend, in which case this implementation will
     * actually need a body.
     *
     * @static
     *
     * @return boolean  Whether or not this Tree:: backend will function.
     */
    function isSupported()
    {
        return true;
    }

    /**
     * Returns just the JS node definitions as a string. This is a
     * no-op for the HTML renderer.
     */
    function renderNodeDefinitions()
    {
    }

    /**
     * Returns the HTML code for a header row, if necessary.
     *
     * @access private
     *
     * @return string  The HTML code of the header row or an empty string.
     */
    function _buildHeader()
    {
        if (!count($this->_header)) {
            return '';
        }

        $html = '<div';
        /* If using alternating row shading, work out correct
         * shade. */
        if ($this->getOption('alternate')) {
            $html .= ' class="item' . $this->_alt_count . '"';
            $this->_alt_count = 1 - $this->_alt_count;
        }
        $html .= '>';

        foreach ($this->_header as $header) {
            $html .= '<div class="leftFloat';
            if (!empty($header['class'])) {
                $html .= ' ' . $header['class'];
            }
            $html .= '"';

            $style = '';
            if (!empty($header['width'])) {
                $style .= 'width:' . $header['width'] . ';';
            }
            if (!empty($header['align'])) {
                $style .= 'text-align:' . $header['align'] . ';';
            }
            if (!empty($style)) {
                $html .= ' style="' . $style . '"';
            }
            $html .= '>';
            $html .= empty($header['html']) ? '&nbsp;' : $header['html'];
            $html .= '</div>';
        }

        return $html . '</div>';
    }

    /**
     * Recursive function to walk through the tree array and build the output.
     *
     * @access private
     *
     * @param string $node_id  The Node ID.
     *
     * @return string  The tree rendering.
     */
    function _buildTree($node_id)
    {
        $output = $this->_buildLine($node_id);

        if (isset($this->_nodes[$node_id]['children']) &&
            $this->_nodes[$node_id]['expanded']) {
            $num_subnodes = count($this->_nodes[$node_id]['children']);
            for ($c = 0; $c < $num_subnodes; $c++) {
                $child_node_id = $this->_nodes[$node_id]['children'][$c];
                $this->_node_pos[$child_node_id] = array();
                $this->_node_pos[$child_node_id]['pos'] = $c + 1;
                $this->_node_pos[$child_node_id]['count'] = $num_subnodes;
                $output .= $this->_buildTree($child_node_id);
            }
        }

        return $output;
    }

    /**
     * Function to create a single line of the tree.
     *
     * @access private
     *
     * @param string $node_id  The Node ID.
     *
     * @return string  The rendered line.
     */
    function _buildLine($node_id)
    {
        $className = 'treeRow';
        if (!empty($this->_nodes[$node_id]['class'])) {
            $className .= ' ' . $this->_nodes[$node_id]['class'];
        }
        /* If using alternating row shading, work out correct
         * shade. */
        if ($this->getOption('alternate')) {
            $className .= ' item' . $this->_alt_count;
            $this->_alt_count = 1 - $this->_alt_count;
        }

        $line = '<div class="' . $className . '">';

        /* If we have headers, track which logical "column" we're in
         * for any given cell of content. */
        $column = 0;

        if (isset($this->_nodes[$node_id]['extra'][HORDE_TREE_EXTRA_LEFT])) {
            $extra = $this->_nodes[$node_id]['extra'][HORDE_TREE_EXTRA_LEFT];
            $cMax = count($extra);
            for ($c = 0; $c < $cMax; $c++) {
                $style = '';
                if (isset($this->_header[$column]['width'])) {
                    $style .= 'width:' . $this->_header[$column]['width'] . ';';
                }

                $line .= '<div class="leftFloat"';
                if (!empty($style)) {
                    $line .= ' style="' . $style . '"';
                }
                $line .= '>' . $extra[$c] . '</div>';

                $column++;
            }
        }

        $style = '';
        if (isset($this->_header[$column]['width'])) {
            $style .= 'width:' . $this->_header[$column]['width'] . ';';
        }
        $line .= '<div class="leftFloat"';
        if (!empty($style)) {
            $line .= ' style="' . $style . '"';
        }
        $line .= '>';

        if ($this->getOption('multiline')) {
            $line .= '<table cellspacing="0"><tr><td>';
        }

        for ($i = $this->_static ? 1 : 0; $i < $this->_nodes[$node_id]['indent']; $i++) {
            $line .= '<img src="' . $this->_img_dir . '/';
            if ($this->_dropline[$i] && $this->getOption('lines', false, true)) {
                $line .= $this->_img_line . '" '
                    . 'alt="|&nbsp;&nbsp;&nbsp;" ';
            } else {
                $line .= $this->_img_blank . '" '
                    . 'alt="&nbsp;&nbsp;&nbsp;" ';
            }
            $line .= 'height="20" width="20" style="vertical-align:middle" />';
        }
        $line .= $this->_setNodeToggle($node_id) . $this->_setNodeIcon($node_id);
        if ($this->getOption('multiline')) {
            $line .= '</td><td>';
        }
        $line .= $this->_setLabel($node_id);

        if ($this->getOption('multiline')) {
            $line .= '</td></tr></table>';
        }

        $line .= '</div>';
        $column++;

        if (isset($this->_nodes[$node_id]['extra'][HORDE_TREE_EXTRA_RIGHT])) {
            $extra = $this->_nodes[$node_id]['extra'][HORDE_TREE_EXTRA_RIGHT];
            $cMax = count($extra);
            for ($c = 0; $c < $cMax; $c++) {
                $style = '';
                if (isset($this->_header[$column]['width'])) {
                    $style .= 'width:' . $this->_header[$column]['width'] . ';';
                }

                $line .= '<div class="leftFloat"';
                if (!empty($style)) {
                    $line .= ' style="' . $style . '"';
                }
                $line .= '>' . $extra[$c] . '</div>';

                $column++;
            }
        }

        return $line . "</div>\n";
    }

    /**
     * Sets the label on the tree line.
     *
     * @access private
     *
     * @param string $node_id  The Node ID.
     *
     * @return string  The label for the tree line.
     */
    function _setLabel($node_id)
    {
        $n = $this->_nodes[$node_id];

        $output = '<span';
        if (!empty($n['onclick'])) {
            $output .= ' onclick="' . $n['onclick'] . '"';
        }
        $output .= '>';

        $label = $n['label'];
        if (!empty($n['url'])) {
            $target = '';
            if (!empty($n['target'])) {
                $target = ' target="' . $n['target'] . '"';
            } elseif ($target = $this->getOption('target')) {
                $target = ' target="' . $target . '"';
            }
            $output .= '<a' . (!empty($n['urlclass']) ? ' class="' . $n['urlclass'] . '"' : '') . ' href="' . $n['url'] . '"' . $target . '>' . $label . '</a>';
        } else {
            $output .= $label;
        }

        return $output . '</span>';
    }

    /**
     * Sets the node toggle on the tree line.
     *
     * @access private
     *
     * @param string $node_id  The Node ID.
     *
     * @return string  The node toggle for the tree line.
     */
    function _setNodeToggle($node_id)
    {
        $link_start = '';

        if (($this->_nodes[$node_id]['indent'] == 0) &&
            isset($this->_nodes[$node_id]['children'])) {
            /* Top level node with children. */
            $this->_dropline[0] = false;
            if ($this->_static) {
                return '';
            } elseif (!$this->getOption('lines', false, true)) {
                $img = $this->_img_blank;
                $alt = '&nbsp;&nbsp;&nbsp;';
            } elseif ($this->_nodes[$node_id]['expanded']) {
                $img = $this->_img_minus_only;
                $alt = '-';
            } else {
                $img = $this->_img_plus_only;
                $alt = '+';
            }
            if (!$this->_static) {
                $url = Util::addParameter(Horde::selfUrl(), HORDE_TREE_TOGGLE . $this->_instance, $node_id);
                $link_start = Horde::link($url);
            }
        } elseif (($this->_nodes[$node_id]['indent'] != 0) &&
            !isset($this->_nodes[$node_id]['children'])) {
            /* Node without children. */
            if ($this->_node_pos[$node_id]['pos'] < $this->_node_pos[$node_id]['count']) {
                /* Not last node. */
                if ($this->getOption('lines', false, true)) {
                    $img = $this->_img_join;
                    $alt = '|-';
                } else {
                    $img = $this->_img_blank;
                    $alt = '&nbsp;&nbsp;&nbsp;';
                }
                $this->_dropline[$this->_nodes[$node_id]['indent']] = true;
            } else {
                /* Last node. */
                if ($this->getOption('lines', false, true)) {
                    $img = $this->_img_join_bottom;
                    $alt = '`-';
                } else {
                    $img = $this->_img_blank;
                    $alt = '&nbsp;&nbsp;&nbsp;';
                }
                $this->_dropline[$this->_nodes[$node_id]['indent']] = false;
            }
        } elseif (isset($this->_nodes[$node_id]['children'])) {
            /* Node with children. */
            if ($this->_node_pos[$node_id]['pos'] < $this->_node_pos[$node_id]['count']) {
                /* Not last node. */
                if (!$this->getOption('lines', false, true)) {
                    $img = $this->_img_blank;
                    $alt = '&nbsp;&nbsp;&nbsp;';
                } elseif ($this->_static) {
                    $img = $this->_img_join;
                    $alt = '|-';
                } elseif ($this->_nodes[$node_id]['expanded']) {
                    $img = $this->_img_minus;
                    $alt = '-';
                } else {
                    $img = $this->_img_plus;
                    $alt = '+';
                }
                $this->_dropline[$this->_nodes[$node_id]['indent']] = true;
            } else {
                /* Last node. */
                if (!$this->getOption('lines', false, true)) {
                    $img = $this->_img_blank;
                    $alt = '&nbsp;&nbsp;&nbsp;';
                } elseif ($this->_static) {
                    $img = $this->_img_join_bottom;
                    $alt = '`-';
                } elseif ($this->_nodes[$node_id]['expanded']) {
                    $img = $this->_img_minus_bottom;
                    $alt = '-';
                } else {
                    $img = $this->_img_plus_bottom;
                    $alt = '+';
                }
                $this->_dropline[$this->_nodes[$node_id]['indent']] = false;
            }
            if (!$this->_static) {
                $url = Util::addParameter(Horde::selfUrl(), HORDE_TREE_TOGGLE . $this->_instance, $node_id);
                $link_start = Horde::link($url);
            }
        } else {
            /* Top level node with no children. */
            if ($this->_static) {
                return '';
            }
            if ($this->getOption('lines', false, true)) {
                $img = $this->_img_null_only;
                $alt = '&nbsp;&nbsp;';
            } else {
                $img = $this->_img_blank;
                $alt = '&nbsp;&nbsp;&nbsp;';
            }
            $this->_dropline[0] = false;
        }

        $link_end = ($link_start) ? '</a>' : '';

        $img = $link_start . '<img src="' . $this->_img_dir . '/' . $img . '"'
            . (isset($alt) ? ' alt="' . $alt . '"' : '')
            . ' height="20" width="20" style="vertical-align:middle" border="0" />'
            . $link_end;

        return $img;
    }

    /**
     * Sets the icon for the node.
     *
     * @access private
     *
     * @param string $node_id  The Node ID.
     *
     * @return string  The node icon for the tree line.
     */
    function _setNodeIcon($node_id)
    {
        $img_dir = isset($this->_nodes[$node_id]['icondir']) ? $this->_nodes[$node_id]['icondir'] : $this->_img_dir;
        if ($img_dir) {
            $img_dir .= '/';
        }

        if (isset($this->_nodes[$node_id]['icon'])) {
            if (empty($this->_nodes[$node_id]['icon'])) {
                return '';
            }
            /* Node has a user defined icon. */
            if (isset($this->_nodes[$node_id]['iconopen']) &&
                $this->_nodes[$node_id]['expanded']) {
                $img = $this->_nodes[$node_id]['iconopen'];
            } else {
                $img = $this->_nodes[$node_id]['icon'];
            }
        } else {
            /* Use standard icon set. */
            if (isset($this->_nodes[$node_id]['children'])) {
                /* Node with children. */
                $img = ($this->_nodes[$node_id]['expanded']) ? $this->_img_folder_open : $this->_img_folder;
            } else {
                /* Leaf node (no children). */
                $img = $this->_img_leaf;
            }
        }

        $imgtxt = '<img src="' . $img_dir . $img . '"';

        /* Does the node have user defined alt text? */
        if (isset($this->_nodes[$node_id]['iconalt'])) {
            $imgtxt .= ' alt="' . htmlspecialchars($this->_nodes[$node_id]['iconalt']) . '"';
        }

        return $imgtxt . ' />';
    }

}