clsItemDB();
$this->tablename = GetTablePrefix()."CountCache";
$this->id_field = "CountCacheId";
$this->NoResourceId=1;
if($id)
$this->LoadFromDatabase($id);
}
function LoadFromDatabase($Id)
{
global $Errors;
if(!isset($Id))
{
$Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase");
return false;
}
$sql = sprintf("SELECT * FROM ".$this->tablename." WHERE ".$this->IdField()." = '%s'",$Id);
$result = $this->adodbConnection->Execute($sql);
if ($result === false)
{
$Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase");
return false;
}
$data = $result->fields;
$this->SetFromArray($data);
$this->Clean();
return true;
}
}
class clsCacheCountList extends clsItemCollection
{
var $CategoryId=0;
var $Loaded = FALSE;
function clsCacheCountList($CatId=NULL)
{
$this->clsItemCollection();
$this->SourceTable = GetTablePrefix()."CountCache";
$this->classname = "clsCacheCount";
if($CatId)
$this->CategoryId = $CatId;
}
function LoadCategoryValues()
{
if($this->Loaded==FALSE)
{
$sql = "SELECT * FROM ".$this->SourceTable. " WHERE CategoryId=".(int)$this->CategoryId;
$this->Query_Item($sql);
$this->Loaded = TRUE;
}
}
function GetItemByField($Field,$Value,$LoadFromDB=TRUE)
{
$found=FALSE;
if(is_array($this->Items))
{
foreach($this->Items as $i)
{
if($i->Get($Field)==$Value && $i->Get("CategoryId")==$this->CategoryId)
{
$found = TRUE;
break;
}
}
}
if(!$found && $LoadFromDB==TRUE)
{
$sql = "SELECT * FROM ".$this->SourceTable." WHERE $Field = '$Value' AND CategoryId=".(int)$this->CategoryId;
$res = $this->adodbConnection->Execute($sql);
if($res && !$res->EOF)
{
$i = $this->AddItemFromArray($res->fields);
}
else
$i = FALSE;
}
return $i;
}
function GetCountObject($ListType,$ItemType,$ExtraId,$TodayOnly)
{
$found=FALSE;
if(is_array($this->Items))
{
foreach($this->Items as $i)
{
if($i->Get("ItemType")==$ItemType && $i->Get("ListType")==$ListType &&
($i->Get("ExtraId")==$ExtraId) && $i->Get("TodayOnly")==$TodayOnly)
{
$found = TRUE;
break;
}
}
}
if(!$found)
{
$ListType=intval($ListType);
$sql = "SELECT * FROM ".$this->SourceTable." WHERE ItemType=$ItemType AND ListType=$ListType AND ExtraId='$ExtraId' AND TodayOnly=$TodayOnly";
$res = $this->adodbConnection->Execute($sql);
if($res && !$res->EOF)
{
$i = $this->AddItemFromArray($res->fields);
$found = TRUE;
}
else
$i = FALSE;
}
if(!$found)
$i = FALSE;
return $i;
}
function GetValue($ListType,$ItemType,$ExtraId,$TodayOnly,$Timeout)
{
$val = "";
if(strlen($ExtraId))
{
$i = $this->GetCountObject($ListType,$ItemType,$ExtraId,$TodayOnly);
if(is_object($i))
{
$d = $i->Get("LastUpdate");
$el = adodb_date("U") - $d;
if($el > $Timeout)
{
$val = "";
}
else
{
if($i->Get("CountCacheId")>0)
{
$val = $i->Get("Value");
}
}
}
unset($i);
}
else
{
$sql = "SELECT Value,LastUpdate FROM ".$this->SourceTable." WHERE ListType=$ListType AND ItemType=$ItemType AND TodayOnly=$TodayOnly";
$rs = $this->adodbConnection->Execute($sql);
if($rs && !$rs->EOF)
{
$d = $rs->fields["LastUpdate"];
$el = adodb_date("U") - $d;
if($el < $Timeout)
{
$val = (int)$rs->fields["Value"];
}
else
{
$val = "";
$rs = FALSE;
}
}
}
return $val;
}
function SetValue($ListType,$ItemType,$ExtraId,$Today,$Value)
{
$c = $this->GetCountObject($ListType,$ItemType,$ExtraId,$Today);
if(is_object($c))
{
$c->Set("Value",$Value);
$c->Set("LastUpdate",adodb_date("U"));
$c->Update();
}
else
{
$c = new $this->classname;
$c->Set("ListType",$ListType);
$c->Set("ItemType",$ItemType);
$c->Set("ExtraId",$ExtraId);
$c->Set("Value",$Value);
$c->Set("TodayOnly",$Today);
$c->Set("LastUpdate",adodb_date("U"));
$c->Create();
}
}
function DeleteValue($ListType,$ItemType,$ExtraId,$Today)
{
$c = $this->GetCountObject($ListType,$ItemType,$ExtraId,$Today);
if(is_object($c))
{
$c->Delete();
}
}
function EraseGlobalTypeCache($ItemType)
{
global $objItemTypes;
$db = &GetADODBConnection();
if( !is_numeric($ItemType) )
{
$sql = 'SELECT ItemType FROM '.$objItemTypes->SourceTable.' WHERE ItemName=\''.$ItemType.'\'';
$ItemType=$db->GetOne($sql);
}
$sql='DELETE FROM '.$this->SourceTable.' WHERE ItemType='.$ItemType.' AND ListType=0';
$rs = $db->Execute($sql);
}
function IncrementParentValues($ValueName,$CatList,$IncVal=1)
{
$sql = "UPDATE ".$this->SourceTable." SET Value=Value+$IncVal WHERE CategoryId IN ($CatList) AND ItemType='$ValueName'";
//echo $sql."
\n";
$this->adodbConnection->Execute($sql);
}
function DecrementParentValues($ValueName,$CatList,$DecVal=1)
{
$sql = "UPDATE ".$this->SourceTable." SET Value=Value-$DecVal WHERE CategoryId IN ($CatList)";
//echo $sql."
\n";
$this->adodbConnection->Execute($sql);
}
function GetCatListTotal($ValueName,$CatList)
{
global $objItemTypes;
if(is_array($CatList))
$CatList = implode(",",$CatList);
if($ValueName>0)
{
$sql = "SELECT SUM(Value) as total FROM ".$this->SourceTable." WHERE CategoryId IN (".$CatList.") AND ItemType='".$ValueName."'";
}
else
{
foreach($objItemTypes->Items as $i)
{
$AutoTypes[] = $i->Get("ItemType");
}
$types = implode(",",$AutoTypes);
$sql = "SELECT SUM(Value) as total FROM ".$this->SourceTable." WHERE CategoryId IN (".$CatList.") AND ItemType IN ($types)";
}
//echo $sql;
$rs = $this->adodbConnection->Execute($sql);
if($rs && !$rs->EOF)
{
return (int)$rs->fields["total"];
}
else
return 0;
}
function GetCategoryCount($CategoryId)
{
$sql = "SELECT SUM(Value) as total FROM ".$this->SourceTable." WHERE CategoryId=".$CategoryId;
//echo $sql."
\n";
$rs = $this->adodbConnection->Execute($sql);
if($rs && !$rs->EOF)
{
return $rs->fields["total"];
}
else
return 0;
}
function RefreshCategoryCount($CategoryId,$ItemType)
{
global $objCatList, $objItemTypes;
}
}
?>