![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00012 class Dataface_Ontology { 00013 00014 var $table; 00015 var $attributes; 00016 var $fieldnames; 00017 var $relationships; 00018 00019 function Dataface_Ontology($tablename){ 00020 $this->table =& Dataface_Table::loadTable($tablename); 00021 } 00022 00023 function &getAttributes(){ 00024 if ( !isset($this->attributes) ){ 00025 $this->buildAttributes(); 00026 } 00027 return $this->attributes; 00028 } 00029 00030 function &getAttribute($name){ 00031 $atts =& $this->getAttributes(); 00032 if ( !isset($atts[$name]) ){ 00033 return PEAR::raiseError("No attribute '$name' exists in this Ontology.", DATAFACE_E_ERROR); 00034 } 00035 return $atts[$name]; 00036 } 00037 00038 function &newIndividual(&$record){ 00039 $ind = new Dataface_Ontology_individual($this, $record); 00040 return $ind; 00041 } 00042 00043 function &newOntology($type, $tablename){ 00044 $ontologies =& self::getRegisteredOntologies(); 00045 if ( !isset($ontologies[$type]) ){ 00046 return PEAR::raiseError("No ontology of type '$type' has been registered.", DATAFACE_E_ERROR); 00047 } 00048 import($ontologies[$type]['path']); 00049 $class = $ontologies[$type]['class']; 00050 $ont = new $class($tablename); 00051 return $ont; 00052 } 00053 00054 public static function registerType($type, $path, $class){ 00055 $ontologies =& self::getRegisteredOntologies(); 00056 $ontologies[$type] = array('type'=>$type, 'path'=>$path, 'class'=>$class); 00057 return true; 00058 } 00059 00060 public static function &getRegisteredOntologies(){ 00061 static $ontologies = 0; 00062 if ( $ontologies === 0 ) $ontologies = array(); 00063 return $ontologies; 00064 } 00065 00066 function buildAttributes(){ 00067 trigger_error("Please implement the ".__FUNCTION__." method", E_USER_ERROR); 00068 } 00069 00070 function getFieldname($attname){ 00071 if ( !isset($this->fieldnames) ){ 00072 // If the fieldNames map hasn't been created yet, we need to 00073 // tell the subclass to create it. We can do this by calling 00074 // getAttributes, which, in turn, calls buildAttributes() 00075 // which should build both the attributes array and the 00076 // fieldNames array 00077 $this->getAttributes(); 00078 } 00079 00080 if ( !isset($this->fieldnames) ){ 00081 throw new Exception("The fieldnames array has not been set so there is a problem with this Ontology. An ontology should populate the fieldNames array inside its buildAttributes() method. If it does not, then there is a problem.", DATAFACE_E_ERROR); 00082 } 00083 return @$this->fieldnames[$attname]; 00084 00085 } 00086 00095 function _is($method, $attname){ 00096 return $this->table->$method( 00097 $this->getFieldname($attname) 00098 ); 00099 } 00100 00112 function validate($attname, $value, $allowBlanks=true){ 00113 if ( method_exists($this, 'validate_'.$attname) ){ 00114 $method = 'validate_'.$attname; 00115 return $this->$method($value, $allowBlanks); 00116 } else { 00117 if ( !$allowBlanks and !trim($value) ) return false; 00118 else return true; 00119 } 00120 } 00121 00122 function getType($attname){ return $this->_is('getType', $attname);} 00123 function isDate($attname){ return $this->_is('isDate', $attname);} 00124 function isBlob($attname){ return $this->_is(__FUNCTION__, $attname);} 00125 function isContainer($attname){ return $this->_is(__FUNCTION__, $attname);} 00126 function isPassword($attname){ return $this->_is(__FUNCTION__, $attname);} 00127 function isText($attname){ return $this->_is(__FUNCTION__, $attname);} 00128 function isXML($attname){ return $this->_is(__FUNCTION__, $attname);} 00129 function isChar($attname){ return $this->_is(__FUNCTION__, $attname);} 00130 function isInt($attname){ return $this->_is(__FUNCTION__, $attname);} 00131 function isFloat($attname){ return $this->_is(__FUNCTION__, $attname);} 00132 00133 00134 } 00135 00136 class Dataface_Ontology_individual { 00137 var $record; 00138 var $ontology; 00139 00140 function Dataface_Ontology_individual(&$ontology, &$record){ 00141 $this->record =& $record; 00142 $this->ontology =& $ontology; 00143 } 00144 00145 function _get($method, $attname){ 00146 return $this->record->$method( 00147 $this->ontology->getFieldname($attname) 00148 ); 00149 } 00150 function getValue($attname){return $this->_get('getValue',$attname);} 00151 function val($attname){ return $this->getValue($attname);} 00152 00153 function display($attname){ return $this->_get('display',$attname);} 00154 function q($attname){ return $this->_get('q', $attname);} 00155 function qq($attname){ return $this->_get('qq', $attname);} 00156 function strval($attname){ return $this->_get('strval', $attname);} 00157 function getValueAsString($attname){ return $this->_get('getValueAsString', $attname);} 00158 function htmlValue($attname){ return $this->_get('htmlValue', $attname);} 00159 00160 00161 } 00162 00163 ?>