ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
5require_once "./Services/Object/classes/class.ilObject2.php";
6
15{
17 const SORT_MANUAL = 1;
18 protected $node_mapping = array();
19 protected $item_sorting = false;
20
26 public function __construct($a_id = 0)
27 {
28 global $DIC;
29
30 $this->db = $DIC->database();
31 $this->type = "tax";
32 parent::__construct($a_id, false);
33 }
34
41 public function initType()
42 {
43 $this->type = "tax";
44 }
45
51 public function setSortingMode($a_val)
52 {
53 $this->sorting_mode = $a_val;
54 }
55
61 public function getSortingMode()
62 {
63 return $this->sorting_mode;
64 }
65
71 public function setItemSorting($a_val)
72 {
73 $this->item_sorting = $a_val;
74 }
75
81 public function getItemSorting()
82 {
84 }
85
92 public function getTree()
93 {
94 if ($this->getId() > 0) {
95 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
96 $tax_tree = new ilTaxonomyTree($this->getId());
97 return $tax_tree;
98 }
99 return false;
100 }
101
108 public function getNodeMapping()
109 {
110 return $this->node_mapping;
111 }
112
113
117 public function doCreate()
118 {
120
121 // create tax data record
122 $ilDB->manipulate("INSERT INTO tax_data " .
123 "(id, sorting_mode, item_sorting) VALUES (" .
124 $ilDB->quote($this->getId(), "integer") . "," .
125 $ilDB->quote((int) $this->getSortingMode(), "integer") . "," .
126 $ilDB->quote((int) $this->getItemSorting(), "integer") .
127 ")");
128
129 // create the taxonomy tree
130 include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
131 $node = new ilTaxonomyNode();
132 $node->setType(""); // empty type
133 $node->setTitle("Root node for taxonomy " . $this->getId());
134 $node->setTaxonomyId($this->getId());
135 $node->create();
136 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
137 $tax_tree = new ilTaxonomyTree($this->getId());
138 $tax_tree->addTree($this->getId(), $node->getId());
139 }
140
147 public function doCloneObject($a_new_obj, $a_target_id, $a_copy_id = null)
148 {
149 $a_new_obj->setTitle($this->getTitle());
150 $a_new_obj->setDescription($this->getDescription());
151 $a_new_obj->setSortingMode($this->getSortingMode());
152
153 $this->node_mapping = array();
154
155 $this->cloneNodes(
156 $a_new_obj,
157 $a_new_obj->getTree()->readRootId(),
158 $this->getTree()->readRootId()
159 );
160 }
161
168 public function cloneNodes($a_new_obj, $a_target_parent, $a_source_parent)
169 {
170 include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
171
172 // get all childs
173 $nodes = $this->getTree()->getChilds($a_source_parent);
174 foreach ($nodes as $node) {
175 switch ($node["type"]) {
176 case "taxn":
177 $tax_node = new ilTaxonomyNode($node["child"]);
178 $new_node = $tax_node->copy($a_new_obj->getId());
179 break;
180 }
181
183 $a_new_obj->getId(),
184 $new_node,
185 $a_target_parent
186 );
187
188 $this->node_mapping[$node["child"]] = $new_node->getId();
189
190 // handle childs
191 $this->cloneNodes($a_new_obj, $new_node->getId(), $node["child"]);
192 }
193 }
194
195
199 public function doDelete()
200 {
202
203 // delete usages
205
206 // get all nodes
207 $tree = $this->getTree();
208 $subtree = $tree->getSubTreeIds($tree->readRootId());
209 $subtree[] = $tree->readRootId();
210
211 // get root node data (important: must happen before we
212 // delete the nodes
213 $root_node_data = $tree->getNodeData($tree->readRootId());
214
215 // delete all nodes
216 include_once("./Services/Taxonomy/classes/class.ilTaxonomyNode.php");
217 foreach ($subtree as $node_id) {
218 // delete node (this also deletes its assignments)
219 $node = new ilTaxonomyNode($node_id);
220 $node->delete();
221 }
222
223 // delete the tree
224 $tree->deleteTree($root_node_data);
225
226 // delete taxonoymy properties record
227 $ilDB->manipulate(
228 "DELETE FROM tax_data WHERE " .
229 " id = " . $ilDB->quote($this->getId(), "integer")
230 );
231 }
232
233
237 public function doRead()
238 {
240
241 $set = $ilDB->query(
242 "SELECT * FROM tax_data " .
243 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
244 );
245 $rec = $ilDB->fetchAssoc($set);
246 $this->setSortingMode($rec["sorting_mode"]);
247 $this->setItemSorting($rec["item_sorting"]);
248 }
249
253 public function doUpdate()
254 {
256
257 $ilDB->manipulate(
258 $t = "UPDATE tax_data SET " .
259 " sorting_mode = " . $ilDB->quote((int) $this->getSortingMode(), "integer") . ", " .
260 " item_sorting = " . $ilDB->quote((int) $this->getItemSorting(), "integer") .
261 " WHERE id = " . $ilDB->quote($this->getId(), "integer")
262 );
263 }
264
271 public static function loadLanguageModule()
272 {
273 global $DIC;
274
275 $lng = $DIC->language();
276
277 $lng->loadLanguageModule("tax");
278 }
279
280
287 public static function saveUsage($a_tax_id, $a_obj_id)
288 {
289 global $DIC;
290
291 $ilDB = $DIC->database();
292
293 if ($a_tax_id > 0 && $a_obj_id > 0) {
294 $ilDB->replace(
295 "tax_usage",
296 array("tax_id" => array("integer", $a_tax_id),
297 "obj_id" => array("integer", $a_obj_id)
298 ),
299 array()
300 );
301 }
302 }
303
310 public static function getUsageOfObject($a_obj_id, $a_include_titles = false)
311 {
312 global $DIC;
313
314 $ilDB = $DIC->database();
315
316 $set = $ilDB->query(
317 "SELECT tax_id FROM tax_usage " .
318 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer")
319 );
320 $tax = array();
321 while ($rec = $ilDB->fetchAssoc($set)) {
322 if (!$a_include_titles) {
323 $tax[] = $rec["tax_id"];
324 } else {
325 $tax[] = array("tax_id" => $rec["tax_id"],
326 "title" => ilObject::_lookupTitle($rec["tax_id"])
327 );
328 }
329 }
330 return $tax;
331 }
332
339 public static function deleteUsagesOfTaxonomy($a_id)
340 {
341 global $DIC;
342
343 $ilDB = $DIC->database();
344
345 $ilDB->manipulate(
346 "DELETE FROM tax_usage WHERE " .
347 " tax_id = " . $ilDB->quote($a_id, "integer")
348 );
349 }
350
351
358 public static function getSubTreeItems($a_comp, $a_obj_id, $a_item_type, $a_tax_id, $a_node)
359 {
360 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
361 $tree = new ilTaxonomyTree($a_tax_id);
362
363 $sub_nodes = $tree->getSubTreeIds($a_node);
364 $sub_nodes[] = $a_node;
365 include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
366
367 $tn_ass = new ilTaxNodeAssignment($a_comp, $a_obj_id, $a_item_type, $a_tax_id);
368 $items = $tn_ass->getAssignmentsOfNode($sub_nodes);
369
370 return $items;
371 }
372
379 protected static function lookup($a_field, $a_id)
380 {
381 global $DIC;
382
383 $ilDB = $DIC->database();
384
385 $set = $ilDB->query(
386 "SELECT " . $a_field . " FROM tax_data " .
387 " WHERE id = " . $ilDB->quote($a_id, "integer")
388 );
389 $rec = $ilDB->fetchAssoc($set);
390
391 return $rec[$a_field];
392 }
393
400 public static function lookupSortingMode($a_id)
401 {
402 return self::lookup("sorting_mode", $a_id);
403 }
404}
An exception for terminatinating execution or to throw for unit testing.
static getUsageOfObject($a_obj_id, $a_include_titles=false)
Get usage of object.
static deleteUsagesOfTaxonomy($a_id)
Delete all usages of a taxonomy.
initType()
Init type.
cloneNodes($a_new_obj, $a_target_parent, $a_source_parent)
Clone nodes.
static lookupSortingMode($a_id)
Lookup sorting mode.
doCreate()
Create a new taxonomy.
getSortingMode()
Get sorting mode.
getNodeMapping()
Get node mapping (used after cloning)
setItemSorting($a_val)
Set item sorting.
getItemSorting()
Get item sorting.
static lookup($a_field, $a_id)
Lookup.
doCloneObject($a_new_obj, $a_target_id, $a_copy_id=null)
clone taxonomy sheet (note: taxonomies have no ref ids and return an object id)
doUpdate()
Upate taxonomy properties.
static getSubTreeItems($a_comp, $a_obj_id, $a_item_type, $a_tax_id, $a_node)
Get all assigned items under a node.
doDelete()
Delete taxonomy object.
doRead()
Read taxonomy properties.
static saveUsage($a_tax_id, $a_obj_id)
Save Usage.
static loadLanguageModule()
Load language module.
__construct($a_id=0)
Constructor.
setSortingMode($a_val)
Set sorting mode.
Class ilObject2 This is an intermediate progress of ilObject class.
getDescription()
get object description
getId()
get object id @access public
getTitle()
get object title @access public
static _lookupTitle($a_id)
lookup object title
Taxonomy node <-> item assignment.
static putInTree( $a_tax_id, $a_node, $a_parent_id="", $a_target_node_id="", $a_order_nr=0)
Put this node into the taxonomy tree.
global $DIC
Definition: saml.php:7
global $ilDB