3
* A class for displaying various tree-like structures.
5
* Extend the Walker class to use it, see examples below. Child classes
6
* do not need to implement all of the abstract methods in the class. The child
7
* only needs to implement the methods that are needed.
16
* What the class handles.
34
* Max number of pages walked by the paged walker
40
protected $max_pages = 1;
43
* Whether the current element has children or not.
45
* To be used in start_el().
51
protected $has_children;
54
* Make private properties readable for backwards compatibility.
59
* @param string $name Property to get.
60
* @return mixed Property.
62
public function __get( $name ) {
67
* Make private properties settable for backwards compatibility.
72
* @param string $name Property to set.
73
* @param mixed $value Property value.
74
* @return mixed Newly-set property.
76
public function __set( $name, $value ) {
77
return $this->$name = $value;
81
* Make private properties checkable for backwards compatibility.
86
* @param string $name Property to check if set.
87
* @return bool Whether the property is set.
89
public function __isset( $name ) {
90
return isset( $this->$name );
94
* Make private properties un-settable for backwards compatibility.
99
* @param string $name Property to unset.
101
public function __unset( $name ) {
102
unset( $this->$name );
106
* Starts the list before the elements are added.
108
* The $args parameter holds additional values that may be used with the child
109
* class methods. This method is called at the start of the output list.
114
* @param string $output Passed by reference. Used to append additional content.
115
* @param int $depth Depth of the item.
116
* @param array $args An array of additional arguments.
118
public function start_lvl( &$output, $depth = 0, $args = array() ) {}
121
* Ends the list of after the elements are added.
123
* The $args parameter holds additional values that may be used with the child
124
* class methods. This method finishes the list at the end of output of the elements.
129
* @param string $output Passed by reference. Used to append additional content.
130
* @param int $depth Depth of the item.
131
* @param array $args An array of additional arguments.
133
public function end_lvl( &$output, $depth = 0, $args = array() ) {}
136
* Start the element output.
138
* The $args parameter holds additional values that may be used with the child
139
* class methods. Includes the element output also.
144
* @param string $output Passed by reference. Used to append additional content.
145
* @param object $object The data object.
146
* @param int $depth Depth of the item.
147
* @param array $args An array of additional arguments.
148
* @param int $current_object_id ID of the current item.
150
public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
153
* Ends the element output, if needed.
155
* The $args parameter holds additional values that may be used with the child class methods.
160
* @param string $output Passed by reference. Used to append additional content.
161
* @param object $object The data object.
162
* @param int $depth Depth of the item.
163
* @param array $args An array of additional arguments.
165
public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
168
* Traverse elements to create list from elements.
170
* Display one element if the element doesn't have any children otherwise,
171
* display the element and its children. Will only traverse up to the max
172
* depth and no ignore elements under that depth. It is possible to set the
173
* max depth to include all depths, see walk() method.
175
* This method should not be called directly, use the walk() method instead.
179
* @param object $element Data object.
180
* @param array $children_elements List of elements to continue traversing.
181
* @param int $max_depth Max depth to traverse.
182
* @param int $depth Depth of current element.
183
* @param array $args An array of arguments.
184
* @param string $output Passed by reference. Used to append additional content.
185
* @return null Null on failure with no changes to parameters.
187
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
192
$id_field = $this->db_fields['id'];
193
$id = $element->$id_field;
195
//display this element
196
$this->has_children = ! empty( $children_elements[ $id ] );
197
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
198
$args[0]['has_children'] = $this->has_children; // Backwards compatibility.
201
$cb_args = array_merge( array(&$output, $element, $depth), $args);
202
call_user_func_array(array($this, 'start_el'), $cb_args);
204
// descend only when the depth is right and there are childrens for this element
205
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
207
foreach( $children_elements[ $id ] as $child ){
209
if ( !isset($newlevel) ) {
211
//start the child delimiter
212
$cb_args = array_merge( array(&$output, $depth), $args);
213
call_user_func_array(array($this, 'start_lvl'), $cb_args);
215
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
217
unset( $children_elements[ $id ] );
220
if ( isset($newlevel) && $newlevel ){
221
//end the child delimiter
222
$cb_args = array_merge( array(&$output, $depth), $args);
223
call_user_func_array(array($this, 'end_lvl'), $cb_args);
227
$cb_args = array_merge( array(&$output, $element, $depth), $args);
228
call_user_func_array(array($this, 'end_el'), $cb_args);
232
* Display array of elements hierarchically.
234
* Does not assume any existing order of elements.
236
* $max_depth = -1 means flatly display every element.
237
* $max_depth = 0 means display all levels.
238
* $max_depth > 0 specifies the number of display levels.
242
* @param array $elements An array of elements.
243
* @param int $max_depth The maximum hierarchical depth.
244
* @return string The hierarchical item output.
246
public function walk( $elements, $max_depth) {
248
$args = array_slice(func_get_args(), 2);
251
if ($max_depth < -1) //invalid parameter
254
if (empty($elements)) //nothing to walk
257
$parent_field = $this->db_fields['parent'];
260
if ( -1 == $max_depth ) {
261
$empty_array = array();
262
foreach ( $elements as $e )
263
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
268
* Need to display in hierarchical order.
269
* Separate elements into two buckets: top level and children elements.
270
* Children_elements is two dimensional array, eg.
271
* Children_elements[10][] contains all sub-elements whose parent is 10.
273
$top_level_elements = array();
274
$children_elements = array();
275
foreach ( $elements as $e) {
276
if ( 0 == $e->$parent_field )
277
$top_level_elements[] = $e;
279
$children_elements[ $e->$parent_field ][] = $e;
283
* When none of the elements is top level.
284
* Assume the first one must be root of the sub elements.
286
if ( empty($top_level_elements) ) {
288
$first = array_slice( $elements, 0, 1 );
291
$top_level_elements = array();
292
$children_elements = array();
293
foreach ( $elements as $e) {
294
if ( $root->$parent_field == $e->$parent_field )
295
$top_level_elements[] = $e;
297
$children_elements[ $e->$parent_field ][] = $e;
301
foreach ( $top_level_elements as $e )
302
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
305
* If we are displaying all levels, and remaining children_elements is not empty,
306
* then we got orphans, which should be displayed regardless.
308
if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
309
$empty_array = array();
310
foreach ( $children_elements as $orphans )
311
foreach( $orphans as $op )
312
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
319
* paged_walk() - produce a page of nested elements
321
* Given an array of hierarchical elements, the maximum depth, a specific page number,
322
* and number of elements per page, this function first determines all top level root elements
323
* belonging to that page, then lists them and all of their children in hierarchical order.
325
* $max_depth = 0 means display all levels.
326
* $max_depth > 0 specifies the number of display levels.
330
* @param int $max_depth The maximum hierarchical depth.
331
* @param int $page_num The specific page number, beginning with 1.
332
* @return string XHTML of the specified page of elements
334
public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
337
if ( empty($elements) || $max_depth < -1 )
340
$args = array_slice( func_get_args(), 4 );
343
$parent_field = $this->db_fields['parent'];
346
if ( -1 == $max_depth )
347
$total_top = count( $elements );
348
if ( $page_num < 1 || $per_page < 0 ) {
352
if ( -1 == $max_depth )
354
$this->max_pages = 1;
357
$start = ( (int)$page_num - 1 ) * (int)$per_page;
358
$end = $start + $per_page;
359
if ( -1 == $max_depth )
360
$this->max_pages = ceil($total_top / $per_page);
364
if ( -1 == $max_depth ) {
365
if ( !empty($args[0]['reverse_top_level']) ) {
366
$elements = array_reverse( $elements );
368
$start = $total_top - $end;
369
$end = $total_top - $oldstart;
372
$empty_array = array();
373
foreach ( $elements as $e ) {
375
if ( $count < $start )
377
if ( $count >= $end )
379
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
385
* Separate elements into two buckets: top level and children elements.
386
* Children_elements is two dimensional array, e.g.
387
* $children_elements[10][] contains all sub-elements whose parent is 10.
389
$top_level_elements = array();
390
$children_elements = array();
391
foreach ( $elements as $e) {
392
if ( 0 == $e->$parent_field )
393
$top_level_elements[] = $e;
395
$children_elements[ $e->$parent_field ][] = $e;
398
$total_top = count( $top_level_elements );
400
$this->max_pages = ceil($total_top / $per_page);
404
if ( !empty($args[0]['reverse_top_level']) ) {
405
$top_level_elements = array_reverse( $top_level_elements );
407
$start = $total_top - $end;
408
$end = $total_top - $oldstart;
410
if ( !empty($args[0]['reverse_children']) ) {
411
foreach ( $children_elements as $parent => $children )
412
$children_elements[$parent] = array_reverse( $children );
415
foreach ( $top_level_elements as $e ) {
418
// For the last page, need to unset earlier children in order to keep track of orphans.
419
if ( $end >= $total_top && $count < $start )
420
$this->unset_children( $e, $children_elements );
422
if ( $count < $start )
425
if ( $count >= $end )
428
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
431
if ( $end >= $total_top && count( $children_elements ) > 0 ) {
432
$empty_array = array();
433
foreach ( $children_elements as $orphans )
434
foreach( $orphans as $op )
435
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
441
public function get_number_of_root_elements( $elements ){
444
$parent_field = $this->db_fields['parent'];
446
foreach ( $elements as $e) {
447
if ( 0 == $e->$parent_field )
453
// Unset all the children for a given top level element.
454
public function unset_children( $e, &$children_elements ){
456
if ( !$e || !$children_elements )
459
$id_field = $this->db_fields['id'];
462
if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
463
foreach ( (array) $children_elements[$id] as $child )
464
$this->unset_children( $child, $children_elements );
466
if ( isset($children_elements[$id]) )
467
unset( $children_elements[$id] );