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.
33 * Max number of pages walked by the paged walker
38 public $max_pages = 1;
41 * Whether the current element has children or not.
43 * To be used in start_el().
51 * Starts the list before the elements are added.
53 * The $args parameter holds additional values that may be used with the child
54 * class methods. This method is called at the start of the output list.
59 * @param string $output Passed by reference. Used to append additional content.
60 * @param int $depth Depth of the item.
61 * @param array $args An array of additional arguments.
63 public function start_lvl( &$output, $depth = 0, $args = array() ) {}
66 * Ends the list of after the elements are added.
68 * The $args parameter holds additional values that may be used with the child
69 * class methods. This method finishes the list at the end of output of the elements.
74 * @param string $output Passed by reference. Used to append additional content.
75 * @param int $depth Depth of the item.
76 * @param array $args An array of additional arguments.
78 public function end_lvl( &$output, $depth = 0, $args = array() ) {}
81 * Start the element output.
83 * The $args parameter holds additional values that may be used with the child
84 * class methods. Includes the element output also.
89 * @param string $output Passed by reference. Used to append additional content.
90 * @param object $object The data object.
91 * @param int $depth Depth of the item.
92 * @param array $args An array of additional arguments.
93 * @param int $current_object_id ID of the current item.
95 public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
98 * Ends the element output, if needed.
100 * The $args parameter holds additional values that may be used with the child class methods.
105 * @param string $output Passed by reference. Used to append additional content.
106 * @param object $object The data object.
107 * @param int $depth Depth of the item.
108 * @param array $args An array of additional arguments.
110 public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
113 * Traverse elements to create list from elements.
115 * Display one element if the element doesn't have any children otherwise,
116 * display the element and its children. Will only traverse up to the max
117 * depth and no ignore elements under that depth. It is possible to set the
118 * max depth to include all depths, see walk() method.
120 * This method should not be called directly, use the walk() method instead.
124 * @param object $element Data object.
125 * @param array $children_elements List of elements to continue traversing.
126 * @param int $max_depth Max depth to traverse.
127 * @param int $depth Depth of current element.
128 * @param array $args An array of arguments.
129 * @param string $output Passed by reference. Used to append additional content.
130 * @return null Null on failure with no changes to parameters.
132 public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
137 $id_field = $this->db_fields['id'];
138 $id = $element->$id_field;
140 //display this element
141 $this->has_children = ! empty( $children_elements[ $id ] );
142 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
143 $args[0]['has_children'] = $this->has_children; // Backwards compatibility.
146 $cb_args = array_merge( array(&$output, $element, $depth), $args);
147 call_user_func_array(array($this, 'start_el'), $cb_args);
149 // descend only when the depth is right and there are childrens for this element
150 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
152 foreach( $children_elements[ $id ] as $child ){
154 if ( !isset($newlevel) ) {
156 //start the child delimiter
157 $cb_args = array_merge( array(&$output, $depth), $args);
158 call_user_func_array(array($this, 'start_lvl'), $cb_args);
160 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
162 unset( $children_elements[ $id ] );
165 if ( isset($newlevel) && $newlevel ){
166 //end the child delimiter
167 $cb_args = array_merge( array(&$output, $depth), $args);
168 call_user_func_array(array($this, 'end_lvl'), $cb_args);
172 $cb_args = array_merge( array(&$output, $element, $depth), $args);
173 call_user_func_array(array($this, 'end_el'), $cb_args);
177 * Display array of elements hierarchically.
179 * Does not assume any existing order of elements.
181 * $max_depth = -1 means flatly display every element.
182 * $max_depth = 0 means display all levels.
183 * $max_depth > 0 specifies the number of display levels.
187 * @param array $elements An array of elements.
188 * @param int $max_depth The maximum hierarchical depth.
189 * @return string The hierarchical item output.
191 public function walk( $elements, $max_depth) {
193 $args = array_slice(func_get_args(), 2);
196 if ($max_depth < -1) //invalid parameter
199 if (empty($elements)) //nothing to walk
202 $parent_field = $this->db_fields['parent'];
205 if ( -1 == $max_depth ) {
206 $empty_array = array();
207 foreach ( $elements as $e )
208 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
213 * Need to display in hierarchical order.
214 * Separate elements into two buckets: top level and children elements.
215 * Children_elements is two dimensional array, eg.
216 * Children_elements[10][] contains all sub-elements whose parent is 10.
218 $top_level_elements = array();
219 $children_elements = array();
220 foreach ( $elements as $e) {
221 if ( 0 == $e->$parent_field )
222 $top_level_elements[] = $e;
224 $children_elements[ $e->$parent_field ][] = $e;
228 * When none of the elements is top level.
229 * Assume the first one must be root of the sub elements.
231 if ( empty($top_level_elements) ) {
233 $first = array_slice( $elements, 0, 1 );
236 $top_level_elements = array();
237 $children_elements = array();
238 foreach ( $elements as $e) {
239 if ( $root->$parent_field == $e->$parent_field )
240 $top_level_elements[] = $e;
242 $children_elements[ $e->$parent_field ][] = $e;
246 foreach ( $top_level_elements as $e )
247 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
250 * If we are displaying all levels, and remaining children_elements is not empty,
251 * then we got orphans, which should be displayed regardless.
253 if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
254 $empty_array = array();
255 foreach ( $children_elements as $orphans )
256 foreach( $orphans as $op )
257 $this->display_element( $op, $empty_array, 1, 0, $args, $output );
264 * paged_walk() - produce a page of nested elements
266 * Given an array of hierarchical elements, the maximum depth, a specific page number,
267 * and number of elements per page, this function first determines all top level root elements
268 * belonging to that page, then lists them and all of their children in hierarchical order.
270 * $max_depth = 0 means display all levels.
271 * $max_depth > 0 specifies the number of display levels.
275 * @param int $max_depth The maximum hierarchical depth.
276 * @param int $page_num The specific page number, beginning with 1.
277 * @return string XHTML of the specified page of elements
279 public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
282 if ( empty($elements) || $max_depth < -1 )
285 $args = array_slice( func_get_args(), 4 );
288 $parent_field = $this->db_fields['parent'];
291 if ( -1 == $max_depth )
292 $total_top = count( $elements );
293 if ( $page_num < 1 || $per_page < 0 ) {
297 if ( -1 == $max_depth )
299 $this->max_pages = 1;
302 $start = ( (int)$page_num - 1 ) * (int)$per_page;
303 $end = $start + $per_page;
304 if ( -1 == $max_depth )
305 $this->max_pages = ceil($total_top / $per_page);
309 if ( -1 == $max_depth ) {
310 if ( !empty($args[0]['reverse_top_level']) ) {
311 $elements = array_reverse( $elements );
313 $start = $total_top - $end;
314 $end = $total_top - $oldstart;
317 $empty_array = array();
318 foreach ( $elements as $e ) {
320 if ( $count < $start )
322 if ( $count >= $end )
324 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
330 * Separate elements into two buckets: top level and children elements.
331 * Children_elements is two dimensional array, e.g.
332 * $children_elements[10][] contains all sub-elements whose parent is 10.
334 $top_level_elements = array();
335 $children_elements = array();
336 foreach ( $elements as $e) {
337 if ( 0 == $e->$parent_field )
338 $top_level_elements[] = $e;
340 $children_elements[ $e->$parent_field ][] = $e;
343 $total_top = count( $top_level_elements );
345 $this->max_pages = ceil($total_top / $per_page);
349 if ( !empty($args[0]['reverse_top_level']) ) {
350 $top_level_elements = array_reverse( $top_level_elements );
352 $start = $total_top - $end;
353 $end = $total_top - $oldstart;
355 if ( !empty($args[0]['reverse_children']) ) {
356 foreach ( $children_elements as $parent => $children )
357 $children_elements[$parent] = array_reverse( $children );
360 foreach ( $top_level_elements as $e ) {
363 // For the last page, need to unset earlier children in order to keep track of orphans.
364 if ( $end >= $total_top && $count < $start )
365 $this->unset_children( $e, $children_elements );
367 if ( $count < $start )
370 if ( $count >= $end )
373 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
376 if ( $end >= $total_top && count( $children_elements ) > 0 ) {
377 $empty_array = array();
378 foreach ( $children_elements as $orphans )
379 foreach( $orphans as $op )
380 $this->display_element( $op, $empty_array, 1, 0, $args, $output );
386 public function get_number_of_root_elements( $elements ){
389 $parent_field = $this->db_fields['parent'];
391 foreach ( $elements as $e) {
392 if ( 0 == $e->$parent_field )
398 // Unset all the children for a given top level element.
399 public function unset_children( $e, &$children_elements ){
401 if ( !$e || !$children_elements )
404 $id_field = $this->db_fields['id'];
407 if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
408 foreach ( (array) $children_elements[$id] as $child )
409 $this->unset_children( $child, $children_elements );
411 if ( isset($children_elements[$id]) )
412 unset( $children_elements[$id] );