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