ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjMediaPool.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "./Services/Object/classes/class.ilObject.php";
5 require_once "./Services/MetaData/classes/class.ilMDLanguageItem.php";
6 require_once("./Modules/Folder/classes/class.ilObjFolder.php");
7 require_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
8 
21 class ilObjMediaPool extends ilObject
22 {
23  var $tree;
24 
31  function ilObjMediaPool($a_id = 0,$a_call_by_reference = true)
32  {
33  // this also calls read() method! (if $a_id is set)
34  $this->type = "mep";
35  $this->ilObject($a_id,$a_call_by_reference);
36  }
37 
43  function setDefaultWidth($a_val)
44  {
45  $this->default_width = $a_val;
46  }
47 
53  function getDefaultWidth()
54  {
55  return $this->default_width;
56  }
57 
63  function setDefaultHeight($a_val)
64  {
65  $this->default_height = $a_val;
66  }
67 
73  function getDefaultHeight()
74  {
75  return $this->default_height;
76  }
77 
81  function read()
82  {
83  global $ilDB;
84 
85  parent::read();
86 
87  $set = $ilDB->query("SELECT * FROM mep_data ".
88  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
89  );
90  if ($rec = $ilDB->fetchAssoc($set))
91  {
92  $this->setDefaultWidth($rec["default_width"]);
93  $this->setDefaultHeight($rec["default_height"]);
94  }
95  $this->tree = ilObjMediaPool::getPoolTree($this->getId());
96  }
97 
98 
106  static function getPoolTree($a_obj_id)
107  {
108  $tree = new ilTree($a_obj_id);
109  $tree->setTreeTablePK("mep_id");
110  $tree->setTableNames("mep_tree", "mep_item");
111 
112  return $tree;
113  }
114 
118  function create()
119  {
120  global $ilDB;
121 
122  parent::create();
123 
124  $ilDB->manipulate("INSERT INTO mep_data ".
125  "(id, default_width, default_height) VALUES (".
126  $ilDB->quote($this->getId(), "integer").", ".
127  $ilDB->quote($this->getDefaultWidth(), "integer").", ".
128  $ilDB->quote($this->getDefaultHeight(), "integer").
129  ")");
130 
131  $this->createMepTree();
132  }
133 
140  function createMepTree()
141  {
142  // create media pool tree
143  $this->tree = new ilTree($this->getId());
144  $this->tree->setTreeTablePK("mep_id");
145  $this->tree->setTableNames('mep_tree','mep_item');
146  $this->tree->addTree($this->getId(), 1);
147  }
148 
149 
153  function &getTree()
154  {
155  return $this->tree;
156  }
157 
164  function update()
165  {
166  global $ilDB;
167 
168  if (!parent::update())
169  {
170  return false;
171  }
172 
173  // put here object specific stuff
174  $ilDB->manipulate("UPDATE mep_data SET ".
175  " default_width = ".$ilDB->quote($this->getDefaultWidth(), "integer").",".
176  " default_height = ".$ilDB->quote($this->getDefaultHeight(), "integer").
177  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
178  );
179 
180  return true;
181  }
182 
183 
194  function delete()
195  {
196  // always call parent delete function first!!
197  if (!parent::delete())
198  {
199  return false;
200  }
201 
202  // get childs
203  $childs = $this->tree->getSubTree($this->tree->getNodeData($this->tree->readRootId()));
204 
205  // delete tree
206  $this->tree->removeTree($this->tree->getTreeId());
207 
208  // delete childs
209  foreach ($childs as $child)
210  {
211  $fid = ilMediaPoolItem::lookupForeignId($child["obj_id"]);
212  switch ($child["type"])
213  {
214  case "mob":
215  if (ilObject::_lookupType($fid) == "mob")
216  {
217  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
218  $mob = new ilObjMediaObject($fid);
219  $mob->delete();
220  }
221  break;
222 
223 /* case "fold":
224  if (ilObject::_lookupType($fid) == "fold")
225  {
226  include_once("./Modules/Folder/classes/class.ilObjFolder.php");
227  $fold = new ilObjFolder($fid, false);
228  $fold->delete();
229  }
230  break;*/
231  }
232  }
233 
234  return true;
235  }
236 
237 
251  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
252  {
253  global $tree;
254 
255  switch ($a_event)
256  {
257  case "link":
258 
259  //var_dump("<pre>",$a_params,"</pre>");
260  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
261  //exit;
262  break;
263 
264  case "cut":
265 
266  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
267  //exit;
268  break;
269 
270  case "copy":
271 
272  //var_dump("<pre>",$a_params,"</pre>");
273  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
274  //exit;
275  break;
276 
277  case "paste":
278 
279  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
280  //exit;
281  break;
282 
283  case "new":
284 
285  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
286  //exit;
287  break;
288  }
289 
290  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
291  if ($a_node_id==$_GET["ref_id"])
292  {
293  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
294  $parent_type = $parent_obj->getType();
295  if($parent_type == $this->getType())
296  {
297  $a_node_id = (int) $tree->getParentId($a_node_id);
298  }
299  }
300 
301  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
302  }
303 
304 
308  function getChilds($obj_id = "", $a_type = "")
309  {
310  $objs = array();
311  $mobs = array();
312  $pgs = array();
313  if ($obj_id == "")
314  {
315  $obj_id = $this->tree->getRootId();
316  }
317 
318  if ($a_type == "fold" || $a_type == "")
319  {
320  $objs = $this->tree->getChildsByType($obj_id, "fold");
321  }
322  if ($a_type == "mob" || $a_type == "")
323  {
324  $mobs = $this->tree->getChildsByType($obj_id, "mob");
325  }
326  foreach($mobs as $key => $mob)
327  {
328  $objs[] = $mob;
329  }
330  if ($a_type == "pg" || $a_type == "")
331  {
332  $pgs = $this->tree->getChildsByType($obj_id, "pg");
333  }
334  foreach($pgs as $key => $pg)
335  {
336  $objs[] = $pg;
337  }
338 
339  return $objs;
340  }
341 
345  function getChildsExceptFolders($obj_id = "")
346  {
347  $objs = array();
348  $mobs = array();
349  if ($obj_id == "")
350  {
351  $obj_id = $this->tree->getRootId();
352  }
353 
354  $objs = $this->tree->getFilteredChilds(array("fold", "dummy"), $obj_id);
355  return $objs;
356  }
357 
361  function getMediaObjects($a_title_filter = "", $a_format_filter = "", $a_keyword_filter = '', $a_caption_filter)
362  {
363  global $ilDB;
364 
365  $query = "SELECT DISTINCT mep_tree.*, object_data.* ".
366  "FROM mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
367  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ";
368 
369  if ($a_format_filter != "" or $a_caption_filter != '')
370  {
371  $query.= " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ";
372  }
373 
374  $query .=
375  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
376  " AND object_data.type = ".$ilDB->quote("mob", "text");
377 
378  // filter
379  if (trim($a_title_filter) != "") // title
380  {
381  $query.= " AND ".$ilDB->like("object_data.title", "text", "%".trim($a_title_filter)."%");
382  }
383  if ($a_format_filter != "") // format
384  {
385  $filter = ($a_format_filter == "unknown")
386  ? ""
387  : $a_format_filter;
388  $query.= " AND ".$ilDB->equals("media_item.format", $filter, "text", true);
389  }
390  if(trim($a_caption_filter))
391  {
392  $query .= 'AND '.$ilDB->like('media_item.caption', 'text', '%'.trim($a_caption_filter).'%');
393  }
394 
395  $query.=
396  " ORDER BY object_data.title";
397 
398  $objs = array();
399  $set = $ilDB->query($query);
400  while ($rec = $ilDB->fetchAssoc($set))
401  {
402  $rec["foreign_id"] = $rec["obj_id"];
403  $rec["obj_id"] = "";
404  $objs[] = $rec;
405  }
406 
407  // Keyword filter
408  if($a_keyword_filter)
409  {
410  include_once './Services/MetaData/classes/class.ilMDKeyword.php';
411  $res = ilMDKeyword::_searchKeywords($a_keyword_filter,'mob',0);
412 
413  foreach($objs as $obj)
414  {
415  if(in_array($obj['foreign_id'],$res))
416  {
417  $filtered[] = $obj;
418  }
419  }
420  return (array) $filtered;
421  }
422  return $objs;
423  }
424 
428  function getAllMobIds($a_id)
429  {
430  global $ilDB;
431 
432  $query = "SELECT foreign_id as id FROM ".
433  " mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
434  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
435  " WHERE mep_tree.mep_id = ".$ilDB->quote($a_id, "integer").
436  " AND mep_item.type = ".$ilDB->quote("mob", "text").
437  " AND object_data.type = ".$ilDB->quote("mob", "text");
438  $set = $ilDB->query($query);
439  $ids = array();
440  while ($rec = $ilDB->fetchAssoc($set))
441  {
442  $ids[] = $rec["id"];
443  }
444  return $ids;
445  }
446 
450  function getUsedFormats()
451  {
452  global $ilDB, $lng;
453 
454  $query = "SELECT DISTINCT media_item.format f FROM mep_tree ".
455  " JOIN mep_item ON (mep_item.obj_id = mep_tree.child) ".
456  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
457  " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ".
458  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
459  " AND object_data.type = ".$ilDB->quote("mob", "text").
460  " ORDER BY f";
461  $formats = array();
462  $set = $ilDB->query($query);
463  while ($rec = $ilDB->fetchAssoc($set))
464  {
465  if ($rec["f"] != "")
466  {
467  $formats[$rec["f"]] = $rec["f"];
468  }
469  else
470  {
471  $formats["unknown"] = $lng->txt("mep_unknown");
472  }
473  }
474 
475  return $formats;
476  }
477 
478  function getParentId($obj_id = "")
479  {
480  if ($obj_id == "")
481  {
482  return false;
483  }
484  if ($obj_id == $this->tree->getRootId())
485  {
486  return false;
487  }
488 
489  return $this->tree->getParentId($obj_id);
490  }
491 
497  function insertInTree($a_obj_id, $a_parent = "")
498  {
499  if (!$this->tree->isInTree($a_obj_id))
500  {
501  $parent = ($a_parent == "")
502  ? $this->tree->getRootId()
503  : $a_parent;
504  $this->tree->insertNode($a_obj_id, $parent);
505  return true;
506  }
507  else
508  {
509  return false;
510  }
511  }
512 
513 
518  function deleteChild($obj_id)
519  {
520  $fid = ilMediaPoolItem::lookupForeignId($obj_id);
523 
524  $node_data = $this->tree->getNodeData($obj_id);
525  $subtree = $this->tree->getSubtree($node_data);
526 
527  // delete tree
528  if($this->tree->isInTree($obj_id))
529  {
530  $this->tree->deleteTree($node_data);
531  }
532 
533  // delete objects
534  foreach ($subtree as $node)
535  {
536  $fid = ilMediaPoolItem::lookupForeignId($node["child"]);
537  if ($node["type"] == "mob")
538  {
539  if (ilObject::_lookupType($fid) == "mob")
540  {
541  $obj =& new ilObjMediaObject($fid);
542  $obj->delete();
543  }
544  }
545 
546  if ($node["type"] == "fold")
547  {
548  if ($fid > 0 && ilObject::_lookupType($fid) == "fold")
549  {
550  $obj = new ilObjFolder($fid, false);
551  $obj->delete();
552  }
553  }
554 
555  if ($node["type"] == "pg")
556  {
557  include_once("./Modules/MediaPool/classes/class.ilMediaPoolPage.php");
558  if (ilMediaPoolPage::_exists($node["child"]))
559  {
560  $pg = new ilMediaPoolPage($node["child"]);
561  $pg->delete();
562  }
563  }
564 
565  include_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
566  $item = new ilMediaPoolItem($node["child"]);
567  $item->delete();
568  }
569  }
570 
577  static function isForeignIdInTree($a_pool_id, $a_foreign_id)
578  {
579  global $ilDB;
580 
581  $set = $ilDB->query("SELECT * FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE ".
582  " foreign_id = ".$ilDB->quote($a_foreign_id, "integer").
583  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer")
584  );
585  if ($rec = $ilDB->fetchAssoc($set))
586  {
587  return true;
588  }
589  return false;
590  }
591 
595  static function isItemIdInTree($a_pool_id, $a_item_id)
596  {
597  global $ilDB;
598 
599  $set = $ilDB->query("SELECT * FROM mep_tree WHERE child = ".
600  $ilDB->quote($a_item_id, "integer").
601  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer"));
602  if ($rec = $ilDB->fetchAssoc($set))
603  {
604  return true;
605  }
606  return false;
607  }
608 
615  function createFolder($a_title, $a_parent = 0)
616  {
617  // perform save
618  $mep_item = new ilMediaPoolItem();
619  $mep_item->setTitle($a_title);
620  $mep_item->setType("fold");
621  $mep_item->create();
622  if ($mep_item->getId() > 0)
623  {
624  $tree = $this->getTree();
625  $parent = $a_parent > 0
626  ? $a_parent
627  : $tree->getRootId();
628  $this->insertInTree($mep_item->getId(), $parent);
629  return $mep_item->getId();
630  }
631  return false;
632  }
633 
640  public function cloneObject($a_target_id,$a_copy_id = 0)
641  {
642  global $ilDB, $ilUser, $ilias;
643 
644  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
645 
646  $new_obj->setTitle($this->getTitle());
647  $new_obj->setDescription($this->getDescription());
648  $new_obj->setDefaultWidth($this->getDefaultWidth());
649  $new_obj->setDefaultHeight($this->getDefaultHeight());
650  $new_obj->update();
651 
652  // copy content
653  $this->copyTreeContent($new_obj, $new_obj->getTree()->readRootId(),
654  $this->getTree()->readRootId());
655 
656  return $new_obj;
657  }
658 
665  function copyTreeContent($a_new_obj, $a_target_parent, $a_source_parent)
666  {
667  include_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
668  include_once("./Modules/MediaPool/classes/class.ilMediaPoolPage.php");
669  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
670 
671  // get all chapters of root lm
672  $nodes = $this->getTree()->getChilds($a_source_parent);
673  foreach ($nodes as $node)
674  {
675  $item = new ilMediaPoolItem();
676  $item->setType($node["type"]);
677  switch ($node["type"])
678  {
679  case "mob":
680  $mob_id = ilMediaPoolItem::lookupForeignId($node["child"]);
681  $mob = new ilObjMediaObject($mob_id);
682  $new_mob = $mob->duplicate();
683  $item->setForeignId($new_mob->getId());
684  $item->setTitle($new_mob->getTitle());
685  $item->create();
686  break;
687 
688  case "pg":
689  $item->setTitle($node["title"]);
690  $item->create();
691  $page = new ilMediaPoolPage($node["child"]);
692  $new_page = new ilMediaPoolPage();
693  $new_page->setId($item->getId());
694  $new_page->create();
695 
696  // copy page
697  $page->copy($new_page->getId(), $new_page->getParentType(), $new_page->getParentId(), true);
698  //$new_page->setXMLContent($page->copyXMLContent(true));
699  //$new_page->buildDom();
700  //$new_page->update();
701  break;
702 
703  case "fold":
704  $item->setTitle($node["title"]);
705  $item->create();
706  break;
707  }
708 
709  // insert item into tree
710  $a_new_obj->insertInTree($item->getId(), $a_target_parent);
711 
712  // handle childs
713  $this->copyTreeContent($a_new_obj, $item->getId(), $node["child"]);
714  }
715  }
716 
717 }
718 ?>