Xataface  2.0alpha2
Xataface Application Framework
 All Data Structures Namespaces Files Functions Variables Groups Pages
XMLTool.php
Go to the documentation of this file.
1 <?php
3 
4  var $ns="df";
5  var $perms_ns = "dfp";
6  var $atts_ns = "dfa";
7  var $http_ns = "http";
8 
9  var $required_tables_loaded = array();
10 
16  var $ifModifiedSince=array();
17 
18 
19  public static function &getOutputCache(){
20  static $cache =0;
21  if ( !is_object($cache) ){
22  import('Dataface/OutputCache.php');
23  $cache = new Dataface_OutputCache();
24  }
25  return $cache;
26  }
27 
28 
29  function setIfModifiedSince($type, $name, $timestamp){
30  $this->ifModifiedSince[$type][$name] = $timestamp;
31  }
32 
33  function getIfModifiedSince($type, $name){
34  if ( isset($this->ifModifiedSince[$type][$name]) ) {
35  return $this->ifModifiedSince[$type][$name];
36  } else {
37  return null;
38  }
39  }
40 
41  function isModified($type, $name, $timestamp){
42  $since = $this->getIfModifiedSince($type, $name);
43  if ( $since < $timestamp ){
44  return true;
45  } else {
46  return false;
47  }
48  }
49 
55  function tableIsModified($name){
56  $cache =& $this->getOutputCache();
57  $time = $this->getIfModifiedSince('tables',$name);
58  if ( $time == null ) return true;
59 
60  return $cache->isModified($time, $name);
61  }
62 
63  function valuelistIsModified($table, $name){
64 
65  }
66 
67 
68  function toXML(&$arg){
69  $class = get_class($arg);
70  if ( is_array($arg) ){
71  return toXML_array($arg);
72  }
73  eval('$result = $this->toXML_'.$class.'($arg);');
74  return $result;
75  }
76 
77  function toXML_array($arg){
78  trigger_error("Not implemented yet.", E_USER_ERROR);
79 
80 
81  }
82 
83  function markRequiredTableLoaded($name){
84 
85  $this->required_tables_loaded[$name] = true;
86  }
87 
88  function addRequiredTable($name){
89  if ( !isset($this->required_tables_loaded[$name]) ) $this->required_tables_loaded[$name] = false;
90  }
91 
92  function requiredTablesToXML(){
93  $out = array();
94  foreach ( $this->required_tables_loaded as $tablename=>$loaded){
95  if (!$loaded){
97  $out[] = $this->toXML($t);
98  unset($t);
99  }
100  }
101  return implode("\n", $out);
102  }
103 
105 
106  $ns = $this->ns;
107  $pns = $this->perms_ns;
108  $ans = $this->atts_ns;
109  list($tablename, $tablelabel) = array_map(array(&$this,'xmlentities'), array($table->tablename, $table->getLabel()));
110  $out = array();
111 
112  if ( !$this->tableIsModified($table->tablename) ){
113  $out[] = "<$ns:table id=\"tables.{$tablename}\" name=\"$tablename\" http:status-code=\"304\" http:status-message=\"Not Modified\"></$ns:table>";
114 
115  } else {
116 
117  $out[] = <<<END
118 <$ns:table id="tables.{$tablename}" name="$tablename" label="$tablelabel">
119 END;
120  $perms = $table->getPermissions();
121  $patts = array();
122  foreach ($perms as $pname=>$pval){
123  $pname = str_replace(' ', '_', $pname);
124  $patts[] = $pns.':'.$pname.'="'.$pval.'"';
125  }
126  $patts = implode(' ',$patts);
127  $out[] = "\t<$ns:permissions $patts/>";
128  foreach ( $table->fields() as $field ){
129  $atts = array();
130  foreach ($field as $key=>$val){
131  if ( is_scalar($val) ){
132  $atts[] = $ans.':'.$key.'="'.$this->xmlentities($val).'"';
133  }
134  }
135  $atts = implode(' ', $atts);
136  $out[] = "\t<$ns:field id=\"tables.{$tablename}.fields.{$field['name']}\" $atts>";
137 
138  $fperms = $table->getPermissions(array('field'=>$field['name']));
139  $fpatts = array();
140  foreach ($fperms as $fpkey=>$fpval){
141  $fpkey = str_replace(' ', '_', $fpkey);
142  $fpatts[] = $ans.':'.$fpkey.'="'.$fpval.'"';
143  }
144  $fpatts = implode(' ',$fpatts);
145  $out[] = "\t\t<$ns:permissions $fpatts/>";
146 
147  $widget = $field['widget'];
148  $watts = array();
149  foreach ($widget as $wkey=>$wval){
150 
151  if ( is_scalar($wval) ){
152  $watts[] = $ans.':'.$wkey.'="'.$this->xmlentities($wval).'"';
153  }
154 
155  }
156  $watts = implode(' ',$watts);
157  $out[] = "\t\t<$ns:widget $watts>";
158  if ( isset($widget['atts']) ){
159  $aatts = array();
160  foreach ($widget['atts'] as $akey=>$aval){
161  if (is_scalar($aval) ){
162  $aatts[] = $ans.':'.$akey.'="'.$this->xmlentities($aval).'"';
163  }
164 
165 
166  }
167  $aatts = implode(' ',$aatts);
168  $out[] = "\t\t\t<$ns:html_atts $aatts />";
169  }
170  $out[] = "\t\t</$ns:widget>";
171  $out[] = "\t</$ns:field>";
172 
173  }
174 
175  foreach ($table->relationships() as $relationship){
176  $out[] = $this->toXML_Dataface_Relationship($relationship);
177  }
178  $out[] = "</$ns:table>";
179  }
180  $this->markRequiredTableLoaded($table->tablename);
181  return implode("\n", $out);
182  }
183 
184 
185  function toXML_Dataface_Relationship(&$relationship){
186  $ns = $this->ns;
187  $ans = $this->atts_ns;
188  $out = array();
189  $atts = array();
190  $name = $this->xmlentities($relationship->getName());
191  foreach ($relationship->_schema as $key=>$val){
192  if ( is_scalar($val) ){
193  $atts[] = $ans.':'.$key.'="'.$this->xmlentities($val).'"';
194  }
195  }
196  $sourceTable =& $relationship->getSourceTable();
197  $tablename = $sourceTable->tablename;
198  $atts = implode(' ',$atts);
199  $out[] = "\t<$ns:relationship id=\"tables.{$tablename}.relationships.{$name}\" name=\"$name\" $atts />";
200  return implode("\n", $out);
201  }
202 
203  function toXML_Dataface_Record(&$record){
204 
205  $ns = $this->ns;
206  $ans = $this->atts_ns;
207  $pns = $this->perms_ns;
208 
209  $out = array();
210  $out[] = "\t\t<$ns:record id=\"".$this->xmlentities($record->getId())."\" table=\"".$record->_table->tablename."\">";
211 
212  $perms = $record->getPermissions();
213  $patts = array();
214  foreach ($perms as $pkey=>$pval){
215  $pkey = str_replace(' ', '_', $pkey);
216  $patts[] = "$pns:$pkey=\"".$this->xmlentities($pval)."\"";
217  }
218  $patts = implode(' ', $patts);
219  $out[] = "\t\t\t<$ns:permissions $patts/>";
220 
221  foreach ( $record->_table->fields() as $field){
222  $value = $record->val($field['name']);
223  $dispVal = $record->display($field['name']);
224  $this->addRequiredTable($field['table']);
225  // Make sure that the table definition for this field is loaded.
226  $out[] = "\t\t\t<$ns:record_value field=\"".$this->xmlentities($field['name'])."\">";
227  $perms = $record->getPermissions(array('field'=>$field['name']));
228  $patts = array();
229  foreach ($perms as $pkey=>$pval){
230  $pkey = str_replace(' ', '_', $pkey);
231  $patts[] = "$pns:$pkey=\"".$this->xmlentities($pval)."\"";
232  }
233  $patts = implode(' ', $patts);
234  $out[] = "\t\t\t\t<$ns:permissions $patts/>";
235  if ( $record->_table->isDate($field['name']) ){
236  $value= $dispVal;
237  }
238 
239  if ( @$field['vocabulary'] ) $valuelist =& $record->_table->getValuelist($field['vocabulary']);
240 
241  if ( !is_array($value) ) $value = array($value);
242 
243  foreach ($value as $vkey=>$vval){
244  // $vkey will be the value that is actually stored in the database.
245  // $vval Will be the resulting value after joins and valuelists are factored in.
246  // We output both to save from having to publish the entire valuelist to the client.
247  $vkey=$vval; // Only fields that use vocabularies should have different key than value.
248  if ( isset($valuelist) and isset($valuelist[$vval]) ){
249 
250  $vval = $valuelist[$vval];
251  }
252  $out[] = "\t\t\t\t<$ns:value key=\"".$this->xmlentities($vkey)."\">".$this->xmlentities($vval)."</$ns:value>";
253  }
254  unset($valuelist);
255 
256  $out[] = "\t\t\t\t<$ns:display_value>".$this->xmlentities($dispVal)."</$ns:display_value>";
257  $out[] = "\t\t\t\t<$ns:html_value>".$this->xmlentities( $record->htmlValue($field['name']))."</$ns:html_value>";
258  $out[] = "\t\t\t</$ns:record_value>";
259 
260 
261  }
262 
263  $out[] = "\t\t</$ns:record>";
264  $out = implode("\n", $out);
265  return $out;
266 
267 
268  }
269 
270  function toXML_Dataface_QueryTool(&$tool){
271  $ns = $this->ns;
272  $ans = $this->atts_ns;
273  $out = array();
274  $tablename = $tool->_tablename;
275  $tool->loadSet();
276 
277 
278 
279  $out[] = "<$ns:results source=\"".$this->xmlentities($tablename)."\" start=\"".$this->xmlentities($tool->start())."\" end=\"".$this->xmlentities($tool->end())."\" limit=\"".$this->xmlentities($tool->limit())."\" cursor=\"".$this->xmlentities($tool->cursor())."\" cardinality=\"".$this->xmlentities($tool->cardinality())."\" found=\"".$this->xmlentities($tool->found())."\" >";
281  foreach ($table->fields() as $field){
282  if ( Dataface_PermissionsTool::checkPermission('view', $table, array('field'=>$field['name']))){
284  $out[] = "\t<$ns:column table=\"".$this->xmlentities($tablename)."\">".$this->xmlentities($field['name'])."</$ns:column>";
285  }
286  }
287 
288  $it =& $tool->iterator();
289  while ($it->hasNext()){
290  $nex =& $it->next();
291  $out[] = $this->toXML_Dataface_Record($nex);
292  unset($nex);
293  }
294  $out[] = "</$ns:results>";
295  return implode("\n", $out);
296  }
297 
298  function getInfo(){
299  $ns = $this->ns;
301 
302  $allowed_fields = array('_auth/auth_type'=>true);
303  if ( isset($app->_conf["xml_public_info"]) ){
304  $allowed_fields = array_merge($app->_conf['xml_public_info'], $allowed_fields);
305  }
306  $af = array();
307  foreach ($allowed_fields as $key=>$value){
308  $path = explode('/', $key);
309  if ( count($path) == 1 ){
310  $af[$key] = $value;
311  } else {
312  $af[$path[0]][$path[1]] = $value;
313  }
314 
315  }
316 
317  $out = array();
318  $out[] = "<$ns:config>";
319  foreach ( $app->_conf as $key=>$value){
320  if ( !@$af[$key] ) continue;
321  $out[] = "\t<$ns:param><$ns:key>".$this->xmlentities($key)."</$ns:key>";
322  if ( is_scalar($value) ){
323  $out[] = "\t\t<$ns:value>".$this->xmlentities($value)."</$ns:value>";
324  } else {
325  $out[] = "\t\t<$ns:value>";
326  foreach ($value as $vkey=>$vval){
327  if ( !@$af[$key][$vkey] ) continue;
328  if ( strcasecmp($vkey,'password') === 0 ) continue;
329  $out[] = "\t\t\t<$ns:param><$ns:key>".$this->xmlentities($vkey)."</$ns:key><$ns:value>".$this->xmlentities($vval)."</$ns:value></$ns:param>";
330  }
331  $out[] = "\t\t</$ns:value>";
332 
333  }
334 
335  $out[] = "\t</$ns:param>";
336  }
337  $out[] = "</$ns:config>";
338  return implode("\n", $out);
339  }
340 
341  function header(){
342  $ns = $this->ns;
343  $ans = $this->atts_ns;
344  $pns = $this->perms_ns;
345  $out = array();
347  header('Content-type: text/xml; charset='.$app->_conf['oe']);
348  $out[] = "<?xml version=\"1.0\"?>";
349  $out[] = "<dataface_document xmlns:$ns=\"http://www.weblite.ca/dataface/2007/df\" xmlns:$ans=\"http://www.weblite.ca/dataface/2007/dfatts\" xmlns:$pns=\"http://www.weblite.ca/dataface/2007/dfperms\" xmlns:http=\"http://www.weblite.ca/dataface/2007/http\">";
350  return implode("\n", $out);
351 
352  }
353 
354  function footer(){
355 
356  return $this->requiredTablesToXML()."\n</dataface_document>";
357  }
358 
359  function xmlentities($string) {
360  return str_replace ( array ( '&', '"', "'", '<', '>', '' ), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;', '&apos;' ), $string );
361  }
362 }