3 * A class for displaying various tree-like structures.
5 * Extend the Walker class to use it, see examples at the 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. Also, the methods are
8 * not strictly abstract in that the parameter definition needs to be followed.
9 * The child classes can have additional parameters.
17 * What the class handles.
35 * Max number of pages walked by the paged walker
44 * Starts the list before the elements are added.
46 * Additional parameters are used in child classes. The args parameter holds
47 * additional values that may be used with the child class methods. This
48 * method is called at the start of the output list.
53 * @param string $output Passed by reference. Used to append additional content.
55 function start_lvl(&$output) {}
58 * Ends the list of after the elements are added.
60 * Additional parameters are used in child classes. The args parameter holds
61 * additional values that may be used with the child class methods. This
62 * method finishes the list at the end of output of the elements.
67 * @param string $output Passed by reference. Used to append additional content.
69 function end_lvl(&$output) {}
72 * Start the element output.
74 * Additional parameters are used in child classes. The args parameter holds
75 * additional values that may be used with the child class methods. Includes
76 * the element output also.
81 * @param string $output Passed by reference. Used to append additional content.
83 function start_el(&$output) {}
86 * Ends the element output, if needed.
88 * Additional parameters are used in child classes. The args parameter holds
89 * additional values that may be used with the child class methods.
94 * @param string $output Passed by reference. Used to append additional content.
96 function end_el(&$output) {}
99 * Traverse elements to create list from elements.
101 * Display one element if the element doesn't have any children otherwise,
102 * display the element and its children. Will only traverse up to the max
103 * depth and no ignore elements under that depth. It is possible to set the
104 * max depth to include all depths, see walk() method.
106 * This method shouldn't be called directly, use the walk() method instead.
110 * @param object $element Data object
111 * @param array $children_elements List of elements to continue traversing.
112 * @param int $max_depth Max depth to traverse.
113 * @param int $depth Depth of current element.
115 * @param string $output Passed by reference. Used to append additional content.
116 * @return null Null on failure with no changes to parameters.
118 function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
123 $id_field = $this->db_fields['id'];
125 //display this element
126 if ( is_array( $args[0] ) )
127 $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] );
128 $cb_args = array_merge( array(&$output, $element, $depth), $args);
129 call_user_func_array(array(&$this, 'start_el'), $cb_args);
131 $id = $element->$id_field;
133 // descend only when the depth is right and there are childrens for this element
134 if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
136 foreach( $children_elements[ $id ] as $child ){
138 if ( !isset($newlevel) ) {
140 //start the child delimiter
141 $cb_args = array_merge( array(&$output, $depth), $args);
142 call_user_func_array(array(&$this, 'start_lvl'), $cb_args);
144 $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
146 unset( $children_elements[ $id ] );
149 if ( isset($newlevel) && $newlevel ){
150 //end the child delimiter
151 $cb_args = array_merge( array(&$output, $depth), $args);
152 call_user_func_array(array(&$this, 'end_lvl'), $cb_args);
156 $cb_args = array_merge( array(&$output, $element, $depth), $args);
157 call_user_func_array(array(&$this, 'end_el'), $cb_args);
161 * Display array of elements hierarchically.
163 * It is a generic function which does not assume any existing order of
164 * elements. max_depth = -1 means flatly display every element. max_depth =
165 * 0 means display all levels. max_depth > 0 specifies the number of
170 * @param array $elements
171 * @param int $max_depth
174 function walk( $elements, $max_depth) {
176 $args = array_slice(func_get_args(), 2);
179 if ($max_depth < -1) //invalid parameter
182 if (empty($elements)) //nothing to walk
185 $id_field = $this->db_fields['id'];
186 $parent_field = $this->db_fields['parent'];
189 if ( -1 == $max_depth ) {
190 $empty_array = array();
191 foreach ( $elements as $e )
192 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
197 * need to display in hierarchical order
198 * separate elements into two buckets: top level and children elements
199 * children_elements is two dimensional array, eg.
200 * children_elements[10][] contains all sub-elements whose parent is 10.
202 $top_level_elements = array();
203 $children_elements = array();
204 foreach ( $elements as $e) {
205 if ( 0 == $e->$parent_field )
206 $top_level_elements[] = $e;
208 $children_elements[ $e->$parent_field ][] = $e;
212 * when none of the elements is top level
213 * assume the first one must be root of the sub elements
215 if ( empty($top_level_elements) ) {
217 $first = array_slice( $elements, 0, 1 );
220 $top_level_elements = array();
221 $children_elements = array();
222 foreach ( $elements as $e) {
223 if ( $root->$parent_field == $e->$parent_field )
224 $top_level_elements[] = $e;
226 $children_elements[ $e->$parent_field ][] = $e;
230 foreach ( $top_level_elements as $e )
231 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
234 * if we are displaying all levels, and remaining children_elements is not empty,
235 * then we got orphans, which should be displayed regardless
237 if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
238 $empty_array = array();
239 foreach ( $children_elements as $orphans )
240 foreach( $orphans as $op )
241 $this->display_element( $op, $empty_array, 1, 0, $args, $output );
248 * paged_walk() - produce a page of nested elements
250 * Given an array of hierarchical elements, the maximum depth, a specific page number,
251 * and number of elements per page, this function first determines all top level root elements
252 * belonging to that page, then lists them and all of their children in hierarchical order.
256 * @param int $max_depth = 0 means display all levels; $max_depth > 0 specifies the number of display levels.
257 * @param int $page_num the specific page number, beginning with 1.
258 * @return XHTML of the specified page of elements
260 function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
263 if ( empty($elements) || $max_depth < -1 )
266 $args = array_slice( func_get_args(), 4 );
269 $id_field = $this->db_fields['id'];
270 $parent_field = $this->db_fields['parent'];
273 if ( -1 == $max_depth )
274 $total_top = count( $elements );
275 if ( $page_num < 1 || $per_page < 0 ) {
279 if ( -1 == $max_depth )
281 $this->max_pages = 1;
284 $start = ( (int)$page_num - 1 ) * (int)$per_page;
285 $end = $start + $per_page;
286 if ( -1 == $max_depth )
287 $this->max_pages = ceil($total_top / $per_page);
291 if ( -1 == $max_depth ) {
292 if ( !empty($args[0]['reverse_top_level']) ) {
293 $elements = array_reverse( $elements );
295 $start = $total_top - $end;
296 $end = $total_top - $oldstart;
299 $empty_array = array();
300 foreach ( $elements as $e ) {
302 if ( $count < $start )
304 if ( $count >= $end )
306 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
312 * separate elements into two buckets: top level and children elements
313 * children_elements is two dimensional array, eg.
314 * children_elements[10][] contains all sub-elements whose parent is 10.
316 $top_level_elements = array();
317 $children_elements = array();
318 foreach ( $elements as $e) {
319 if ( 0 == $e->$parent_field )
320 $top_level_elements[] = $e;
322 $children_elements[ $e->$parent_field ][] = $e;
325 $total_top = count( $top_level_elements );
327 $this->max_pages = ceil($total_top / $per_page);
331 if ( !empty($args[0]['reverse_top_level']) ) {
332 $top_level_elements = array_reverse( $top_level_elements );
334 $start = $total_top - $end;
335 $end = $total_top - $oldstart;
337 if ( !empty($args[0]['reverse_children']) ) {
338 foreach ( $children_elements as $parent => $children )
339 $children_elements[$parent] = array_reverse( $children );
342 foreach ( $top_level_elements as $e ) {
345 //for the last page, need to unset earlier children in order to keep track of orphans
346 if ( $end >= $total_top && $count < $start )
347 $this->unset_children( $e, $children_elements );
349 if ( $count < $start )
352 if ( $count >= $end )
355 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
358 if ( $end >= $total_top && count( $children_elements ) > 0 ) {
359 $empty_array = array();
360 foreach ( $children_elements as $orphans )
361 foreach( $orphans as $op )
362 $this->display_element( $op, $empty_array, 1, 0, $args, $output );
368 function get_number_of_root_elements( $elements ){
371 $parent_field = $this->db_fields['parent'];
373 foreach ( $elements as $e) {
374 if ( 0 == $e->$parent_field )
380 // unset all the children for a given top level element
381 function unset_children( $e, &$children_elements ){
383 if ( !$e || !$children_elements )
386 $id_field = $this->db_fields['id'];
389 if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
390 foreach ( (array) $children_elements[$id] as $child )
391 $this->unset_children( $child, $children_elements );
393 if ( isset($children_elements[$id]) )
394 unset( $children_elements[$id] );