File: CategoryTree.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (214 lines) | stat: -rw-r--r-- 7,433 bytes parent folder | download
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
<?php
/*
 * $Horde: horde/lib/CategoryTree.php,v 1.4.2.7 2003/01/03 12:48:37 jan Exp $
 *
 * Copyright 2002-2003 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

/**
 * Class for drawing a hierarchical tree structure from a category
 * export (CATEGORY_FORMAT_3D). Icons, styles, etc. can all be
 * defined.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @version $Revision: 1.4.2.7 $
 * @since   Horde 2.1
 * @package horde.category
 */
class CategoryTree {

    /** The category object to draw the tree for.
        @object Category $category */
    var $category;

    /** The template path to use.
        @string $templates */
    var $templates;

    /** The default image for any elements of the tree which don't
        have an image defined.
        @var string $image */
    var $image = 'group.gif';

    /** An array of images, indexed by element name, to be displayed
        for specific tree elements.
        @var array $images */
    var $images = array();

    /** The default CSS style name for any table rows which don't have
        a CSS style defined.
        @var string $style */
    var $style = 'text';

    /** An array of CSS styles, indexed by element name, to be
        displayed for specific tree elements.
        @var array $styles */
    var $styles = array();

    /** The default CSS link style name for any table rows which don't have
        a CSS style defined.
        @var string $linkstyle */
    var $linkstyle = 'text';

    /** An array of CSS link styles, indexed by element name, to be
        displayed for specific tree elements.  @var array $linkstyles
        */
    var $linkstyles = array();

    /** The default action for any element which doesn't have specific
        actions defined. This is a hash with two elements, 'text' for
        the link text, and 'url' for the link URL.
        @var array $action */
    var $action = null;

    /** An array of actions, indexed by element name, to be displayed
        for specific tree elements. These follow the same format as
        the default action.
        @see $action
        @var array $actions */
    var $actions = array();

    function CategoryTree(&$category, $templates)
    {
        if (isset($category)) {
            $this->category = $category;
        }
        $this->templates = $templates;
    }

    function setCategory(&$category)
    {
        $this->category = &$category;
    }

    function setImage($image, $element = null)
    {
        if (isset($element)) {
            $this->images[$element] = $image;
        } else {
            $this->image = $image;
        }
    }

    function setStyle($style, $element = null)
    {
        if (isset($element)) {
            $this->styles[$element] = $style;
        } else {
            $this->style = $style;
        }
    }

    function setLinkStyle($linkstyle, $element = null)
    {
        if (isset($element)) {
            $this->linkstyles[$element] = $linkstyle;
        } else {
            $this->linkstyle = $linkstyle;
        }
    }

    function setAction($action, $element = null)
    {
        if (isset($element)) {
            $this->actions[$element] = $action;
        } else {
            $this->action = $action;
        }
    }

    function draw($root = '-1', $maxi = null, $maxj = null)
    {
        $data = $this->category->export(CATEGORY_FORMAT_3D, $root);
        if (count($data) == 0) {
            $data = array(0 => array(0 => array('p' => '0.0',
                                                'name' => 'root')),
                          'x' => 0,
                          'y' => 0);
        }

        if (!isset($maxi)) {
            $maxi = $data['x'];
        }
        if (!isset($maxj)) {
            $maxj = $data['y'];
        }

        $imageGrid[0][0] = 'line';
        for ($n = $maxj; $n >= 0; $n--) {
            for ($m = $maxi; $m >= 0; $m--) {
                if (!empty($data[$m][$n])) {
                    // Something here
                    $imageGrid[$m][$n] = null;
                } else {
                    // Empty by default
                    $imageGrid[$m][$n] = 'blank';

                    // Picture on the right?
                    if ($m < $maxi) {
                        if (!empty($data[$m + 1][$n])) { // Picture on the right
                            $imageGrid[$m][$n] = 'joinbottom';
                            if ($n < $maxj) {
                                if (!empty($imageGrid[$m][$n + 1]) && $imageGrid[$m][$n + 1] !== 'blank') { // Picture on the right and line below
                                    $imageGrid[$m][$n] = 'join';
                                }
                            }
                        } else { // Nothing on the right
                            if ($n < $maxj) {
                                if (!empty($imageGrid[$m][$n + 1]) && $imageGrid[$m][$n + 1] !== 'blank') { // Nothing on the right and line below
                                    $imageGrid[$m][$n] = 'line';
                                }
                            }
                        }
                    }
                }
            }
        }

        include $this->templates . '/tablehead.inc';
        for ($n = 0; $n <= $maxj; $n++)	{
            $curRow = -1;
            include $this->templates . '/rowstart.inc';
            for ($m = 0; $m <= $maxi; $m++) {
                $style = !@empty($this->styles[$data[$m][$n]['name']]) ? $this->styles[$data[$m][$n]['name']] : $this->style;
                if (!isset($imageGrid[$m][$n])) {
                    echo '<td class="' . $style . '">';
                    if (!empty($this->images[$data[$m][$n]['name']])) {
                        Horde::pimg($this->images[$data[$m][$n]['name']]);
                    } else {
                        Horde::pimg($this->image);
                    }
                    echo '</td><td valign="center" class="' . $style . '">&nbsp;';
                    echo $data[$m][$n]['name'];
                    $curRow = $m;
                    $m = $maxi + 1;
                } else {
                    echo '<td>' . Horde::img('tree/' . $imageGrid[$m][$n] . '.gif', 'hspace="0" vspace="0" width="20" height="20"');
                }
                echo '</td>';
            }

            $actions = isset($this->actions[$data[$curRow][$n]['name']]) ? $this->actions[$data[$curRow][$n]['name']] : $this->action;
            if (!empty($actions) && is_array($actions) && count($actions) > 0) {
                $linktext = '';
                $linkstyle = !empty($this->linkstyles[$data[$curRow][$n]['name']]) ? $this->linkstyles[$data[$curRow][$n]['name']] : $this->linkstyle;
                foreach ($actions as $action) {
                    if (!empty($linktext)) {
                        $linktext .= ' | ';
                    }
                    $link = Horde::url(Horde::addParameter($action['url'], 'category=' . urlencode($data[$curRow][$n]['name'])));
                    $linktext .= Horde::link($link, $action['text'], $linkstyle) . $action['text'] . '</a>';
                }
            } else {
                $linktext = '';
            }
            include $this->templates . '/rowend.inc';
        }
        include $this->templates . '/tablefoot.inc';
    }

}
?>