[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 class BB_Walker { 4 var $tree_type; 5 var $db_fields; 6 7 //abstract callbacks 8 function start_lvl($output) { return $output; } 9 function end_lvl($output) { return $output; } 10 function start_el($output) { return $output; } 11 function end_el($output) { return $output; } 12 13 function _init() { 14 $this->parents = array(); 15 $this->depth = 1; 16 $this->previous_element = ''; 17 } 18 19 function walk($elements, $to_depth) { 20 $args = array_slice(func_get_args(), 2); 21 $output = ''; 22 23 // padding at the end 24 $last_element->{$this->db_fields['parent']} = 0; 25 $last_element->{$this->db_fields['id']} = 0; 26 $elements[] = $last_element; 27 28 $flat = (-1 == $to_depth) ? true : false; 29 foreach ( $elements as $element ) 30 $output .= call_user_func_array( array(&$this, 'step'), array_merge( array($element, $to_depth), $args ) ); 31 32 return $output; 33 } 34 35 function step( $element, $to_depth ) { 36 if ( !isset($this->depth) ) 37 $this->_init(); 38 39 $args = array_slice(func_get_args(), 2); 40 $id_field = $this->db_fields['id']; 41 $parent_field = $this->db_fields['parent']; 42 43 $flat = (-1 == $to_depth) ? true : false; 44 45 $output = ''; 46 47 // If flat, start and end the element and skip the level checks. 48 if ( $flat ) { 49 // Start the element. 50 if ( isset($element->$id_field) && $element->$id_field != 0 ) { 51 $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 52 call_user_func_array(array(&$this, 'start_el'), $cb_args); 53 } 54 55 // End the element. 56 if ( isset($element->$id_field) && $element->$id_field != 0 ) { 57 $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 58 call_user_func_array(array(&$this, 'end_el'), $cb_args); 59 } 60 61 return; 62 } 63 64 // Walk the tree. 65 if ( !empty($element) && !empty($this->previous_element) && $element->$parent_field == $this->previous_element->$id_field ) { 66 // Previous element is my parent. Descend a level. 67 array_unshift($this->parents, $this->previous_element); 68 if ( !$to_depth || ($this->depth < $to_depth) ) { //only descend if we're below $to_depth 69 $cb_args = array_merge( array(&$output, $this->depth), $args); 70 call_user_func_array(array(&$this, 'start_lvl'), $cb_args); 71 } else if ( $to_depth && $this->depth == $to_depth ) { // If we've reached depth, end the previous element. 72 $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth), $args); 73 call_user_func_array(array(&$this, 'end_el'), $cb_args); 74 } 75 $this->depth++; //always do this so when we start the element further down, we know where we are 76 } else if ( !empty($element) && !empty($this->previous_element) && $element->$parent_field == $this->previous_element->$parent_field) { 77 // On the same level as previous element. 78 if ( !$to_depth || ($this->depth <= $to_depth) ) { 79 $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 80 call_user_func_array(array(&$this, 'end_el'), $cb_args); 81 } 82 } else if ( $this->depth > 1 ) { 83 // Ascend one or more levels. 84 if ( !$to_depth || ($this->depth <= $to_depth) ) { 85 $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 86 call_user_func_array(array(&$this, 'end_el'), $cb_args); 87 } 88 89 while ( $parent = array_shift($this->parents) ) { 90 $this->depth--; 91 if ( !$to_depth || ($this->depth < $to_depth) ) { 92 $cb_args = array_merge( array(&$output, $this->depth), $args); 93 call_user_func_array(array(&$this, 'end_lvl'), $cb_args); 94 $cb_args = array_merge( array(&$output, $parent, $this->depth - 1), $args); 95 call_user_func_array(array(&$this, 'end_el'), $cb_args); 96 } 97 if ( !empty($element) && isset($this->parents[0]) && $element->$parent_field == $this->parents[0]->$id_field ) { 98 break; 99 } 100 } 101 } else if ( !empty($this->previous_element) ) { 102 // Close off previous element. 103 if ( !$to_depth || ($this->depth <= $to_depth) ) { 104 $cb_args = array_merge( array(&$output, $this->previous_element, $this->depth - 1), $args); 105 call_user_func_array(array(&$this, 'end_el'), $cb_args); 106 } 107 } 108 109 // Start the element. 110 if ( !$to_depth || ($this->depth <= $to_depth) ) { 111 if ( !empty($element) && $element->$id_field != 0 ) { 112 $cb_args = array_merge( array(&$output, $element, $this->depth - 1), $args); 113 call_user_func_array(array(&$this, 'start_el'), $cb_args); 114 } 115 } 116 117 $this->previous_element = $element; 118 return $output; 119 } 120 } 121 122 class BB_Walker_Blank extends BB_Walker { // Used for template functions 123 var $tree_type; 124 var $db_fields = array( 'id' => '', 'parent' => '' ); 125 126 var $start_lvl = ''; 127 var $end_lvl = ''; 128 129 //abstract callbacks 130 function start_lvl( $output, $depth ) { 131 if ( !$this->start_lvl ) 132 return ''; 133 $indent = str_repeat("\t", $depth); 134 $output .= $indent . "$this->start_lvl\n"; 135 return $output; 136 } 137 138 function end_lvl( $output, $depth ) { 139 if ( !$this->end_lvl ) 140 return ''; 141 $indent = str_repeat("\t", $depth); 142 $output .= $indent . "$this->end_lvl\n"; 143 return $output; 144 } 145 146 function start_el() { return ''; } 147 function end_el() { return ''; } 148 } 149 150 class BB_Loop { 151 var $elements; 152 var $walker; 153 var $_preserve = array(); 154 var $_looping = false; 155 156 function &start( $elements, $walker = 'BB_Walker_Blank' ) { 157 $null = null; 158 $a = new BB_Loop( $elements ); 159 if ( !$a->elements ) 160 return $null; 161 $a->walker = new $walker; 162 return $a; 163 } 164 165 function __construct( &$elements ) { 166 $this->elements = $elements; 167 if ( !is_array($this->elements) || empty($this->elements) ) 168 return $this->elements = false; 169 } 170 171 function BB_Loop( &$elements ) { 172 $this->__construct( $elements ); 173 } 174 175 function step() { 176 if ( !is_array($this->elements) || !current($this->elements) || !is_object($this->walker) ) 177 return false; 178 179 if ( !$this->_looping ) { 180 $r = reset($this->elements); 181 $this->_looping = true; 182 } else { 183 $r = next($this->elements); 184 } 185 186 if ( !$args = func_get_args() ) 187 $args = array( 0 ); 188 echo call_user_func_array( array(&$this->walker, 'step'), array_merge(array(current($this->elements)), $args) ); 189 return $r; 190 } 191 192 function pad( $pad, $offset = 0 ) { 193 if ( !is_array($this->elements) || !is_object($this->walker) ) 194 return false; 195 196 if ( is_numeric($pad) ) 197 return $pad * ($this->walker->depth - 1) + (int) $offset; 198 199 return str_repeat( $pad, $this->walker->depth - 1 ); 200 } 201 202 function preserve( $array ) { 203 if ( !is_array( $array ) ) 204 return false; 205 206 foreach ( $array as $key ) 207 $this->_preserve[$key] = isset( $GLOBALS[$key] ) ? $GLOBALS[$key] : null; 208 } 209 210 function reinstate() { 211 foreach ( $this->_preserve as $key => $value ) 212 $GLOBALS[$key] = $value; 213 } 214 215 function classes( $output = 'string' ) { 216 if ( !is_array($this->elements) || !is_object($this->walker) ) 217 return false; 218 $classes = array(); 219 220 $current = current($this->elements); 221 222 if ( $prev = prev($this->elements) ) 223 next($this->elements); 224 else 225 reset($this->elements); 226 227 if ( $next = next($this->elements) ) 228 prev($this->elements); 229 else 230 end($this->elements); 231 232 if ( !empty($next) && $next->{$this->walker->db_fields['parent']} == $current->{$this->walker->db_fields['id']} ) 233 $classes[] = 'bb-parent'; 234 elseif ( !empty($next) && $next->{$this->walker->db_fields['parent']} == $current->{$this->walker->db_fields['parent']} ) 235 $classes[] = 'bb-precedes-sibling'; 236 else 237 $classes[] = 'bb-last-child'; 238 239 if ( !empty($prev) && $current->{$this->walker->db_fields['parent']} == $prev->{$this->walker->db_fields['id']} ) 240 $classes[] = 'bb-first-child'; 241 elseif ( !empty($prev) && $current->{$this->walker->db_fields['parent']} == $prev->{$this->walker->db_fields['parent']} ) 242 $classes[] = 'bb-follows-sibling'; 243 elseif ( $prev ) 244 $classes[] = 'bb-follows-niece'; 245 246 if ( $this->walker->depth > 1 ) 247 $classes[] = 'bb-child'; 248 else 249 $classes[] = 'bb-root'; 250 251 if ( $output === 'string' ) 252 $classes = join(' ', $classes); 253 254 return $classes; 255 } 256 257 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 7 01:01:35 2017 | Cross-referenced by PHPXref 0.7.1 |