[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/inc/generic/model/ -> _genericcategorycache.class.php (source)

   1  <?php
   2  /**

   3   * This file implements the GenericCategoryCache class.

   4   *

   5   * This is the object handling genreric category lists.

   6   *

   7   * This file is part of the evoCore framework - {@link http://evocore.net/}

   8   * See also {@link http://sourceforge.net/projects/evocms/}.

   9   *

  10   * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}

  11   * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.

  12   *

  13   * {@internal License choice

  14   * - If you have received this file as part of a package, please find the license.txt file in

  15   *   the same folder or the closest folder above for complete license terms.

  16   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  17   *   then you must choose one of the following licenses before using the file:

  18   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  19   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  20   * }}

  21   *

  22   * {@internal Open Source relicensing agreement:

  23   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license

  24   * PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project

  25   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  26   * }}

  27   *

  28   * @package evocore

  29   *

  30   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  31   * @author fplanque: Francois PLANQUE.

  32   * @author mbruneau: Marc BRUNEAU / PROGIDISTRI

  33   *

  34   * @version $Id: _genericcategorycache.class.php,v 1.3 2007/10/08 08:33:15 fplanque Exp $

  35   */
  36  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  37  
  38  
  39  load_class('generic/model/_genericcache.class.php');
  40  
  41  
  42  /**

  43   * GenericCategoryCache class

  44   *

  45   *

  46   * @package evocore

  47   */
  48  class GenericCategoryCache extends GenericCache
  49  {
  50  
  51      var $subset_cache = array();
  52  
  53      /**

  54       * Which property of the objects defines the subset

  55       */
  56      var $subset_property;
  57  
  58      /**

  59       * Which subsets have been loaded

  60       */
  61      var $loaded_subsets = array();
  62  
  63      /**

  64       * These are the level 0 categories (which have no parent)

  65       */
  66      var $root_cats = array();
  67  
  68      /**

  69       * These are the level 0 categories (which have no parent) for each subset

  70       */
  71      var $subset_root_cats = array();
  72  
  73      /**

  74       * Have the children been revealed for all subsets yet?

  75       */
  76      var $revealed_all_children = false;
  77      /**

  78       * Have the children been revealed for all subsets yet?

  79       */
  80      var $revealed_subsets = array();
  81  
  82  
  83      /**

  84       * Constructor

  85       */
  86  	function GenericCategoryCache( $objtype, $load_all, $tablename, $prefix = '', $dbIDname = 'ID', $name_field = NULL, $subset_property = NULL )
  87      {
  88          parent::GenericCache( $objtype, $load_all, $tablename, $prefix, $dbIDname, $name_field );
  89  
  90          $this->subset_property = $subset_property;
  91      }
  92  
  93  
  94      /**

  95       * Empty/reset the cache

  96       */
  97  	function clear()
  98      {
  99           $this->subset_cache = array();
 100           $this->loaded_subsets = array();
 101          $this->root_cats = array();
 102          $this->subset_root_cats = array();
 103          $this->revealed_all_children = false;
 104          $this->revealed_subsets = array();
 105          parent::clear();
 106       }
 107  
 108  
 109      /**

 110       * Add a dataobject to the cache

 111       */
 112  	function add( & $Obj )
 113      {
 114          global $Debuglog;
 115  
 116          if( parent::add( $Obj ) )
 117          {    // Successfuly added
 118  
 119              if( !empty($this->subset_property) )
 120              {    // Also add to subset cache:
 121                  $this->subset_cache[$Obj->{$this->subset_property}][$Obj->ID] = & $Obj;
 122              }
 123              return true;
 124          }
 125  
 126          return false;
 127      }
 128  
 129  
 130      /**

 131       * Reveal children

 132       *

 133       * After this each Category will have an array pointing to its direct children

 134       *

 135       * @param integer|NULL NULL for all subsets

 136       */
 137  	function reveal_children( $subset_ID = NULL )
 138      {
 139          if( $this->revealed_all_children )
 140          {    // ALL Children have already been revealed: (can happen even if we require a subset *now*)
 141              return;
 142              /* RETURN */

 143          }
 144  
 145          if( empty($this->subset_property) )
 146          {    // We are not handling subsets
 147  
 148              // Make sure everything has been loaded:

 149          $this->load_all();
 150  
 151              // Reveal children:

 152              if( !empty( $this->cache ) )
 153              {    // There are loaded categories, so loop on all loaded categories to set their children list if it has:
 154                  foreach( $this->cache as $cat_ID => $GenericCategory )
 155                  {
 156                      // echo $GenericCategory->name;

 157                      if( ! is_null( $GenericCategory->parent_ID ) )
 158                      {    // This category has a parent, so add it to its parent children list:
 159                          $this->cache[$GenericCategory->parent_ID]->add_children( $this->cache[$cat_ID] );
 160                      }
 161                      else
 162                      {    // This category has no parent, so add it to the parent categories list
 163                          $this->root_cats[] = & $this->cache[$cat_ID];
 164                      }
 165                  }
 166              }
 167  
 168              $this->revealed_all_children = true;
 169          }
 170          else
 171          {    // We are handling subsets
 172  
 173              if( is_null( $subset_ID ) )
 174              {    // No specific subset requested, we are going to reveal all subsets
 175  
 176                  // Make sure everything has been loaded:

 177                  $this->load_all();
 178  
 179              echo 'REVEALING ALL SUBSETS in a row. Is this needed?';
 180  
 181                  foreach( $this->subset_cache as $subset_ID => $dummy )
 182                  {
 183                      $this->reveal_children( $subset_ID );
 184                  }
 185  
 186                  $this->revealed_all_children = true;
 187              }
 188              else
 189              {    // We're interested in a specific subset
 190                  if( !empty( $this->revealed_subsets[$subset_ID] ) )
 191                  {    // Children have already been revealed:
 192                      return;
 193                      /* RETURN */

 194                  }
 195  
 196                  // Make sure the requested subset has been loaded:

 197              $this->load_subset($subset_ID);
 198  
 199                  // Reveal children:

 200                  if( !empty( $this->subset_cache[$subset_ID] ) )
 201                  {    // There are loaded categories, so loop on all loaded categories to set their children list if it has:
 202                      foreach( $this->subset_cache[$subset_ID] as $cat_ID => $dummy )    // "as" would give a freakin copy of the object :(((
 203                      {
 204                          $GenericCategory = & $this->subset_cache[$subset_ID][$cat_ID];
 205                          // echo '<br>'.$cat_ID;

 206                          // echo $GenericCategory->name;

 207                          if( ! is_null( $GenericCategory->parent_ID ) )
 208                          {    // This category has a parent, so add it to its parent children list:
 209                              // echo ' parent='.$GenericCategory->parent_ID;

 210                              if( ! isset($this->cache[$GenericCategory->parent_ID] ) )
 211                              {
 212                                  global $Debuglog;
 213                                  $Debuglog->add( 'Detected <strong>orphan cat</strong> with ID='.$GenericCategory->ID.' and non existent parent ID='.$GenericCategory->parent_ID );
 214                              }
 215                              else
 216                              {
 217                                  // echo ' add child';

 218                                  $this->cache[$GenericCategory->parent_ID]->add_children( $this->cache[$cat_ID] );
 219                              }
 220                          }
 221                          else
 222                          {    // This category has no parent, so add it to the parent categories list
 223                              $this->root_cats[] = & $this->cache[$cat_ID];
 224                              $this->subset_root_cats[$this->cache[$cat_ID]->{$this->subset_property}][] = & $this->cache[$cat_ID];
 225                          }
 226                      }
 227                  }
 228  
 229                  // Children have been revealed.

 230                  $this->revealed_subsets[$subset_ID] = true;
 231              }
 232          }
 233      }
 234  
 235  
 236      /**

 237       * Return recursive display of loaded categories

 238       *

 239       * @param array callback funtions (to format the display)

 240       * @param integer|NULL NULL for all subsets

 241       * @param array categories list to display

 242       * @param int depth of  categories list

 243       *

 244       * @return string recursive list of all loaded categories

 245       */
 246  	function recurse( $callbacks, $subset_ID = NULL, $cat_array = NULL, $level = 0 )
 247      {
 248          // Make sure children have been revealed for specific subset:

 249          $this->reveal_children( $subset_ID );
 250  
 251          if( is_null( $cat_array ) )
 252          {    // Get all parent categories:
 253              if( is_null( $subset_ID ) )
 254              {
 255                  $cat_array = $this->root_cats;
 256              }
 257              elseif( isset( $this->subset_root_cats[$subset_ID] ) )
 258              {    // We have root cats for the requested subset:
 259                  $cat_array = $this->subset_root_cats[$subset_ID];
 260              }
 261              else
 262              {
 263                  $cat_array = array();
 264              }
 265          }
 266  
 267          $r = '';
 268  
 269          if( is_array( $callbacks['before_level'] ) )
 270          { // object callback:
 271              $r .= $callbacks['before_level'][0]->{$callbacks['before_level'][1]}( $level ); // <ul>

 272          }
 273          else
 274          {
 275              $r .= $callbacks['before_level']( $level ); // <ul>

 276          }
 277  
 278          foreach( $cat_array as $cat )
 279          {
 280              if( is_array( $callbacks['line'] ) )
 281              { // object callback:
 282                  $r .= $callbacks['line'][0]->{$callbacks['line'][1]}( $cat, $level ); // <li> Category  - or - <tr><td>Category</td></tr> ...

 283              }
 284              else
 285              {
 286                  $r .= $callbacks['line']( $cat, $level ); // <li> Category  - or - <tr><td>Category</td></tr> ...

 287              }
 288  
 289              if( !empty( $cat->children ) )
 290              {    // Add children categories:
 291                  $r .= $this->recurse( $callbacks, $subset_ID, $cat->children, $level+1 );
 292              }
 293              elseif( is_array( $callbacks['no_children'] ) )
 294              { // object callback:
 295                  $r .= $callbacks['no_children'][0]->{$callbacks['no_children'][1]}( $cat, $level ); // </li>

 296              }
 297              else
 298              {
 299                  $r .= $callbacks['no_children']( $cat, $level ); // </li>

 300              }
 301  
 302          }
 303  
 304          if( is_array( $callbacks['after_level'] ) )
 305          { // object callback:
 306              $r .= $callbacks['after_level'][0]->{$callbacks['after_level'][1]}( $level ); // </ul>

 307          }
 308          else
 309          {
 310              $r .= $callbacks['after_level']( $level ); // </ul>

 311          }
 312  
 313          return $r;
 314      }
 315  
 316  
 317      /**

 318       * Return recursive select options list of all loaded categories

 319       *

 320       * @param integer selected category in the select input

 321       * @param integer NULL for all subsets

 322       * @param boolean Include the root element?

 323       * @param array GenercCategory objects to display (will recurse from those starting points)

 324       * @param integer depth of categories list

 325       * @param array IDs of categories to exclude (their children will be ignored to)

 326       *

 327       * @return string select options list of all loaded categories

 328       */
 329  	function recurse_select( $selected = NULL, $subset_ID = NULL, $include_root = false, $Cat_array = NULL,
 330                              $level = 0, $exclude_array = array() )
 331      {
 332          // Make sure children have been revealed for specific subset:

 333          $this->reveal_children( $subset_ID );
 334  
 335          if( is_null( $Cat_array ) )
 336          {    // Get all parent categorie:
 337              $Cat_array = $this->root_cats;
 338          }
 339  
 340          $r = '';
 341  
 342          if( $include_root )
 343          {
 344              $r .= '<option value="">'.T_('Root').'</option>';
 345              $level++;
 346          }
 347  
 348          foreach( $Cat_array as $GenericCategory )
 349          {
 350              if( in_array( $GenericCategory->ID, $exclude_array ) )
 351              {    // We want to exclude that cat.
 352                  continue;
 353              }
 354  
 355              // Set category indentation in the select:

 356              $indent = '';
 357              for($i = 0; $i < $level; $i++)
 358              {
 359                  $indent .='&nbsp;&nbsp;';
 360              }
 361              // Set category option:

 362              $r .= '<option value="'.$GenericCategory->ID.'" ';
 363              if( $GenericCategory->ID == $selected ) $r .= ' selected="selected"';
 364              $r .= ' >'.$indent.$GenericCategory->name.'</option>';
 365  
 366              if( !empty( $GenericCategory->children ) )
 367              {    // Add children categories:
 368                  $r .= $this->recurse_select( $selected, $subset_ID, false, $GenericCategory->children, $level+1 );
 369              }
 370          }
 371  
 372          return $r;
 373      }
 374  
 375  }
 376  
 377  /*

 378   * $Log: _genericcategorycache.class.php,v $

 379   * Revision 1.3  2007/10/08 08:33:15  fplanque

 380   * rollback until needed

 381   *

 382   * Revision 1.1  2007/06/25 11:00:16  fplanque

 383   * MODULES (refactored MVC)

 384   *

 385   * Revision 1.15  2007/04/26 00:11:11  fplanque

 386   * (c) 2007

 387   *

 388   * Revision 1.14  2006/12/10 02:01:11  fplanque

 389   * doc

 390   *

 391   * Revision 1.13  2006/12/10 01:52:27  fplanque

 392   * old cats are now officially dead :>

 393   *

 394   * Revision 1.12  2006/12/09 02:37:44  fplanque

 395   * Prevent user from creating loops in the chapter tree

 396   * (still needs a check before writing to DB though)

 397   *

 398   * Revision 1.11  2006/11/26 01:42:09  fplanque

 399   * doc

 400   */
 401  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics