University of Southern California

Model.php

00001 <?php
00012 error_reporting(E_ALL | E_STRICT);
00013 date_default_timezone_set("America/Los_Angeles");
00014 
00020 class Model {  
00021   protected $version_msg = "2.0rc";
00022   protected $attributes = array();
00023   protected $js_attributes = array();
00024   protected $error_msgs = array();
00025   protected $confpath = "";
00026   
00032   function Model($conf = NULL) {
00033     if ($conf === NULL) {
00034      // Don't do anything.
00035     } else if (is_object($conf) && method_exists($conf,"getKeys") && method_exists($conf,"get")) {
00036       foreach ($conf->getKeys() as $key) {
00037         $this->set($key,$conf->get($key));
00038       }
00039     } else if (file_exists($conf) && is_readable($conf)) {
00040       $this->confpath = $conf;
00041       $this->parseConf(file($conf));
00042     } else if (file_exists("_conf/".$conf) && is_readable("_conf/".$conf)) {
00043       $this->confpath = "_conf/".$conf;
00044       $this->parseConf(file("_conf/".$conf));
00045     } else if (file_exists("../_conf/".$conf) && is_readable("../_conf/".$conf)) {
00046       $this->confpath = "../_conf/".$conf;
00047       $this->parseConf(file("../_conf/".$conf));
00048     } else if (file_exists("../../_conf/".$conf) && is_readable("../../_conf/".$conf)) {
00049       $this->confpath = "../../_conf/".$conf;
00050       $this->parseConf(file("../../_conf/".$conf));
00051     } else {
00052       $this->error("Can't access configuration ".$conf);
00053     }
00054   }
00055   
00061   private function parseConf($lines) {
00062     foreach($lines as $line) {
00063       // Strip Comments
00064       strpos($line,"#") !== false ? list($content,$junk) = explode("#",trim($line),2):$content = trim($line);
00065       // Skip empty lines
00066       if (trim($content) != "") {
00067         // Look for public/private key pairs
00068         list ($key,$value) = explode(":",$content,2);
00069         $isPrivate = false;
00070         if (strpos($value,"private:") === 0) {
00071           list($junk,$value) = explode(":",$value,2);
00072           $isPrivate = true;
00073         }
00074         $isPrivate === false ? $this->js_attributes[$key] = $value:"";
00075         $this->attributes[$key] = $value;          
00076       }
00077     }
00078   }
00079   
00085   function get($key) {
00086     if (isset($this->attributes[$key])) {
00087       return $this->attributes[$key];
00088     }
00089     return false;
00090   }
00091   
00096   function getKeys() {
00097     return array_keys($this->attributes);
00098   }
00099 
00107   function set($key,$value = NULL,$is_js_attribute = false) {
00108     if ($value === NULL) {
00109       if (isset($this->attributes[$key])) {
00110         unset($this->attributes[$key]);
00111       }
00112       if (isset($this->js_attributes[$key])) {
00113         unset($this->js_attributes[$key]);
00114       }
00115     } else {
00116       $this->attributes[$key] = $value;
00117       if ($is_js_attribute) {
00118         $this->js_attributes[$key] = $value;
00119       } else if (isset($this->js_attributes[$key])) {
00120         unset($this->js_attributes[$key]);
00121       }
00122       return isset($this->attributes[$key]);
00123     }
00124     return true;
00125   }
00126   
00132   function remove($key) {
00133     return $this->set($key,NULL);
00134   }
00135   
00140   function version () {
00141     return $this->version_msg;
00142   }
00143 
00149   function error ($msg) {
00150     return ($this->error_msgs[] = $msg);
00151   }
00152 
00153 
00158   function errorCount() {
00159     return count($this->error_msgs);
00160   }
00161   
00162 
00170   function errors ($prefix = "\nWARNING: ", $separator = "\n\t") {
00171      return $prefix.implode($separator,$this->error_msgs);
00172   }
00173 
00174 
00179   function errorReset() {
00180     $this->error_msgs = array();
00181     return true;
00182   }  
00183    
00191   public function toJS($js_object_name) {
00192     $text = "$js_object_name = new Object();\n";
00193     foreach ($this->js_attributes as $key => $value) {
00194       if (is_array($value) || is_object($value)) {
00195         $text .= $js_object_name."['$key'] = eval('".json_encode($value)."');\n";
00196       } else if (is_numeric($value)) {
00197         $text .= $js_object_name."['$key'] = ".$value.";\n";
00198       } else if ($value === true) {
00199         $text .= $js_object_name."['$key'] = true;\n";
00200       } else if ($value === false) {
00201         $text .= $js_object_name."['$key'] = false;\n";
00202       } else {
00203         $text .= $js_object_name."['$key'] = '".str_replace(array("'","\\"),array("\\'",'\\'),$value)."';\n";
00204       }
00205     }
00206     return $text;
00207   }
00208 }
00209 
00210 ?>