ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjTaxonomy.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once "./Services/Object/classes/class.ilObject2.php";
6 
14 class ilObjTaxonomy extends ilObject2
15 {
16  const SORT_ALPHABETICAL = 0;
17  const SORT_MANUAL = 1;
18  protected $node_mapping = array();
19 
25  function ilObjTaxonomy($a_id = 0)
26  {
27  $this->type = "tax";
28  parent::ilObject($a_id, false);
29  }
30 
37  function initType()
38  {
39  $this->type = "tax";
40  }
41 
47  function setSortingMode($a_val)
48  {
49  $this->sorting_mode = $a_val;
50  }
51 
57  function getSortingMode()
58  {
59  return $this->sorting_mode;
60  }
61 
68  function getTree()
69  {
70  if ($this->getId() > 0)
71  {
72  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
73  $tax_tree = new ilTaxonomyTree($this->getId());
74  return $tax_tree;
75  }
76  return false;
77  }
78 
85  function getNodeMapping()
86  {
87  return $this->node_mapping;
88  }
89 
90 
94  function doCreate()
95  {
96  global $ilDB;
97 
98  // create tax data record
99  $ilDB->manipulate("INSERT INTO tax_data ".
100  "(id, sorting_mode) VALUES (".
101  $ilDB->quote($this->getId(), "integer").",".
102  $ilDB->quote((int) $this->getSortingMode(), "integer").
103  ")");
104 
105  // create the taxonomy tree
106  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
107  $node = new ilTaxonomyNode();
108  $node->setType(""); // empty type
109  $node->setTitle("Root node for taxonomy ".$this->getId());
110  $node->setTaxonomyId($this->getId());
111  $node->create();
112  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
113  $tax_tree = new ilTaxonomyTree($this->getId());
114  $tax_tree->addTree($this->getId(), $node->getId());
115  }
116 
123  function doCloneObject($a_new_obj, $a_target_id, $a_copy_id)
124  {
125  global $log, $lng;
126 
127  $a_new_obj->setTitle($this->getTitle());
128  $a_new_obj->setDescription($this->getDescription());
129  $a_new_obj->setSortingMode($this->getSortingMode());
130 
131  $this->node_mapping = array();
132 
133  $this->cloneNodes($a_new_obj, $a_new_obj->getTree()->readRootId(),
134  $this->getTree()->readRootId());
135  }
136 
143  function cloneNodes($a_new_obj, $a_target_parent, $a_source_parent)
144  {
145  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
146 
147  // get all childs
148  $nodes = $this->getTree()->getChilds($a_source_parent);
149  foreach ($nodes as $node)
150  {
151  switch ($node["type"])
152  {
153  case "taxn":
154  $tax_node = new ilTaxonomyNode($node["child"]);
155  $new_node = $tax_node->copy($a_new_obj->getId());
156  break;
157  }
158 
159  ilTaxonomyNode::putInTree($a_new_obj->getId(),
160  $new_node, $a_target_parent);
161 
162  $this->node_mapping[$node["child"]] = $new_node->getId();
163 
164  // handle childs
165  $this->cloneNodes($a_new_obj, $new_node->getId(), $node["child"]);
166  }
167 
168  }
169 
170 
174  function doDelete()
175  {
176  global $ilDB;
177 
178  // delete usages
180 
181  // get all nodes
182  $tree = $this->getTree();
183  $subtree = $tree->getSubTreeIds($tree->readRootId());
184  $subtree[] = $tree->readRootId();
185 
186  // get root node data (important: must happen before we
187  // delete the nodes
188  $root_node_data = $tree->getNodeData($tree->readRootId());
189 
190  // delete all nodes
191  include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
192  foreach ($subtree as $node_id)
193  {
194  // delete node (this also deletes its assignments)
195  $node = new ilTaxonomyNode($node_id);
196  $node->delete();
197  }
198 
199  // delete the tree
200  $tree->deleteTree($root_node_data);
201 
202  // delete taxonoymy properties record
203  $ilDB->manipulate("DELETE FROM tax_data WHERE ".
204  " id = ".$ilDB->quote($this->getId(), "integer")
205  );
206 
207  }
208 
209 
213  function doRead()
214  {
215  global $ilDB;
216 
217  $set = $ilDB->query("SELECT * FROM tax_data ".
218  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
219  );
220  $rec = $ilDB->fetchAssoc($set);
221  $this->setSortingMode($rec["sorting_mode"]);
222  }
223 
227  function doUpdate()
228  {
229  global $ilDB;
230 
231  $ilDB->manipulate("UPDATE tax_data SET ".
232  " sorting_mode = ".$ilDB->quote((int) $this->getSortingMode(), "integer").
233  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
234  );
235  }
236 
243  static function loadLanguageModule()
244  {
245  global $lng;
246 
247  $lng->loadLanguageModule("tax");
248  }
249 
250 
257  static function saveUsage($a_tax_id, $a_obj_id)
258  {
259  global $ilDB;
260 
261  $ilDB->replace("tax_usage",
262  array("tax_id" => array("integer", $a_tax_id),
263  "obj_id" => array("integer", $a_obj_id)
264  ),
265  array()
266  );
267  }
268 
275  static function getUsageOfObject($a_obj_id)
276  {
277  global $ilDB;
278 
279  $set = $ilDB->query("SELECT tax_id FROM tax_usage ".
280  " WHERE obj_id = ".$ilDB->quote($a_obj_id, "integer")
281  );
282  $tax = array();
283  while ($rec = $ilDB->fetchAssoc($set))
284  {
285  $tax[] = $rec["tax_id"];
286  }
287  return $tax;
288  }
289 
296  static function deleteUsagesOfTaxonomy($a_id)
297  {
298  global $ilDB;
299 
300  $ilDB->manipulate("DELETE FROM tax_usage WHERE ".
301  " tax_id = ".$ilDB->quote($a_id, "integer")
302  );
303 
304  }
305 
306 
313  static function getSubTreeItems($a_tax_id, $a_node)
314  {
315  include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
316  $tree = new ilTaxonomyTree($a_tax_id);
317 
318  $sub_nodes = $tree->getSubTreeIds($a_node);
319  $sub_nodes[] = $a_node;
320  include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
321  $items = ilTaxNodeAssignment::getAssignmentsOfNode($sub_nodes);
322 
323  return $items;
324  }
325 
332  static protected function lookup($a_field, $a_id)
333  {
334  global $ilDB;
335 
336  $set = $ilDB->query("SELECT ".$a_field." FROM tax_data ".
337  " WHERE id = ".$ilDB->quote($a_id, "integer")
338  );
339  $rec = $ilDB->fetchAssoc($set);
340 
341  return $rec[$a_field];
342  }
343 
350  public static function lookupSortingMode($a_id)
351  {
352  return self::lookup("sorting_mode", $a_id);
353  }
354 }
355 ?>