ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilTaxonomyNode.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
15{
19 protected $db;
20
21 public $type;
22 public $id;
23 public $title;
24
29 public function __construct($a_id = 0)
30 {
31 global $DIC;
32
33 $this->db = $DIC->database();
34 $this->id = $a_id;
35
36 // include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
37 // $this->taxonomy_tree = new ilTaxonomyTree();
38
39 if ($a_id != 0) {
40 $this->read();
41 }
42
43 $this->setType("taxn");
44 }
45
51 public function setTitle($a_title)
52 {
53 $this->title = $a_title;
54 }
55
61 public function getTitle()
62 {
63 return $this->title;
64 }
65
71 public function setType($a_type)
72 {
73 $this->type = $a_type;
74 }
75
81 public function getType()
82 {
83 return $this->type;
84 }
85
91 public function setId($a_id)
92 {
93 $this->id = $a_id;
94 }
95
101 public function getId()
102 {
103 return $this->id;
104 }
105
111 public function setOrderNr($a_val)
112 {
113 $this->order_nr = $a_val;
114 }
115
121 public function getOrderNr()
122 {
123 return $this->order_nr;
124 }
125
131 public function setTaxonomyId($a_val)
132 {
133 $this->taxonomy_id = $a_val;
134 }
135
141 public function getTaxonomyId()
142 {
143 return $this->taxonomy_id;
144 }
145
149 public function read()
150 {
152
153 if (!isset($this->data_record)) {
154 $query = "SELECT * FROM tax_node WHERE obj_id = " .
155 $ilDB->quote($this->id, "integer");
156 $obj_set = $ilDB->query($query);
157 $this->data_record = $ilDB->fetchAssoc($obj_set);
158 }
159 $this->setType($this->data_record["type"]);
160 $this->setTitle($this->data_record["title"]);
161 $this->setOrderNr($this->data_record["order_nr"]);
162 $this->setTaxonomyId($this->data_record["tax_id"]);
163 }
164
168 public function create()
169 {
171
172 if ($this->getTaxonomyId() <= 0) {
173 die("ilTaxonomyNode->create: No taxonomy ID given");
174 }
175
176 // insert object data
177 $id = $ilDB->nextId("tax_node");
178 $query = "INSERT INTO tax_node (obj_id, title, type, create_date, order_nr, tax_id) " .
179 "VALUES (" .
180 $ilDB->quote($id, "integer") . "," .
181 $ilDB->quote($this->getTitle(), "text") . "," .
182 $ilDB->quote($this->getType(), "text") . ", " .
183 $ilDB->now() . ", " .
184 $ilDB->quote((int) $this->getOrderNr(), "integer") . ", " .
185 $ilDB->quote((int) $this->getTaxonomyId(), "integer") .
186 ")";
187 $ilDB->manipulate($query);
188 $this->setId($id);
189 }
190
194 public function update()
195 {
197
198 $query = "UPDATE tax_node SET " .
199 " title = " . $ilDB->quote($this->getTitle(), "text") .
200 " ,order_nr = " . $ilDB->quote((int) $this->getOrderNr(), "integer") .
201 " WHERE obj_id = " . $ilDB->quote($this->getId(), "integer");
202
203 $ilDB->manipulate($query);
204 }
205
209 public function delete()
210 {
212
213 // delete all assignments of the node
214 include_once("./Services/Taxonomy/classes/class.ilTaxNodeAssignment.php");
216
217 $query = "DELETE FROM tax_node WHERE obj_id= " .
218 $ilDB->quote($this->getId(), "integer");
219 $ilDB->manipulate($query);
220 }
221
225 public function copy($a_tax_id = 0)
226 {
227 $taxn = new ilTaxonomyNode();
228 $taxn->setTitle($this->getTitle());
229 $taxn->setType($this->getType());
230 $taxn->setOrderNr($this->getOrderNr());
231 if ($a_tax_id == 0) {
232 $taxn->setTaxonomyId($this->getTaxonomyId());
233 } else {
234 $taxn->setTaxonomyId($a_tax_id);
235 }
236
237 $taxn->create();
238
239 return $taxn;
240 }
241
247 protected static function _lookup($a_obj_id, $a_field)
248 {
249 global $DIC;
250
251 $ilDB = $DIC->database();
252
253 $query = "SELECT $a_field FROM tax_node WHERE obj_id = " .
254 $ilDB->quote($a_obj_id, "integer");
255 $obj_set = $ilDB->query($query);
256 $obj_rec = $ilDB->fetchAssoc($obj_set);
257
258 return $obj_rec[$a_field];
259 }
260
267 public static function _lookupTitle($a_obj_id)
268 {
269 global $DIC;
270
271 $ilDB = $DIC->database();
272
273 return self::_lookup($a_obj_id, "title");
274 }
275
279 public static function putInTree(
280 $a_tax_id,
281 $a_node,
282 $a_parent_id = "",
283 $a_target_node_id = "",
284 $a_order_nr = 0
285 ) {
286 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
287 $tax_tree = new ilTaxonomyTree($a_tax_id);
288
289 // determine parent
290 $parent_id = ($a_parent_id != "")
291 ? $a_parent_id
292 : $tax_tree->readRootId();
293
294 // determine target
295 if ($a_target_node_id != "") {
296 $target = $a_target_node_id;
297 } else {
298 // determine last child that serves as predecessor
299 $childs = $tax_tree->getChilds($parent_id);
300
301 if (count($childs) == 0) {
302 $target = IL_FIRST_NODE;
303 } else {
304 $target = $childs[count($childs) - 1]["obj_id"];
305 }
306 }
307
308 if ($tax_tree->isInTree($parent_id) && !$tax_tree->isInTree($a_node->getId())) {
309 $tax_tree->insertNode($a_node->getId(), $parent_id, $target);
310 }
311 }
312
319 public static function writeOrderNr($a_node_id, $a_order_nr)
320 {
321 global $DIC;
322
323 $ilDB = $DIC->database();
324
325 $ilDB->manipulate(
326 "UPDATE tax_node SET " .
327 " order_nr = " . $ilDB->quote($a_order_nr, "integer") .
328 " WHERE obj_id = " . $ilDB->quote($a_node_id, "integer")
329 );
330 }
331
338 public static function writeTitle($a_node_id, $a_title)
339 {
340 global $DIC;
341
342 $ilDB = $DIC->database();
343
344 $ilDB->manipulate(
345 "UPDATE tax_node SET " .
346 " title = " . $ilDB->quote($a_title, "text") .
347 " WHERE obj_id = " . $ilDB->quote($a_node_id, "integer")
348 );
349 }
350
354 public static function getNextOrderNr($a_tax_id, $a_parent_id)
355 {
356 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
357 $tax_tree = new ilTaxonomyTree($a_tax_id);
358 if ($a_parent_id == 0) {
359 $a_parent_id = $tax_tree->readRootId();
360 }
361 $childs = $tax_tree->getChilds($a_parent_id);
362 $max = 0;
363
364 foreach ($childs as $c) {
365 if ($c["order_nr"] > $max) {
366 $max = $c["order_nr"] + 10;
367 }
368 }
369
370 return $max;
371 }
372
379 public static function fixOrderNumbers($a_tax_id, $a_parent_id)
380 {
381 include_once("./Services/Taxonomy/classes/class.ilTaxonomyTree.php");
382 $tax_tree = new ilTaxonomyTree($a_tax_id);
383 if ($a_parent_id == 0) {
384 $a_parent_id = $tax_tree->readRootId();
385 }
386 $childs = $tax_tree->getChilds($a_parent_id);
387 $childs = ilUtil::sortArray($childs, "order_nr", "asc", true);
388
389 $cnt = 10;
390 foreach ($childs as $c) {
391 self::writeOrderNr($c["child"], $cnt);
392 $cnt += 10;
393 }
394 }
395}
An exception for terminatinating execution or to throw for unit testing.
const IL_FIRST_NODE
Definition: class.ilTree.php:5
static deleteAllAssignmentsOfNode($a_node_id)
Delete assignments of node.
static writeOrderNr($a_node_id, $a_order_nr)
Write order nr.
copy($a_tax_id=0)
Copy taxonomy node.
__construct($a_id=0)
Constructor @access public.
getTaxonomyId()
Get taxonomy id.
setTaxonomyId($a_val)
Set taxonomy id.
static fixOrderNumbers($a_tax_id, $a_parent_id)
Fix order numbers.
setTitle($a_title)
Set title.
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.
static writeTitle($a_node_id, $a_title)
Write title.
setId($a_id)
Set Node ID.
static _lookup($a_obj_id, $a_field)
Lookup.
read()
Read data from database.
setType($a_type)
Set type.
getOrderNr()
Get order nr.
static _lookupTitle($a_obj_id)
Lookup Title.
create()
Create taxonomy node.
setOrderNr($a_val)
Set order nr.
static getNextOrderNr($a_tax_id, $a_parent_id)
Put this node into the taxonomy tree.
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$c
Definition: cli.php:37
global $DIC
Definition: goto.php:24
$query
global $ilDB