Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 1
<?php
2
//
3
// +----------------------------------------------------------------------+
4
// | PEAR :: DB_NestedSet_TreeMenu                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Jason Rust <jrust@rustyparts.com>                           |
17
// +----------------------------------------------------------------------+
18
//
19
// $Id: TreeMenu.php,v 1.13 2003/10/07 00:11:27 datenpunk Exp $
20
//
21
 
22
require_once 'HTML/TreeMenu.php';
23
 
24
// {{{ DB_NestedSet_TreeMenu:: class
25
 
26
/**
27
* A helper class to translate the data from a nested set table into a HTML_TreeMenu object
28
* so that it can be used to create a dynamic tree menu using the PEAR HTML_TreeMenu class.
29
*
30
* @see          docs/TreeMenu_example.php
31
* @author       Jason Rust <jrust@rustyparts.com>
32
* @package      DB_NestedSet
33
* @version      $Revision: 1.13 $
34
* @access       public
35
*/
36
// }}}
37
class DB_NestedSet_TreeMenu extends DB_NestedSet_Output {
38
    // {{{ properties
39
 
40
    /**
41
    * @var array The current menu structure
42
    * @access private
43
    */
44
    var $_structTreeMenu = false;
45
 
46
    // }}}
47
    // {{{ DB_NestedSet_TreeMenu
48
 
49
    function &DB_NestedSet_TreeMenu($params) {
50
        $this->_structTreeMenu =& $this->_createFromStructure($params);
51
    }
52
 
53
    // }}}
54
    // {{{ _createFromStructure()
55
 
56
    /**
57
    * <pre>Creates a HTML_TreeMenu structure based off of the results from getAllNodes() method
58
    * of the DB_NestedSet class.  The needed parameters are:
59
    * o 'structure' => the result from $nestedSet->getAllNodes(true)
60
    * o 'textField' => the field in the table that has the text for node
61
    * o 'linkField' => the field in the table that has the link for the node
62
    * o 'options' => (optional) an array of any additional options to pass to the node when
63
    * Additionally these parameters may be added to the individual nodes to control their
64
    * behavior:
65
    * o 'ensureVisible' => (optional) whether or not the field should be forced as visible
66
    *                creating it such as 'icon' or 'expandedIcon'
67
    * o 'events' => (optional) an array of any events to pass to the node when creating it
68
    *               such as 'onclick' or 'onexpand'</pre>
69
    * </pre>
70
    * @access public
71
    * @return object A HTML_TreeMenu object
72
    */
73
    function &_createFromStructure($params)
74
    {
75
        // Basically we go through the array of nodes checking to see
76
        // if each node has children and if so recursing.  The reason this
77
        // works is because the data from getAllNodes() is ordered by level
78
        // so a root node will always be first, and sub children will always
79
        // be after them.
80
        if (!isset($params['treeMenu'])) {
81
            $treeMenu =& new HTML_TreeMenu();
82
        } else {
83
            $treeMenu =& $params['treeMenu'];
84
        }
85
 
86
        // always start at level 1
87
        if (!isset($params['currentLevel'])) {
88
            $params['currentLevel'] = 1;
89
        }
90
 
91
        // have to use a while loop here because foreach works on a copy of the array and
92
        // the child nodes are passed by reference during the recursion so that the parent
93
        // will know when they have been hit.
94
        reset($params['structure']);
95
        while(list($key, $node) = each($params['structure'])) {
96
            // see if we've already been here before
97
            if (isset($node['hit'])) {
98
                continue;
99
            }
100
 
101
            // mark that we've hit this node
102
            $params['structure'][$key]['hit'] = $node['hit'] = true;
103
            $tag = array(
104
            'text' => $node[$params['textField']],
105
            'link' => $node[$params['linkField']],
106
            'ensureVisible' => isset($node['ensureVisible']) ? $node['ensureVisible'] : false,
107
            );
108
            $options = isset($params['options']) ? array_merge($params['options'], $tag) : $tag;
109
            $events = isset($node['events']) ? $node['events'] : array();
110
            $parentNode =& $treeMenu->addItem(new HTML_TreeNode($options, $events));
111
            // see if it has children
112
            if (($node['r'] - 1) != $node['l']) {
113
                $children = array();
114
                // harvest all the children
115
                $tempStructure = $params['structure'];
116
                foreach ($tempStructure as $childKey => $childNode) {
117
                    if (!isset($childNode['hit']) &&
118
                    $childNode['l'] > $node['l'] &&
119
                    $childNode['r'] < $node['r'] &&
120
                    $childNode['rootid'] == $node['rootid']) {
121
                        // important that we assign it by reference here, so that when the child
122
                        // marks itself 'hit' the parent loops will know
123
                        $children[] =& $params['structure'][$childKey];
124
                    }
125
                }
126
 
127
                $recurseParams = $params;
128
                $recurseParams['structure'] = $children;
129
                $recurseParams['treeMenu'] =& $parentNode;
130
                $recurseParams['currentLevel']++;
131
                $this->_createFromStructure($recurseParams);
132
            }
133
        }
134
 
135
        return $treeMenu;
136
    }
137
 
138
    // }}}
139
    // {{{ printTree()
140
 
141
    /**
142
    * Print's the current tree using the output driver
143
    *
144
    * @access public
145
    */
146
    function printTree() {
147
        $options = $this->_getOptions('printTree');
148
        $tree =& new HTML_TreeMenu_DHTML($this->_structTreeMenu, $options);
149
        $tree->printMenu();
150
    }
151
 
152
    // }}}
153
    // {{{ printListbox()
154
 
155
    /**
156
    * Print's a listbox representing the current tree
157
    *
158
    * @access public
159
    */
160
    function printListbox() {
161
        $options = $this->_getOptions('printListbox');
162
        $listBox  =& new HTML_TreeMenu_Listbox($this->_structTreeMenu, $options);
163
        $listBox->printMenu();
164
    }
165
    // }}}
166
 
167
    // }}}
168
    // {{{ tree_toHTML()
169
 
170
    /**
171
    * Returns the HTML for the DHTML-menu. This method can be
172
    * used instead of printMenu() to use the menu system
173
    * with a template system.
174
    *
175
    * @access public
176
    * @return string The HTML for the menu
177
    * @Author Emanuel Zueger
178
    */
179
    function tree_toHTML() {
180
        $options = $this->_getOptions('toHTML');
181
        $tree  =& new HTML_TreeMenu_DHTML($this->_structTreeMenu, $options);
182
        return $tree->toHTML();
183
    }
184
 
185
    // }}}
186
    // {{{ listbox_toHTML()
187
 
188
    /**
189
    * Returns the HTML for the listbox. This method can be
190
    * used instead of printListbox() to use the menu system
191
    * with a template system.
192
    *
193
    * @access public
194
    * @return string The HTML for the listbox
195
    * @author Emanuel Zueger
196
    */
197
    function listbox_toHTML() {
198
        $options = $this->_getOptions('toHTML');
199
        $listBox  =& new HTML_TreeMenu_Listbox($this->_structTreeMenu, $options);
200
        return $listBox->toHTML();
201
    }
202
 
203
    // }}}
204
}
205
?>