<?php
/**
* @author Benjamin Gillissen <hide@address.com>
*
* **************************************************************
Copyright (C) 2007 Benjamin Gillissen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html
* **************************************************************
*/
class tpl_data {
private static $CACHE;
private static $FETCH;
public static function newcontext(){
$c = CORE::hash(NULL, 5);
self::$CACHE[$c] = Array();
return $c;
}
public static function clearcontext($c){
if ( ! isset(self::$CACHE[$c]) ){
errors::raise("Data Context '$c' could not cleared, does not exists", CORE_LOG_ERROR, 'TPL');
return FALSE;
}
unset(self::$CACHE[$c]);
return TRUE;
}
public static function setdata($c, $data){
if ( ! isset(self::$CACHE[$c]) ){
errors::raise("Unknow Data Context '$c', data could not be set", CORE_LOG_ERROR, 'TPL');
return FALSE;
}
if ( !is_array($data) ){
errors::raise("SetData expect second parameters to be an array, in context '$c'", CORE_LOG_ERROR, 'TPL');
return FALSE;
}
//echo "Setting Cache $c value to : ";print_r($data);echo "<br/>\n";
self::$CACHE[$c] = $data;
return TRUE;
}
public static function appendata($c, $data, $k){
if ( ! isset(self::$CACHE[$c]) ){
errors::raise("Unknow Data Context '$c', data could not be append to $k", CORE_LOG_ERROR, 'TPL');
return FALSe;
}
//echo "Append To Cache $c as k $k : $data<br/>\n";
self::$CACHE[$c][$k] = $data;
return TRUE;
}
public static function fetch($c, $k=NULL){
if ( ! isset(self::$CACHE[$c]) ){
$err = new error("Unknow Data Context '$c', data could not be fetched", CORE_LOG_ERROR, 'TPL');
return $err;
}
if ( !isset(self::$FETCH[$c]) ){ self::$FETCH[$c] = -1; }
$i=0;
reset(self::$CACHE[$c]);
foreach(self::$CACHE[$c] as $k => $val ){
if ( $i > self::$FETCH[$c] ){
self::$FETCH[$c] = $i;
return Array($k, $val);
}
$i++;
}
self::$FETCH[$c] = -1;
return FALSE;
}
//fetch context data one by one, return array($k, $val);
public static function fetch_strings($c, $k=NULL){
if ( ! isset(self::$CACHE[$c]) ){
$err = new error("Unknow Data Context '$c', data could not be fetched", CORE_LOG_ERROR, 'TPL');
return $err;
}
if ( !isset(self::$FETCH[$c]) ){ self::$FETCH[$c] = -1; }
$i=0;
reset(self::$CACHE[$c]);
foreach(self::$CACHE[$c] as $k => $val ){
if ( $i > self::$FETCH[$c] AND ! is_array($val) ){
self::$FETCH[$c] = $i;
//echo "Strings for $c return $k => $val<br/>\n";
return Array($k, $val);
}
$i++;
}
self::$FETCH[$c] = -1;
return FALSE;
}
public static function fetch_arrays($c, $k=NULL){
if ( ! isset(self::$CACHE[$c]) ){
$err = new error("Unknow Data Context '$c', data could not be fetched", CORE_LOG_ERROR, 'TPL');
return $err;
}
if ( !isset(self::$FETCH[$c]) ){ self::$FETCH[$c] = -1;}
$i=0;
reset(self::$CACHE[$c]);
foreach(self::$CACHE[$c] as $k => $val ){
if ( $i > self::$FETCH[$c] AND is_array($val) ){
self::$FETCH[$c] = $i;
//echo "Arrays for $c return $k => $val<br/>\n";
return Array($k, $val);
}
$i++;
}
self::$FETCH[$c] = -1;
return FALSE;
}
public static function fetch_reset($c){
if ( ! isset(self::$CACHE[$c]) ){
errors::raise("Unknow Data Context '$c', data fetch counter could not be reset", CORE_LOG_ERROR, 'TPL');
return FALSE;
}
self::$FETCH[$c] = -1;
reset(self::$CACHE[$c]);
return TRUE;
}
}