Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

ConfigDBBackend.class.php

Go to the documentation of this file.
00001 <?php
00006 include_once("core/config/IConfigBackend.class.php");
00007 include_once("core/db/dbjames.class.php");
00008 
00012 class ConfigDBBackend extends IConfigBackend
00013 {
00014         var $idM;       
00015         var $idG;       
00016         var $idU;       
00017         
00018         var $dbObj;     
00019         
00024         function ConfigDBBackend($petition)
00025         {
00026                 $this->idM = $petition->GetModule();
00027                 $this->idG = $petition->GetGroup();
00028                 $this->idU= $petition->GetUser();
00029                 
00030                 // If it is a system petition...
00031                 if ($this->idM == null) $this->idM = 0;
00032                 if ($this->idG == null) $this->idG = 0;
00033                 if ($this->idU == null) $this->idU = 0;
00034                 
00035                 $this->dbObj = new DBJames();
00036         }
00037         
00043         function GetConfig()
00044         {
00045                 $result = array();
00046                 
00047                 $query  = " SELECT ConfigValues.*, ConfigKeys.keyName FROM ";
00048                 $query .= " ConfigValues LEFT JOIN ConfigKeys ON ConfigValues.idK = ConfigKeys.idK ";
00049                 $query .= " WHERE ConfigValues.idU=".$this->idU." ";
00050                 $query .= " AND ConfigValues.idG=".$this->idG." ";
00051                 $query .= " AND ConfigKeys.idM=".$this->idM;
00052                 
00053                 $this->dbObj->Query($query);
00054                 
00055                 /* Carga la configuración */
00056                 while (! $this->dbObj->EOF)
00057                 {
00058                         $result[$this->dbObj->row["key"]]=$this->dbObj->row["value"];
00059                         $this->dbObj->Next();
00060                 }
00061                 
00062                 return $result;
00063         }
00064         
00069         function SetConfig($keys)
00070         {
00071                 if (count($keys) > 0)
00072                 {
00073                         foreach($keys as $key => $value)
00074                         {
00075                                 $this->_MakeQuery($key, $value);
00076                         }
00077                 }
00078         }
00079         
00086         function _MakeQuery($key, $value)
00087         {
00088                 $idK = $this->_GetIdK($key);
00089                 
00090                 // First, we try to update the value in the DB
00091                 $result = $this->_UpdateValue($idK, $value);
00092                 
00093                 // If there was an error, maybe that it is the first time it
00094                 // is added in the DB, so we'll try an insert.
00095                 if (! $result)
00096                         $result = $this->_InsertValue($idK, $value);
00097                         
00098                 return $result;
00099         }
00100         
00106         function _GetIdK($key)
00107         { 
00108                 $query  = " SELECT * FROM ConfigKeys ";
00109                 $query .= " WHERE ConfigKeys.idM=".$this->idM;
00110                 $query .= " AND ConfigKeys.key='".$key."'";
00111                 
00112                 $this->dbObj->Query($query);
00113                 
00114                 if ($this->dbObj->numRows == 0)
00115                 {
00116                         return false;
00117                 }
00118                 else
00119                 {
00120                         return $this->dbObj->row["idK"];
00121                 }
00122         }
00123         
00130         function _InsertValue($idK, $value)
00131         {
00132                 $query  = " INSERT INTO ConfigValues ";
00133                 $query .= " ( idU, idG, idK, value ) ";
00134                 $query .= " VALUES ( ".$this->idU.", ".$this->idG;
00135                 $query .= ", ".$idK.", '".$value."' )";
00136 
00137                 $this->dbObj->Execute($query);
00138         
00139                 // It will be false if there was an error
00140                 return $this->dbObj->rs;
00141         }
00142 
00149         function _UpdateValue($idK, $value)
00150         {          
00151                 $query  = " UPDATE ConfigValues ";
00152                 $query .= " SET value='".$value."' ";
00153                 $query .= " WHERE idU=".$this->idU." AND ";
00154                 $query .= " idG=".$this->idG." AND idK=".$idK;
00155 
00156                 $this->dbObj->Execute($query);
00157 
00158                 if (! $this->dbObj->rs || $this->dbObj->numRows == 0)
00159                 {
00160                         return false;
00161                 }
00162                 else
00163                         return $this->dbObj->rs;
00164         }               
00165                 
00166 }
00167 ?>

Generated on Wed Nov 19 20:29:34 2003 for James by doxygen 1.3.4