BB_Walker

The BuddyPress BB Walker class.

Defined (1)

The class is defined in the following location(s).

/bp-forums/bbpress/bb-includes/class.bb-walker.php  
  1. class BB_Walker { 
  2. var $tree_type; 
  3. var $db_fields; 
  4.  
  5. //abstract callbacks 
  6. function start_lvl($output) { return $output; } 
  7. function end_lvl($output) { return $output; } 
  8. function start_el($output) { return $output; } 
  9. function end_el($output) { return $output; } 
  10.  
  11. function _init() { 
  12. $this->parents = array(); 
  13. $this->depth = 1; 
  14. $this->previous_element = ''; 
  15. }  
  16.  
  17. function walk($elements, $to_depth) { 
  18. $args = array_slice(func_get_args(), 2); 
  19. $output = ''; 
  20.  
  21. // padding at the end 
  22. $last_element->{$this->db_fields['parent']} = 0; 
  23. $last_element->{$this->db_fields['id']} = 0; 
  24. $elements[] = $last_element; 
  25.  
  26. $flat = (-1 == $to_depth) ? true : false; 
  27. foreach ( $elements as $element ) 
  28. $output .= call_user_func_array( array(&$this, 'step'), array_merge( array($element, $to_depth), $args ) ); 
  29.  
  30. return $output; 
  31.  
  32. function step( $element, $to_depth ) { 
  33. if ( !isset($this->depth) ) 
  34. $this->_init(); 
  35.  
  36. $args = array_slice(func_get_args(), 2); 
  37. $id_field = $this->db_fields['id']; 
  38. $parent_field = $this->db_fields['parent']; 
  39.  
  40. $flat = (-1 == $to_depth) ? true : false; 
  41.  
  42. $output = ''; 
  43.  
  44. // If flat, start and end the element and skip the level checks. 
  45. if ( $flat ) { 
  46. // Start the element. 
  47. if ( isset($element->$id_field) && $element->$id_field != 0 ) { 
  48. $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 
  49. call_user_func_array(array(&$this, 'start_el'), $cb_args); 
  50.  
  51. // End the element. 
  52. if ( isset($element->$id_field) && $element->$id_field != 0 ) { 
  53. $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 
  54. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  55.  
  56. return; 
  57.  
  58. // Walk the tree. 
  59. if ( !empty($element) && !empty($this->previous_element) && $element->$parent_field == $this->previous_element->$id_field ) { 
  60. // Previous element is my parent. Descend a level. 
  61. array_unshift($this->parents, $this->previous_element); 
  62. if ( !$to_depth || ($this->depth < $to_depth) ) { //only descend if we're below $to_depth 
  63. $cb_args = array_merge( array(&$output, $this->depth), $args); 
  64. call_user_func_array(array(&$this, 'start_lvl'), $cb_args); 
  65. } else if ( $to_depth && $this->depth == $to_depth ) { // If we've reached depth, end the previous element. 
  66. $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth), $args); 
  67. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  68. $this->depth++; //always do this so when we start the element further down, we know where we are 
  69. } else if ( !empty($element) && !empty($this->previous_element) && $element->$parent_field == $this->previous_element->$parent_field) { 
  70. // On the same level as previous element. 
  71. if ( !$to_depth || ($this->depth <= $to_depth) ) { 
  72. $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 
  73. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  74. } else if ( $this->depth > 1 ) { 
  75. // Ascend one or more levels. 
  76. if ( !$to_depth || ($this->depth <= $to_depth) ) { 
  77. $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 
  78. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  79.  
  80. while ( $parent = array_shift($this->parents) ) { 
  81. $this->depth--; 
  82. if ( !$to_depth || ($this->depth < $to_depth) ) { 
  83. $cb_args = array_merge( array(&$output, $this->depth), $args); 
  84. call_user_func_array(array(&$this, 'end_lvl'), $cb_args); 
  85. $cb_args = array_merge( array(&$output, $parent, $this->depth - 1), $args); 
  86. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  87. if ( !empty($element) && isset($this->parents[0]) && $element->$parent_field == $this->parents[0]->$id_field ) { 
  88. break; 
  89. } else if ( !empty($this->previous_element) ) { 
  90. // Close off previous element. 
  91. if ( !$to_depth || ($this->depth <= $to_depth) ) { 
  92. $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 
  93. call_user_func_array(array(&$this, 'end_el'), $cb_args); 
  94.  
  95. // Start the element. 
  96. if ( !$to_depth || ($this->depth <= $to_depth) ) { 
  97. if ( !empty($element) && $element->$id_field != 0 ) { 
  98. $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 
  99. call_user_func_array(array(&$this, 'start_el'), $cb_args); 
  100.  
  101. $this->previous_element = $element; 
  102. return $output;