ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilDclFileRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
28 use ilDclFileFieldHelper;
29
30 protected const FILE_TMP_NAME = 'tmp_name';
31 protected const FILE_NAME = "name";
32 protected const FILE_TYPE = "type";
33
34 protected \ILIAS\ResourceStorage\Services $irss;
36 protected \ILIAS\FileUpload\FileUpload $upload;
37
39 {
40 global $DIC;
42 $this->stakeholder = new ilDataCollectionStakeholder();
43 $this->irss = $DIC->resourceStorage();
44 $this->upload = $DIC->upload();
45 }
46
47 public function parseValue($value)
48 {
49 $has_record_id = $this->http->wrapper()->query()->has('record_id');
50 $is_confirmed = $this->http->wrapper()->post()->has('save_confirmed');
51 $has_save_confirmation = ($this->getRecord()->getTable()->getSaveConfirmation() && !$has_record_id);
52
53 if (($value[self::FILE_TMP_NAME] ?? '') !== '' && (!$has_save_confirmation || $is_confirmed)) {
54 return $this->handleFileUpload($value, $has_save_confirmation);
55 } else {
56 if (($value[self::FILE_TMP_NAME] ?? '') !== '') {
57 return $value;
58 } else {
59 return $this->getValue();
60 }
61 }
62 }
63
64 protected function handleFileUpload(array $value, bool $has_save_confirmation): mixed
65 {
66 if ($has_save_confirmation) {
68 $this->http->wrapper()->post()->retrieve('ilfilehash', $this->refinery->kindlyTo()->string()),
69 'field_' . $this->getField()->getId(),
70 $value[self::FILE_NAME],
71 $value[self::FILE_TYPE]
72 );
73
74 $file_stream = ILIAS\Filesystem\Stream\Streams::ofResource(fopen($move_file, 'rb'));
75 } else {
76 $move_file = $value[self::FILE_TMP_NAME];
77
78 $file_stream = Streams::ofResource(fopen($move_file, 'rb'));
79 }
80
81 $file_title = $value[self::FILE_NAME] ?? basename($move_file);
82
83 $old = $this->getValue();
84 if (is_string($old) && ($rid = $this->irss->manage()->find($old)) !== null) {
85 $this->irss->manage()->replaceWithStream($rid, $file_stream, $this->stakeholder, $file_title);
86 } else {
87 $rid = $this->irss->manage()->stream($file_stream, $this->stakeholder, $file_title);
88 }
89
90 return $rid->serialize();
91 }
92
93 public function setValueFromForm(ilPropertyFormGUI $form): void
94 {
95 if ($this->value !== null && $form->getItemByPostVar("field_" . $this->getField()->getId())->getDeletionFlag()) {
96 $this->removeData();
97 $this->setValue(null, true);
98 $this->doUpdate();
99 }
100 parent::setValueFromForm($form);
101 }
102
103 public function delete(): void
104 {
105 if ($this->value !== null) {
106 $this->removeData();
107 }
108 parent::delete();
109 }
110
111 protected function removeData(): void
112 {
113 if (null !== $rid = $this->irss->manage()->find($this->getValue())) {
114 $this->irss->manage()->remove($rid, $this->stakeholder);
115 }
116 }
117
118 public function parseExportValue($value)
119 {
120 return $this->valueToFileTitle($value);
121 }
122
123 public function parseSortingValue($value, bool $link = true)
124 {
125 return $this->valueToFileTitle($value);
126 }
127
128 public function afterClone(): void
129 {
130 if ($this->value !== null) {
131 $value = null;
132 $current = $this->valueToCurrentRevision($this->value);
133 if ($current !== null) {
134 $new_rid = $this->irss->manage()->clone($current->getIdentification());
135 $value = $new_rid->serialize();
136 }
137 $this->setValue($value, true);
138 $this->doUpdate();
139 }
140 }
141}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
setValue($value, bool $omit_parsing=false)
Set value for record field.
@noinspection AutoloadingIssuesInspection
parseValue($value)
Function to parse incoming data from form input value $value.
handleFileUpload(array $value, bool $has_save_confirmation)
ilDataCollectionStakeholder $stakeholder
parseExportValue($value)
Function to parse incoming data from form input value $value.
ILIAS ResourceStorage Services $irss
parseSortingValue($value, bool $link=true)
Returns sortable value for the specific field-types.
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
static getTempFilename(string $a_hash, string $a_field, string $a_name, string $a_type, ?string $a_index=null, ?string $a_sub_index=null)
This class represents a property form user interface.
getItemByPostVar(string $a_post_var)
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26