ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataSet.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
24 abstract class ilDataSet
25 {
26  var $dircnt;
27 
31  function __construct()
32  {
33  }
34 
47  final public function init($a_entity, $a_target_release)
48  {
49  $this->entity = $a_entity;
50  $this->target_release = $a_target_release;
51  $this->data = array();
52  }
53 
59  abstract public function getSupportedVersions($a_entity);
60 
69  abstract protected function getTypes($a_entity, $a_version);
70 
75  abstract protected function getXmlNamespace($a_entity, $a_target_release);
76 
83  abstract function readData($a_entity, $a_version, $a_ids);
84 
91  function setExportDirectories($a_relative, $a_absolute)
92  {
93  $this->relative_export_dir = $a_relative;
94  $this->absolute_export_dir = $a_absolute;
95  }
96 
102  function setImportDirectory($a_val)
103  {
104  $this->import_directory = $a_val;
105  }
106 
113  {
114  return $this->import_directory;
115  }
116 
122  function setDSPrefix($a_val)
123  {
124  $this->var = $a_val;
125  }
126 
132  function getDSPrefix()
133  {
134  return $this->var;
135  }
136 
137  function getDSPrefixString()
138  {
139  if ($this->getDSPrefix() != "")
140  {
141  return $this->getDSPrefix().":";
142  }
143  }
144 
152  function getDirectDataFromQuery($a_query, $a_convert_to_leading_upper = true)
153  {
154  global $ilDB;
155 
156  $set = $ilDB->query($a_query);
157  $this->data = array();
158  while ($rec = $ilDB->fetchAssoc($set))
159  {
160  if ($a_convert_to_leading_upper)
161  {
162  $tmp = array();
163  foreach ($rec as $k => $v)
164  {
165  $tmp[$this->convertToLeadingUpper($k)]
166  = $v;
167  }
168  $rec = $tmp;
169  }
170 
171  $this->data[] = $rec;
172  }
173  }
174 
181  function convertToLeadingUpper($a_str)
182  {
183  $a_str = strtoupper(substr($a_str, 0, 1)).substr($a_str, 1);
184  while (is_int($pos = strpos($a_str, "_")))
185  {
186  $a_str = substr($a_str, 0, $pos).
187  strtoupper(substr($a_str, $pos+1, 1)).
188  substr($a_str, $pos+2);
189  }
190  return $a_str;
191  }
192 
193 
197  final function getJsonRepresentation()
198  {
199  if ($this->version === false)
200  {
201  return false;
202  }
203 
204  $arr["entity"] = $this->getJsonEntityName();
205  $arr["version"] = $this->version;
206  $arr["install_id"] = IL_INST_ID;
207  $arr["install_url"] = ILIAS_HTTP_PATH;
208  $arr["types"] = $this->getJsonTypes();
209  $arr["set"] = array();
210  foreach ($this->data as $d)
211  {
212  $arr["set"][] = $this->getJsonRecord($d);
213  }
214 
215  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
216 
217  return ilJsonUtil::encode($arr);
218  }
219 
241  final function getXmlRepresentation($a_entity, $a_target_release,
242  $a_ids, $a_field = "", $a_omit_header = false, $a_omit_types = false)
243  {
244  $this->dircnt = 1;
245 
246  // step 1: check target release and supported versions
247 
248 
249 
250  // step 2: init writer
251  include_once "./Services/Xml/classes/class.ilXmlWriter.php";
252  $writer = new ilXmlWriter();
253  if (!$a_omit_header)
254  {
255  $writer->xmlHeader();
256  }
257 
258  // collect namespaces
259  $namespaces = $prefixes = array();
260  $this->getNamespaces($namespaces, $a_entity, $a_target_release);
261  $atts = array("InstallationId" => IL_INST_ID,
262  "InstallationUrl" => ILIAS_HTTP_PATH, "TopEntity" => $a_entity);
263  $cnt = 1;
264  foreach ($namespaces as $entity => $ns)
265  {
266  $prefix = "ns".$cnt;
267  $prefixes[$entity] = $prefix;
268 // $atts["xmlns:".$prefix] = $ns;
269  $cnt++;
270  }
271 
272  $writer->xmlStartTag($this->getDSPrefixString().'DataSet', $atts);
273 
274  // add types
275  if (!$a_omit_types)
276  {
277  $this->addTypesXml($writer, $a_entity, $a_target_release);
278  }
279 
280  // add records
281  $this->addRecordsXml($writer, $prefixes, $a_entity, $a_target_release, $a_ids, $a_field = "");
282 
283 
284  $writer->xmlEndTag($this->getDSPrefixString()."DataSet");
285 //if ($a_entity == "mep")
286 //{
287 // echo "<pre>".htmlentities($writer->xmlDumpMem(true))."</pre>"; exit;
288 //}
289  return $writer->xmlDumpMem(false);
290  }
291 
292 
299  function addRecordsXml($a_writer, $a_prefixes, $a_entity, $a_target_release, $a_ids, $a_field = "")
300  {
301  $types = $this->getXmlTypes($a_entity, $a_target_release);
302 
303  $this->readData($a_entity, $a_target_release, $a_ids, $a_field);
304  if (is_array($this->data))
305  {
306  foreach ($this->data as $d)
307  {
308  $a_writer->xmlStartTag($this->getDSPrefixString()."Rec",
309  array("Entity" => $this->getXmlEntityName($a_entity, $a_target_release)));
310 
311  // entity tag
312  $a_writer->xmlStartTag($this->getXmlEntityTag($a_entity, $a_target_release));
313 
314  $rec = $this->getXmlRecord($a_entity, $a_target_release, $d);
315  foreach ($rec as $f => $c)
316  {
317  switch ($types[$f])
318  {
319  case "directory":
320  if ($this->absolute_export_dir != "" && $this->relative_export_dir != "")
321  {
322  ilUtil::makeDirParents($this->absolute_export_dir."/dsDir_".$this->dircnt);
323  ilUtil::rCopy($c, $this->absolute_export_dir."/dsDir_".$this->dircnt);
324 //echo "<br>copy-".$c."-".$this->absolute_export_dir."/dsDir_".$this->dircnt."-";
325  $c = $this->relative_export_dir."/dsDir_".$this->dircnt;
326  $this->dircnt++;
327  }
328  break;
329  }
330  // this changes schema/dtd
331  //$a_writer->xmlElement($a_prefixes[$a_entity].":".$f,
332  // array(), $c);
333  $a_writer->xmlElement($f, array(), $c);
334  }
335 
336  $a_writer->xmlEndTag($this->getXmlEntityTag($a_entity, $a_target_release));
337 
338  $a_writer->xmlEndTag($this->getDSPrefixString()."Rec");
339 
340  // foreach record records of dependent entities (no record)
341  $deps = $this->getDependencies($a_entity, $a_target_release, $rec, $a_ids);
342  if (is_array($deps))
343  {
344  foreach ($deps as $dp => $par)
345  {
346  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_target_release, $par["ids"], $par["field"]);
347  }
348  }
349  }
350  }
351  else if ($this->data === false)
352  {
353  // false -> add records of dependent entities (no record)
354  $deps = $this->getDependencies($a_entity, $a_target_release, null, $a_ids);
355  if (is_array($deps))
356  {
357  foreach ($deps as $dp => $par)
358  {
359  $this->addRecordsXml($a_writer, $a_prefixes, $dp, $a_target_release, $par["ids"], $par["field"]);
360  }
361  }
362  }
363  }
364 
370  private function addTypesXml($a_writer, $a_entity, $a_target_release)
371  {
372  $types = $this->getXmlTypes($a_entity, $a_target_release);
373 
374  // add types of current entity
375  if (is_array($types))
376  {
377  $a_writer->xmlStartTag($this->getDSPrefixString()."Types",
378  array("Entity" => $this->getXmlEntityName($a_entity, $a_target_release),
379  "TargetRelease" => $a_target_release));
380  foreach ($this->getXmlTypes($a_entity, $a_target_release) as $f => $t)
381  {
382  $a_writer->xmlElement($this->getDSPrefixString().'FieldType',
383  array("Name" => $f, "Type" => $t));
384  }
385  $a_writer->xmlEndTag($this->getDSPrefixString()."Types");
386  }
387 
388  // add types of dependent entities
389  $deps = $this->getDependencies($a_entity, $a_target_release, null, null);
390  if (is_array($deps))
391  {
392  foreach ($deps as $dp => $w)
393  {
394  $this->addTypesXml($a_writer, $dp, $a_target_release);
395  }
396  }
397 
398  }
399 
407  function getNamespaces(&$namespaces, $a_entity, $a_target_release)
408  {
409  $ns = $this->getXmlNamespace($a_entity, $a_target_release);
410  if ($ns != "")
411  {
412  $namespaces[$a_entity] = $ns;
413  }
414  // add types of dependent entities
415  $deps = $this->getDependencies($a_entity, $a_target_release, null, null);
416  if (is_array($deps))
417  {
418  foreach ($deps as $dp => $w)
419  {
420  $this->getNamespaces($namespaces, $dp, $a_target_release);
421  }
422  }
423  }
424 
431  function getXmlRecord($a_entity, $a_version, $a_set)
432  {
433  return $a_set;
434  }
435 
442  function getJsonRecord($a_set)
443  {
444  return $a_set;
445  }
446 
452  function getXmlTypes($a_entity, $a_version)
453  {
454  return $this->getTypes($a_entity, $a_version);
455  }
456 
462  function getJsonTypes($a_entity, $a_version)
463  {
464  return $this->getTypes($a_entity, $a_version);
465  }
466 
473  function getXMLEntityName($a_entity, $a_version)
474  {
475  return $a_entity;
476  }
477 
484  function getXMLEntityTag($a_entity, $a_target_release)
485  {
486  return $this->convertToLeadingUpper($a_entity);
487  }
488 
493  function getJsonEntityName($a_entity, $a_version)
494  {
495  return $a_entity;
496  }
497 
503  function setImport($a_val)
504  {
505  $this->import = $a_val;
506  }
507 
513  function getImport()
514  {
515  return $this->import;
516  }
517 }
518 
519 ?>