[ 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/chapters/model/ -> _chaptercache.class.php (source)

   1  <?php
   2  /**

   3   * This file implements the ChapterCache class.

   4   *

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

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

   7   *

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

   9   *

  10   * {@internal License choice

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

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

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

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

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

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

  17   * }}

  18   *

  19   * @package evocore

  20   *

  21   * @author fplanque: Francois PLANQUE

  22   *

  23   * @version $Id: _chaptercache.class.php,v 1.1 2007/06/25 10:59:26 fplanque Exp $

  24   */
  25  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  26  
  27  load_class( 'generic/model/_genericcategorycache.class.php' );
  28  load_class( 'chapters/model/_chapter.class.php' );
  29  
  30  /**

  31   * ChapterCache Class

  32   *

  33   * @package evocore

  34   */
  35  class ChapterCache extends GenericCategoryCache
  36  {
  37      /**

  38       * Lazy filled index of url titles

  39       */
  40      var $urlname_index = array();
  41  
  42  
  43      /**

  44       * Constructor

  45       */
  46  	function ChapterCache()
  47      {
  48          parent::GenericCategoryCache( 'Chapter', false, 'T_categories', 'cat_', 'cat_ID', 'cat_name', 'blog_ID' );
  49      }
  50  
  51  
  52      /**

  53       * Empty/reset the cache

  54       */
  55  	function clear()
  56      {
  57          $this->urlname_index = array();
  58          parent::clear();
  59      }
  60  
  61  
  62      /**

  63       * Get an object from cache by ID

  64       *

  65       * Load the cache if necessary (all at once if allowed).

  66       *

  67       * @param integer ID of object to load

  68       * @param boolean true if function should die on error

  69       * @param boolean true if function should die on empty/null

  70        * @param integer|NULL NULL for all subsets

  71       * @return reference on cached object

  72       */
  73      function & get_by_ID( $req_ID, $halt_on_error = true, $halt_on_empty = true, $subset_ID = NULL )
  74      {
  75          global $DB, $Debuglog;
  76  
  77          if( empty($req_ID) )
  78          {
  79              if($halt_on_empty)
  80              {
  81                  debug_die( "Requested Chapter from $this->dbtablename without ID!" );
  82              }
  83              $r = NULL;
  84              return $r;
  85          }
  86  
  87          if( !empty( $this->cache[ $req_ID ] ) )
  88          { // Already in cache
  89              $Debuglog->add( "Accessing Chapter($req_ID) from cache", 'dataobjects' );
  90              return $this->cache[ $req_ID ];
  91          }
  92          elseif( !$this->all_loaded )
  93          { // Not in cache, but not everything is loaded yet
  94              if( $this->load_all || is_null($subset_ID) )
  95              { // It's ok to just load everything:
  96                  $this->load_all();
  97              }
  98              else
  99              { // Load just the requested object:
 100                  $Debuglog->add( "Loading <strong>$this->objtype($req_ID)</strong> into cache", 'dataobjects' );
 101                  // Note: $req_ID MUST be an unsigned integer. This is how DataObject works.

 102                  $sql = "SELECT *
 103                            FROM T_categories
 104                           WHERE cat_ID = $req_ID
 105                             AND cat_blog_ID = ".$subset_ID;
 106  
 107                  if( $row = $DB->get_row( $sql, OBJECT, 0, 'ChapterCache::get_by_ID()' ) )
 108                  {
 109                      if( ! $this->instantiate( $row ) )
 110                      {
 111                          $Debuglog->add( 'Could not add() object to cache!', 'dataobjects' );
 112                      }
 113                  }
 114                  else
 115                  {
 116                      $Debuglog->add( 'Could not get DataObject by ID. Query: '.$sql, 'dataobjects' );
 117                  }
 118              }
 119          }
 120  
 121          if( empty( $this->cache[ $req_ID ] ) )
 122          { // Requested object does not exist
 123              // $Debuglog->add( 'failure', 'dataobjects' );

 124              if( $halt_on_error )
 125              {
 126                  debug_die( "Requested $this->objtype does not exist!" );
 127              }
 128              $r = false;
 129              return $r;
 130          }
 131  
 132          return $this->cache[ $req_ID ];
 133      }
 134  
 135  
 136  
 137      /**

 138       * Get an object from cache by urlname

 139       *

 140       * Load the cache if necessary (all at once if allowed).

 141       *

 142       * @param string ID of object to load

 143       * @param boolean true if function should die on error

 144       * @return reference on cached object

 145       */
 146      function & get_by_urlname( $req_urlname, $halt_on_error = true )
 147      {
 148          global $DB, $Debuglog;
 149  
 150          if( !isset( $this->urlname_index[$req_urlname] ) )
 151          { // not yet in cache:
 152              // Load just the requested object:

 153              $Debuglog->add( "Loading <strong>$this->objtype($req_urlname)</strong> into cache" );
 154              $sql = "SELECT *
 155                        FROM $this->dbtablename
 156                       WHERE cat_urlname = ".$DB->quote($req_urlname);
 157              $row = $DB->get_row( $sql );
 158              if( empty( $row ) )
 159              {    // Requested object does not exist
 160                  if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
 161                  // put into index:

 162                  $this->urlname_index[$req_urlname] = false;
 163  
 164                  return $this->urlname_index[$req_urlname];
 165              }
 166  
 167              $this->instantiate( $row );
 168  
 169              // put into index:

 170              $this->urlname_index[$req_urlname] = & $this->cache[ $row->cat_ID ];
 171          }
 172          else
 173          {
 174              $Debuglog->add( "Retrieving <strong>$this->objtype($req_urlname)</strong> from cache" );
 175          }
 176  
 177          return $this->urlname_index[$req_urlname];
 178      }
 179  
 180  
 181  
 182      /**

 183       * Load a keyed subset of the cache

 184       *

 185        * @param integer|NULL NULL for all subsets

 186       */
 187  	function load_subset( $subset_ID )
 188      {
 189          global $DB, $Debuglog;
 190  
 191          if( $this->all_loaded || isset( $this->loaded_subsets[$subset_ID] ) )
 192          { // Already loaded
 193              return false;
 194          }
 195  
 196          // fp> TODO: This kills other subsets. BAD if we want to handle multiple subsets independently

 197          $this->clear( true );
 198  
 199          $Debuglog->add( 'ChapterCache - Loading <strong>chapters('.$subset_ID.')</strong> into cache', 'dataobjects' );
 200          $sql = 'SELECT *
 201                              FROM T_categories
 202                           WHERE cat_blog_ID = '.$subset_ID.'
 203                           ORDER BY cat_name';
 204  
 205          foreach( $DB->get_results( $sql, OBJECT, 'Loading chapters('.$subset_ID.') into cache' ) as $row )
 206          {
 207              // Instantiate a custom object

 208              $this->instantiate( $row );
 209          }
 210  
 211          $this->loaded_subsets[$subset_ID] = true;
 212  
 213          return true;
 214      }
 215  
 216  
 217      /**

 218       * @param integer

 219       * @param integer

 220       * @param integer

 221       */
 222  	function move_Chapter_subtree( $chapter_ID, $src_collection_ID, $dest_collection_ID )
 223      {
 224          /**

 225           * @var DB

 226           */
 227          global $DB;
 228  
 229          // Make sure children have been revealed for specific subset:

 230          $this->reveal_children( $src_collection_ID );
 231  
 232          // fp>We get the Chapter AFTER reveal_children, because something is wrong with reveal_children or get_by_ID

 233          // I don't know what right now, but if we get Chapter earlier, we'll be stuck with an old copy of it that does NOT have the children

 234          // TODO: find out what's freakin wrong

 235          $Chapter = $this->get_by_ID($chapter_ID);
 236  
 237          $chapters_to_move = array();
 238          // Get $chapters_to_move:

 239          $this->recurse_move_subtree( $Chapter, $chapters_to_move );
 240          // pre_dump( $chapters_to_move );

 241  
 242          $DB->begin();
 243  
 244          // Move to root:

 245          if( $parent_Chapter = $Chapter->get_parent_Chapter() )
 246          {    // Was not already at root, cut it and move it:
 247              // echo 'Move to root';

 248              $Chapter->set( 'parent_ID', NULL );
 249              $Chapter->dbupdate();
 250          }
 251  
 252          // Move Chapters to new Blog:

 253          $sql = 'UPDATE T_categories
 254                               SET cat_blog_ID = '.$dest_collection_ID.'
 255                           WHERE cat_blog_ID = '.$src_collection_ID /* extra security */ .'
 256                                AND cat_ID IN ('.implode( ',', $chapters_to_move ).')';
 257          $DB->query( $sql );
 258  
 259          $DB->commit();
 260  
 261          // Now the cache is badly screwed. Reseting it is fair enough, because this won't happen very often.

 262          $this->clear();
 263      }
 264  
 265  
 266      /**

 267       * Support function

 268       *

 269       * @param Chapter

 270       * @param array

 271       */
 272  	function recurse_move_subtree( & $Chapter, & $list_array )
 273      {
 274          // Add this to the list:

 275          $list_array[] = $Chapter->ID;
 276  
 277          foreach( $Chapter->children as $child_Chapter )
 278          {
 279              $this->recurse_move_subtree( $child_Chapter, $list_array );
 280          }
 281      }
 282  
 283  
 284      /**

 285       * Instanciate a new object within this cache

 286        *

 287        * @param object|NULL

 288        * @param integer|NULL subset to use for new object

 289       */
 290      function & new_obj( $row = NULL, $subset_ID = NULL )
 291      {
 292          // Instantiate a custom object

 293          $Chapter = new Chapter( $row, $subset_ID ); // Copy

 294  
 295          return $Chapter;
 296      }
 297  }
 298  
 299  /*

 300   * $Log: _chaptercache.class.php,v $

 301   * Revision 1.1  2007/06/25 10:59:26  fplanque

 302   * MODULES (refactored MVC)

 303   *

 304   * Revision 1.10  2007/04/26 00:11:06  fplanque

 305   * (c) 2007

 306   *

 307   * Revision 1.9  2006/12/10 01:52:27  fplanque

 308   * old cats are now officially dead :>

 309   *

 310   * Revision 1.8  2006/11/24 18:27:23  blueyed

 311   * Fixed link to b2evo CVS browsing interface in file docblocks

 312   *

 313   * Revision 1.7  2006/11/22 21:53:23  blueyed

 314   * doc

 315   */
 316  ?>


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