b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _class_dataobjectcache.php

Documentation is available at _class_dataobjectcache.php

  1. <?php
  2. /**
  3.  * Data Object Cache Class
  4.  *
  5.  * "data objects by fplanque" :P
  6.  *
  7.  * b2evolution - {@link http://b2evolution.net/}
  8.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  9.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  10.  *
  11.  * @package evocore
  12.  */
  13. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  14.  
  15. /**
  16.  * Data Object Cache Class
  17.  *
  18.  * @package evocore
  19.  * @version beta
  20.  */
  21. {
  22.     /**#@+
  23.      * @access private
  24.      */
  25.     var    $objtype;
  26.     var    $dbtablename;
  27.     var $dbprefix;
  28.     var $dbIDname;
  29.     var $cache array();
  30.     var $load_add false;
  31.     var $all_loaded false;
  32.     /**#@-*/
  33.  
  34.     /**
  35.      * Constructor
  36.      *
  37.      * {@internal DataObjectCache::DataObjectCache(-) }}
  38.      *
  39.      * @param string Name of DataObject class we are cacheing
  40.      * @param boolean true if it's OK to just load all items!
  41.      * @param string Name of table in database
  42.      * @param string Prefix of fields in the table
  43.      * @param string Name of the ID field (including prefix)
  44.      */
  45.     function DataObjectCache$objtype$load_all$tablename$prefix ''$dbIDname 'ID' )
  46.     {
  47.         $this->objtype $objtype;
  48.         $this->load_all $load_all;
  49.         $this->dbtablename $tablename;
  50.         $this->dbprefix $prefix;
  51.         $this->dbIDname $dbIDname;
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Load the cache **extensively**
  57.      *
  58.      * {@internal DataObjectCache::load_all(-) }}
  59.      */
  60.     function load_all()
  61.     {
  62.         global $DB;
  63.  
  64.         if$this->all_loaded )
  65.             return    false;    // Already loaded;
  66.  
  67.         debug_log"Loading <strong>$this->objtype(ALL)</strong> into cache);
  68.         $sql "SELECT * FROM $this->dbtablename";
  69.         $rows $DB->get_results$sql );
  70.         $dbIDname $this->dbIDname;
  71.         $objtype $this->objtype;
  72.         ifcount($rows) ) foreach$rows as $row )
  73.         {
  74.             $this->cache$row->$dbIDname new $objtype$row )// COPY!
  75.             // $obj = $this->cache[ $row->$dbIDname ];
  76.             // $obj->disp( 'name' );
  77.         }
  78.  
  79.         $this->all_loaded true;
  80.  
  81.         return true;
  82.     }
  83.  
  84.  
  85.     /**
  86.      * Load a list of objects into the cache
  87.      *
  88.      * {@internal DataObjectCache::load_list(-) }}
  89.      *
  90.      * @param string list of IDs of objects to load
  91.      */
  92.     function load_list$req_list )
  93.     {
  94.         global $DB;
  95.  
  96.         debug_log"Loading <strong>$this->objtype($req_list)</strong> into cache);
  97.         $sql "SELECT * FROM $this->dbtablename WHERE $this->dbIDname IN ($req_list)";
  98.         $rows $DB->get_results$sql );
  99.         $dbIDname $this->dbIDname;
  100.         $objtype $this->objtype;
  101.         ifcount($rows) ) foreach$rows as $row )
  102.         {
  103.             $this->cache$row->$dbIDname new $objtype$row )// COPY!
  104.             // $obj = $this->cache[ $row->$dbIDname ];
  105.             // $obj->disp( 'name' );
  106.         }
  107.     }
  108.  
  109.  
  110.     /**
  111.      * Add a dataobject to the cache
  112.      *
  113.      * {@internal DataObjectCache::add(-) }}
  114.      */
  115.     function add$Obj )
  116.     {
  117.         ifisset($Obj->ID&& $Obj->ID != )
  118.         {
  119.             $this->cache[$Obj->ID$Obj;
  120.             return true;
  121.         }
  122.         return false;
  123.     }
  124.  
  125.  
  126.     /**
  127.      * Clear the cache **extensively**
  128.      *
  129.      * {@internal DataObjectCache::clear(-) }}
  130.      */
  131.     function clear()
  132.     {
  133.         $this->cache array();
  134.         $this->all_loaded false;
  135.     }
  136.  
  137.  
  138.     /**
  139.      * Get an object from cache by ID
  140.      *
  141.      * Load the cache if necessary
  142.      *
  143.      * {@internal DataObjectCache::get_by_ID(-) }}
  144.      *
  145.      * @param integer ID of object to load
  146.      * @param boolean false if you want to return false on error
  147.      */
  148.     function get_by_ID$req_ID$halt_on_error true )
  149.     {
  150.         global $DB;
  151.  
  152.         if!empty$this->cache$req_ID ) )
  153.         {    // Already in cache
  154.             // debug_log( "Accessing $this->objtype($req_ID) from cache" );
  155.             return $this->cache$req_ID ];
  156.         }
  157.         elseif!$this->all_loaded )
  158.         {    // Not in cache, but not everything is loaded yet
  159.             if$this->load_all )
  160.             {    // It's ok to just load everything:
  161.                 $this->load_all();
  162.             }
  163.             else
  164.             // Load just the requested object:
  165.                 debug_log"Loading <strong>$this->objtype($req_ID)</strong> into cache);
  166.                 $sql "SELECT * FROM $this->dbtablename WHERE $this->dbIDname = $req_ID";
  167.                 $row $DB->get_row$sql );
  168.                 $dbIDname $this->dbIDname;
  169.                 $objtype $this->objtype;
  170.                 $this->cache$row->$dbIDname new $objtype$row )// COPY!
  171.             }
  172.         }
  173.  
  174.         ifempty$this->cache$req_ID ) )
  175.         {    // Requested object does not exist
  176.             if$halt_on_error die"Requested $this->objtype does not exist!);
  177.             return false;
  178.         }
  179.  
  180.         return $this->cache$req_ID ];
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Display form option list with cache contents
  186.      *
  187.      * Load the cache if necessary
  188.      *
  189.      * {@internal DataObjectCache::get_by_ID(-) }}
  190.      *
  191.      * @param integer selected ID
  192.      * @param boolean provide a choice for "none" with ID 0
  193.      */
  194.     function option_list$default 0$allow_none false )
  195.     {
  196.         global $cache_Groups;
  197.         
  198.         if$this->all_loaded )
  199.             $this->load_all();
  200.  
  201.         if$allow_none )
  202.         {
  203.             echo '<option value="0"';
  204.             if== $default echo ' selected="selected"';
  205.             echo '>'T_('None','</option>'."\n";
  206.         }
  207.  
  208.         foreach$this->cache as $loop_Obj )
  209.         {
  210.             echo '<option value="'.$loop_Obj->ID.'"';
  211.             if$loop_Obj->ID == $default echo ' selected="selected"';
  212.             echo '>';
  213.             $loop_Obj->disp'name' );
  214.             echo '</option>'."\n";
  215.         }
  216.     }
  217. }
  218. ?>

Documentation generated on Tue, 20 May 2008 01:52:52 +0200 by phpDocumentor 1.4.2