User management for the world wide web

Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

Properties.php

00001 <?php
00002 
00009 class GenieGate_Properties_Section {
00010 
00011   var $DBH;
00012   var $uid;
00013   var $skey;
00014   var $prmap = array(); // property names to numerical ID's.
00015   var $pm;
00016 
00022   function GenieGate_Properties_Section(&$pm, $uid,$skey){
00023     $this->DBH = $pm->DBH;
00024     $this->pm = $pm;
00025     $this->skey = $skey;
00026     $this->uid = mysql_escape_string($uid);
00027     $this->prmap = $pm->getPropertyId($skey); // Map
00028     if(! $this->prmap){
00029       $this->prmap = array(); // Empty array.
00030     }
00031   }
00041   function getDescription($property_key = FALSE){    
00042     return($this->pm->getDescription($this->skey, $property_key));
00043   }
00048   function getUserId(){ return($this->uid); }
00053   function getSectionKey(){ return($this->skey); }
00054 
00059   function getPropertyNames(){
00060     return(array_keys($this->prmap));
00061   }
00069   function isProperty($property) {
00070     return(isset($this->prmap[$property]));
00071   }
00078   function getProperty($name){
00079     $prid = $this->prmap[$name];
00080     if(! $prid){
00081       return;
00082     }    
00083     $sql = "SELECT val FROM ua_prop WHERE prid='$prid' AND uid='$this->uid'";
00084     $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00085     if($result){
00086       list($v) = mysql_fetch_row($result);
00087       mysql_free_result($result);
00088       return($v);
00089     }
00090     return;
00091   }
00097   function setProperty($name,$value){
00098     $old_v = $this->getProperty($name);
00099     $prid = $this->prmap[$name];
00100     if(! $prid){
00101       die("Invalid property: [$this->skey/$name]");
00102     }
00103     if(isset($old_v)){
00104       $sql = sprintf("UPDATE ua_prop SET val='%s' WHERE uid='$this->uid' AND prid='$prid'",
00105              mysql_escape_string($value));
00106     }else{
00107       $sql = sprintf("INSERT INTO ua_prop (prid,uid,val) VALUES('$prid','$this->uid','%s')",
00108              mysql_escape_string($value));
00109     }
00110     mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00111     $this->pm->firePropertyChanged($this->uid,$this->skey,$name,$old_v,$value);
00112   }
00113 
00114 
00119   function removeAll(){
00120     $this->pm->removeUser($this->skey,$this->uid);
00121   }
00122 
00123 }
00124 
00138 class GenieGate_Properties {
00139   
00140   var $DBH;
00141 
00142   //Section names -> sid's
00143   var $SECT_CACHE = array();
00144 
00145   var $LISTENERS = array();
00146 
00150   function GenieGate_Properties(&$dbh){
00151     $this->DBH = $dbh;
00152   }
00153   
00162   function getSection($section,$uid){
00163     return(new GenieGate_Properties_Section($this,$uid,$section));
00164   }
00165 
00183   function createSection($skey,$name){
00184     $sid = $this->getSectionId($skey);
00185     if($sid){
00186       die("Section $skey already exists");     
00187     }
00188     $sql = sprintf("INSERT INTO ua_psect (skey,name) VALUES('%s','%s')",
00189            mysql_escape_string($skey),
00190            mysql_escape_string($name));
00191     mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00192     $id = mysql_insert_id(); 
00193     $this->SECT_CACHE[$skey] = $id;
00194     return($id);
00195   }
00203   function updateSection($skey,$label){
00204       $sid = $this->getSectionId($skey);
00205       if(! $sid){
00206           $this->createSection($skey,$label);
00207           return;
00208       }
00209       $sql = sprintf("UPDATE ua_psect SET name='%s' WHERE sid='%s'",
00210                      mysql_escape_string($label),
00211                      $sid);
00212       mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00213       return($id);      
00214   }
00215 
00224   function getSectionId($skey){
00225     if(! isset($this->SECT_CACHE[$skey])){
00226       $key = mysql_escape_string($skey);
00227       $sql = "SELECT sid FROM ua_psect WHERE skey='$key'";
00228       $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00229       if($result){
00230           $row =  mysql_fetch_row($result); 
00231           if($row[0]){
00232               $this->SECT_CACHE[$skey] = $row[0];
00233           }else{
00234               $this->SECT_CACHE[$skey] = ''; // so isset() works on it.
00235           }
00236           mysql_free_result($result);
00237       }
00238     }
00239     return($this->SECT_CACHE[$skey]);
00240   }
00241 
00242 
00260   function getSections($depth = FALSE){
00261     $sql = "SELECT skey,name FROM ua_psect";
00262     $all = array();
00263     $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00264     if($result){
00265       while( list($key,$name) = mysql_fetch_row($result)){
00266     if($depth){
00267       $all[$key] = array($name,$this->getSectionProperties($key));
00268     }else{
00269       $all[$key] = $name;
00270     }
00271       }
00272       mysql_free_result($result);
00273     }
00274     return($all);
00275   }
00276 
00277 
00286   function removeUser($skey,$uid){
00287     $sid = $this->getSectionId($skey);
00288     $uid = mysql_escape_string($uid);
00289 
00290     $rm_sql = "DELETE FROM ua_prop WHERE prid='%s' AND uid='%s'";
00291 
00292     // Tricky, we need to first get a list of all the properties that will be changed.
00293     // so we can alert the plugins about it.
00294     $sql = "SELECT p.prid,uid,sid,pkey,val FROM ";
00295     $sql .= "ua_prnam as n, ua_prop as p ";
00296     $sql .= "WHERE n.sid='$sid' AND p.prid=n.prid AND uid='$uid'";
00297     $qry = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00298     if($qry){
00299       while( list($prid,$ud,$sc,$pn,$val) = mysql_fetch_row($qry)){
00300     // Remove said property.
00301     mysql_query(sprintf($rm_sql,$prid,$ud),$this->DBH) or die(mysql_error($this->DBH)); 
00302     // Tell the listeners about it. 
00303     $this->firePropertyRemoved($ud,$skey,$pn,$val);
00304       }
00305     }
00306   }
00307 
00308 
00315   function getSectionProperties($skey){
00316     $all = array();
00317     $sid = $this->getSectionId($skey);
00318     $sql = "SELECT pkey,diz FROM ua_prnam WHERE sid='$sid'";
00319     $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00320     if($result){
00321       while(list($key,$diz) = mysql_fetch_row($result)){
00322     $all[$key] = $diz;
00323       }
00324       mysql_free_result($result);
00325     }
00326     return($all);
00327   }
00328 
00335   function createProperty($skey,$name,$desc){
00336     $sid = $this->getSectionId($skey);   
00337     if(! $sid){
00338       die("Section $skey unknown");
00339     }
00340     if(preg_match("/[^\w\.]/",$name)){
00341       die("Property names must be alphanumeric");
00342     }
00343     $prid = $this->getPropertyId($skey,$name);
00344     if($prid){
00345       return(FALSE);
00346     }
00347     $sql = sprintf("INSERT INTO ua_prnam(sid,pkey,diz) VALUES('$sid','%s','%s')",
00348            mysql_escape_string($name),
00349            mysql_escape_string($desc));
00350     mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00351     $prid = mysql_insert_id(); 
00352     $this->firePropertyCreated($skey,$name,$desc);
00353     return($prid);
00354   }
00355 
00356 
00365   function undefineProperty($skey,$prop){    
00366     $sid = $this->getSectionId($skey);
00367     if(! $sid){
00368       return;
00369     }    
00370     $psql = "SELECT prid FROM ua_prnam WHERE sid='$sid' AND pkey='$prop'";
00371     $result = mysql_query($psql,$this->DBH) or die(mysql_error($this->DBH));
00372     if($result){
00373       $this->firePropertyUndefine($skey,$prop);
00374       $row = mysql_fetch_row($result);
00375       $prid = $row[0];
00376       mysql_free_result($result);      
00377       // Nuke everyones properties.
00378       mysql_query("DELETE FROM ua_prop WHERE prid='$prid'",$this->DBH) or die(mysql_error($this->DBH));
00379       mysql_query("DELETE FROM ua_prnam WHERE prid='$prid'",$this->DBH) or die(mysql_error($this->DBH));
00380       return(TRUE);
00381     }
00382     return(FALSE);
00383   }
00384 
00394   function getPropertyId($section_key,$property_key = FALSE){
00395     $sid = $this->getSectionId($section_key);
00396     if(! $property_key){
00397       return($this->_getPropertyId($sid)); // Pretend to polymorph.
00398     }
00399     $pkey = mysql_escape_string($property_key);
00400     $sql = "SELECT prid FROM ua_prnam WHERE sid='$sid' AND pkey='$pkey'";
00401     $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));                
00402     if($result){
00403       $row =  mysql_fetch_row($result);      
00404       mysql_free_result($result);
00405       return($row[0]);
00406     }
00407     return(FALSE);
00408   }
00409    
00410   // What getPropertyId($section_key) should be.
00411   function _getPropertyId($sid){    
00412     $sql = "SELECT pkey,prid FROM ua_prnam WHERE sid='$sid'";
00413     $result = mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00414 
00415     if($result){
00416       $map = array();
00417       while( list($key,$id) = mysql_fetch_row($result) ) {
00418     $map[$key] = $id;
00419       }
00420       return($map);
00421     }
00422 
00423     return(FALSE);
00424   }
00425 
00435   function getDescription($section_key, $property_key = FALSE){
00436     $sid = $this->getSectionId($section_key);
00437     if(! $property_key){            
00438       return($this->_getSectionDescription($sid));
00439     }
00440     $key = mysql_escape_string($property_key);
00441     $sql = "SELECT diz FROM ua_prnam WHERE sid='$sid' AND pkey='$key'";
00442     $result = mysql_query($sql,$this->DBH) or die(mysql_error());
00443     if($result){
00444       $diz =  mysql_fetch_row($result);      
00445       mysql_free_result($result);
00446       return($diz[0]);
00447     }
00448     return;
00449   }
00450 
00451   // Return the description of a section.
00452   function _getSectionDescription($sid){    
00453     $result = mysql_query("SELECT name FROM ua_psect WHERE sid='$sid'",$this->DBH) or die(mysql_error());
00454     if($result){
00455       list($diz) =  mysql_fetch_row($result);      
00456       mysql_free_result($result);
00457       return($diz);
00458     }
00459     return;
00460   }
00461 
00462 
00463 
00468   function addListener(&$listener){
00469     array_push($this->LISTENERS,$listener);
00470   }
00471 
00475   function changePropertyLabel($sect,$prop,$label){
00476     $sid = $this->getSectionId($sect);
00477     $prop = mysql_escape_string($prop);
00478     $label = mysql_escape_string($label);
00479     $sql = "UPDATE ua_prnam SET diz='$label' WHERE sid='$sid' AND pkey='$prop'";
00480     mysql_query($sql,$this->DBH) or die(mysql_error($this->DBH));
00481   }
00482 
00486   function firePropertyCreated($section,$property,$description){
00487     foreach($this->LISTENERS as $l){
00488       $l->propertyCreated($this,$section,$property,$description);
00489     }
00490   }
00494   function firePropertyChanged($uid,$section, $property, $old_value, $new_value) {
00495     foreach($this->LISTENERS as $l){
00496       $l->propertyChanged($this,$uid, $section,$property,$old_value, $new_value);
00497     }
00498   }
00499 
00500   function firePropertyRemoved($uid,$skey,$prop,$old_value){
00501     foreach($this->LISTENERS as $l){
00502       $l->propertyRemoved($this,$section,$property,$description,$old_value);
00503     }
00504   }
00505 
00506 
00507   function firePropertyUndefine($skey,$prop){
00508     foreach($this->LISTENERS as $l){
00509       $l->propertyUndefine($this,$skey,$prop);
00510     }
00511   }
00512 
00513 }
00514 
00515 ?>

DoxyGen Documentation generated by DoxyGen