<?php
$g_bu_table = array();
$g_bu_colIdxs = array();
ensure_cached_table( $g_bu_colIdxs );
/**
* Clears all cached data. This is called after any DB updates. It can be called at any time since loading is lazy.
*/
function clear_cached_data(){
global $g_bu_states;
global $g_bu_table;
$g_bu_states = null;
$g_bu_table = array();
debug_println("cache cleared", DEBUG_MED );
}
/**
* Make sure that the main table data is populated
*/
function ensure_cached_table( &$colIdxs = null){
global $g_bu_table;
if(count($g_bu_table)>0) return;
getbu_list_all( $g_bu_table, $colIdxs, 'order by BU_DATE desc, BU_TIME desc' );
debug_println("cache loaded", DEBUG_MED );
}
/**
* Reload a single record by buid. The new row is returned by ref.
*/
function &reload_cached_rec( $buid ){
global $g_bu_table;
global $g_bu_colIdxs;
$row = getbu_row( $buid, $g_bu_colIdxs );
for( $i=0; $i<count($g_bu_table); $i++){
if($g_bu_table[ $i ][ $g_bu_colIdxs['BUID']] == $row[$g_bu_colIdxs['BUID']]){
$g_bu_table[ $i ] = $row;
return $g_bu_table[ $i ];
}
}
}
/**
* Returns a reference to the row with the given buid.
*/
function &find_rec_by_id( $buid ){
global $g_bu_table;
global $g_bu_colIdxs;
for( $i=0; $i<count($g_bu_table); $i++){
if($g_bu_table[ $i ][ $g_bu_colIdxs['BUID']] == $buid ){
return $g_bu_table[ $i ];
}
}
debug_println("rec not found($buid). Count is ".count($g_bu_table), DEBUG_MED );
return null;
}
/**
* Delete a single record from the cache.
*/
function delete_cached_rec( $buid ){
global $g_bu_table;
global $g_bu_colIdxs;
for( $i=0; $i<count($g_bu_table); $i++){
if($g_bu_table[ $i ][ $g_bu_colIdxs['BUID']] = $buid ){
unset($g_bu_table[ $i ]);
return;
}
}
}
?>