|
1
|
|
|
<?php |
|
2
|
|
|
// $Id: tree.php 506 2006-05-26 23:10:37Z skalpa $ |
|
3
|
|
|
// ------------------------------------------------------------------------ // |
|
4
|
|
|
// XOOPS - PHP Content Management System // |
|
5
|
|
|
// Copyright (c) 2000 XOOPS.org // |
|
6
|
|
|
// <http://www.xoops.org/> // |
|
7
|
|
|
// ------------------------------------------------------------------------ // |
|
8
|
|
|
// This program is free software; you can redistribute it and/or modify // |
|
9
|
|
|
// it under the terms of the GNU General Public License as published by // |
|
10
|
|
|
// the Free Software Foundation; either version 2 of the License, or // |
|
11
|
|
|
// (at your option) any later version. // |
|
12
|
|
|
// // |
|
13
|
|
|
// You may not change or alter any portion of this comment or credits // |
|
14
|
|
|
// of supporting developers from this source code or any supporting // |
|
15
|
|
|
// source code which is considered copyrighted (c) material of the // |
|
16
|
|
|
// original comment or credit authors. // |
|
17
|
|
|
// // |
|
18
|
|
|
// This program is distributed in the hope that it will be useful, // |
|
19
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|
20
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|
21
|
|
|
// GNU General Public License for more details. // |
|
22
|
|
|
// // |
|
23
|
|
|
// You should have received a copy of the GNU General Public License // |
|
24
|
|
|
// along with this program; if not, write to the Free Software // |
|
25
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|
26
|
|
|
// ------------------------------------------------------------------------ // |
|
27
|
|
|
// Author: Kazumi Ono (AKA onokazu) // |
|
28
|
|
|
// URL: http://www.xoops.org/ http://jp.xoops.org/ http://www.myweb.ne.jp/ // |
|
29
|
|
|
// Project: The XOOPS Project (http://www.xoops.org/) // |
|
30
|
|
|
// ------------------------------------------------------------------------- // |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A tree structures with {@link XoopsObject}s as nodes |
|
34
|
|
|
* |
|
35
|
|
|
* @package kernel |
|
36
|
|
|
* @subpackage core |
|
37
|
|
|
* |
|
38
|
|
|
* @author Kazumi Ono <[email protected]> |
|
39
|
|
|
* @copyright (c) 2000-2003 The Xoops Project - www.xoops.org |
|
40
|
|
|
*/ |
|
41
|
|
|
class Bookshop_XoopsObjectTree |
|
42
|
|
|
{ |
|
43
|
|
|
/**#@+ |
|
44
|
|
|
* @access private |
|
45
|
|
|
*/ |
|
46
|
|
|
public $_parentId; |
|
47
|
|
|
public $_myId; |
|
48
|
|
|
public $_rootId = null; |
|
49
|
|
|
public $_tree = array(); |
|
50
|
|
|
public $_objects; |
|
51
|
|
|
/**#@-*/ |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Constructor |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $objectArr Array of {@link XoopsObject}s |
|
57
|
|
|
* @param string $myId field name of object ID |
|
58
|
|
|
* @param string $parentId field name of parent object ID |
|
59
|
|
|
* @param string $rootId field name of root object ID |
|
|
|
|
|
|
60
|
|
|
**/ |
|
61
|
|
|
public function __construct(&$objectArr, $myId, $parentId, $rootId = null) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->_objects =& $objectArr; |
|
64
|
|
|
$this->_myId = $myId; |
|
65
|
|
|
$this->_parentId = $parentId; |
|
66
|
|
|
if (isset($rootId)) { |
|
67
|
|
|
$this->_rootId = $rootId; |
|
68
|
|
|
} |
|
69
|
|
|
$this->_initialize(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Initialize the object |
|
74
|
|
|
* |
|
75
|
|
|
* @access private |
|
76
|
|
|
**/ |
|
77
|
|
|
public function _initialize() |
|
78
|
|
|
{ |
|
79
|
|
|
foreach (array_keys($this->_objects) as $i) { |
|
80
|
|
|
$key1 = $this->_objects[$i]->getVar($this->_myId); |
|
81
|
|
|
$this->_tree[$key1]['obj'] =& $this->_objects[$i]; |
|
82
|
|
|
$key2 = $this->_objects[$i]->getVar($this->_parentId); |
|
83
|
|
|
$this->_tree[$key1]['parent'] = $key2; |
|
84
|
|
|
$this->_tree[$key2]['child'][] = $key1; |
|
85
|
|
|
if (isset($this->_rootId)) { |
|
86
|
|
|
$this->_tree[$key1]['root'] = $this->_objects[$i]->getVar($this->_rootId); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the tree |
|
93
|
|
|
* |
|
94
|
|
|
* @return array Associative array comprising the tree |
|
95
|
|
|
**/ |
|
96
|
|
|
public function &getTree() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->_tree; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* returns an object from the tree specified by its id |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $key ID of the object to retrieve |
|
105
|
|
|
* @return object Object within the tree |
|
106
|
|
|
**/ |
|
107
|
|
|
public function &getByKey($key) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->_tree[$key]['obj']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* returns an array of all the first child object of an object specified by its id |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $key ID of the parent object |
|
116
|
|
|
* @return array Array of children of the parent |
|
117
|
|
|
**/ |
|
118
|
|
|
public function getFirstChild($key) |
|
119
|
|
|
{ |
|
120
|
|
|
$ret = array(); |
|
121
|
|
|
if (isset($this->_tree[$key]['child'])) { |
|
122
|
|
|
foreach ($this->_tree[$key]['child'] as $childkey) { |
|
123
|
|
|
$ret[$childkey] =& $this->_tree[$childkey]['obj']; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $ret; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* returns an array of all child objects of an object specified by its id |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $key ID of the parent |
|
134
|
|
|
* @param array $ret (Empty when called from client) Array of children from previous recursions. |
|
135
|
|
|
* @return array Array of child nodes. |
|
136
|
|
|
**/ |
|
137
|
|
|
public function getAllChild($key, $ret = array()) |
|
138
|
|
|
{ |
|
139
|
|
|
if (isset($this->_tree[$key]['child'])) { |
|
140
|
|
|
foreach ($this->_tree[$key]['child'] as $childkey) { |
|
141
|
|
|
$ret[$childkey] =& $this->_tree[$childkey]['obj']; |
|
142
|
|
|
$children =& $this->getAllChild($childkey, $ret); |
|
143
|
|
|
foreach (array_keys($children) as $newkey) { |
|
144
|
|
|
$ret[$newkey] =& $children[$newkey]; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $ret; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* returns an array of all parent objects. |
|
154
|
|
|
* the key of returned array represents how many levels up from the specified object |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $key ID of the child object |
|
157
|
|
|
* @param array $ret (empty when called from outside) Result from previous recursions |
|
158
|
|
|
* @param int $uplevel (empty when called from outside) level of recursion |
|
159
|
|
|
* @return array Array of parent nodes. |
|
160
|
|
|
**/ |
|
161
|
|
|
public function getAllParent($key, $ret = array(), $uplevel = 1) |
|
162
|
|
|
{ |
|
163
|
|
|
if (isset($this->_tree[$key]['parent']) && isset($this->_tree[$this->_tree[$key]['parent']]['obj'])) { |
|
164
|
|
|
$ret[$uplevel] =& $this->_tree[$this->_tree[$key]['parent']]['obj']; |
|
165
|
|
|
$parents =& $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel + 1); |
|
166
|
|
|
foreach (array_keys($parents) as $newkey) { |
|
167
|
|
|
$ret[$newkey] =& $parents[$newkey]; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $ret; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Make options for a select box from |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $fieldName Name of the member variable from the |
|
178
|
|
|
* node objects that should be used as the title for the options. |
|
179
|
|
|
* @param string $selected Value to display as selected |
|
180
|
|
|
* @param int $key ID of the object to display as the root of select options |
|
181
|
|
|
* @param string $ret (reference to a string when called from outside) Result from previous recursions |
|
182
|
|
|
* @param string $prefix_orig String to indent items at deeper levels |
|
183
|
|
|
* @param string $prefix_curr String to indent the current item |
|
184
|
|
|
* |
|
185
|
|
|
* @return void |
|
186
|
|
|
@access private |
|
187
|
|
|
*/ |
|
188
|
|
|
public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '') |
|
189
|
|
|
{ |
|
190
|
|
|
if ($key > 0) { |
|
191
|
|
|
$value = $this->_tree[$key]['obj']->getVar($this->_myId); |
|
192
|
|
|
$ret .= '<option value=\'' . $value . '\''; |
|
193
|
|
|
if ($value == $selected) { |
|
194
|
|
|
$ret .= ' selected="selected"'; |
|
195
|
|
|
} |
|
196
|
|
|
$ret .= '>' . $prefix_curr . $this->_tree[$key]['obj']->getVar($fieldName) . '</option>'; |
|
197
|
|
|
$prefix_curr .= $prefix_orig; |
|
198
|
|
|
} |
|
199
|
|
|
if (isset($this->_tree[$key]['child']) && !empty($this->_tree[$key]['child'])) { |
|
200
|
|
|
foreach ($this->_tree[$key]['child'] as $childkey) { |
|
201
|
|
|
$this->_makeSelBoxOptions($fieldName, $selected, $childkey, $ret, $prefix_orig, $prefix_curr); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Make a select box with options from the tree |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $name Name of the select box |
|
210
|
|
|
* @param string $fieldName Name of the member variable from the node objects that should be used as the title for the options. |
|
211
|
|
|
* @param string $prefix String to indent deeper levels |
|
212
|
|
|
* @param string $selected Value to display as selected |
|
213
|
|
|
* @param bool|string $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
|
214
|
|
|
* @param integer $key ID of the object to display as the root of select options |
|
215
|
|
|
* @param string $additional |
|
216
|
|
|
* @return string HTML select box |
|
217
|
|
|
*/ |
|
218
|
|
|
public function makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = '', $key = 0, $additional = '') |
|
219
|
|
|
{ |
|
220
|
|
|
$ret = "<select id='" . $name . "' name='" . $name . "'"; |
|
221
|
|
|
if ($additional != '') { |
|
222
|
|
|
$ret .= $additional; |
|
223
|
|
|
} |
|
224
|
|
|
$ret .= '>'; |
|
225
|
|
|
if ($addEmptyOption != '') { |
|
226
|
|
|
$ret .= '<option value=\'0\'>' . $addEmptyOption . '</option>'; |
|
227
|
|
|
} |
|
228
|
|
|
$this->_makeSelBoxOptions($fieldName, $selected, $key, $ret, $prefix); |
|
229
|
|
|
|
|
230
|
|
|
return $ret . '</select>'; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.