ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilPCMediaObject.php
Go to the documentation of this file.
1<?php
2
20
27{
28 protected \ILIAS\MediaObjects\MediaObjectManager $mob_manager;
31 protected DOMNode $mal_node;
32 protected ilObjUser $user;
33 protected DOMNode $mob_node;
34 protected \ILIAS\DI\UIServices $ui;
35 protected ?ilObjMediaObject $mediaobject = null;
36 protected ilLanguage $lng;
38 protected static string $modal_show_signal = "";
39 protected static string $modal_suffix = "";
40
41 public function init(): void
42 {
43 global $DIC;
44
45 $this->user = $DIC->user();
46 $this->setType("media");
47 $this->ui = $DIC->ui();
48 $this->lng = $DIC->language();
49 $this->mob_usage_repo = $DIC->mediaObjects()
50 ->internal()
51 ->repo()
52 ->usage();
53 $this->ctrl = $DIC->ctrl();
54 $this->mob_manager = $DIC->mediaObjects()->internal()->domain()->mediaObject();
55 }
56
57 public function readMediaObject(int $a_mob_id = 0): void
58 {
59 if ($a_mob_id > 0) {
60 $mob = new ilObjMediaObject($a_mob_id);
61 $this->setMediaObject($mob);
62 }
63 }
64
65 public function setHierId(string $a_hier_id): void
66 {
67 $this->hier_id = $a_hier_id;
68 }
69
70 public function setMediaObject(ilObjMediaObject $a_mediaobject): void
71 {
72 $this->mediaobject = $a_mediaobject;
73 }
74
75 public function getMediaObject(): ?ilObjMediaObject
76 {
77 return $this->mediaobject;
78 }
79
80 public function createMediaObject(): void
81 {
82 $this->setMediaObject(new ilObjMediaObject());
83 }
84
85 public function create(): void
86 {
87 $this->createPageContentNode();
88 }
89
90 public function createAlias(
91 ilPageObject $a_pg_obj,
92 string $a_hier_id,
93 string $a_pc_id = ""
94 ): void {
95 $this->setDomNode($this->dom_doc->createElement("PageContent"));
96 $a_pg_obj->insertContent($this, $a_hier_id, IL_INSERT_AFTER, $a_pc_id);
97 $mob_node = $this->dom_doc->createElement("MediaObject");
98 $mob_node = $this->getDomNode()->appendChild($mob_node);
99 $this->mal_node = $this->dom_doc->createElement("MediaAlias");
100 $this->mal_node = $mob_node->appendChild($this->mal_node);
101 $this->mal_node->setAttribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
102
103 // standard view
104 $item_node = $this->dom_doc->createElement("MediaAliasItem");
105 $item_node = $mob_node->appendChild($item_node);
106 $item_node->setAttribute("Purpose", "Standard");
107 $media_item = $this->getMediaObject()->getMediaItem("Standard");
108 if (is_null($media_item)) {
109 return;
110 }
111
112 $layout_node = $this->dom_doc->createElement("Layout");
113 $layout_node = $item_node->appendChild($layout_node);
114 if ($media_item->getWidth() > 0) {
115 //$layout_node->set_attribute("Width", $media_item->getWidth());
116 }
117 if ($media_item->getHeight() > 0) {
118 //$layout_node->set_attribute("Height", $media_item->getHeight());
119 }
120 $layout_node->setAttribute("HorizontalAlign", "Left");
121
122 // caption
123 if ($media_item->getCaption() != "") {
124 $cap_node = $this->dom_doc->createElement("Caption");
125 $cap_node = $item_node->appendChild($cap_node);
126 $cap_node->setAttribute("Align", "bottom");
127 $this->dom_util->setContent($cap_node, $media_item->getCaption());
128 }
129
130 // text representation
131 if ($media_item->getTextRepresentation() != "") {
132 $tr_node = $this->dom_doc->createElement("TextRepresentation");
133 $tr_node = $item_node->appendChild($tr_node);
134 $this->dom_util->setContent($tr_node, $media_item->getTextRepresentation());
135 }
136
137 $pars = $media_item->getParameters();
138 foreach ($pars as $par => $val) {
139 $par_node = $this->dom_doc->createElement("Parameter");
140 $par_node = $item_node->appendChild($par_node);
141 $par_node->setAttribute("Name", $par);
142 $par_node->setAttribute("Value", $val);
143 }
144
145 // fullscreen view
146 $fullscreen_item = $this->getMediaObject()->getMediaItem("Fullscreen");
147 if (is_object($fullscreen_item)) {
148 $item_node = $this->dom_doc->createElement("MediaAliasItem");
149 $item_node = $mob_node->appendChild($item_node);
150 $item_node->setAttribute("Purpose", "Fullscreen");
151
152 // width and height
153 $layout_node = $this->dom_doc->createElement("Layout");
154 $layout_node = $item_node->appendChild($layout_node);
155 if ($fullscreen_item->getWidth() > 0) {
156 $layout_node->setAttribute("Width", $fullscreen_item->getWidth());
157 }
158 if ($fullscreen_item->getHeight() > 0) {
159 $layout_node->setAttribute("Height", $fullscreen_item->getHeight());
160 }
161
162 // caption
163 if ($fullscreen_item->getCaption() != "") {
164 $cap_node = $this->dom_doc->createElement("Caption");
165 $cap_node = $item_node->appendChild($cap_node);
166 $cap_node->setAttribute("Align", "bottom");
167 $this->dom_util->setContent($cap_node, $fullscreen_item->getCaption());
168 }
169
170 // text representation
171 if ($fullscreen_item->getTextRepresentation() != "") {
172 $tr_node = $this->dom_doc->createElement("TextRepresentation");
173 $tr_node = $item_node->appendChild($tr_node);
174 $this->dom_util->setContent($tr_node, $fullscreen_item->getTextRepresentation());
175 }
176
177 $pars = $fullscreen_item->getParameters();
178 foreach ($pars as $par => $val) {
179 $par_node = $this->dom_doc->createElement("Parameter");
180 $par_node = $item_node->appendChild($par_node);
181 $par_node->setAttribute("Name", $par);
182 $par_node->setAttribute("Value", $val);
183 }
184 }
185 }
186
187 protected function getMediaAliasNode(): ?DOMNode
188 {
189 if (is_object($this->getChildNode())) {
190 $mal_node = $this->getChildNode()->firstChild;
191 if (is_object($mal_node) && $mal_node->nodeName == "MediaAlias") {
192 return $mal_node;
193 }
194 }
195 return null;
196 }
197
203 public function updateObjectReference(): void
204 {
205 $this->getMediaAliasNode()?->setAttribute("OriginId", "il__mob_" . $this->getMediaObject()->getId());
206 }
207
208 public function dumpXML(): string
209 {
210 return $this->dom_util->dump($this->getDomNode());
211 }
212
213 public function setClass(string $a_class): void
214 {
215 $this->dom_util->setAttribute($this->getMediaAliasNode(), "Class", $a_class);
216 }
217
221 public function getClass(): string
222 {
223 return (string) $this->getMediaAliasNode()?->getAttribute("Class");
224 }
225
229 public function setCaptionClass(string $a_class): void
230 {
231 $this->dom_util->setAttribute($this->getMediaAliasNode(), "CaptionClass", $a_class);
232 }
233
234 public function getCaptionClass(): string
235 {
236 return (string) $this->getMediaAliasNode()?->getAttribute("CaptionClass");
237 }
238
239 public static function getLangVars(): array
240 {
241 return array("pc_mob");
242 }
243
251 public static function afterPageUpdate(
252 ilPageObject $a_page,
253 DOMDocument $a_domdoc,
254 string $a_xml,
255 bool $a_creation
256 ): void {
257 if (!$a_page->getImportMode()) {
259 $a_page->getParentType() . ":pg",
260 $a_page->getId(),
261 0,
262 $a_page->getLanguage()
263 );
264 }
265 self::saveMobUsage($a_page, $a_domdoc);
266 if (!$a_page->getImportMode()) {
267 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
268 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
269 $mob_obj = new ilObjMediaObject($mob);
270 $usages = $mob_obj->getUsages(false);
271 if (count($usages) == 0) { // delete, if no usage exists
272 $mob_obj->delete();
273 }
274 }
275 }
276 }
277 }
278
279 public static function beforePageDelete(
280 ilPageObject $a_page
281 ): void {
282 $mob_ids = ilObjMediaObject::_getMobsOfObject(
283 $a_page->getParentType() . ":pg",
284 $a_page->getId(),
285 0,
286 $a_page->getLanguage()
287 );
288
290 $a_page->getParentType() . ":pg",
291 $a_page->getId(),
292 null,
293 $a_page->getLanguage()
294 );
295
296 foreach ($mob_ids as $mob) { // check, whether media object can be deleted
297 if (ilObject::_exists($mob) && ilObject::_lookupType($mob) == "mob") {
298 $mob_obj = new ilObjMediaObject($mob);
299 $usages = $mob_obj->getUsages(false);
300 if (count($usages) == 0) { // delete, if no usage exists
301 $mob_obj->delete();
302 }
303 }
304 }
305 }
306
314 public static function afterPageHistoryEntry(
315 ilPageObject $a_page,
316 DOMDocument $a_old_domdoc,
317 string $a_old_xml,
318 int $a_old_nr
319 ): void {
320 self::saveMobUsage($a_page, $a_old_domdoc, $a_old_nr);
321 }
322
323 public static function saveMobUsage(
324 ilPageObject $a_page,
325 DOMDocument $a_domdoc,
326 int $a_old_nr = 0
327 ): array {
328 $usages = array();
329
330 // media aliases
331 $xpath = new DOMXPath($a_domdoc);
332 $nodes = $xpath->query('//MediaAlias');
333 foreach ($nodes as $node) {
334 $id_arr = explode("_", $node->getAttribute("OriginId"));
335 $mob_id = $id_arr[count($id_arr) - 1];
336 if ($mob_id > 0 && $id_arr[1] == "") {
337 $usages[$mob_id] = true;
338 }
339 }
340
341 // media objects
342 $xpath = new DOMXPath($a_domdoc);
343 $nodes = $xpath->query('//MediaObject/MetaData/General/Identifier');
344 foreach ($nodes as $node) {
345 $mob_entry = $node->getAttribute("Entry");
346 $mob_arr = explode("_", $mob_entry);
347 $mob_id = $mob_arr[count($mob_arr) - 1];
348 if ($mob_id > 0 && $mob_arr[1] == "") {
349 $usages[$mob_id] = true;
350 }
351 }
352
353 // internal links
354 $xpath = new DOMXPath($a_domdoc);
355 $nodes = $xpath->query("//IntLink[@Type='MediaObject']");
356 foreach ($nodes as $node) {
357 $mob_target = $node->getAttribute("Target");
358 $mob_arr = explode("_", $mob_target);
359 //echo "<br>3<br>";
360 //echo $mob_target."<br>";
361 //var_dump($mob_arr);
362 $mob_id = $mob_arr[count($mob_arr) - 1];
363 if ($mob_id > 0 && $mob_arr[1] == "") {
364 $usages[$mob_id] = true;
365 }
366 }
367
369 $a_page->getParentType() . ":pg",
370 $a_page->getId(),
371 $a_old_nr,
372 $a_page->getLanguage()
373 );
374 foreach ($usages as $mob_id => $val) {
375 // save usage, if object exists...
376 if (ilObject::_lookupType($mob_id) == "mob") {
378 $mob_id,
379 $a_page->getParentType() . ":pg",
380 $a_page->getId(),
381 $a_old_nr,
382 $a_page->getLanguage()
383 );
384 }
385 }
386
387 return $usages;
388 }
389
391 string $a_output,
392 string $a_mode,
393 bool $a_abstract_only = false
394 ): string {
395 global $DIC;
396
397 $this->global_tpl = $DIC['tpl'];
398 $ilUser = $this->user;
399
400 if ($a_mode == "offline") {
401 $page = $this->getPage();
402
404 $page->getParentType() . ":pg",
405 $page->getId(),
406 0,
407 $page->getLanguage()
408 );
409 foreach ($mob_ids as $mob_id) {
410 $mob = new ilObjMediaObject($mob_id);
411 $srts = $mob->getSrtFiles();
412 foreach ($srts as $srt) {
413 if ($ilUser->getLanguage() == $srt["language"]) {
414 $srt_content = "";
415 $file_path = ilObjMediaObject::_getDirectory($mob->getId()) . "/" . $srt["full_path"];
416 if (is_file($file_path)) {
417 $srt_content = $file_path;
418 } else {
419 $srt_content = $this->mob_manager->getLocationContent(
420 $mob->getId(),
421 $srt["full_path"]
422 );
423 }
424 if ($srt_content !== "") {
425 $a_output = str_replace(
426 "[[[[[mobsubtitle;il__mob_" . $mob->getId() . "_Standard]]]]]",
427 $srt_content,
428 $a_output
429 );
430 }
431 }
432 }
433 }
434 }
435
436 if ($a_abstract_only) {
437 return $a_output;
438 }
439
440 if ($a_mode === "edit") {
441 $a_output = str_replace(
442 "{{{{{Unsupported Media Type}}}}}",
443 $this->ui->renderer()->render(
444 $this->ui->factory()->messageBox()->info(
445 $this->lng->txt("copg_unsupported_media_type")
446 )
447 ),
448 $a_output
449 );
450 // currently media objects, when in translation mode
451 $a_output = str_replace(
452 "{{{{{No Media Type}}}}}",
453 '<div class="copg-new-content-placeholder">' .
454 $this->ui->renderer()->render(
455 $this->ui->factory()->symbol()->icon()->standard("pemed", "pemed", 'medium')
456 ) . "</div>",
457 $a_output
458 );
459 } else {
460 $a_output = str_replace(
461 "{{{{{Unsupported Media Type}}}}}",
462 "",
463 $a_output
464 );
465 $a_output = str_replace(
466 "{{{{{No Media Type}}}}}",
467 "",
468 $a_output
469 );
470 }
471
472 // add fullscreen modals
473 $page = $this->getPage();
474 $suffix = "-" . $page->getParentType() . "-" . $page->getId();
475 $modal = $this->ui->factory()->modal()->roundtrip(
476 $this->lng->txt("cont_fullscreen"),
477 $this->ui->factory()->legacy()->content("<iframe class='il-copg-mob-fullscreen' id='il-copg-mob-fullscreen" . $suffix . "'></iframe>")
478 );
479 $show_signal = $modal->getShowSignal();
480
481 $js = "
482 $(function () {
483 il.COPagePres.setFullscreenModalShowSignal('$show_signal', '$suffix');
484 });
485 ";
486 self::$modal_show_signal = $show_signal;
487 self::$modal_suffix = $suffix;
488 $this->global_tpl->addOnloadCode($js);
489
490 // async ensures to have onloadcode of modal in output
491 // if other main tpl is used, see #32198
492 // note: if always rendered async, $ not defined errors will be thrown in non-async cases
493 if ($this->ctrl->isAsynch()) {
494 $html = $this->ui->renderer()->renderAsync($modal);
495 } else {
496 $html = $this->ui->renderer()->render($modal);
497 }
498 return $a_output . "<div class='il-copg-mob-fullscreen-modal'>" . $html . "</div>";
499 }
500
501 public function getOnloadCode(string $a_mode): array
502 {
503 $onload_code = [];
504 // necessary due to 32198 (other main template used)
505 if (self::$modal_show_signal !== "") {
506 $onload_code[] = "il.COPagePres.setFullscreenModalShowSignal('" . self::$modal_show_signal .
507 "', '" . self::$modal_suffix . "');";
508 }
509 return $onload_code;
510 }
511
512 public function getJavascriptFiles(
513 string $a_mode
514 ): array {
515 return [];
516 }
517
518 public function getCssFiles(
519 string $a_mode
520 ): array {
521 return [];
522 }
523
525 {
526 $std_alias_item = new ilMediaAliasItem(
527 $this->getDomDoc(),
528 $this->getHierId(),
529 "Standard",
530 $this->getPCId()
531 );
532 return $std_alias_item;
533 }
534
536 {
537 $std_alias_item = new ilMediaAliasItem(
538 $this->getDomDoc(),
539 $this->getHierId(),
540 "Fullscreen",
541 $this->getPCId()
542 );
543 return $std_alias_item;
544 }
545
546 public function checkInstanceEditing(): bool
547 {
548 // if any properties are set on the instance,
549 // that are not accessible through the quick editing screen
550 // -> offer instance editing
551 $std_alias_item = $this->getStandardMediaAliasItem();
552 if ($std_alias_item->hasAnyPropertiesSet()) {
553 return true;
554 }
555 if ($this->getMediaObject()->hasFullscreenItem()) {
556 $full_alias_item = $this->getFullscreenMediaAliasItem();
557 if ($full_alias_item->hasAnyPropertiesSet()) {
558 return true;
559 }
560 }
561
562 // if the media object has any other use outside of the current page
563 // -> offer instance editing
565 $mob = $this->getMediaObject();
566 $page = $this->getPage();
567 if (is_object($mob)) {
568 $usages = $mob->getUsages();
569 $other_usages = array_filter($usages, function ($usage) use ($page) {
570 return ($usage["type"] != $page->getParentType() . ":pg" || $usage["id"] != $page->getId());
571 });
572 if (count($other_usages) > 0) {
573 return true;
574 }
575 }
576 // see https://mantis.ilias.de/view.php?id=38582
577 // we allow instance editing regardless of number of usages
578 return true;
579 }
580
581 public static function deleteHistoryLowerEqualThan(
582 string $parent_type,
583 int $page_id,
584 string $lang,
585 int $delete_lower_than_nr
586 ): void {
587 global $DIC;
588
589 $mob_usage_repo = $DIC->mediaObjects()
590 ->internal()
591 ->repo()
592 ->usage();
593
595
596 $mob_ids = $mob_usage_repo->getHistoryUsagesLowerEqualThan(
597 $parent_type . ":pg",
598 $page_id,
599 $delete_lower_than_nr,
600 $lang
601 );
602
603 $mob_usage_repo->deleteHistoryUsagesLowerEqualThan(
604 $parent_type . ":pg",
605 $page_id,
606 $delete_lower_than_nr,
607 $lang
608 );
609
610 foreach ($mob_ids as $mob_id) {
611 $usages = ilObjMediaObject::lookupUsages($mob_id, true);
612 $log->debug("...check deletion of mob $mob_id. Usages: " . count($usages));
613 if (count($usages) == 0) {
614 if (ilObject::_lookupType($mob_id) === "mob") {
615 $mob = new ilObjMediaObject($mob_id);
616 $log->debug("Deleting Mob ID: " . $mob_id);
617 $mob->delete();
618 }
619 }
620 }
621 }
622
623 public static function handleCopiedContent(
624 DOMDocument $a_domdoc,
625 bool $a_self_ass = true,
626 bool $a_clone_mobs = false,
627 int $new_parent_id = 0,
628 int $obj_copy_id = 0
629 ): void {
630 global $DIC;
631
632 if (!$a_clone_mobs) {
633 return;
634 }
635
636 $dom_util = $DIC->copage()->internal()->domain()->domUtil();
637 $path = "//MediaObject/MediaAlias";
638 $nodes = $dom_util->path($a_domdoc, $path);
639 foreach ($nodes as $node) {
640 $or_id = $node->getAttribute("OriginId");
641
642 $inst_id = ilInternalLink::_extractInstOfTarget($or_id);
644
645 if (!($inst_id > 0)) {
646 if ($mob_id > 0) {
647 $media_object = new ilObjMediaObject($mob_id);
648 $new_mob = $media_object->duplicate();
649 $node->setAttribute("OriginId", "il__mob_" . $new_mob->getId());
650 }
651 }
652 }
653 }
654}
deleteHistoryUsagesLowerEqualThan(string $a_type, int $a_id, int $a_usage_hist_nr, string $a_lang="-")
getHistoryUsagesLowerEqualThan(string $a_type, int $a_id, int $a_usage_hist_nr, string $a_lang="-")
const IL_INSERT_AFTER
language handling
static getLogger(string $a_component_id)
Get component logger.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static lookupUsages(int $a_id, bool $a_include_history=true)
Lookup usages of media object.
static _getDirectory(int $a_mob_id)
Get absolute directory.
static _deleteAllUsages(string $a_type, int $a_id, ?int $a_usage_hist_nr=0, string $a_lang="-")
static _saveUsage(int $a_mob_id, string $a_type, int $a_id, int $a_usage_hist_nr=0, string $a_lang="-")
Save usage of mob within another container (e.g.
static _getMobsOfObject(string $a_type, int $a_id, int|false $a_usage_hist_nr=0, string $a_lang="-")
User class.
static _lookupType(int $id, bool $reference=false)
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
Class ilPCMediaObject Media content object (see ILIAS DTD)
readMediaObject(int $a_mob_id=0)
static deleteHistoryLowerEqualThan(string $parent_type, int $page_id, string $lang, int $delete_lower_than_nr)
Overwrite in derived classes, if old history entries are being deleted.
static saveMobUsage(ilPageObject $a_page, DOMDocument $a_domdoc, int $a_old_nr=0)
ILIAS DI UIServices $ui
updateObjectReference()
Updates the media object referenced by the media alias.
getCssFiles(string $a_mode)
setHierId(string $a_hier_id)
getOnloadCode(string $a_mode)
UsageDBRepository $mob_usage_repo
getClass()
Get characteristic of section.
static beforePageDelete(ilPageObject $a_page)
Before page is being deleted.
ilCtrlInterface $ctrl
ILIAS MediaObjects MediaObjectManager $mob_manager
createAlias(ilPageObject $a_pg_obj, string $a_hier_id, string $a_pc_id="")
static afterPageHistoryEntry(ilPageObject $a_page, DOMDocument $a_old_domdoc, string $a_old_xml, int $a_old_nr)
After page history entry has been created.
getJavascriptFiles(string $a_mode)
modifyPageContentPostXsl(string $a_output, string $a_mode, bool $a_abstract_only=false)
Modify page content after xsl.
static string $modal_suffix
setClass(string $a_class)
setCaptionClass(string $a_class)
Set caption style class of media object.
static handleCopiedContent(DOMDocument $a_domdoc, bool $a_self_ass=true, bool $a_clone_mobs=false, int $new_parent_id=0, int $obj_copy_id=0)
Handle copied content.
static getLangVars()
Get lang vars needed for editing.
setMediaObject(ilObjMediaObject $a_mediaobject)
static string $modal_show_signal
static afterPageUpdate(ilPageObject $a_page, DOMDocument $a_domdoc, string $a_xml, bool $a_creation)
After page has been updated (or created)
ilObjMediaObject $mediaobject
ilGlobalPageTemplate $global_tpl
Content object of ilPageObject (see ILIAS DTD).
createPageContentNode(bool $a_set_this_node=true)
Create page content node (always use this method first when adding a new element)
setDomNode(DOMNode $node)
setType(string $a_type)
Set Type.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
insertContent(ilPageContent $a_cont_obj, string $a_pos, int $a_mode=IL_INSERT_AFTER, string $a_pcid="", bool $remove_placeholder=true)
insert a content node before/after a sibling or as first child of a parent
return['delivery_method'=> 'php',]
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...
$log
Definition: ltiresult.php:34
$path
Definition: ltiservices.php:30
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26
getLanguage()