ILIAS  release_8 Revision v8.24
class.ilPropertyFormGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\HTTP;
23
31{
32 private array $kept_uploads = [];
33 protected bool $required_text = false;
34 protected ilLanguage $lng;
35 protected ilCtrl $ctrl;
36 protected ilTemplate $tpl;
37 protected ?ilObjUser $user = null;
38 protected ?ilSetting $settings = null;
39 private array $buttons = array();
40 private array $items = array();
41 protected string $mode = "std";
42 protected bool $check_input_called = false;
43 protected bool $disable_standard_message = false;
44 protected string $top_anchor = "il_form_top";
45 protected string $title = '';
46 protected string $titleicon = "";
47 protected string $description = "";
48 protected string $tbl_width = "";
49 protected bool $show_top_buttons = true;
50 protected bool $hide_labels = false;
51 protected bool $force_top_buttons = false;
53 protected ?Refinery\Factory $refinery = null;
54
56 protected $onload_code = [];
57
58 public function __construct()
59 {
60 global $DIC;
61
62 $this->lng = $DIC->language();
63 $this->ctrl = $DIC->ctrl();
64
65 $this->user = null;
66 if (isset($DIC["ilUser"])) {
67 $this->user = $DIC["ilUser"];
68 }
69
70 $this->settings = null;
71 if (isset($DIC["ilSetting"])) {
72 $this->settings = $DIC["ilSetting"];
73 }
74
75 $lng = $DIC->language();
76
77 $lng->loadLanguageModule("form");
78
79 // avoid double submission
80 $this->setPreventDoubleSubmission(true);
81
82 // do it as early as possible
83 if (isset($DIC["http"])) {
84 $this->http = $DIC->http();
85 }
86 if (isset($DIC["refinery"])) {
87 $this->refinery = $DIC->refinery();
88 }
89 $this->rebuildUploadedFiles();
90 if (isset($DIC["tpl"])) { // some unit tests will fail otherwise
91 $this->global_tpl = $DIC['tpl'];
92 }
93 }
94
99 public function executeCommand()
100 {
101 $ilCtrl = $this->ctrl;
102
103 $next_class = $ilCtrl->getNextClass($this);
104
105 switch ($next_class) {
106 case 'ilformpropertydispatchgui':
107 $ilCtrl->saveParameter($this, 'postvar');
108 $form_prop_dispatch = new ilFormPropertyDispatchGUI();
109 $item = $this->getItemByPostVar($this->getRequestedPostVar());
110 $form_prop_dispatch->setItem($item);
111 return $ilCtrl->forwardCommand($form_prop_dispatch);
112 }
113 return false;
114 }
115
116 protected function getRequestedPostVar(): ?string
117 {
118 $t = $this->refinery->kindlyTo()->string();
119 $w = $this->http->wrapper();
120 if ($w->post()->has("postvar")) {
121 return $w->post()->retrieve("postvar", $t);
122 }
123 if ($w->query()->has("postvar")) {
124 return $w->query()->retrieve("postvar", $t);
125 }
126 return null;
127 }
128
129 final public function setTableWidth(string $a_width): void
130 {
131 $this->tbl_width = $a_width;
132 }
133
134 final public function getTableWidth(): string
135 {
136 return $this->tbl_width;
137 }
138
139 // Set Mode ('std', 'subform').
140 public function setMode(string $a_mode): void
141 {
142 $this->mode = $a_mode;
143 }
144
145 public function getMode(): string
146 {
147 return $this->mode;
148 }
149
150 public function setTitle(string $a_title): void
151 {
152 $this->title = $a_title;
153 }
154
155 public function getTitle(): string
156 {
157 return $this->title;
158 }
159
160 public function setTitleIcon(string $a_titleicon): void
161 {
162 $this->titleicon = $a_titleicon;
163 }
164
165 public function getTitleIcon(): string
166 {
167 return $this->titleicon;
168 }
169
170 public function setDescription(string $a_val): void
171 {
172 $this->description = $a_val;
173 }
174
175 public function getDescription(): string
176 {
177 return $this->description;
178 }
179
180 public function setTopAnchor(string $a_val): void
181 {
182 $this->top_anchor = $a_val;
183 }
184
185 public function getTopAnchor(): string
186 {
187 return $this->top_anchor;
188 }
189
190 public function setShowTopButtons(bool $a_val): void
191 {
192 $this->show_top_buttons = $a_val;
193 }
194
195 public function getShowTopButtons(): bool
196 {
198 }
199
200 public function setForceTopButtons(bool $a_val): void
201 {
202 $this->force_top_buttons = $a_val;
203 }
204
205 public function getForceTopButtons(): bool
206 {
208 }
209
213 public function addItem($a_item): void
214 {
215 $a_item->setParentForm($this);
216 $this->items[] = $a_item;
217 }
218
219 public function removeItemByPostVar(
220 string $a_post_var,
221 bool $a_remove_unused_headers = false
222 ): void {
223 foreach ($this->items as $key => $item) {
224 if (method_exists($item, "getPostVar") && $item->getPostVar() == $a_post_var) {
225 unset($this->items[$key]);
226 }
227 }
228
229 // remove section headers if they do not contain any items anymore
230 if ($a_remove_unused_headers) {
231 $unset_keys = array();
232 $last_item = null;
233 $last_key = null;
234 foreach ($this->items as $key => $item) {
235 if ($item instanceof ilFormSectionHeaderGUI && $last_item instanceof ilFormSectionHeaderGUI) {
236 $unset_keys[] = $last_key;
237 }
238 $last_item = $item;
239 $last_key = $key;
240 }
241 if ($last_item instanceof ilFormSectionHeaderGUI) {
242 $unset_keys[] = $last_key;
243 }
244 foreach ($unset_keys as $key) {
245 unset($this->items[$key]);
246 }
247 }
248 }
249
250 public function getItemByPostVar(string $a_post_var): ?ilFormPropertyGUI
251 {
252 foreach ($this->items as $item) {
253 if ($item->getType() != "section_header") {
254 //if ($item->getPostVar() == $a_post_var)
255 $ret = $item->getItemByPostVar($a_post_var);
256 if (is_object($ret)) {
257 return $ret;
258 }
259 }
260 }
261
262 return null;
263 }
264
265 public function setItems(array $a_items): void
266 {
267 $this->items = $a_items;
268 }
269
270 public function getItems(): array
271 {
272 return $this->items;
273 }
274
279 public function getInputItemsRecursive(): array
280 {
281 $inputItems = array();
282
283 foreach ($this->items as $item) {
284 if ($item->getType() == 'section_header') {
285 continue;
286 }
287
288 $inputItems[] = $item;
289
290 if ($item instanceof ilSubEnabledFormPropertyGUI) {
291 $inputItems = array_merge($inputItems, $item->getSubInputItemsRecursive());
292 }
293 }
294
295 return $inputItems;
296 }
297
298 public function setDisableStandardMessage(bool $a_val): void
299 {
300 $this->disable_standard_message = $a_val;
301 }
302
303 public function getDisableStandardMessage(): bool
304 {
305 return $this->disable_standard_message;
306 }
307
308 // Get a value indicating whether the labels should be hidden or not.
309 public function getHideLabels(): bool
310 {
311 return $this->hide_labels;
312 }
313
314 public function setHideLabels(bool $a_value = true): void
315 {
316 $this->hide_labels = $a_value;
317 }
318
319 public function setValuesByArray(
320 array $a_values,
321 bool $a_restrict_to_value_keys = false
322 ): void {
323 foreach ($this->items as $item) {
324 if (!($a_restrict_to_value_keys) ||
325 in_array($item->getPostVar(), array_keys($a_values))) {
326 $item->setValueByArray($a_values);
327 }
328 }
329 }
330
331 public function setValuesByPost()
332 {
333 global $DIC;
334
335 if (!isset($DIC["http"])) {
336 return null;
337 }
338
339 foreach ($this->items as $item) {
340 $item->setValueByArray($DIC->http()->request()->getParsedBody());
341 }
342 }
343
344 public function checkInput(): bool
345 {
346 global $DIC;
347
348 if ($this->check_input_called) {
349 die("Error: ilPropertyFormGUI->checkInput() called twice.");
350 }
351
352 $ok = true;
353 foreach ($this->items as $item) {
354 $item_ok = $item->checkInput();
355 if (!$item_ok) {
356 $ok = false;
357 }
358 }
359
360 // check if POST is missing completely (if post_max_size exceeded)
361 $post = $this->http->request()->getParsedBody();
362 if (count($this->items) > 0 && count($post) === 0) {
363 $ok = false;
364 }
365
366 $this->check_input_called = true;
367
368 // try to keep uploads for another try
369 $filehash = $this->getFileHash();
370 if (!$ok && !is_null($filehash) && $filehash && count($_FILES)) {
371 $hash = $filehash;
372
373 foreach ($_FILES as $field => $data) {
374 // only try to keep files that are ok
375 // see 25484: Wrong error handling when uploading icon instead of tile
376 $item = $this->getItemByPostVar($field);
377 if (is_null($item) || !$item->checkInput()) {
378 continue;
379 }
380 // we support up to 2 nesting levels (see test/assessment)
381 if (is_array($data["tmp_name"])) {
382 foreach ($data["tmp_name"] as $idx => $upload) {
383 if (is_array($upload)) {
384 foreach ($upload as $idx2 => $file) {
385 if ($file && is_uploaded_file($file)) {
386 $file_name = $data["name"][$idx][$idx2];
387 $file_type = $data["type"][$idx][$idx2];
388 $this->keepFileUpload($hash, $field, $file, $file_name, $file_type, (string) $idx, (string) $idx2);
389 }
390 }
391 } elseif ($upload && is_uploaded_file($upload)) {
392 $file_name = $data["name"][$idx];
393 $file_type = $data["type"][$idx];
394 $this->keepFileUpload($hash, $field, $upload, $file_name, $file_type, (string) $idx);
395 }
396 }
397 } else {
398 $this->keepFileUpload($hash, $field, $data["tmp_name"], $data["name"], $data["type"]);
399 }
400 }
401 }
402 $http = $DIC->http();
403 $txt = $DIC->language()->txt("form_input_not_valid");
404 switch ($http->request()->getHeaderLine('Accept')) {
405 // When JS asks for a valid JSON-Response, we send the success and message as JSON
406 case 'application/json':
407 $stream = \ILIAS\Filesystem\Stream\Streams::ofString(json_encode([
408 'success' => $ok,
409 'message' => $txt,
410 ]));
411 $http->saveResponse($http->response()->withBody($stream));
412
413 return $ok;
414
415 // Otherwise, we send it using ilUtil, and it will be rendered in the Template
416 default:
417
418 if (!$ok && !$this->getDisableStandardMessage()) {
419 $this->global_tpl->setOnScreenMessage('failure', $txt);
420 }
421
422 return $ok;
423 }
424 }
425
426 protected function getFileHash(): ?string
427 {
428 if (is_null($this->refinery)) {
429 return null;
430 }
431 // try to keep uploads for another try
432 $t = $this->refinery->kindlyTo()->string();
433 $w = $this->http->wrapper();
434 $filehash = null;
435 if ($w->post()->has("ilfilehash")) {
436 $filehash = $w->post()->retrieve("ilfilehash", $t);
437 }
438 return $filehash;
439 }
440
448 public function getInput(
449 string $a_post_var,
450 bool $ensureValidation = true
451 ) {
452 // this check ensures, that checkInput has been called (incl. stripSlashes())
453 if (!$this->check_input_called && $ensureValidation) {
454 throw new LogicException('Error: ilPropertyFormGUI->getInput() called without calling checkInput() first.');
455 }
456
457 $item = $this->getItemByPostVar($a_post_var);
458 if (is_object($item) && method_exists($item, "getInput")) {
459 return $item->getInput();
460 }
461
462 $post = $this->http->request()->getParsedBody();
463 return $post[$a_post_var] ?? '';
464 }
465
466 public function addCommandButton(
467 string $a_cmd,
468 string $a_text,
469 string $a_id = ""
470 ): void {
471 $this->buttons[] = array("cmd" => $a_cmd, "text" => $a_text, "id" => $a_id);
472 }
473
474
475 public function getCommandButtons(): array
476 {
477 return $this->buttons;
478 }
479
480 public function clearCommandButtons(): void
481 {
482 $this->buttons = array();
483 }
484
485 public function getContent(): string
486 {
487 global $DIC;
489 $tpl = $DIC["tpl"];
491
494
495 $tpl->addJavaScript("./Services/JavaScript/js/Basic.js");
496 $tpl->addJavaScript("Services/Form/js/Form.js");
497
498 $this->tpl = new ilTemplate("tpl.property_form.html", true, true, "Services/Form");
499
500 // check if form has not title and first item is a section header
501 // -> use section header for title and remove section header
502 // -> command buttons are presented on top
503 $fi = $this->items[0] ?? null;
504 if ($this->getMode() == "std" &&
505 $this->getTitle() == "" &&
506 is_object($fi) && $fi->getType() == "section_header"
507 ) {
508 $this->setTitle($fi->getTitle());
509 unset($this->items[0]);
510 }
511
512
513 // title icon
514 if ($this->getTitleIcon() != "" && is_file($this->getTitleIcon())) {
515 $this->tpl->setCurrentBlock("title_icon");
516 $this->tpl->setVariable("IMG_ICON", $this->getTitleIcon());
517 $this->tpl->parseCurrentBlock();
518 }
519
520 // title
521 if ($this->getTitle() != "") {
522 // commands on top
523 if (count($this->buttons) > 0 && $this->getShowTopButtons() && (count($this->items) > 2 || $this->force_top_buttons)) {
524 // command buttons
525 foreach ($this->buttons as $button) {
526 $this->tpl->setCurrentBlock("cmd2");
527 $this->tpl->setVariable("CMD", $button["cmd"]);
528 $this->tpl->setVariable("CMD_TXT", $button["text"]);
529 if ($button["id"] != "") {
530 $this->tpl->setVariable("CMD2_ID", " id='" . $button["id"] . "_top'");
531 }
532 $this->tpl->parseCurrentBlock();
533 }
534 $this->tpl->setCurrentBlock("commands2");
535 $this->tpl->parseCurrentBlock();
536 }
537
538 if (is_object($ilSetting)) {
539 if ($ilSetting->get('char_selector_availability') > 0) {
541 $char_selector = ilCharSelectorGUI::_getCurrentGUI();
542 if ($char_selector->getConfig()->getAvailability() == ilCharSelectorConfig::ENABLED) {
543 $char_selector->addToPage();
544 $this->tpl->touchBlock('char_selector');
545 }
546 }
547 }
548 }
549
550 $this->tpl->setCurrentBlock("header");
551 $this->tpl->setVariable("TXT_TITLE", $this->getTitle());
552 //$this->tpl->setVariable("LABEL", $this->getTopAnchor());
553 $this->tpl->setVariable("TXT_DESCRIPTION", $this->getDescription());
554 $this->tpl->parseCurrentBlock();
555 }
556 $this->tpl->touchBlock("item");
557
558 // properties
559 $this->required_text = false;
560 foreach ($this->items as $item) {
561 if ($item->getType() != "hidden") {
562 $this->insertItem($item);
563 }
564 }
565
566 // required
567 if ($this->required_text && $this->getMode() == "std") {
568 $this->tpl->setCurrentBlock("required_text");
569 $this->tpl->setVariable("TXT_REQUIRED", $lng->txt("required_field"));
570 $this->tpl->parseCurrentBlock();
571 }
572
573 // command buttons
574 foreach ($this->buttons as $button) {
575 $this->tpl->setCurrentBlock("cmd");
576 $this->tpl->setVariable("CMD", $button["cmd"]);
577 $this->tpl->setVariable("CMD_TXT", $button["text"]);
578
579 if ($button["id"] != "") {
580 $this->tpl->setVariable("CMD_ID", " id='" . $button["id"] . "'");
581 }
582
583 $this->tpl->parseCurrentBlock();
584 }
585
586 // #18808
587 if ($this->getMode() != "subform") {
588 // try to keep uploads even if checking input fails
589 if ($this->getMultipart()) {
590 $hash = $this->getFileHash() ?? null;
591 if (!$hash) {
592 $hash = md5(uniqid((string) mt_rand(), true));
593 }
594 $fhash = new ilHiddenInputGUI("ilfilehash");
595 $fhash->setValue($hash);
596 $this->addItem($fhash);
597 }
598 }
599
600 // hidden properties
601 $hidden_fields = false;
602 foreach ($this->items as $item) {
603 if ($item->getType() == "hidden") {
604 $item->insert($this->tpl);
605 $hidden_fields = true;
606 }
607 }
608
609 if ($this->required_text || count($this->buttons) > 0 || $hidden_fields) {
610 $this->tpl->setCurrentBlock("commands");
611 $this->tpl->parseCurrentBlock();
612 }
613
614
615 if ($this->getMode() == "subform") {
616 $this->tpl->touchBlock("sub_table");
617 } else {
618 $this->tpl->touchBlock("std_table");
619 $this->tpl->setVariable('STD_TABLE_WIDTH', $this->getTableWidth());
620 }
621
622 return $this->tpl->get();
623 }
624
625 protected function hideRequired(string $a_type): bool
626 {
627 // #15818
628 return $a_type == "non_editable_value";
629 }
630
634 public function insertItem(
635 $item,
636 bool $a_sub_item = false
637 ): void {
638 global $DIC;
639 $tpl = $DIC["tpl"];
641
642
643 //$cfg = array();
644
645 //if(method_exists($item, "getMulti") && $item->getMulti())
646 if ($item instanceof ilMultiValuesItem && $item->getMulti()) {
647 $tpl->addJavascript("./Services/Form/js/ServiceFormMulti.js");
648
649 $this->tpl->setCurrentBlock("multi_in");
650 $this->tpl->setVariable("ID", $item->getFieldId());
651 $this->tpl->parseCurrentBlock();
652
653 $this->tpl->touchBlock("multi_out");
654
655
656 // add hidden item to enable preset multi items
657 // not used yet, should replace hidden field stuff
658 $multi_values = $item->getMultiValues();
659 if (is_array($multi_values) && sizeof($multi_values) > 1) {
660 $multi_value = new ilHiddenInputGUI("ilMultiValues~" . $item->getPostVar());
661 $multi_value->setValue(implode("~", $multi_values));
662 $this->addItem($multi_value);
663 }
664 //$cfg["multi_values"] = $multi_values;
665 }
666
667 $item->insert($this->tpl);
668
669 if ($item->getType() == "file" || $item->getType() == "image_file") {
670 $this->setMultipart(true);
671 }
672
673 if ($item->getType() != "section_header") {
674 //$cfg["id"] = $item->getFieldId();
675
676 // info text
677 if ($item->getInfo() != "") {
678 $this->tpl->setCurrentBlock("description");
679 $this->tpl->setVariable(
680 "PROPERTY_DESCRIPTION",
681 $item->getInfo()
682 );
683 $this->tpl->setVariable(
684 "DESCRIPTION_FOR_ID",
685 $item->getFieldId()
686 );
687 $this->tpl->parseCurrentBlock();
688 }
689
690 if ($this->getMode() == "subform") {
691 // required
692 if (!$this->hideRequired($item->getType())) {
693 if ($item->getRequired()) {
694 $this->tpl->touchBlock("sub_required");
695 $this->required_text = true;
696 }
697 }
698
699 // hidden title (for accessibility, e.g. file upload)
700 if ($item->getHiddenTitle() != "") {
701 $this->tpl->setCurrentBlock("sub_hid_title");
702 $this->tpl->setVariable(
703 "SPHID_TITLE",
704 $item->getHiddenTitle()
705 );
706 $this->tpl->parseCurrentBlock();
707 }
708
709 $this->tpl->setCurrentBlock("sub_prop_start");
710 $this->tpl->setVariable("PROPERTY_TITLE", $item->getTitle());
711 $this->tpl->setVariable("PROPERTY_CLASS", "il_" . $item->getType());
712 if ($item->getType() != "non_editable_value" && $item->getFormLabelFor() != "") {
713 $this->tpl->setVariable("FOR_ID", ' for="' . $item->getFormLabelFor() . '" ');
714 }
715 $this->tpl->setVariable("LAB_ID", $item->getFieldId());
716 } else {
717 // required
718 if (!$this->hideRequired($item->getType())) {
719 if ($item->getRequired()) {
720 $this->tpl->touchBlock("required");
721 $this->required_text = true;
722 }
723 }
724
725 // hidden title (for accessibility, e.g. file upload)
726 if ($item->getHiddenTitle() != "") {
727 $this->tpl->setCurrentBlock("std_hid_title");
728 $this->tpl->setVariable(
729 "PHID_TITLE",
730 $item->getHiddenTitle()
731 );
732 $this->tpl->parseCurrentBlock();
733 }
734
735 $this->tpl->setCurrentBlock("std_prop_start");
736 $this->tpl->setVariable("PROPERTY_TITLE", $item->getTitle());
737 if ($item->getType() != "non_editable_value" && $item->getFormLabelFor() != "") {
738 $this->tpl->setVariable("FOR_ID", ' for="' . $item->getFormLabelFor() . '" ');
739 }
740 $this->tpl->setVariable("LAB_ID", $item->getFieldId());
741 if ($this->getHideLabels()) {
742 $this->tpl->setVariable("HIDE_LABELS_STYLE", " ilFormOptionHidden");
743 }
744 }
745 $this->tpl->parseCurrentBlock();
746
747 // alert
748 if ($item->getType() != "non_editable_value" && $item->getAlert() != "") {
749 $this->tpl->setCurrentBlock("alert");
750 $this->tpl->setVariable(
751 "IMG_ALERT",
752 ilUtil::getImagePath("icon_alert.svg")
753 );
754 $this->tpl->setVariable(
755 "ALT_ALERT",
756 $lng->txt("alert")
757 );
758 $this->tpl->setVariable(
759 "TXT_ALERT",
760 $item->getAlert()
761 );
762 $this->tpl->setVariable(
763 "ALERT_FOR_ID",
764 $item->getFieldId()
765 );
766 $this->tpl->parseCurrentBlock();
767 }
768
769 // subitems
770 $sf = null;
771 if ($item->getType() != "non_editable_value" or 1) {
772 $sf = $item->getSubForm();
773 if ($item->hideSubForm() && is_object($sf)) {
774 if ($this->global_tpl) {
775 $dsfid = $item->getFieldId();
776 $this->global_tpl->addOnloadCode(
777 "il.Form.hideSubForm('subform_$dsfid');"
778 );
779 }
780 $this->addAsyncOnloadCode("il.Form.hideSubForm('subform_$dsfid');");
781 }
782 }
783
784 $sf_content = "";
785 if (is_object($sf)) {
786 $sf_content = $sf->getContent();
787 if ($sf->getMultipart()) {
788 $this->setMultipart(true);
789 }
790 $this->tpl->setCurrentBlock("sub_form");
791 $this->tpl->setVariable("PROP_SUB_FORM", $sf_content);
792 $this->tpl->setVariable("SFID", $item->getFieldId());
793 $this->tpl->parseCurrentBlock();
794 }
795
796 $this->tpl->setCurrentBlock("prop");
797 /* not used yet
798 $this->tpl->setVariable("ID", $item->getFieldId());
799 $this->tpl->setVariable("CFG", json_encode($cfg, JSON_THROW_ON_ERROR));*/
800 $this->tpl->parseCurrentBlock();
801 }
802
803
804 $this->tpl->touchBlock("item");
805 }
806
807 public function addAsyncOnloadCode(string $code): void
808 {
809 $this->onload_code[] = $code;
810 }
811
812 public function getHTML(): string
813 {
814 $html = parent::getHTML();
815
816 // #13531 - get content that has to reside outside of the parent form tag, e.g. panels/layers
817 foreach ($this->items as $item) {
818 // #13536 - ilFormSectionHeaderGUI does NOT extend ilFormPropertyGUI ?!
819 if (method_exists($item, "getContentOutsideFormTag")) {
820 $outside = $item->getContentOutsideFormTag();
821 if ($outside) {
822 $html .= $outside;
823 }
824 }
825 }
826 if ($this->ctrl->isAsynch()) {
827 $html = $this->appendOnloadCode($html);
828 }
829 return $html;
830 }
831
832 public function getHTMLAsync(): string
833 {
834 $html = $this->getHTML();
835 if (!$this->ctrl->isAsynch()) {
836 $html = $this->appendOnloadCode($html);
837 }
838 return $html;
839 }
840
841 protected function appendOnloadCode(string $html): string
842 {
843 if (count($this->onload_code) > 0) {
844 $html .= "<script>";
845 foreach ($this->onload_code as $code) {
846 $html .= $code . "\n";
847 }
848 $html .= "</script>";
849 }
850 return $html;
851 }
852
853 //
854 // UPLOAD HANDLING
855 //
856
869 protected function keepFileUpload(
870 string $a_hash,
871 string $a_field,
872 string $a_tmp_name,
873 string $a_name,
874 string $a_type,
875 ?string $a_index = null,
876 ?string $a_sub_index = null
877 ): void {
878 if (in_array($a_tmp_name, $this->kept_uploads)) {
879 return; // already kept
880 }
881
882 if (trim($a_tmp_name) == "") {
883 return;
884 }
885
886 $a_name = ilFileUtils::getASCIIFilename($a_name);
887
888 $tmp_file_name = implode("~~", array(session_id(),
889 $a_hash,
890 $a_field,
891 $a_index,
892 $a_sub_index,
893 str_replace("/", "~~", $a_type),
894 str_replace("~~", "_", $a_name)));
895
896 // make sure temp directory exists
897 $temp_path = ilFileUtils::getDataDir() . "/temp";
898 if (!is_dir($temp_path)) {
900 }
901
902 ilFileUtils::moveUploadedFile($a_tmp_name, $tmp_file_name, $temp_path . "/" . $tmp_file_name);
903
905 $file_input = $this->getItemByPostVar($a_field);
906 $file_input->setPending($a_name);
907 $this->kept_uploads[] = $a_tmp_name;
908 }
909
918 public function getFileUpload(
919 string $a_field,
920 ?string $a_index = null,
921 ?string $a_sub_index = null
922 ): array {
923 $res = array();
924 if ($a_index) {
925 if ($_FILES[$a_field]["tmp_name"][$a_index][$a_sub_index] ?? false) {
926 $res = array(
927 "tmp_name" => $_FILES[$a_field]["tmp_name"][$a_index][$a_sub_index],
928 "name" => $_FILES[$a_field]["name"][$a_index][$a_sub_index],
929 "type" => $_FILES[$a_field]["type"][$a_index][$a_sub_index],
930 "error" => $_FILES[$a_field]["error"][$a_index][$a_sub_index],
931 "size" => $_FILES[$a_field]["size"][$a_index][$a_sub_index],
932 "is_upload" => $_FILES[$a_field]["is_upload"][$a_index][$a_sub_index] ?? true
933 );
934 }
935 } elseif ($a_sub_index) {
936 if ($_FILES[$a_field]["tmp_name"][$a_index] ?? false) {
937 $res = array(
938 "tmp_name" => $_FILES[$a_field]["tmp_name"][$a_index],
939 "name" => $_FILES[$a_field]["name"][$a_index],
940 "type" => $_FILES[$a_field]["type"][$a_index],
941 "error" => $_FILES[$a_field]["error"][$a_index],
942 "size" => $_FILES[$a_field]["size"][$a_index],
943 "is_upload" => $_FILES[$a_field]["is_upload"][$a_index] ?? true
944 );
945 }
946 } else {
947 if ($_FILES[$a_field]["tmp_name"] ?? false) {
948 $res = array(
949 "tmp_name" => $_FILES[$a_field]["tmp_name"],
950 "name" => $_FILES[$a_field]["name"],
951 "type" => $_FILES[$a_field]["type"],
952 "error" => $_FILES[$a_field]["error"],
953 "size" => $_FILES[$a_field]["size"],
954 "is_upload" => $_FILES[$a_field]["is_upload"] ?? true
955 );
956 }
957 }
958 return $res;
959 }
960
961 public function hasFileUpload(
962 string $a_field,
963 ?string $a_index = null,
964 ?string $a_sub_index = null
965 ): bool {
966 $data = $this->getFileUpload($a_field, $a_index, $a_sub_index);
967 return (bool) ($data["tmp_name"] ?? false);
968 }
969
981 public function moveFileUpload(
982 string $a_target_directory,
983 string $a_field,
984 ?string $a_target_name = null,
985 ?string $a_index = null,
986 ?string $a_sub_index = null
987 ): string {
988 if (!is_dir($a_target_directory)) {
989 return "";
990 }
991
992 $data = $this->getFileUpload($a_field, $a_index, $a_sub_index);
993 if ($data["tmp_name"] && file_exists($data["tmp_name"])) {
994 if ($a_target_name) {
995 $data["name"] = $a_target_name;
996 }
997
998 $target_file = $a_target_directory . "/" . $data["name"];
999 $target_file = str_replace("//", "/", $target_file);
1000
1001 if ($data["is_upload"]) {
1002 if (!ilFileUtils::moveUploadedFile($data["tmp_name"], $data["name"], $target_file)) {
1003 return "";
1004 }
1005 } else {
1006 if (!ilFileUtils::rename($data["tmp_name"], $target_file)) {
1007 return "";
1008 }
1009 }
1010
1011 return $target_file;
1012 }
1013 return "";
1014 }
1015
1016 protected function rebuildUploadedFiles(): void
1017 {
1018 $file_hash = (string) $this->getFileHash();
1019 if ($file_hash != "") {
1020 $temp_path = ilFileUtils::getDataDir() . "/temp";
1021 if (is_dir($temp_path)) {
1022 $temp_files = glob($temp_path . "/" . session_id() . "~~" . $file_hash . "~~*");
1023 if (is_array($temp_files)) {
1024 foreach ($temp_files as $full_file) {
1025 $file = explode("~~", basename($full_file));
1026 $field = $file[2];
1027 $idx = $file[3];
1028 $idx2 = $file[4];
1029 $type = $file[5] . "/" . $file[6];
1030 $name = $file[7];
1031
1032 if ($idx2 != "") {
1033 if (!isset($_FILES[$field]["tmp_name"][$idx][$idx2])) {
1034 $_FILES[$field]["tmp_name"][$idx][$idx2] = $full_file;
1035 $_FILES[$field]["name"][$idx][$idx2] = $name;
1036 $_FILES[$field]["type"][$idx][$idx2] = $type;
1037 $_FILES[$field]["error"][$idx][$idx2] = 0;
1038 $_FILES[$field]["size"][$idx][$idx2] = filesize($full_file);
1039 $_FILES[$field]["is_upload"][$idx][$idx2] = false;
1040 }
1041 } elseif ($idx != "") {
1042 if (!isset($_FILES[$field]["tmp_name"][$idx])) {
1043 $_FILES[$field]["tmp_name"][$idx] = $full_file;
1044 $_FILES[$field]["name"][$idx] = $name;
1045 $_FILES[$field]["type"][$idx] = $type;
1046 $_FILES[$field]["error"][$idx] = 0;
1047 $_FILES[$field]["size"][$idx] = filesize($full_file);
1048 $_FILES[$field]["is_upload"][$idx] = false;
1049 }
1050 } else {
1051 if (!isset($_FILES[$field]["tmp_name"])) {
1052 $_FILES[$field]["tmp_name"] = $full_file;
1053 $_FILES[$field]["name"] = $name;
1054 $_FILES[$field]["type"] = $type;
1055 $_FILES[$field]["error"] = 0;
1056 $_FILES[$field]["size"] = filesize($full_file);
1057 $_FILES[$field]["is_upload"] = false;
1058 }
1059 }
1060 }
1061 }
1062 }
1063 }
1064 }
1065}
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:43
static _isAllowed()
Check if the CharSelector is allowed for the current GUI.
static _getCurrentGUI(ilObjTest $a_test_obj=null)
Get the GUI that is used for the currently available selector (other GUI instances may exist for conf...
Class ilCtrl provides processing control methods.
getNextClass($a_gui_class=null)
@inheritDoc
static getASCIIFilename(string $a_filename)
static rename(string $a_source, string $a_target)
static getDataDir()
get data directory (outside webspace)
static createDirectory(string $a_dir, int $a_mod=0755)
create directory
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setPreventDoubleSubmission(bool $a_val)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property in a property form.
getItemByPostVar(string $a_post_var)
Get item by post var.
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...
language handling
loadLanguageModule(string $a_module)
Load language module.
User class.
This class represents a property form user interface.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
hasFileUpload(string $a_field, ?string $a_index=null, ?string $a_sub_index=null)
moveFileUpload(string $a_target_directory, string $a_field, ?string $a_target_name=null, ?string $a_index=null, ?string $a_sub_index=null)
Move upload to target directory.
setDisableStandardMessage(bool $a_val)
setTableWidth(string $a_width)
getFileUpload(string $a_field, ?string $a_index=null, ?string $a_sub_index=null)
Get file upload data.
ilGlobalTemplateInterface $global_tpl
insertItem( $item, bool $a_sub_item=false)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
removeItemByPostVar(string $a_post_var, bool $a_remove_unused_headers=false)
setTitleIcon(string $a_titleicon)
Refinery Factory $refinery
getItemByPostVar(string $a_post_var)
getInputItemsRecursive()
returns a flat array of all input items including the possibly existing subitems recursively
setHideLabels(bool $a_value=true)
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...
special template class to simplify handling of ITX/PEAR
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
static initDom(?ilGlobalTemplateInterface $a_main_tpl=null)
Init YUI Dom.
static initEvent(?ilGlobalTemplateInterface $a_main_tpl=null)
Init YUI Event.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
$txt
Definition: error.php:13
global $DIC
Definition: feed.php:28
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...
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$res
Definition: ltiservices.php:69
$post
Definition: ltitoken.php:49
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $key
Consumer key/client ID value.
Definition: System.php:193
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: ByTrying.php:21
global $ilSetting
Definition: privfeed.php:17
$type
$http
Definition: raiseError.php:7
$lng