ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMediaCreationGUI.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2020 ILIAS open source, Extended GPL, see docs/LICENSE */
4
10{
11 const TYPE_VIDEO = 1;
12 const TYPE_AUDIO = 2;
13 const TYPE_IMAGE = 3;
14 const TYPE_OTHER = 4;
15 const TYPE_ALL = 5;
16
17 const POOL_VIEW_FOLDER = "fold";
18 const POOL_VIEW_ALL = "all";
19
20 protected $accept_types = [1,2,3,4];
21
25 protected $lng;
26
30 protected $ctrl;
31
35 protected $main_tpl;
36
40 protected $after_upload;
41
46
51
55 protected $access;
56
57 protected $all_suffixes = [];
58 protected $all_mime_types = [];
59
63 protected $ui;
64
68 protected $requested_mep;
69
74
78 public function __construct(
79 array $accept_types,
80 closure $after_upload,
81 closure $after_url_saving,
82 closure $after_pool_insert
83 ) {
84 global $DIC;
85
86 $this->lng = $DIC->language();
87 $this->lng->loadLanguageModule("mob");
88 $this->lng->loadLanguageModule("content");
89 $this->access = $DIC->access();
90
91 $this->ctrl = $DIC->ctrl();
92 $this->main_tpl = $DIC->ui()->mainTemplate();
93 $this->ui = $DIC->ui();
94
95 $this->accept_types = $accept_types;
96 $this->after_upload = $after_upload;
97 $this->after_url_saving = $after_url_saving;
98 $this->after_pool_insert = $after_pool_insert;
99
100 $this->ctrl->saveParameter($this, ["mep", "pool_view"]);
101
102 $this->requested_mep = ((int) $_POST["mep"] > 0)
103 ? (int) $_POST["mep"]
104 : (int) $_GET["mep"];
105
106 $this->pool_view = (in_array($_GET["pool_view"], [self::POOL_VIEW_FOLDER, self::POOL_VIEW_ALL]))
107 ? $_GET["pool_view"]
108 : self::POOL_VIEW_FOLDER;
109 }
110
115 public function setAllSuffixes($a_val)
116 {
117 $this->all_suffixes = $a_val;
118 }
119
124 public function getAllSuffixes()
125 {
126 return $this->all_suffixes;
127 }
128
133 public function setAllMimeTypes($a_val)
134 {
135 $this->all_mime_types = $a_val;
136 }
137
142 public function getAllMimeTypes()
143 {
145 }
146
147
152 protected function getSuffixes()
153 {
154 $suffixes = [];
155 if (in_array(self::TYPE_ALL, $this->accept_types)) {
156 $suffixes = $this->getAllSuffixes();
157 }
158 if (in_array(self::TYPE_VIDEO, $this->accept_types)) {
159 $suffixes[] = "mp4";
160 }
161 return $suffixes;
162 }
163
168 protected function getMimeTypes()
169 {
170 $mimes = [];
171 if (in_array(self::TYPE_ALL, $this->accept_types)) {
172 $mimes = $this->getAllMimeTypes();
173 }
174 if (in_array(self::TYPE_VIDEO, $this->accept_types)) {
175 $mimes[] = "video/vimeo";
176 $mimes[] = "video/mp4";
177 }
178 return $mimes;
179 }
180
184 public function executeCommand()
185 {
187
188 $next_class = $ctrl->getNextClass($this);
189 $cmd = $ctrl->getCmd("creationSelection");
190
191 switch ($next_class) {
192
193 case "ilpropertyformgui":
194 $form = $this->initPoolSelection();
195 $ctrl->forwardCommand($form);
196 break;
197
198 default:
199 if (in_array($cmd, ["creationSelection", "uploadFile", "saveUrl", "cancel", "listPoolItems",
200 "insertFromPool", "poolSelection", "selectPool", "applyFilter", "resetFilter"])) {
201 $this->$cmd();
202 }
203 }
204 }
205
209 protected function creationSelection()
210 {
212
213 $acc = new \ilAccordionGUI();
214 $acc->setBehaviour(\ilAccordionGUI::FIRST_OPEN);
215 $cnt = 1;
216 $forms = [
217 $this->initUploadForm(),
218 $this->initUrlForm(),
219 $this->initPoolSelection()
220 ];
221 foreach ($forms as $form_type => $cf) {
222 $htpl = new \ilTemplate("tpl.creation_acc_head.html", true, true, "Services/Object");
223
224 // using custom form titles (used for repository plugins)
225 $form_title = "";
226 if (method_exists($this, "getCreationFormTitle")) {
227 $form_title = $this->getCreationFormTitle($form_type);
228 }
229 if (!$form_title) {
230 $form_title = $cf->getTitle();
231 }
232
233 // move title from form to accordion
234 $htpl->setVariable("TITLE", $this->lng->txt("option") . " " . $cnt . ": " .
235 $form_title);
236 $cf->setTitle(null);
237 $cf->setTitleIcon(null);
238 $cf->setTableWidth("100%");
239
240 $acc->addItem($htpl->get(), $cf->getHTML());
241
242 $cnt++;
243 }
244 $main_tpl->setContent($acc->getHTML());
245 }
246
250 public function initUploadForm()
251 {
254
255 include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
256 $form = new \ilPropertyFormGUI();
257
258 $fi = new \ilFileInputGUI($lng->txt("file"), "file");
259 $fi->setSuffixes($this->getSuffixes());
260 $fi->setRequired(true);
261 $form->addItem($fi);
262
263 $form->addCommandButton("uploadFile", $lng->txt("upload"));
264 $form->addCommandButton("cancel", $lng->txt("cancel"));
265
266 $form->setTitle($lng->txt("mob_upload_file"));
267 $form->setFormAction($ctrl->getFormAction($this));
268
269 return $form;
270 }
271
275 public function initUrlForm()
276 {
279
280 include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
281 $form = new \ilPropertyFormGUI();
282
283 //
284 $ti = new \ilTextInputGUI($lng->txt("mob_url"), "url");
285 $ti->setInfo($lng->txt("mob_url_info"));
286 $ti->setRequired(true);
287 $form->addItem($ti);
288
289 $form->addCommandButton("saveUrl", $lng->txt("save"));
290 $form->addCommandButton("cancel", $lng->txt("cancel"));
291
292 $form->setTitle($lng->txt("mob_external_url"));
293 $form->setFormAction($ctrl->getFormAction($this));
294
295 return $form;
296 }
297
301 public function initPoolSelection()
302 {
305
306 include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
307 $form = new \ilPropertyFormGUI();
308
309 // mediacast
310 $mcst = new ilRepositorySelector2InputGUI($lng->txt("obj_mep"), "mep", false);
311 $exp = $mcst->getExplorerGUI();
312 $exp->setSelectableTypes(["mep"]);
313 $exp->setTypeWhiteList(["root", "mep", "cat", "crs", "grp", "fold"]);
314 $mcst->setRequired(true);
315 $form->addItem($mcst);
316
317 $form->addCommandButton("listPoolItems", $lng->txt("continue"));
318 $form->addCommandButton("cancel", $lng->txt("cancel"));
319
320 $form->setTitle($lng->txt("mob_choose_from_pool"));
321 $form->setFormAction($ctrl->getFormAction($this));
322
323 return $form;
324 }
325
329 protected function uploadFile()
330 {
331 $form = $this->initUploadForm();
332
333 if (!$form->checkInput()) {
334 $form->setValuesByPost();
335 $this->main_tpl->setContent($form->getHTML());
336 //$this->creationSelection();
337 } else {
338 $mob = new ilObjMediaObject();
339 $mob->create();
340
341 //handle standard purpose
342 $mediaItem = new ilMediaItem();
343 $mob->addMediaItem($mediaItem);
344 $mediaItem->setPurpose("Standard");
345
346 // determine and create mob directory, move uploaded file to directory
347 $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
348 if (!is_dir($mob_dir)) {
349 $mob->createDirectory();
350 }
351 $file_name = ilUtil::getASCIIFilename($_FILES['file']["name"]);
352 $file_name = str_replace(" ", "_", $file_name);
353
354 $file = $mob_dir . "/" . $file_name;
355 $title = $file_name;
356 $locationType = "LocalFile";
357 $location = $title;
358 ilUtil::moveUploadedFile($_FILES['file']['tmp_name'], $file_name, $file);
360
361 // get mime type, if not already set!
363
364 // set real meta and object data
365 $mediaItem->setFormat($format);
366 $mediaItem->setLocation($location);
367 $mediaItem->setLocationType($locationType);
368 $mediaItem->setHAlign("Left");
369 $mob->setTitle($title);
370 $mob->setDescription($format);
371
372 $mob->update();
373
374 $mob = new ilObjMediaObject($mob->getId());
375 $mob->generatePreviewPic(320, 240);
376
377 //
378 // @todo: save usage
379 //
380
381 ($this->after_upload)($mob->getId());
382 }
383 }
384
388 protected function cancel()
389 {
391 $ctrl->returnToParent($this);
392 }
393
397 protected function saveUrl()
398 {
399 $form = $this->initUrlForm();
400
401 if (!$form->checkInput()) {
402 $form->setValuesByPost();
403 $this->main_tpl->setContent($form->getHTML());
404 } else {
405 $mob = new ilObjMediaObject();
406 $mob->create();
407
408 //handle standard purpose
409 $mediaItem = new ilMediaItem();
410 $mob->addMediaItem($mediaItem);
411 $mediaItem->setPurpose("Standard");
412
413 // determine and create mob directory, move uploaded file to directory
414 $mob_dir = ilObjMediaObject::_getDirectory($mob->getId());
415 if (!is_dir($mob_dir)) {
416 $mob->createDirectory();
417 }
418 $locationType = "Reference";
419 $url = $form->getInput("url");
420 $title = $url;
421
422 // get mime type, if not already set!
424
425 // set real meta and object data
426 $mediaItem->setFormat($format);
427 $mediaItem->setLocation($url);
428 $mediaItem->setLocationType("Reference");
429 $mediaItem->setHAlign("Left");
430 $mob->setTitle($title);
431 $mob->setDescription($format);
432 /*try {
433 $mob->getExternalMetadata();
434 } catch (Exception $e) {
435 ilUtil::sendFailure($e->getMessage());
436 $form->setValuesByPost();
437 $this->main_tpl->setContent($form->getHTML());
438 return;
439 }*/
440
441 $long_desc = $mob->getLongDescription();
442 $mob->update();
443
444 //
445 // @todo: save usage
446 //
447
448 ($this->after_url_saving)($mob->getId(), $long_desc);
449 }
450 }
451
455 public function listPoolItems()
456 {
460 $ui = $this->ui;
462
463 if ($this->requested_mep > 0 &&
464 $access->checkAccess("write", "", $this->requested_mep)
465 && ilObject::_lookupType(ilObject::_lookupObjId($this->requested_mep)) == "mep") {
466 $tb = new ilToolbarGUI();
467
468 // button: select pool
469 $tb->addButton(
470 $lng->txt("cont_switch_to_media_pool"),
471 $ctrl->getLinkTarget($this, "poolSelection")
472 );
473
474 // view mode: pool view (folders/all media objects)
475 $f = $ui->factory();
476 $lng->loadLanguageModule("mep");
477 $ctrl->setParameter($this, "pool_view", self::POOL_VIEW_FOLDER);
478 $actions[$lng->txt("folders")] = $ctrl->getLinkTarget($this, "listPoolItems");
479 $ctrl->setParameter($this, "pool_view", self::POOL_VIEW_ALL);
480 $actions[$lng->txt("mep_all_mobs")] = $ctrl->getLinkTarget($this, "listPoolItems");
481 $ctrl->setParameter($this, "pool_view", $this->pool_view);
482 $aria_label = $lng->txt("cont_change_pool_view");
483 $view_control = $f->viewControl()->mode($actions, $aria_label)->withActive(($this->pool_view == self::POOL_VIEW_FOLDER)
484 ? $lng->txt("folders") : $lng->txt("mep_all_mobs"));
485 $tb->addSeparator();
486 $tb->addComponent($view_control);
487
488 $html = $tb->getHTML();
489
490 $pool_table = $this->getPoolTable();
491
492 $html .= $pool_table->getHTML();
493
494 $main_tpl->setContent($html);
495 }
496 }
497
501 protected function applyFilter()
502 {
503 $mpool_table = $this->getPoolTable();
504 $mpool_table->resetOffset();
505 $mpool_table->writeFilterToSession();
506 $this->ctrl->redirect($this, "listPoolItems");
507 }
508
512 protected function resetFilter()
513 {
514 $mpool_table = $this->getPoolTable();
515 $mpool_table->resetOffset();
516 $mpool_table->resetFilter();
517 $this->ctrl->redirect($this, "listPoolItems");
518 }
519
525 protected function getPoolTable()
526 {
527 $pool = new ilObjMediaPool($this->requested_mep);
528 $mpool_table = new ilMediaPoolTableGUI(
529 $this,
530 "listPoolItems",
531 $pool,
532 "mep_folder",
534 $this->pool_view == self::POOL_VIEW_ALL
535 );
536 $mpool_table->setFilterCommand("applyFilter");
537 $mpool_table->setResetCommand("resetFilter");
538 $mpool_table->setInsertCommand("insertFromPool");
539 return $mpool_table;
540 }
541
545 public function selectPool()
546 {
548
549 $ctrl->setParameter($this, "mep", $_GET["mep_ref_id"]);
550 $ctrl->redirect($this, "listPoolItems");
551 }
552
556 public function poolSelection()
557 {
559 $exp = new ilPoolSelectorGUI(
560 $this,
561 "poolSelection",
562 null,
563 "selectPool",
564 "",
565 "mep_ref_id"
566 );
567 $exp->setTypeWhiteList(array("root", "cat", "grp", "fold", "crs", "mep"));
568 $exp->setClickableTypes(array('mep'));
569 if (!$exp->handleCommand()) {
570 $main_tpl->setContent($exp->getHTML());
571 }
572 }
573
577 protected function insertFromPool()
578 {
579 if (!is_array($_POST["id"])) {
580 ilUtil::sendFailure($this->lng->txt("select_one"));
581 $this->listPoolItems();
582 return;
583 }
584 $mob_ids = [];
585 foreach ($_POST["id"] as $pool_entry_id) {
586 $id = ilMediaPoolItem::lookupForeignId($pool_entry_id);
587 $mob = new ilObjMediaObject((int) $id);
588 if (!in_array($mob->getMediaItem("Standard")->getFormat(), $this->getMimeTypes())) {
589 ilUtil::sendFailure($this->lng->txt("mob_mime_type_not_allowed") . ": " .
590 $mob->getMediaItem("Standard")->getFormat());
591 $this->listPoolItems();
592 return;
593 }
594 $mob_ids[] = $id;
595 }
596 ($this->after_pool_insert)($mob_ids);
597 }
598}
$location
Definition: buildRTE.php:44
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
listPoolItems()
Insert media object from pool.
creationSelection()
Creation selection.
selectPool()
Select concrete pool.
setAllSuffixes($a_val)
Set All suffixes.
setAllMimeTypes($a_val)
Set All mime types.
__construct(array $accept_types, closure $after_upload, closure $after_url_saving, closure $after_pool_insert)
Constructor.
initUploadForm()
Init upload form.
getAllMimeTypes()
Get All mime types.
getAllSuffixes()
Get All suffixes.
initPoolSelection()
Init pool selectio form.
executeCommand()
Execute command.
insertFromPool()
Insert media from pool.
Class ilMediaItem Media Item, component of a media object (file or reference)
static lookupForeignId($a_id)
Lookup Foreign Id.
TableGUI class for recent changes in wiki.
Class ilObjMediaObject.
static getMimeType($a_file, $a_external=null)
get mime type for file
static _getDirectory($a_mob_id)
Get absolute directory.
Media pool object.
static _lookupObjId($a_id)
static _lookupType($a_id, $a_reference=false)
lookup object type
Select media pool for adding objects into pages.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static getASCIIFilename($a_filename)
convert utf8 to ascii filename
static renameExecutables($a_dir)
Rename uploaded executables for security reasons.
global $DIC
Definition: goto.php:24
$format
Definition: metadata.php:218
$url
ui()
Definition: ui.php:5