-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathACTree.class.php
413 lines (365 loc) · 14.6 KB
/
ACTree.class.php
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
<?php
/**
* @license GPL
* @version $Id: $
* @abstract
* http://www.phpriot.com/articles/nested-trees-2/7
* This code is in development, it should extend the ACTable class, and use native
* db calls.
*
*/
if(!function_exists("pg_query")) {
function pg_query($sql) {
return mysql_query($sql);
}
function pg_fetch_object($res) {
return mysql_fetch_object($res);
}
}
class NestedTree
{
/**
* Constructor. Set the database table name and necessary field names
*
* @param string $table Name of the tree database table
* @param string $idField Name of the primary key ID field
* @param string $parentField Name of the parent ID field
* @param string $sortField Name of the field to sort data.
*/
function NestedTree($table, $idField, $parentField, $sortField)
{
$this->table = $table;
$this->fields = array('id' => $idField,
'parent' => $parentField,
'sort' => $sortField);
}
/**
* A utility function to return an array of the fields
* that need to be selected in SQL select queries
*
* @return array An indexed array of fields to select
*/
function _getFields()
{
return array($this->fields['id'], $this->fields['parent'], $this->fields['sort'],
'nleft', 'nright', 'nlevel');
}
/**
* Fetch the node data for the node identified by $id
*
* @param int $id The ID of the node to fetch
* @return object An object containing the node's
* data, or null if node not found
*/
function getNode($id)
{
$query = sprintf('select %s from %s where %s = %d', join(',', $this->_getFields()),
$this->table,
$this->fields['id'],
$id);
$result = pg_query($query);
if ($row = pg_fetch_object($result))
return $row;
return null;
}
/**
* Fetch the descendants of a node, or if no node is specified, fetch the
* entire tree. Optionally, only return child data instead of all descendant
* data.
*
* @param int $id The ID of the node to fetch descendant data for.
* Specify an invalid ID (e.g. 0) to retrieve all data.
* @param bool $includeSelf Whether or not to include the passed node in the
* the results. This has no meaning if fetching entire tree.
* @param bool $childrenOnly True if only returning children data. False if
* returning all descendant data
* @return array The descendants of the passed now
*/
function getDescendants($id = 0, $includeSelf = false, $childrenOnly = false)
{
$idField = $this->fields['id'];
$node = $this->getNode($id);
if (is_null($node)) {
$nleft = 0;
$nright = 0;
$parent_id = 0;
}
else {
$nleft = $node->nleft;
$nright = $node->nright;
$parent_id = $node->$idField;
}
if ($childrenOnly) {
if ($includeSelf) {
$query = sprintf('select %s from %s where %s = %d or %s = %d order by nleft',
join(',', $this->_getFields()),
$this->table,
$this->fields['id'],
$parent_id,
$this->fields['parent'],
$parent_id);
}
else {
$query = sprintf('select %s from %s where %s = %d order by nleft',
join(',', $this->_getFields()),
$this->table,
$this->fields['parent'],
$parent_id);
}
}
else {
if ($nleft > 0 && $includeSelf) {
$query = sprintf('select %s from %s where nleft >= %d and nright <= %d order by nleft',
join(',', $this->_getFields()),
$this->table,
$nleft,
$nright);
}
else if ($nleft > 0) {
$query = sprintf('select %s from %s where nleft > %d and nright < %d order by nleft',
join(',', $this->_getFields()),
$this->table,
$nleft,
$nright);
}
else {
$query = sprintf('select %s from %s order by nleft',
join(',', $this->_getFields()),
$this->table);
}
}
$result = pg_query($query);
$arr = array();
while ($row = pg_fetch_object($result)) {
$arr[$row->$idField] = $row;
}
return $arr;
}
/**
* Fetch the children of a node, or if no node is specified, fetch the
* top level items.
*
* @param int $id The ID of the node to fetch child data for.
* @param bool $includeSelf Whether or not to include the passed node in the
* the results.
* @return array The children of the passed node
*/
function getChildren($id = 0, $includeSelf = false)
{
return $this->getDescendants($id, $includeSelf, true);
}
/**
* Fetch the path to a node. If an invalid node is passed, an empty array is returned.
* If a top level node is passed, an array containing on that node is included (if
* 'includeSelf' is set to true, otherwise an empty array)
*
* @param int $id The ID of the node to fetch child data for.
* @param bool $includeSelf Whether or not to include the passed node in the
* the results.
* @return array An array of each node to passed node
*/
function getPath($id = 0, $includeSelf = false)
{
$node = $this->getNode($id);
if (is_null($node))
return array();
if ($includeSelf) {
$query = sprintf('select %s from %s where nleft <= %d and nright >= %d order by nlevel',
join(',', $this->_getFields()),
$this->table,
$node->nleft,
$node->nright);
}
else {
$query = sprintf('select %s from %s where nleft < %d and nright > %d order by nlevel',
join(',', $this->_getFields()),
$this->table,
$node->nleft,
$node->nright);
}
$result = pg_query($query);
$idField = $this->fields['id'];
$arr = array();
while ($row = pg_fetch_object($result)) {
$arr[$row->$idField] = $row;
}
return $arr;
}
/**
* Check if one node descends from another node. If either node is not
* found, then false is returned.
*
* @param int $descendant_id The node that potentially descends
* @param int $ancestor_id The node that is potentially descended from
* @return bool True if $descendant_id descends from $ancestor_id, false otherwise
*/
function isDescendantOf($descendant_id, $ancestor_id)
{
$node = $this->getNode($ancestor_id);
if (is_null($node))
return false;
$query = sprintf('select count(*) as is_descendant
from %s
where %s = %d
and nleft > %d
and nright < %d',
$this->table,
$this->fields['id'],
$descendant_id,
$node->nleft,
$node->nright);
$result = pg_query($query);
if ($row = pg_fetch_object($result)) {
return $row->is_descendant > 0;
}
return false;
}
/**
* Check if one node is a child of another node. If either node is not
* found, then false is returned.
*
* @param int $child_id The node that is possibly a child
* @param int $parent_id The node that is possibly a parent
* @return bool True if $child_id is a child of $parent_id, false otherwise
*/
function isChildOf($child_id, $parent_id)
{
$query = sprintf('select count(*) as is_child from %s where %s = %d and %s = %d',
$this->table,
$this->fields['id'],
$child_id,
$this->fields['parent'],
$parent_id);
$result = pg_query($query);
if ($row = pg_fetch_object($result)) {
return $row->is_child > 0;
}
return false;
}
/**
* Find the number of descendants a node has
*
* @param int $id The ID of the node to search for. Pass 0 to count all nodes in the tree.
* @return int The number of descendants the node has, or -1 if the node isn't found.
*/
function numDescendants($id)
{
if ($id == 0) {
$query = sprintf('select count(*) as num_descendants from %s', $this->table);
$result = pg_query($query);
if ($row = pg_fetch_object($result))
return (int) $row->num_descendants;
}
else {
$node = $this->getNode($id);
if (!is_null($node)) {
return ($node->nright - $node->nleft - 1) / 2;
}
}
return -1;
}
/**
* Find the number of children a node has
*
* @param int $id The ID of the node to search for. Pass 0 to count the first level items
* @return int The number of descendants the node has, or -1 if the node isn't found.
*/
function numChildren($id)
{
$query = sprintf('select count(*) as num_children from %s where %s = %d',
$this->table,
$this->fields['parent'],
$id);
$result = pg_query($query);
if ($row = pg_fetch_object($result))
return (int) $row->num_children;
return -1;
}
/**
* Fetch the tree data, nesting within each node references to the node's children
*
* @return array The tree with the node's child data
*/
function getTreeWithChildren()
{
$idField = $this->fields['id'];
$parentField = $this->fields['parent'];
$query = sprintf('select %s from %s order by %s',
join(',', $this->_getFields()),
$this->table,
$this->fields['sort']);
$result = pg_query($query);
// create a root node to hold child data about first level items
$root = new stdClass;
$root->$idField = 0;
$root->children = array();
$arr = array($root);
// populate the array and create an empty children array
while ($row = pg_fetch_object($result)) {
$arr[$row->$idField] = $row;
$arr[$row->$idField]->children = array();
}
// now process the array and build the child data
foreach ($arr as $id => $row) {
if (isset($row->$parentField))
$arr[$row->$parentField]->children[$id] = $id;
}
return $arr;
}
/**
* Rebuilds the tree data and saves it to the database
*/
function rebuild()
{
$data = $this->getTreeWithChildren();
$n = 0; // need a variable to hold the running n tally
$level = 0; // need a variable to hold the running level tally
// invoke the recursive function. Start it processing
// on the fake "root node" generated in getTreeWithChildren().
// because this node doesn't really exist in the database, we
// give it an initial nleft value of 0 and an nlevel of 0.
$this->_generateTreeData($data, 0, 0, $n);
// at this point the the root node will have nleft of 0, nlevel of 0
// and nright of (tree size * 2 + 1)
foreach ($data as $id => $row) {
// skip the root node
if ($id == 0)
continue;
$query = sprintf('update %s set nlevel = %d, nleft = %d, nright = %d where %s = %d',
$this->table,
$row->nlevel,
$row->nleft,
$row->nright,
$this->fields['id'],
$id);
pg_query($query);
}
}
/**
* Generate the tree data. A single call to this generates the n-values for
* 1 node in the tree. This function assigns the passed in n value as the
* node's nleft value. It then processes all the node's children (which
* in turn recursively processes that node's children and so on), and when
* it is finally done, it takes the update n-value and assigns it as its
* nright value. Because it is passed as a reference, the subsequent changes
* in subrequests are held over to when control is returned so the nright
* can be assigned.
*
* @param array &$arr A reference to the data array, since we need to
* be able to update the data in it
* @param int $id The ID of the current node to process
* @param int $level The nlevel to assign to the current node
* @param int &$n A reference to the running tally for the n-value
*/
function _generateTreeData(&$arr, $id, $level, &$n)
{
$arr[$id]->nlevel = $level;
$arr[$id]->nleft = $n++;
// loop over the node's children and process their data
// before assigning the nright value
foreach ($arr[$id]->children as $child_id) {
$this->_generateTreeData($arr, $child_id, $level + 1, $n);
}
$arr[$id]->nright = $n++;
}
}