ILIAS  Release_4_2_x_branch Revision 61807
 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 "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  // create media pool tree
132  $this->tree =& new ilTree($this->getId());
133  $this->tree->setTreeTablePK("mep_id");
134  $this->tree->setTableNames('mep_tree','mep_item');
135  $this->tree->addTree($this->getId(), 1);
136 
137  }
138 
142  function &getTree()
143  {
144  return $this->tree;
145  }
146 
153  function update()
154  {
155  global $ilDB;
156 
157  if (!parent::update())
158  {
159  return false;
160  }
161 
162  // put here object specific stuff
163  $ilDB->manipulate("UPDATE mep_data SET ".
164  " default_width = ".$ilDB->quote($this->getDefaultWidth(), "integer").",".
165  " default_height = ".$ilDB->quote($this->getDefaultHeight(), "integer").
166  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
167  );
168 
169  return true;
170  }
171 
172 
183  function delete()
184  {
185  // always call parent delete function first!!
186  if (!parent::delete())
187  {
188  return false;
189  }
190 
191  // get childs
192  $childs = $this->tree->getSubTree($this->tree->getNodeData($this->tree->readRootId()));
193 
194  // delete tree
195  $this->tree->removeTree($this->tree->getTreeId());
196 
197  // delete childs
198  foreach ($childs as $child)
199  {
200  $fid = ilMediaPoolItem::lookupForeignId($child["obj_id"]);
201  switch ($child["type"])
202  {
203  case "mob":
204  if (ilObject::_lookupType($fid) == "mob")
205  {
206  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
207  $mob = new ilObjMediaObject($fid);
208  $mob->delete();
209  }
210  break;
211 
212 /* case "fold":
213  if (ilObject::_lookupType($fid) == "fold")
214  {
215  include_once("./Modules/Folder/classes/class.ilObjFolder.php");
216  $fold = new ilObjFolder($fid, false);
217  $fold->delete();
218  }
219  break;*/
220  }
221  }
222 
223  return true;
224  }
225 
235  function initDefaultRoles()
236  {
237  global $rbacadmin;
238 
239  // create a local role folder
240  //$rfoldObj = $this->createRoleFolder("Local roles","Role Folder of forum obj_no.".$this->getId());
241 
242  // create moderator role and assign role to rolefolder...
243  //$roleObj = $rfoldObj->createRole("Moderator","Moderator of forum obj_no.".$this->getId());
244  //$roles[] = $roleObj->getId();
245 
246  //unset($rfoldObj);
247  //unset($roleObj);
248 
249  return $roles ? $roles : array();
250  }
251 
265  function notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params = 0)
266  {
267  global $tree;
268 
269  switch ($a_event)
270  {
271  case "link":
272 
273  //var_dump("<pre>",$a_params,"</pre>");
274  //echo "Module name ".$this->getRefId()." triggered by link event. Objects linked into target object ref_id: ".$a_ref_id;
275  //exit;
276  break;
277 
278  case "cut":
279 
280  //echo "Module name ".$this->getRefId()." triggered by cut event. Objects are removed from target object ref_id: ".$a_ref_id;
281  //exit;
282  break;
283 
284  case "copy":
285 
286  //var_dump("<pre>",$a_params,"</pre>");
287  //echo "Module name ".$this->getRefId()." triggered by copy event. Objects are copied into target object ref_id: ".$a_ref_id;
288  //exit;
289  break;
290 
291  case "paste":
292 
293  //echo "Module name ".$this->getRefId()." triggered by paste (cut) event. Objects are pasted into target object ref_id: ".$a_ref_id;
294  //exit;
295  break;
296 
297  case "new":
298 
299  //echo "Module name ".$this->getRefId()." triggered by paste (new) event. Objects are applied to target object ref_id: ".$a_ref_id;
300  //exit;
301  break;
302  }
303 
304  // At the beginning of the recursive process it avoids second call of the notify function with the same parameter
305  if ($a_node_id==$_GET["ref_id"])
306  {
307  $parent_obj =& $this->ilias->obj_factory->getInstanceByRefId($a_node_id);
308  $parent_type = $parent_obj->getType();
309  if($parent_type == $this->getType())
310  {
311  $a_node_id = (int) $tree->getParentId($a_node_id);
312  }
313  }
314 
315  parent::notify($a_event,$a_ref_id,$a_parent_non_rbac_id,$a_node_id,$a_params);
316  }
317 
318 
322  function getChilds($obj_id = "", $a_type = "")
323  {
324  $objs = array();
325  $mobs = array();
326  $pgs = array();
327  if ($obj_id == "")
328  {
329  $obj_id = $this->tree->getRootId();
330  }
331 
332  if ($a_type == "fold" || $a_type == "")
333  {
334  $objs = $this->tree->getChildsByType($obj_id, "fold");
335  }
336  if ($a_type == "mob" || $a_type == "")
337  {
338  $mobs = $this->tree->getChildsByType($obj_id, "mob");
339  }
340  foreach($mobs as $key => $mob)
341  {
342  $objs[] = $mob;
343  }
344  if ($a_type == "pg" || $a_type == "")
345  {
346  $pgs = $this->tree->getChildsByType($obj_id, "pg");
347  }
348  foreach($pgs as $key => $pg)
349  {
350  $objs[] = $pg;
351  }
352 
353  return $objs;
354  }
355 
359  function getChildsExceptFolders($obj_id = "")
360  {
361  $objs = array();
362  $mobs = array();
363  if ($obj_id == "")
364  {
365  $obj_id = $this->tree->getRootId();
366  }
367 
368  $objs = $this->tree->getFilteredChilds(array("fold", "dummy"), $obj_id);
369  return $objs;
370  }
371 
375  function getMediaObjects($a_title_filter = "", $a_format_filter = "", $a_keyword_filter = '', $a_caption_filter)
376  {
377  global $ilDB;
378 
379  $query = "SELECT DISTINCT mep_tree.*, object_data.* ".
380  "FROM mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
381  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ";
382 
383  if ($a_format_filter != "" or $a_caption_filter != '')
384  {
385  $query.= " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ";
386  }
387 
388  $query .=
389  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
390  " AND object_data.type = ".$ilDB->quote("mob", "text");
391 
392  // filter
393  if (trim($a_title_filter) != "") // title
394  {
395  $query.= " AND ".$ilDB->like("object_data.title", "text", "%".trim($a_title_filter)."%");
396  }
397  if ($a_format_filter != "") // format
398  {
399  $filter = ($a_format_filter == "unknown")
400  ? ""
401  : $a_format_filter;
402  $query.= " AND ".$ilDB->equals("media_item.format", $filter, "text", true);
403  }
404  if(trim($a_caption_filter))
405  {
406  $query .= 'AND '.$ilDB->like('media_item.caption', 'text', '%'.trim($a_caption_filter).'%');
407  }
408 
409  $query.=
410  " ORDER BY object_data.title";
411 
412  $objs = array();
413  $set = $ilDB->query($query);
414  while ($rec = $ilDB->fetchAssoc($set))
415  {
416  $rec["foreign_id"] = $rec["obj_id"];
417  $rec["obj_id"] = "";
418  $objs[] = $rec;
419  }
420 
421  // Keyword filter
422  if($a_keyword_filter)
423  {
424  include_once './Services/MetaData/classes/class.ilMDKeyword.php';
425  $res = ilMDKeyword::_searchKeywords($a_keyword_filter,'mob',0);
426 
427  foreach($objs as $obj)
428  {
429  if(in_array($obj['foreign_id'],$res))
430  {
431  $filtered[] = $obj;
432  }
433  }
434  return (array) $filtered;
435  }
436  return $objs;
437  }
438 
442  function getAllMobIds($a_id)
443  {
444  global $ilDB;
445 
446  $query = "SELECT foreign_id as id FROM ".
447  " mep_tree JOIN mep_item ON (mep_tree.child = mep_item.obj_id) ".
448  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
449  " WHERE mep_tree.mep_id = ".$ilDB->quote($a_id, "integer").
450  " AND mep_item.type = ".$ilDB->quote("mob", "text").
451  " AND object_data.type = ".$ilDB->quote("mob", "text");
452  $set = $ilDB->query($query);
453  $ids = array();
454  while ($rec = $ilDB->fetchAssoc($set))
455  {
456  $ids[] = $rec["id"];
457  }
458  return $ids;
459  }
460 
464  function getUsedFormats()
465  {
466  global $ilDB, $lng;
467 
468  $query = "SELECT DISTINCT media_item.format f FROM mep_tree ".
469  " JOIN mep_item ON (mep_item.obj_id = mep_tree.child) ".
470  " JOIN object_data ON (mep_item.foreign_id = object_data.obj_id) ".
471  " JOIN media_item ON (media_item.mob_id = object_data.obj_id) ".
472  " WHERE mep_tree.mep_id = ".$ilDB->quote($this->getId(), "integer").
473  " AND object_data.type = ".$ilDB->quote("mob", "text").
474  " ORDER BY f";
475  $formats = array();
476  $set = $ilDB->query($query);
477  while ($rec = $ilDB->fetchAssoc($set))
478  {
479  if ($rec["f"] != "")
480  {
481  $formats[$rec["f"]] = $rec["f"];
482  }
483  else
484  {
485  $formats["unknown"] = $lng->txt("mep_unknown");
486  }
487  }
488 
489  return $formats;
490  }
491 
492  function getParentId($obj_id = "")
493  {
494  if ($obj_id == "")
495  {
496  return false;
497  }
498  if ($obj_id == $this->tree->getRootId())
499  {
500  return false;
501  }
502 
503  return $this->tree->getParentId($obj_id);
504  }
505 
511  function insertInTree($a_obj_id, $a_parent = "")
512  {
513  if (!$this->tree->isInTree($a_obj_id))
514  {
515  $parent = ($a_parent == "")
516  ? $this->tree->getRootId()
517  : $a_parent;
518  $this->tree->insertNode($a_obj_id, $parent);
519  return true;
520  }
521  else
522  {
523  return false;
524  }
525  }
526 
527 
532  function deleteChild($obj_id)
533  {
534  $fid = ilMediaPoolItem::lookupForeignId($obj_id);
537 
538  $node_data = $this->tree->getNodeData($obj_id);
539  $subtree = $this->tree->getSubtree($node_data);
540 
541  // delete tree
542  if($this->tree->isInTree($obj_id))
543  {
544  $this->tree->deleteTree($node_data);
545  }
546 
547  // delete objects
548  foreach ($subtree as $node)
549  {
550  $fid = ilMediaPoolItem::lookupForeignId($node["child"]);
551  if ($node["type"] == "mob")
552  {
553  if (ilObject::_lookupType($fid) == "mob")
554  {
555  $obj =& new ilObjMediaObject($fid);
556  $obj->delete();
557  }
558  }
559 
560  if ($node["type"] == "fold")
561  {
562  if ($fid > 0 && ilObject::_lookupType($fid) == "fold")
563  {
564  $obj = new ilObjFolder($fid, false);
565  $obj->delete();
566  }
567  }
568 
569  if ($node["type"] == "pg")
570  {
571  include_once("./Modules/MediaPool/classes/class.ilMediaPoolPage.php");
572  if (ilMediaPoolPage::_exists($node["child"]))
573  {
574  $pg = new ilMediaPoolPage($node["child"]);
575  $pg->delete();
576  }
577  }
578 
579  include_once("./Modules/MediaPool/classes/class.ilMediaPoolItem.php");
580  $item = new ilMediaPoolItem($node["child"]);
581  $item->delete();
582  }
583  }
584 
591  static function isForeignIdInTree($a_pool_id, $a_foreign_id)
592  {
593  global $ilDB;
594 
595  $set = $ilDB->query("SELECT * FROM mep_tree JOIN mep_item ON (child = obj_id) WHERE ".
596  " foreign_id = ".$ilDB->quote($a_foreign_id, "integer").
597  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer")
598  );
599  if ($rec = $ilDB->fetchAssoc($set))
600  {
601  return true;
602  }
603  return false;
604  }
605 
609  static function isItemIdInTree($a_pool_id, $a_item_id)
610  {
611  global $ilDB;
612 
613  $set = $ilDB->query("SELECT * FROM mep_tree WHERE child = ".
614  $ilDB->quote($a_item_id, "integer").
615  " AND mep_id = ".$ilDB->quote($a_pool_id, "integer"));
616  if ($rec = $ilDB->fetchAssoc($set))
617  {
618  return true;
619  }
620  return false;
621  }
622 
629  function createFolder($a_title, $a_parent = 0)
630  {
631  // perform save
632  $mep_item = new ilMediaPoolItem();
633  $mep_item->setTitle($a_title);
634  $mep_item->setType("fold");
635  $mep_item->create();
636  if ($mep_item->getId() > 0)
637  {
638  $tree = $this->getTree();
639  $parent = $a_parent > 0
640  ? $a_parent
641  : $tree->getRootId();
642  $this->insertInTree($mep_item->getId(), $parent);
643  return $mep_item->getId();
644  }
645  return false;
646  }
647 }
648 ?>