<?php
include_once __DIR__."/../shd/common.php";
$g_bu_table = array();
$g_bu_colIdxs = array();
ensure_cached_table( $g_bu_colIdxs );
/**
* Output the cache in a readable format. Rather than being a 2D array, it
* is a 1D array, each entry corresponding to a single row.
*
* $width_limits
* This array contains the size limits of each field in field order. The array may be smaller than the
* total number of fields in which case the remaining fields will expand as necessary.
*/
function format_cache( $width_limits=array() ){
global $g_bu_table;
global $g_bu_colIdxs;
//get the index names in order
$arr_header = array();
$keys = array_keys($g_bu_colIdxs);
foreach( $keys as $key){
$ordinal_pos = $g_bu_colIdxs[$key];
$arr_header[$ordinal_pos] = $key;
}
//make a copy and push the header values on the front as a single row
$arr_all = $g_bu_table;
array_unshift( $arr_all, $arr_header);
return format_array_2d( $arr_all, $width_limits );
}
/**
* Clears all cached data.
*/
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 asc, BU_TIME asc' );
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;
}
}
}
?>