[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> class.bb-walker.php (source)

   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 BB_Loop( &$elements ) {
 166          $this->elements = $elements;
 167          if ( !is_array($this->elements) || empty($this->elements) )
 168              return $this->elements = false;
 169      }
 170  
 171  	function step() {
 172          if ( !is_array($this->elements) || !current($this->elements) || !is_object($this->walker) )
 173              return false;
 174  
 175          if ( !$this->_looping ) {
 176              $r = reset($this->elements);
 177              $this->_looping = true;
 178          } else {
 179              $r = next($this->elements);
 180          }
 181  
 182          if ( !$args = func_get_args() )
 183              $args = array( 0 );
 184          echo call_user_func_array( array(&$this->walker, 'step'), array_merge(array(current($this->elements)), $args) );
 185          return $r;
 186      }
 187  
 188  	function pad( $pad, $offset = 0 ) {
 189          if ( !is_array($this->elements) || !is_object($this->walker) )
 190              return false;
 191  
 192          if ( is_numeric($pad) )
 193              return $pad * ($this->walker->depth - 1) + (int) $offset;
 194  
 195          return str_repeat( $pad, $this->walker->depth - 1 );
 196      }
 197  
 198  	function preserve( $array ) {
 199          if ( !is_array( $array ) )
 200              return false;
 201  
 202          foreach ( $array as $key )
 203              $this->_preserve[$key] = isset( $GLOBALS[$key] ) ? $GLOBALS[$key] : null;
 204      }
 205  
 206  	function reinstate() {
 207          foreach ( $this->_preserve as $key => $value )
 208              $GLOBALS[$key] = $value;
 209      }
 210  
 211  	function classes( $output = 'string' ) {
 212          if ( !is_array($this->elements) || !is_object($this->walker) )
 213              return false;
 214          $classes = array();
 215  
 216          $current = current($this->elements);
 217  
 218          if ( $prev = prev($this->elements) )
 219              next($this->elements);
 220          else        
 221              reset($this->elements);
 222  
 223          if ( $next = next($this->elements) )
 224              prev($this->elements);
 225          else
 226              end($this->elements);
 227  
 228          if ( !empty($next) && $next->{$this->walker->db_fields['parent']} == $current->{$this->walker->db_fields['id']} )
 229              $classes[] = 'bb-parent';
 230          elseif ( !empty($next) && $next->{$this->walker->db_fields['parent']} == $current->{$this->walker->db_fields['parent']} )
 231              $classes[] = 'bb-precedes-sibling';
 232          else
 233              $classes[] = 'bb-last-child';
 234  
 235          if ( !empty($prev) && $current->{$this->walker->db_fields['parent']} == $prev->{$this->walker->db_fields['id']} )
 236              $classes[] = 'bb-first-child';
 237          elseif ( !empty($prev) && $current->{$this->walker->db_fields['parent']} == $prev->{$this->walker->db_fields['parent']} )
 238              $classes[] = 'bb-follows-sibling';
 239          elseif ( $prev )
 240              $classes[] = 'bb-follows-niece';
 241  
 242          if ( $this->walker->depth > 1 )
 243              $classes[] = 'bb-child';
 244          else
 245              $classes[] = 'bb-root';
 246  
 247          if ( $output === 'string' )
 248              $classes = join(' ', $classes);
 249  
 250          return $classes;
 251      }
 252  
 253  }


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7