ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjMediaPool.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected ?int $default_width = null;
27  protected ?int $default_height = null;
28  protected ilTree $mep_tree;
29  public bool $for_translation = false;
30 
31  public function __construct(
32  int $a_id = 0,
33  bool $a_call_by_reference = true
34  ) {
35  global $DIC;
36 
37  $this->db = $DIC->database();
38  $this->lng = $DIC->language();
39  // this also calls read() method! (if $a_id is set)
40  $this->type = "mep";
41  parent::__construct($a_id, $a_call_by_reference);
42  }
43 
44  public function setDefaultWidth(?int $a_val): void
45  {
46  if ($a_val === 0) {
47  $a_val = null;
48  }
49  $this->default_width = $a_val;
50  }
51 
52  public function getDefaultWidth(): ?int
53  {
54  return $this->default_width;
55  }
56 
57  public function setDefaultHeight(?int $a_val): void
58  {
59  if ($a_val === 0) {
60  $a_val = null;
61  }
62  $this->default_height = $a_val;
63  }
64 
65  public function getDefaultHeight(): ?int
66  {
67  return $this->default_height;
68  }
69 
73  public function setForTranslation(bool $a_val): void
74  {
75  $this->for_translation = $a_val;
76  }
77 
78  public function getForTranslation(): bool
79  {
81  }
82 
83  public function read(): void
84  {
85  $ilDB = $this->db;
86 
87  parent::read();
88 
89  $set = $ilDB->query(
90  "SELECT * FROM mep_data " .
91  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
92  );
93  if ($rec = $ilDB->fetchAssoc($set)) {
94  $this->setDefaultWidth($rec["default_width"]);
95  $this->setDefaultHeight($rec["default_height"]);
96  $this->setForTranslation($rec["for_translation"]);
97  }
98  $this->mep_tree = self::_getPoolTree($this->getId());
99  }
100 
101 
105  public static function _getPoolTree(int $a_obj_id): ilTree
106  {
107  $tree = new ilTree($a_obj_id);
108  $tree->setTreeTablePK("mep_id");
109  $tree->setTableNames("mep_tree", "mep_item");
110 
111  return $tree;
112  }
113 
114  public function getPoolTree(): ilTree
115  {
116  return self::_getPoolTree($this->getId());
117  }
118 
119  public function create(): int
120  {
121  $ilDB = $this->db;
122 
123  $id = parent::create();
124 
125  $ilDB->manipulate("INSERT INTO mep_data " .
126  "(id, default_width, default_height, for_translation) VALUES (" .
127  $ilDB->quote($this->getId(), "integer") . ", " .
128  $ilDB->quote($this->getDefaultWidth(), "integer") . ", " .
129  $ilDB->quote($this->getDefaultHeight(), "integer") . ", " .
130  $ilDB->quote($this->getForTranslation(), "integer") .
131  ")");
132 
133  $this->createMepTree();
134  return $id;
135  }
136 
137  public function createMepTree(): void
138  {
139  // create media pool tree
140  $this->mep_tree = new ilTree($this->getId());
141  $this->mep_tree->setTreeTablePK("mep_id");
142  $this->mep_tree->setTableNames('mep_tree', 'mep_item');
143  $this->mep_tree->addTree($this->getId(), 1);
144  }
145 
146  public function getTree(): ilTree
147  {
148  return $this->mep_tree;
149  }
150 
151  public function update(): bool
152  {
153  $ilDB = $this->db;
154 
155  if (!parent::update()) {
156  return false;
157  }
158 
159  // put here object specific stuff
160  $ilDB->manipulate(
161  "UPDATE mep_data SET " .
162  " default_width = " . $ilDB->quote($this->getDefaultWidth(), "integer") . "," .
163  " default_height = " . $ilDB->quote($this->getDefaultHeight(), "integer") . "," .
164  " for_translation = " . $ilDB->quote($this->getForTranslation(), "integer") . " " .
165  " WHERE id = " . $ilDB->quote($this->getId(), "integer")
166  );
167 
168  return true;
169  }
170 
171 
172  public function delete(): bool
173  {
174  // always call parent delete function first!!
175  if (!parent::delete()) {
176  return false;
177  }
178 
179  // get childs
180  $childs = $this->mep_tree->getSubTree($this->mep_tree->getNodeData($this->mep_tree->readRootId()));
181 
182  // delete tree
183  $this->mep_tree->removeTree($this->mep_tree->getTreeId());
184 
185  // delete childs
186  foreach ($childs as $child) {
187  $fid = ilMediaPoolItem::lookupForeignId($child["obj_id"]);
188  switch ($child["type"]) {
189  case "mob":
190  if (ilObject::_lookupType($fid) === "mob") {
191  $mob = new ilObjMediaObject($fid);
192  $mob->delete();
193  }
194  break;
195  }
196  }
197 
198  return true;
199  }
200 
204  public function getChilds(
205  int $obj_id = 0,
206  string $a_type = ""
207  ): array {
208  $objs = array();
209  $mobs = array();
210  $pgs = array();
211  if ($obj_id === 0) {
212  $obj_id = $this->mep_tree->getRootId();
213  }
214 
215  if ($a_type === "fold" || $a_type === "") {
216  $objs = $this->mep_tree->getChildsByType($obj_id, "fold");
217  }
218  if ($a_type === "mob" || $a_type === "") {
219  $mobs = $this->mep_tree->getChildsByType($obj_id, "mob");
220  }
221  foreach ($mobs as $key => $mob) {
222  $objs[] = $mob;
223  }
224  if ($a_type === "pg" || $a_type === "") {
225  $pgs = $this->mep_tree->getChildsByType($obj_id, "pg");
226  }
227  foreach ($pgs as $key => $pg) {
228  $objs[] = $pg;
229  }
230 
231  return $objs;
232  }
233 
234  public function getChildsExceptFolders(
235  int $obj_id = 0
236  ): array {
237  if ($obj_id === 0) {
238  $obj_id = $this->mep_tree->getRootId();
239  }
240 
241  return $this->mep_tree->getFilteredChilds(array("fold", "dummy"), $obj_id);
242  }
243 
248  public static function getAllMobIds(int $a_id): array
249  {
250  global $DIC;
251 
252  $ilDB = $DIC->database();
253 
254  $query = "SELECT foreign_id as id FROM " .
255  " mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) " .
256  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) " .
257  " WHERE mep_tree.mep_id = " . $ilDB->quote($a_id, "integer") .
258  " AND mep_item.type = " . $ilDB->quote("mob", "text") .
259  " AND object_data.type = " . $ilDB->quote("mob", "text");
260  $set = $ilDB->query($query);
261  $ids = array();
262  while ($rec = $ilDB->fetchAssoc($set)) {
263  $ids[] = (int) $rec["id"];
264  }
265  return $ids;
266  }
267 
271  public function getUsedFormats(): array
272  {
273  $ilDB = $this->db;
274  $lng = $this->lng;
275 
276  $query = "SELECT DISTINCT media_item.format f FROM mep_tree " .
277  " JOIN mep_item ON (mep_item.obj_id = mep_tree.child) " .
278  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) " .
279  " JOIN media_item ON (media_item.mob_id = object_data.obj_id) " .
280  " WHERE mep_tree.mep_id = " . $ilDB->quote($this->getId(), "integer") .
281  " AND object_data.type = " . $ilDB->quote("mob", "text") .
282  " ORDER BY f";
283  $formats = array();
284  $set = $ilDB->query($query);
285  while ($rec = $ilDB->fetchAssoc($set)) {
286  if ($rec["f"] != "") {
287  $formats[$rec["f"]] = $rec["f"];
288  } else {
289  $formats["unknown"] = $lng->txt("mep_unknown");
290  }
291  }
292 
293  return $formats;
294  }
295 
296  public function getParentId(int $obj_id = 0): ?int
297  {
298  if ($obj_id === 0) {
299  return null;
300  }
301  if ($obj_id === $this->mep_tree->getRootId()) {
302  return null;
303  }
304 
305  return (int) $this->mep_tree->getParentId($obj_id);
306  }
307 
311  public function insertInTree(
312  int $a_obj_id,
313  ?int $a_parent = null
314  ): bool {
315  if (!$this->mep_tree->isInTree($a_obj_id)) {
316  $parent = (is_null($a_parent))
317  ? $this->mep_tree->getRootId()
318  : $a_parent;
319  $this->mep_tree->insertNode($a_obj_id, $parent);
320  return true;
321  }
322 
323  return false;
324  }
325 
326 
330  public function deleteChild(int $obj_id): void
331  {
332  $node_data = $this->mep_tree->getNodeData($obj_id);
333  $subtree = $this->mep_tree->getSubTree($node_data);
334 
335  // delete tree
336  if ($this->mep_tree->isInTree($obj_id)) {
337  $this->mep_tree->deleteTree($node_data);
338  }
339 
340  // delete objects
341  foreach ($subtree as $node) {
342  $fid = ilMediaPoolItem::lookupForeignId($node["child"]);
343  if ($node["type"] === "mob" && ilObject::_lookupType($fid) === "mob") {
344  $obj = new ilObjMediaObject($fid);
345  $obj->delete();
346  }
347 
348  if ($node["type"] === "fold" && $fid > 0 && ilObject::_lookupType($fid) === "fold") {
349  $obj = new ilObjFolder($fid, false);
350  $obj->delete();
351  }
352 
353  if ($node["type"] === "pg" && ilPageObject::_exists("mep", $node["child"])) {
354  $pg = new ilMediaPoolPage($node["child"]);
355  $pg->delete();
356  }
357 
358  $item = new ilMediaPoolItem($node["child"]);
359  $item->delete();
360  }
361  }
362 
366  public static function isForeignIdInTree(
367  int $a_pool_id,
368  int $a_foreign_id
369  ): bool {
370  global $DIC;
371 
372  $ilDB = $DIC->database();
373 
374  $set = $ilDB->query(
375  "SELECT * FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE " .
376  " foreign_id = " . $ilDB->quote($a_foreign_id, "integer") .
377  " AND mep_id = " . $ilDB->quote($a_pool_id, "integer")
378  );
379  if ($rec = $ilDB->fetchAssoc($set)) {
380  return true;
381  }
382  return false;
383  }
384 
388  public static function isItemIdInTree(
389  int $a_pool_id,
390  int $a_item_id
391  ): bool {
392  global $DIC;
393 
394  $ilDB = $DIC->database();
395 
396  $set = $ilDB->query("SELECT * FROM mep_tree WHERE child = " .
397  $ilDB->quote($a_item_id, "integer") .
398  " AND mep_id = " . $ilDB->quote($a_pool_id, "integer"));
399  if ($rec = $ilDB->fetchAssoc($set)) {
400  return true;
401  }
402  return false;
403  }
404 
405  public function createFolder(
406  string $a_title,
407  int $a_parent = 0
408  ): ?int {
409  // perform save
410  $mep_item = new ilMediaPoolItem();
411  $mep_item->setTitle($a_title);
412  $mep_item->setType("fold");
413  $mep_item->create();
414  if ($mep_item->getId() > 0) {
415  $tree = $this->getTree();
416  $parent = $a_parent > 0
417  ? $a_parent
418  : $tree->getRootId();
419  $this->insertInTree($mep_item->getId(), $parent);
420  return $mep_item->getId();
421  }
422  return null;
423  }
424 
431  public function cloneObject(int $target_id, int $copy_id = 0, bool $omit_tree = false): ?ilObject
432  {
434  $new_obj = parent::cloneObject($target_id, $copy_id, $omit_tree);
435 
436  $new_obj->setTitle($this->getTitle());
437  $new_obj->setDescription($this->getDescription());
438  $new_obj->setDefaultWidth($this->getDefaultWidth());
439  $new_obj->setDefaultHeight($this->getDefaultHeight());
440  $new_obj->update();
441 
442  // copy content
443  $this->copyTreeContent(
444  $new_obj,
445  $new_obj->getTree()->readRootId(),
446  $this->getTree()->readRootId()
447  );
448 
449  return $new_obj;
450  }
451 
452  public function copyTreeContent(
453  ilObjMediaPool $a_new_obj,
454  int $a_target_parent,
455  int $a_source_parent
456  ): void {
457  // get all childs
458  $nodes = $this->getTree()->getChilds($a_source_parent);
459  foreach ($nodes as $node) {
460  $item = new ilMediaPoolItem();
461  $item->setType($node["type"]);
462  switch ($node["type"]) {
463  case "mob":
464  $mob_id = ilMediaPoolItem::lookupForeignId($node["child"]);
465  $mob = new ilObjMediaObject($mob_id);
466  $new_mob = $mob->duplicate();
467  $item->setForeignId($new_mob->getId());
468  $item->setTitle($new_mob->getTitle());
469  $item->create();
470  break;
471 
472  case "pg":
473  $item->setTitle($node["title"]);
474  $item->create();
475  $page = new ilMediaPoolPage($node["child"]);
476  $new_page = new ilMediaPoolPage();
477  $new_page->setParentId($a_new_obj->getId());
478  $new_page->setId($item->getId());
479  $new_page->create(false);
480 
481  // copy page
482  $page->copy($new_page->getId(), $new_page->getParentType(), $new_page->getParentId(), true);
483  break;
484 
485  case "fold":
486  $item->setTitle($node["title"]);
487  $item->create();
488  break;
489  }
490 
491  // insert item into tree
492  $a_new_obj->insertInTree($item->getId(), $a_target_parent);
493 
494  // handle childs
495  $this->copyTreeContent($a_new_obj, $item->getId(), $node["child"]);
496  }
497  }
498 
502  public function exportXML(string $a_mode = ""): void
503  {
504  if (in_array($a_mode, array("master", "masternomedia"))) {
505  $exp = new ilExport();
506  $conf = $exp->getConfig("Modules/MediaPool");
507  $conf->setMasterLanguageOnly(true, ($a_mode === "master"));
508  $exp->exportObject($this->getType(), $this->getId(), "4.4.0");
509  }
510  }
511 
512  public static function getAdvMDSubItemTitle(int $a_obj_id, string $a_sub_type, int $a_sub_id): string
513  {
515  $snippets = $repo->getItems((int) $a_obj_id);
516  foreach ($snippets as $snippet) {
517  if ((int) $snippet['obj_id'] === (int) $a_sub_id) {
518  return $snippet['title'];
519  }
520  }
521  return '';
522  }
523 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$mobs
Definition: imgupload.php:70
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
__construct(int $a_id=0, bool $a_call_by_reference=true)
static lookupForeignId(int $a_id)
setForTranslation(bool $a_val)
ilTree $tree
setTreeTablePK(string $a_column_name)
set column containing primary key in tree table
$target_id
Definition: goto.php:52
static getAdvMDSubItemTitle(int $a_obj_id, string $a_sub_type, int $a_sub_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insertInTree(int $a_obj_id, ?int $a_parent=null)
Insert into tree.
getChilds(int $obj_id=0, string $a_type="")
global $DIC
Definition: feed.php:28
setTableNames(string $a_table_tree, string $a_table_obj_data, string $a_table_obj_reference="")
set table names The primary key of the table containing your object_data must be &#39;obj_id&#39; You may use...
getChildsExceptFolders(int $obj_id=0)
exportXML(string $a_mode="")
createFolder(string $a_title, int $a_parent=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _exists(string $a_parent_type, int $a_id, string $a_lang="", bool $a_no_cache=false)
Checks whether page exists.
ilLanguage $lng
ilDBInterface $db
static getAllMobIds(int $a_id)
string $key
Consumer key/client ID value.
Definition: System.php:193
static isForeignIdInTree(int $a_pool_id, int $a_foreign_id)
Check whether foreign id is in tree.
$query
getParentId(int $obj_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _getPoolTree(int $a_obj_id)
deleteChild(int $obj_id)
Delete a child of media tree.
__construct(Container $dic, ilPlugin $plugin)
setDefaultWidth(?int $a_val)
static _lookupType(int $id, bool $reference=false)
copyTreeContent(ilObjMediaPool $a_new_obj, int $a_target_parent, int $a_source_parent)
static isItemIdInTree(int $a_pool_id, int $a_item_id)
Check whether a mep item id is in the media pool.
setDefaultHeight(?int $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$formats
Definition: date.php:77