ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
MediaPoolManager.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MediaPool;
22 
24 {
25  public function __construct(
26  protected InternalDomainService $domain,
27  protected int $obj_id
28  ) {
29  }
30 
31  public function copySelectedFromEditClipboard(int $target_id): void
32  {
34  foreach ($ids as $id) {
35  $this->copyItemFromEditClipboard($id, $target_id);
36  }
37  }
38 
39  public function copyItemFromEditClipboard(string $insert_id, int $target_id): void
40  {
41  $id = explode(":", $insert_id);
42  $type = $id[0];
43  $id = (int) $id[1];
44 
45  if ($type === "mob") { // media object
46  if (!\ilObjMediaPool::isForeignIdInTree($this->obj_id, $id)) {
47  $item = new \ilMediaPoolItem();
48  $item->setType("mob");
49  $item->setForeignId($id);
50  $item->setTitle(\ilObject::_lookupTitle($id));
51  $item->create();
52  if ($item->getId() > 0) {
53  $this->domain->tree($this->obj_id)->insertInMepTree($item->getId(), $target_id);
54  }
55  }
56  }
57  if ($type === "incl") { // content snippet
58  if (!\ilObjMediaPool::isItemIdInTree($this->obj_id, $id)) {
59  $original = new \ilMediaPoolPage($id);
60 
61  // copy the page into the pool
62  $item = new \ilMediaPoolItem();
63  $item->setType("pg");
64  $item->setTitle(\ilMediaPoolItem::lookupTitle($id));
65  $item->create();
66  if ($item->getId() > 0) {
67  $this->domain->tree($this->obj_id)->insertInMepTree($item->getId(), $target_id);
68 
69  // create page
70  $page = new \ilMediaPoolPage();
71  $page->setId($item->getId());
72  $page->setParentId($this->obj_id);
73  $page->create(false);
74 
75  // copy content
76  $original->copy($page->getId(), $page->getParentType(), $page->getParentId(), true);
77 
78 
79  // copy adv metadata
81  if (count($pool_ids) === 1) {
82  $source_pool_id = current($pool_ids);
83  $this->copyMetadataOfItem(
84  $source_pool_id,
85  $this->obj_id,
86  $id,
87  $page->getId()
88  );
89  }
90  }
91  }
92  }
93  }
94 
95  // move action
96  public function pasteFromClipboard(int $target_folder_id): void
97  {
98  $target_tree = $this->domain->tree($this->obj_id);
99 
100  // sanity check
101  $move_ids = \ilSession::get("mep_move_ids");
102  if (is_array($move_ids)) {
103  foreach ($move_ids as $id) {
104  $pool_ids = \ilMediaPoolItem::getPoolForItemId($id);
105 
106  if (!in_array(\ilMediaPoolItem::lookupType($target_folder_id), ["fold", "dummy"])) {
107  throw new InvalidTargetException("Invalid target " . $target_folder_id .
108  " (" . \ilMediaPoolItem::lookupType($target_folder_id) . ")");
109  }
110  if ($this->isTargetWithinSource($id, $target_folder_id)) {
111  throw new InvalidTargetException("Invalid target " . $target_folder_id .
112  " (" . \ilMediaPoolItem::lookupType($target_folder_id) . ")");
113  }
114 
115  $subnodes = [];
116  $source_pool_id = 0;
117  foreach ($pool_ids as $pool_id) {
118  $source_pool_id = $pool_id;
119  $source_tree = $this->domain->tree($pool_id);
120  $subnodes = $source_tree->getSubtree($source_tree->getNodeData($id));
121  $source_tree->deleteTree($source_tree->getNodeData($id));
122  }
123 
124  $target_tree->insertNode($id, $target_folder_id);
125  $this->copyMetadataOfItem(
126  $source_pool_id,
127  $this->obj_id,
128  $id,
129  $id
130  );
131  foreach ($subnodes as $node) {
132  if ($node["child"] != $id) {
133  $target_tree->insertNode($node["child"], $node["parent"]);
134  $this->copyMetadataOfItem(
135  $source_pool_id,
136  $this->obj_id,
137  (int) $node["child"],
138  (int) $node["child"]
139  );
140  }
141  }
142  }
143  }
144  \ilSession::clear("mep_move_ids");
145  }
146 
147  protected function copyMetadataOfItem(
148  int $source_pool_id,
149  int $target_pool_id,
150  int $source_child_id,
151  int $target_child_id
152  ): void {
153  if (\ilMediaPoolItem::lookupType($source_child_id) === "pg") {
155  0,
156  $source_pool_id,
157  $target_pool_id,
158  "mpg",
159  $source_child_id,
160  $target_child_id
161  );
162 
163  $this->domain->metadata()->cloneLOM(
164  $source_pool_id,
165  $source_child_id,
166  "mpg",
167  $target_pool_id,
168  $target_child_id,
169  "mpg"
170  );
171  }
172  }
173 
174  public function isTargetWithinSource(int $source_id, int $target_folder_id): bool
175  {
176  $pool_ids = \ilMediaPoolItem::getPoolForItemId($source_id);
177 
178  /*
179  $parent_id = $this->mep_request->getItemId();
180  if (ilMediaPoolItem::lookupType($parent_id) !== "fold") {
181  $parent_id = $target_tree->readRootId();
182  }*/
183 
184  $subnodes = [];
185  foreach ($pool_ids as $pool_id) {
186  $source_tree = $this->domain->tree($pool_id);
187 
188  // if source tree == target tree, check if target is within source tree
189  $subnodes = $source_tree->getSubtree($source_tree->getNodeData($source_id));
190  foreach ($subnodes as $subnode) {
191  if ((int) $subnode["child"] === (int) $target_folder_id) {
192  return true;
193  }
194  }
195  }
196  return false;
197  }
198 }
static get(string $a_var)
pasteFromClipboard(int $target_folder_id)
static getPoolForItemId(int $a_id)
static lookupTitle(int $a_id)
static _lookupTitle(int $obj_id)
copySelectedFromEditClipboard(int $target_id)
static isForeignIdInTree(int $a_pool_id, int $a_foreign_id)
Check whether foreign id is in tree.
copyItemFromEditClipboard(string $insert_id, int $target_id)
__construct(protected InternalDomainService $domain, protected int $obj_id)
isTargetWithinSource(int $source_id, int $target_folder_id)
copyMetadataOfItem(int $source_pool_id, int $target_pool_id, int $source_child_id, int $target_child_id)
static _cloneValues(int $copy_id, int $a_source_id, int $a_target_id, ?string $a_sub_type=null, ?int $a_source_sub_id=null, ?int $a_target_sub_id=null)
Clone Advanced Meta Data.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static lookupType(int $a_id)
static isItemIdInTree(int $a_pool_id, int $a_item_id)
Check whether a mep item id is in the media pool.
static clear(string $a_var)