ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilPCFileListGUI.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  protected string $requested_file_ref_id;
28  protected ilObjUser $user;
29  protected ilTabsGUI $tabs;
30  protected ilTree $tree;
32  protected ilSetting $settings;
33 
34  public function __construct(
35  ilPageObject $a_pg_obj,
36  ?ilPageContent $a_content_obj,
37  string $a_hier_id,
38  string $a_pc_id = ""
39  ) {
40  global $DIC;
41 
42  $this->user = $DIC->user();
43  $this->tabs = $DIC->tabs();
44  $this->ctrl = $DIC->ctrl();
45  $this->tpl = $DIC["tpl"];
46  $this->tree = $DIC->repositoryTree();
47  $this->lng = $DIC->language();
48  $this->toolbar = $DIC->toolbar();
49  $this->settings = $DIC->settings();
50  parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
51  $this->setCharacteristics(array("FileListItem" => $this->lng->txt("cont_FileListItem")));
52  $this->requested_file_ref_id = $this->request->getString("file_ref_id");
53  }
54 
58  public function executeCommand(): void
59  {
60  // get next class that processes or forwards current command
61  $next_class = $this->ctrl->getNextClass($this);
62 
63  $this->getCharacteristicsOfCurrentStyle(["flist_li"]); // scorm-2004
64 
65  // get current command
66  $cmd = $this->ctrl->getCmd();
67 
68  switch ($next_class) {
69  default:
70  $this->$cmd();
71  break;
72  }
73  }
74 
78  public function insert(ilPropertyFormGUI $a_form = null): void
79  {
80  $ilTabs = $this->tabs;
82 
83  if (in_array($sub_command, ["insertNew", "insertFromRepository", "insertFromWorkspace"])) {
84  $this->edit_repo->setSubCmd($sub_command);
85  }
86 
87  if (($sub_command == "") && $this->edit_repo->getSubCmd() != "") {
88  $sub_command = $this->edit_repo->getSubCmd();
89  }
90 
91  switch ($sub_command) {
92  case "insertFromWorkspace":
93  $this->insertFromWorkspace();
94  break;
95 
96  case "insertFromRepository":
97  $this->insertFromRepository();
98  break;
99 
100  case "selectFile":
101  $this->selectFile();
102  break;
103 
104  default:
105  $this->setTabs();
106  $ilTabs->setSubTabActive("cont_new_file");
107 
108  $this->displayValidationError();
109 
110  if ($a_form != null) {
111  $form = $a_form;
112  } else {
113  $form = $this->initEditForm("create");
114  }
115  $this->tpl->setContent($form->getHTML());
116  break;
117  }
118  }
119 
120  public function selectFile(): void
121  {
122  $ilTabs = $this->tabs;
123  $this->setTabs();
124  $ilTabs->setSubTabActive("cont_file_from_repository");
125 
126  $this->displayValidationError();
127  $form = $this->initEditForm("select_file");
128 
129  $this->tpl->setContent($form->getHTML());
130  }
131 
135  public function insertFromRepository(string $a_cmd = "insert"): void
136  {
137  $ilTabs = $this->tabs;
138  $ilCtrl = $this->ctrl;
139  $tpl = $this->tpl;
140 
141  if ($a_cmd == "insert") {
142  $this->setTabs();
143  } else {
144  $this->setItemTabs($a_cmd);
145  }
146 
147  $ilTabs->setSubTabActive("cont_file_from_repository");
148  $ilCtrl->setParameter($this, "subCmd", "insertFromRepository");
149 
150  $exp = new ilPCFileItemFileSelectorGUI(
151  $this,
152  $a_cmd,
153  $this,
154  $a_cmd,
155  "file_ref_id"
156  );
157  if (!$exp->handleCommand()) {
158  $tpl->setContent($exp->getHTML());
159  }
160  }
161 
165  public function insertFromWorkspace(
166  string $a_cmd = "insert"
167  ): void {
168  $ilTabs = $this->tabs;
169  $ilCtrl = $this->ctrl;
170  $tpl = $this->tpl;
171  $ilUser = $this->user;
172 
173  if ($a_cmd == "insert") {
174  $this->setTabs();
175  } else {
176  $this->setItemTabs($a_cmd);
177  }
178 
179  $ilTabs->setSubTabActive("cont_file_from_workspace");
180 
181  $exp = new ilWorkspaceExplorerGUI($ilUser->getId(), $this, $a_cmd, $this, $a_cmd, "fl_wsp_id");
182  $ilCtrl->setParameter($this, "subCmd", "selectFile");
183  $exp->setCustomLinkTarget($ilCtrl->getLinkTarget($this, $a_cmd));
184  $ilCtrl->setParameter($this, "subCmd", "insertFromWorkspace");
185  $exp->setTypeWhiteList(array("wsrt", "wfld", "file"));
186  $exp->setSelectableTypes(array("file"));
187  if ($exp->handleCommand()) {
188  return;
189  }
190  $tpl->setContent($exp->getHTML());
191  }
192 
196  public function create(): void
197  {
198  global $DIC;
199 
200  $mode = ($this->requested_file_ref_id != "")
201  ? "select_file"
202  : "create";
203  $form = $this->initEditForm($mode);
204  if (!$form->checkInput()) {
205  $form->setValuesByPost();
206  $this->insert($form);
207  return;
208  }
209 
210  // from personal workspace
211  if (substr($this->requested_file_ref_id, 0, 4) == "wsp_") {
212  $fileObj = new ilObjFile(substr($this->requested_file_ref_id, 4), false);
213  }
214  // upload
215  elseif ($this->requested_file_ref_id == "") {
216  $fileObj = new ilObjFile();
217  $fileObj->setType("file");
218  $fileObj->setTitle($_FILES["file"]["name"]);
219  $fileObj->setDescription("");
220  $fileObj->setFileName($_FILES["file"]["name"]);
221  $fileObj->setMode("filelist");
222  $fileObj->create();
223  // upload file to filesystem
224 
225  $upload = $DIC->upload();
226  if ($upload->hasBeenProcessed() !== true) {
227  $upload->process();
228  }
229 
230  $fileObj->getUploadFile(
231  $_FILES["file"]["tmp_name"],
232  $_FILES["file"]["name"]
233  );
234  }
235  // from repository
236  else {
237  $fileObj = new ilObjFile($this->requested_file_ref_id);
238  }
239  $this->setCurrentTextLang($form->getInput("flst_language"));
240 
241  //echo "::".is_object($this->dom).":";
242  $this->content_obj = new ilPCFileList($this->getPage());
243  $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
244  $this->content_obj->setListTitle(
245  $form->getInput("flst_title"),
246  $form->getInput("flst_language")
247  );
248  $this->content_obj->appendItem(
249  $fileObj->getId(),
250  $fileObj->getFileName(),
251  $fileObj->getFileType()
252  );
253 
254  $this->updated = $this->pg_obj->update();
255  if ($this->updated === true) {
256  $this->afterCreation();
257  } else {
258  $this->insert();
259  }
260  }
261 
262  public function afterCreation(): void
263  {
264  $this->pg_obj->stripHierIDs();
265  $this->pg_obj->addHierIDs();
266  $this->ctrl->setParameter($this, "hier_id", $this->content_obj->readHierId());
267  $this->ctrl->setParameter($this, "pc_id", $this->content_obj->readPCId());
268  $this->ctrl->redirect($this, "editFiles");
269  }
270 
271  public function edit(): void
272  {
273  $this->editFiles();
274  }
275 
279  public function editProperties(): void
280  {
281  $this->setTabs(false);
282 
283  $form = $this->initEditForm();
284  $this->tpl->setContent($form->getHTML());
285  }
286 
290  public function initEditForm(string $a_mode = "edit"): ilPropertyFormGUI
291  {
292  $lng = $this->lng;
293  $ilCtrl = $this->ctrl;
294  $ilUser = $this->user;
295 
296  $ti = null;
297  $si = null;
298  $form = new ilPropertyFormGUI();
299 
300  if ($a_mode != "add_file") {
301  // title
302  $ti = new ilTextInputGUI($lng->txt("title"), "flst_title");
303  $ti->setMaxLength(80);
304  $ti->setSize(40);
305  $form->addItem($ti);
306 
307  // language
309  $si = new ilSelectInputGUI($lng->txt("language"), "flst_language");
310  $si->setOptions($lang);
311  $form->addItem($si);
312  }
313 
314  if (in_array($a_mode, array("create", "add_file"))) {
315  // file
316  $fi = new ilFileInputGUI($lng->txt("file"), "file");
317  $fi->setRequired(true);
318  $form->addItem($fi);
319  } elseif (in_array($a_mode, array("select_file"))) {
320  // file
321  $ne = new ilNonEditableValueGUI($lng->txt("file"), "");
322 
323  $file_ref_id = (int) $this->requested_file_ref_id;
324  $fl_wsp_id = $this->request->getInt("fl_wsp_id");
325 
326  if ($file_ref_id > 0) {
327  $file_obj = new ilObjFile($file_ref_id);
328  if (is_object($file_obj)) {
329  // ref id as hidden input
330  $hi = new ilHiddenInputGUI("file_ref_id");
331  $hi->setValue($file_ref_id);
332  $form->addItem($hi);
333 
334  $ne->setValue($file_obj->getTitle());
335  }
336  } elseif ($fl_wsp_id > 0) {
337  // we need the object id for the instance
338  $tree = new ilWorkspaceTree($ilUser->getId());
339  $node = $tree->getNodeData($fl_wsp_id);
340 
341  $file_obj = new ilObjFile($node["obj_id"], false);
342  if (is_object($file_obj)) {
343  // ref id as hidden input
344  $hi = new ilHiddenInputGUI("file_ref_id");
345  $hi->setValue("wsp_" . (int) $node["obj_id"]);
346  $form->addItem($hi);
347 
348  $ne->setValue($file_obj->getTitle());
349  }
350  $this->tpl->parseCurrentBlock();
351  }
352 
353  $form->addItem($ne);
354  }
355 
356 
357  switch ($a_mode) {
358  case "edit":
359  $ti->setValue($this->content_obj->getListTitle());
360  $si->setValue($this->content_obj->getLanguage());
361  $form->addCommandButton("saveProperties", $lng->txt("save"));
362  $form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
363  $form->setTitle($lng->txt("cont_edit_file_list_properties"));
364  break;
365 
366  case "create":
367  case "select_file":
368  if ($this->getCurrentTextLang() != "") {
369  $s_lang = $this->getCurrentTextLang();
370  } else {
371  $s_lang = $ilUser->getLanguage();
372  }
373  $si->setValue($s_lang);
374  $form->addCommandButton("create_flst", $lng->txt("save"));
375  $form->addCommandButton("cancelCreate", $lng->txt("cancel"));
376  $form->setTitle($lng->txt("cont_insert_file_list"));
377  break;
378 
379  case "add_file":
380  $form->addCommandButton("insertNewFileItem", $lng->txt("save"));
381  $form->addCommandButton("editFiles", $lng->txt("cancel"));
382  $form->setTitle($lng->txt("cont_insert_file_item"));
383  break;
384  }
385 
386  $form->setFormAction($ilCtrl->getFormAction($this));
387 
388  return $form;
389  }
390 
391 
395  public function saveProperties(): void
396  {
397  $form = $this->initEditForm("edit");
398  $form->checkInput();
399  $this->content_obj->setListTitle(
400  $form->getInput("flst_title"),
401  $form->getInput("flst_language")
402  );
403  $this->updated = $this->pg_obj->update();
404  if ($this->updated === true) {
405  $this->tpl->setOnScreenMessage('success', $this->lng->txt("msg_obj_modified"), true);
406  $this->ctrl->redirect($this, "editProperties");
407  } else {
408  $this->pg_obj->addHierIDs();
409  $this->edit();
410  }
411  }
412 
416  public function editFiles(): void
417  {
418  $tpl = $this->tpl;
419  $ilToolbar = $this->toolbar;
420  $ilCtrl = $this->ctrl;
421  $lng = $this->lng;
422 
423  $this->setTabs(false);
424  $this->tabs->activateTab("cont_ed_edit_files");
425 
426  $ilToolbar->addButton(
427  $lng->txt("cont_add_file"),
428  $ilCtrl->getLinkTarget($this, "addFileItem")
429  );
430 
432  $fl = $this->content_obj;
433  $table_gui = new ilPCFileListTableGUI($this, "editFiles", $fl);
434  $tpl->setContent($table_gui->getHTML());
435  }
436 
440  public function setTabs(bool $a_create = true): void
441  {
442  $ilTabs = $this->tabs;
443  $ilCtrl = $this->ctrl;
444  $lng = $this->lng;
446 
447  if ($a_create) {
448  $cmd = "insert";
449 
450  $ilCtrl->setParameter($this, "subCmd", "insertNew");
451  $ilTabs->addSubTabTarget(
452  "cont_new_file",
453  $ilCtrl->getLinkTarget($this, $cmd),
454  $cmd
455  );
456 
457  $ilCtrl->setParameter($this, "subCmd", "insertFromRepository");
458  $ilTabs->addSubTabTarget(
459  "cont_file_from_repository",
460  $ilCtrl->getLinkTarget($this, $cmd),
461  $cmd
462  );
463  $ilCtrl->setParameter($this, "subCmd", "");
464 
465  if (!$ilSetting->get("disable_personal_workspace") &&
466  !$ilSetting->get("disable_wsp_files")) {
467  $ilCtrl->setParameter($this, "subCmd", "insertFromWorkspace");
468  $ilTabs->addSubTabTarget(
469  "cont_file_from_workspace",
470  $ilCtrl->getLinkTarget($this, $cmd),
471  $cmd
472  );
473  $ilCtrl->setParameter($this, "subCmd", "");
474  }
475  } else {
476  $ilTabs->setBackTarget(
477  $lng->txt("pg"),
478  $this->ctrl->getParentReturn($this)
479  );
480 
481  $ilTabs->addTarget(
482  "cont_ed_edit_files",
483  $ilCtrl->getLinkTarget($this, "editFiles"),
484  "editFiles",
485  get_class($this)
486  );
487 
488  $ilTabs->addTarget(
489  "cont_ed_edit_prop",
490  $ilCtrl->getLinkTarget($this, "editProperties"),
491  "editProperties",
492  get_class($this)
493  );
494  }
495  }
496 
501  public function addFileItem(): void
502  {
503  $ilCtrl = $this->ctrl;
504 
505  $files = $this->content_obj->getFileList();
506 
507  if (count($files) >= 1) {
508  $ilCtrl->setParameterByClass(
509  "ilpcfileitemgui",
510  "hier_id",
511  $files[count($files) - 1]["hier_id"]
512  );
513  $ilCtrl->setParameterByClass(
514  "ilpcfileitemgui",
515  "pc_id",
516  $files[count($files) - 1]["pc_id"]
517  );
518  $ilCtrl->redirectByClass("ilpcfileitemgui", "newItemAfter");
519  } else {
520  $ilCtrl->redirect($this, "newFileItem");
521  }
522  }
523 
527  public function deleteFileItem(): void
528  {
529  $ilCtrl = $this->ctrl;
530 
531  $fid = $this->request->getIntArray("fid");
532  if (count($fid) > 0) {
533  $this->content_obj->deleteFileItems(array_keys($fid));
534  }
535  $this->updated = $this->pg_obj->update();
536  $ilCtrl->redirect($this, "editFiles");
537  }
538 
542  public function savePositions(): void
543  {
544  $ilCtrl = $this->ctrl;
545 
546  $pos = $this->request->getIntArray("position");
547  if (count($pos) > 0) {
548  $this->content_obj->savePositions($pos);
549  }
550  $this->updated = $this->pg_obj->update();
551  $ilCtrl->redirect($this, "editFiles");
552  }
553 
557  public function savePositionsAndClasses(): void
558  {
559  $ilCtrl = $this->ctrl;
560 
561  $pos = $this->request->getIntArray("position");
562  $class = $this->request->getStringArray("class");
563  if (count($pos) > 0) {
564  $this->content_obj->savePositions($pos);
565  }
566  if (count($class) > 0) {
567  $this->content_obj->saveStyleClasses($class);
568  }
569  $this->updated = $this->pg_obj->update();
570  $ilCtrl->redirect($this, "editFiles");
571  }
572 
576  public function checkStyleSelection(): bool
577  {
578  // check whether there is more than one style class
579  $chars = $this->getCharacteristics();
580 
581  $classes = $this->content_obj->getAllClasses();
582  if (count($chars) > 1) {
583  return true;
584  }
585  foreach ($classes as $class) {
586  if ($class != "" && $class != "FileListItem") {
587  return true;
588  }
589  }
590  return false;
591  }
592 
593  //
594  //
595  // New file item
596  //
597  //
598 
602  public function newFileItem(): void
603  {
604  $ilTabs = $this->tabs;
605 
607  if (in_array($sub_command, ["insertNew", "insertFromRepository", "insertFromWorkspace"])) {
608  $this->edit_repo->setSubCmd($sub_command);
609  }
610 
611  if (($sub_command == "") && $this->edit_repo->getSubCmd() != "") {
612  $sub_command = $this->edit_repo->getSubCmd();
613  }
614 
615  switch ($sub_command) {
616  case "insertFromWorkspace":
617  $this->insertFromWorkspace("newFileItem");
618  break;
619 
620  case "insertFromRepository":
621  $this->insertFromRepository("newFileItem");
622  break;
623 
624  case "selectFile":
625  $this->insertNewFileItem($this->requested_file_ref_id);
626  break;
627 
628  default:
629  $this->setItemTabs("newFileItem");
630  $ilTabs->setSubTabActive("cont_new_file");
631 
632  $this->displayValidationError();
633 
634  $form = $this->initEditForm("add_file");
635  $this->tpl->setContent($form->getHTML());
636  break;
637  }
638  }
639 
643  public function insertNewFileItem(int $a_file_ref_id = 0): void
644  {
645  $ilUser = $this->user;
646 
647  $fl_wsp_id = $this->request->getInt("fl_wsp_id");
648 
649  // from personal workspace
650  if ($fl_wsp_id > 0) {
651  // we need the object id for the instance
652  $tree = new ilWorkspaceTree($ilUser->getId());
653  $node = $tree->getNodeData($fl_wsp_id);
654 
655  $file_obj = new ilObjFile($node["obj_id"], false);
656  }
657  // upload
658  elseif ($a_file_ref_id == 0) {
659  $file_obj = $this->createFileItem();
660  }
661  // from repository
662  else {
663  $file_obj = new ilObjFile($a_file_ref_id);
664  }
665  if (is_object($file_obj)) {
666  $this->content_obj->appendItem(
667  $file_obj->getId(),
668  $file_obj->getTitle(),
669  $file_obj->getFileType()
670  );
671  $this->updated = $this->pg_obj->update();
672  if ($this->updated === true) {
673  //$this->ctrl->returnToParent($this, "jump".$this->hier_id);
674  $this->ctrl->redirect($this, "editFiles");
675  }
676  }
677 
678  $this->newFileItem();
679  }
680 
684  public function createFileItem(): ?ilObjFile
685  {
686  global $DIC;
687 
688  $lng = $this->lng;
689 
690  if ($_FILES["file"]["name"] == "") {
691  throw new ilCOPageFileHandlingException($lng->txt("upload_error_file_not_found"));
692  }
693 
694  $form = $this->initEditForm();
695  // see #22541
696  // $form->checkInput();
697 
698  $fileObj = new ilObjFile();
699  $fileObj->setType("file");
700  $fileObj->setTitle($_FILES["file"]["name"]);
701  $fileObj->setDescription("");
702  $fileObj->setFileName($_FILES["file"]["name"]);
703  $fileObj->setMode("filelist");
704  $fileObj->create();
705  // upload file to filesystem
706 
707  $upload = $DIC->upload();
708  if ($upload->hasBeenProcessed() !== true) {
709  $upload->process();
710  }
711 
712  $fileObj->getUploadFile(
713  $_FILES["file"]["tmp_name"],
714  $_FILES["file"]["name"]
715  );
716 
717  return $fileObj;
718  }
719 
720 
724  public function setItemTabs(string $a_cmd = ""): void
725  {
726  $ilTabs = $this->tabs;
727  $ilCtrl = $this->ctrl;
729 
730  $ilTabs->addTarget(
731  "cont_back",
732  $this->ctrl->getParentReturn($this),
733  "",
734  ""
735  );
736 
737  if ($a_cmd != "") {
738  $ilCtrl->setParameter($this, "subCmd", "insertNew");
739  $ilTabs->addSubTabTarget(
740  "cont_new_file",
741  $ilCtrl->getLinkTarget($this, $a_cmd),
742  $a_cmd
743  );
744 
745  $ilCtrl->setParameter($this, "subCmd", "insertFromRepository");
746  $ilTabs->addSubTabTarget(
747  "cont_file_from_repository",
748  $ilCtrl->getLinkTarget($this, $a_cmd),
749  $a_cmd
750  );
751  $ilCtrl->setParameter($this, "subCmd", "");
752 
753  if (!$ilSetting->get("disable_personal_workspace") &&
754  !$ilSetting->get("disable_wsp_files")) {
755  $ilCtrl->setParameter($this, "subCmd", "insertFromWorkspace");
756  $ilTabs->addSubTabTarget(
757  "cont_file_from_workspace",
758  $ilCtrl->getLinkTarget($this, $a_cmd),
759  $a_cmd
760  );
761  $ilCtrl->setParameter($this, "subCmd", "");
762  }
763  }
764  }
765 }
newFileItem()
New file item (called, if there is no file item in an existing)
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
getCharacteristicsOfCurrentStyle(array $a_type)
Get characteristics of current style and call setCharacteristics, if style is given.
This class represents a selection list property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
redirect(object $a_gui_obj, string $a_cmd=null, string $a_anchor=null, bool $is_async=false)
__construct(ilPageObject $a_pg_obj, ?ilPageContent $a_content_obj, string $a_hier_id, string $a_pc_id="")
createFileItem()
insert new file item
setTabs(bool $a_create=true)
Set Tabs.
deleteFileItem()
Delete file items from list.
This class represents a file property in a property form.
insertFromRepository(string $a_cmd="insert")
Insert file from repository.
insertFromWorkspace(string $a_cmd="insert")
Insert file from personal workspace.
setParameterByClass(string $a_class, string $a_parameter, $a_value)
setOptions(array $a_options)
setCharacteristics(array $a_chars)
Content object of ilPageObject (see ILIAS DTD).
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
editProperties()
edit properties form
global $DIC
Definition: feed.php:28
insertNewFileItem(int $a_file_ref_id=0)
insert new file item after another item
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
User Interface for Editing of Page Content Objects (Paragraphs, Tables, ...)
executeCommand()
execute command
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(VocabulariesInterface $vocabularies)
setContent(string $a_html)
Sets content for standard template.
Class ilPageObject Handles PageObjects of ILIAS Learning Modules (see ILIAS DTD)
Class ilObjFile.
Explorer for selecting a personal workspace item.
ilGlobalTemplateInterface $tpl
setItemTabs(string $a_cmd="")
output tabs
setCurrentTextLang(string $lang_key)
setRequired(bool $a_required)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lang
Definition: xapiexit.php:26
checkStyleSelection()
Checks whether style selection shoudl be available or not.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $ilSetting
Definition: privfeed.php:18
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initEditForm(string $a_mode="edit")
Init edit form.
create()
create new file list in dom and update page in db
insert(ilPropertyFormGUI $a_form=null)
insert new file list form
saveProperties()
save table properties in db and return to page edit screen
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
savePositionsAndClasses()
Save positions of file items and style classes.
addFileItem()
Add file item.
savePositions()
Save positions of file items.