<?php
class clsSysCacheItem extends clsItemDB 
{
    function clsSysCacheItem($id = NULL)
    {
        $this->clsItemDB();
        $this->tablename = GetTablePrefix()."SysCache";
        $this->type=12;
        $this->BasePermission="";
        $this->id_field = "SysCacheId";
        $this->NoResourceId=1;
        $this->debuglevel=0;
        
        if($id != NULL)
        {
            if(is_numeric($id))
            { 
                $this->LoadFromDatabase($id);
            }
            else
                $this->LoadByName($name);
        }
    }
    
    function DetectCahanges($name, $value)
    {
    	
    }

	function LoadFromDatabase($Id)
    {
        global $Errors;
   
        $sql = sprintf("SELECT * FROM ".$this->tablename." WHERE ".$this->id_field."='%s'",$Id);

        $result = $this->adodbConnection->Execute($sql);
        if ($result === false || $result->EOF)
        {
            $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase");
            return false;
        }        
		$data = $result->fields;       
        $this->SetFromArray($data);
        $this->Clean();
        return true;
    }    

    function LoadByName($name)
    {
        global $Errors;
   
        $sql = sprintf("SELECT * FROM ".$this->tablename." WHERE Name ='%s'",$name);

        $result = $this->adodbConnection->Execute($sql);
        if ($result === false || $result->EOF)
        {
            $Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadByName");
            return false;
        }
        
		$data = $result->fields;       
        $this->SetFromArray($data);
        $this->Clean();
        return true;
    }
}

class clsSysCacheList extends clsItemCollection 
{

    function clsSysCacheList()
    {
        $this->clsItemCollection();
        $this->SourceTable=GetTablePrefix()."SysCache";
        $this->classname = "clsSysCacheItem";

    }

    function &GetItemByModule($name, $module, $GroupList="", $LoadFromDB=TRUE)
    {
        $found=FALSE;

        if(is_array($this->Items))
        {
          foreach($this->Items as $i)
          {
            if($i->Get("Name")==$name && $i->Get("Module")==$module)
            {
              $found = TRUE;
              break;
            }
          }
        }
        if(!$found && $LoadFromDB==TRUE)
        {
            $sql = "SELECT * FROM ".$this->SourceTable." WHERE Name = '$name' AND Module='$module' AND GroupList='$GroupList'";
            //echo $sql;
            $res = $this->adodbConnection->Execute($sql);

            if($res && !$res->EOF)
            {
                $i = $this->AddItemFromArray($res->fields);
                $i->tablename = $this->SourceTable;
                $i->Clean();
                $found = TRUE;
            }
            else
                $i = FALSE;
        }
        if(!$found)
        {        
            unset($i);
            $i = FALSE;
        }
        return $i;        
    }

    function &GetContextItem($name,$module,$context, $GroupList="", $LoadFromDB=TRUE)
    {
        $found=FALSE;

        if(is_array($this->Items))
        {
          foreach($this->Items as $i)
          {
            if($i->Get("Name")==$name && $i->Get("Module")==$module && $i->Get("Context")==$context)
            {           	
              $found = TRUE;
              break;
            }
          }
        }
        

        if(!$found && $LoadFromDB==TRUE)
        {
            unset($i);
            $sql = "SELECT * FROM ".$this->SourceTable." WHERE Name = '$name' AND Module='$module' AND Context='$context' AND GroupList='$GroupList'";
            
            //echo $sql."<br>\n";   
            $res = $this->adodbConnection->Execute($sql);

            if($res && !$res->EOF)
            {
                $i = $this->AddItemFromArray($res->fields);
                $i->tablename = $this->SourceTable;
                $i->Clean();
                $found = TRUE;
            }
            else
                $i = FALSE;
        }
        if(!$found)
        {        
            unset($i);
            $i = FALSE;
        }
        return $i;           
    }

    function &AddCacheItem($name,$value,$module="",$expire=0,$context="",$GroupList="")
    {
		$i = new clsSysCacheItem();

		$i->tablename = $this->SourceTable;
        $i->Set(array("Name","Value","Expire","GroupList"),array($name,$value,$expire, $GroupList));
        if(strlen($module))
            $i->Set("Module",$module);
        $i->Set("Context",$context);
        $conn = &GetADODBConnection();
        $sql = 'SELECT * FROM '.$i->tablename.' 
        			WHERE Name="'.$name.'" 
        				AND GroupList='.(int)$GroupList.'
        				AND Context="'.$context.'"
        				AND Module="'.$module.'"';
        $rs = $conn->Execute($sql);
        if ($rs->EOF) 
        {
			$i->Create();
        }
		return $i;        
    }

    function &EditCacheItem($name,$value,$module="",$expire=0,$context="",$GroupList="")
    {
        if(strlen($context))
        {        
          $i =& $this->GetItemByModule($name,$module, $GroupList,TRUE);
        }
        else
            $i =& $this->GetContextItem($name,$module,$context,$GroupList);
        if(is_object($i))
        {
            if($i->Get("Value")!=$value && $name==$i->Get("Name") && $i->Get("Context")==$context && 
               $GroupList==$i->Get("GroupList"))
            {            
              $i->Set(array("Name","Value","Expire","GroupList"),array($name,$value,$expire, $GroupList));
              if(strlen($module))
                $i->Set("Module",$module);
              $i->Set("Context",$context);
            
              $i->Update();
            }
            else
            {
                $this->AddCacheItem($name,$value,$module,$expire, $context, $GroupList);
            }
        }
        else
        {
            $this->AddCacheItem($name,$value,$module,$expire, $context, $GroupList);
        }
    }

    function DeleteCacheItem($name,$module="")
    {
        $i =& $this->GetItemByModule($name,$module,TRUE);
        if(is_object($i))
        {
            $i->Delete();
        }
    }

    function GetContextValue($Name,$Module,$Context="",$GroupList="")
    {
        $ret = "";
        $i =& $this->GetContextItem($Name,$Module,$Context, $GroupList);
        if(is_object($i))
            $ret = $i->Get("Value");
        return $ret;
    }

    function GetValue($Name,$Module="",$GroupList="")
    {
        $ret = "";
        $i =& $this->GetItemByModule($Name,$Module,$GroupList);
        if(is_object($i))
            $ret = $i->Get("Value");
        return $ret;
    }    

    function PurgeExpired()
    {
        $sql = "DELETE FROM ".$this->SourceTable." WHERE Expire>0 AND Expire <".adodb_date("U");
        $this->adodbConnection->Execute($sql);        
    }

    function PurgeCategory($CatId)
    {
        $sql = "DELETE FROM ".$this->SourceTable." WHERE Context LIKE ':m".$CatId."-%'";
        $this->adodbConnection->Execute($sql);
    }

    function DeleteCachedItem($where)
    {
        $sql = "DELETE FROM ".$this->SourceTable;
        if(strlen($where))
            $sql .= " WHERE $where";
        $this->adodbConnection->Execute($sql);
        //echo $sql."<br>\n";
    }
}
?>