Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
Ontology.php
Go to the documentation of this file.
1 <?php
13 
14  var $table;
18 
20  $this->table =& Dataface_Table::loadTable($tablename);
21  }
22 
23  function &getAttributes(){
24  if ( !isset($this->attributes) ){
25  $this->buildAttributes();
26  }
27  return $this->attributes;
28  }
29 
30  function &getAttribute($name){
31  $atts =& $this->getAttributes();
32  if ( !isset($atts[$name]) ){
33  return PEAR::raiseError("No attribute '$name' exists in this Ontology.", DATAFACE_E_ERROR);
34  }
35  return $atts[$name];
36  }
37 
38  function &newIndividual(&$record){
39  $ind = new Dataface_Ontology_individual($this, $record);
40  return $ind;
41  }
42 
43  static function &newOntology($type, $tablename){
44  $ontologies =& self::getRegisteredOntologies();
45  if ( !isset($ontologies[$type]) ){
46  return PEAR::raiseError("No ontology of type '$type' has been registered.", DATAFACE_E_ERROR);
47  }
48  import($ontologies[$type]['path']);
49  $class = $ontologies[$type]['class'];
50  $ont = new $class($tablename);
51  return $ont;
52  }
53 
54  public static function registerType($type, $path, $class){
55  $ontologies =& self::getRegisteredOntologies();
56  $ontologies[$type] = array('type'=>$type, 'path'=>$path, 'class'=>$class);
57  return true;
58  }
59 
60  public static function &getRegisteredOntologies(){
61  static $ontologies = 0;
62  if ( $ontologies === 0 ) $ontologies = array();
63  return $ontologies;
64  }
65 
66  function buildAttributes(){
67  trigger_error("Please implement the ".__FUNCTION__." method", E_USER_ERROR);
68  }
69 
70  function getFieldname($attname){
71  if ( !isset($this->fieldnames) ){
72  // If the fieldNames map hasn't been created yet, we need to
73  // tell the subclass to create it. We can do this by calling
74  // getAttributes, which, in turn, calls buildAttributes()
75  // which should build both the attributes array and the
76  // fieldNames array
77  $this->getAttributes();
78  }
79 
80  if ( !isset($this->fieldnames) ){
81  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);
82  }
83  return @$this->fieldnames[$attname];
84 
85  }
86 
95  function _is($method, $attname){
96  return $this->table->$method(
97  $this->getFieldname($attname)
98  );
99  }
100 
112  function validate($attname, $value, $allowBlanks=true){
113  if ( method_exists($this, 'validate_'.$attname) ){
114  $method = 'validate_'.$attname;
115  return $this->$method($value, $allowBlanks);
116  } else {
117  if ( !$allowBlanks and !trim($value) ) return false;
118  else return true;
119  }
120  }
121 
122  function getType($attname){ return $this->_is('getType', $attname);}
123  function isDate($attname){ return $this->_is('isDate', $attname);}
124  function isBlob($attname){ return $this->_is(__FUNCTION__, $attname);}
125  function isContainer($attname){ return $this->_is(__FUNCTION__, $attname);}
126  function isPassword($attname){ return $this->_is(__FUNCTION__, $attname);}
127  function isText($attname){ return $this->_is(__FUNCTION__, $attname);}
128  function isXML($attname){ return $this->_is(__FUNCTION__, $attname);}
129  function isChar($attname){ return $this->_is(__FUNCTION__, $attname);}
130  function isInt($attname){ return $this->_is(__FUNCTION__, $attname);}
131  function isFloat($attname){ return $this->_is(__FUNCTION__, $attname);}
132 
133 
134 }
135 
137  var $record;
139 
141  $this->record =& $record;
142  $this->ontology =& $ontology;
143  }
144 
145  function _get($method, $attname){
146  return $this->record->$method(
147  $this->ontology->getFieldname($attname)
148  );
149  }
150  function getValue($attname){return $this->_get('getValue',$attname);}
151  function val($attname){ return $this->getValue($attname);}
152 
153  function display($attname){ return $this->_get('display',$attname);}
154  function q($attname){ return $this->_get('q', $attname);}
155  function qq($attname){ return $this->_get('qq', $attname);}
156  function strval($attname){ return $this->_get('strval', $attname);}
157  function getValueAsString($attname){ return $this->_get('getValueAsString', $attname);}
158  function htmlValue($attname){ return $this->_get('htmlValue', $attname);}
159 
160 
161 }
162 
163 ?>