ILIAS  release_8 Revision v8.24
class.ilDclFileuploadRecordFieldModel.php
Go to the documentation of this file.
1<?php
2
21
23{
24 private \ILIAS\FileUpload\FileUpload $upload;
25
27 {
29 global $DIC;
30 $this->upload = $DIC->upload();
31 }
32
36 public function parseValue($value)
37 {
38 if ($value === -1) { //marked for deletion.
39 return null;
40 }
41
42 $file = $value;
43
44 $has_record_id = $this->http->wrapper()->query()->has('record_id');
45 $is_confirmed = $this->http->wrapper()->post()->has('save_confirmed');
46 $has_save_confirmation = ($this->getRecord()->getTable()->getSaveConfirmation() && !$has_record_id);
47
48 if (
49 is_array($file)
50 && isset($file['tmp_name'])
51 && $file['tmp_name'] !== ""
52 && (!$has_save_confirmation || $is_confirmed)
53 ) {
54 if ($has_save_confirmation) {
55 $ilfilehash = $this->http->wrapper()->post()->retrieve(
56 'ilfilehash',
57 $this->refinery->kindlyTo()->string()
58 );
59
61 $ilfilehash,
62 'field_' . $this->getField()->getId(),
63 $file["name"],
64 $file["type"]
65 );
66
67 $file_stream = ILIAS\Filesystem\Stream\Streams::ofResource(fopen($move_file, 'rb'));
68 } else {
69 $move_file = $file['tmp_name'];
70
71 if (false === $this->upload->hasBeenProcessed()) {
72 $this->upload->process();
73 }
74
75 if (false === $this->upload->hasUploads()) {
76 throw new ilException($this->lng->txt('upload_error_file_not_found'));
77 }
78
79 $file_stream = Streams::ofResource(fopen($move_file, 'rb'));
80 }
81
82 $file_title = $file["name"] ?? basename($move_file);
83
84 $file_obj = new ilObjFile();
85 $file_obj->setType("file");
86 $file_obj->setTitle($file_title);
87 $file_obj->setFileName($file_title);
88 $file_obj->setMode(ilObjFile::MODE_OBJECT);
89 $file_obj->create();
90
91 $file_obj->appendStream($file_stream, $file_title);
92 $file_obj->setTitle($file_title);
93 $file_obj->setFileName($file_title);
94
95 $file_obj->update();
96
97 $file_id = $file_obj->getId();
98 $return = $file_id;
99 // handover for save-confirmation
100 } else {
101 if (is_array($file) && isset($file['tmp_name']) && $file['tmp_name'] != "") {
102 $return = $file;
103 } else {
104 $return = $this->getValue();
105 }
106 }
107
108 return $return;
109 }
110
111 public function addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation): void
112 {
113 if (is_array($this->getValue())) {
114 foreach ($this->getValue() as $key => $value) {
115 $confirmation->addHiddenItem('field_' . $this->field->getId() . '[' . $key . ']', $value);
116 }
117 }
118 }
119
125 public function setValue($value, bool $omit_parsing = false): void
126 {
127 $this->loadValue();
128
129 if (!$omit_parsing) {
130 $tmp = $this->parseValue($value);
131 $old = $this->value;
132 //if parse value fails keep the old value
133 if ($tmp !== false) {
134 $this->value = $tmp;
135 //delete old file from filesystem
136 if ($old && $old != $tmp) {
137 $this->getRecord()->deleteFile($old);
138 }
139 }
140 } else {
141 $this->value = $value;
142 }
143 }
144
148 public function parseExportValue($value): ?string
149 {
151 return null;
152 }
153
154 $file = $value;
155 if ($file != "-") {
156 $file_obj = new ilObjFile($file, false);
157 $file_name = $file_obj->getFileName();
158
159 return $file_name;
160 }
161
162 return $file;
163 }
164
169 public function parseSortingValue($value, bool $link = true): string
170 {
172 return '';
173 }
174 $file_obj = new ilObjFile($value, false);
175
176 return $file_obj->getTitle();
177 }
178
182 public function setValueFromForm(ilPropertyFormGUI $form): void
183 {
184 $value = $form->getInput("field_" . $this->getField()->getId());
185 if ($form->getItemByPostVar("field_" . $this->getField()->getId())->getDeletionFlag()) {
186 $value = -1;
187 }
188 $this->setValue($value);
189 }
190
194 public function afterClone(): void
195 {
199
200 if (!$record_field || !$record_field->getValue()) {
201 return;
202 }
203
204 $file_old = new ilObjFile($record_field->getValue(), false);
205 $file_new = $file_old->cloneObject(0, 0, true);
206
207 $this->setValue($file_new->getId(), true);
208 $this->doUpdate();
209 }
210}
Mime type determination.
Definition: MimeType.php:28
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:65
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
addHiddenItem(string $a_post_var, string $a_value)
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...
doUpdate()
Update object in database.
static getRecordFieldCache(object $record, object $field)
static getCloneOf(int $id, string $type)
__construct(ilDclBaseRecordModel $record, ilDclBaseFieldModel $field)
addHiddenItemsToConfirmation(ilConfirmationGUI $confirmation)
setValue($value, bool $omit_parsing=false)
Set value for record field.
setValueFromForm(ilPropertyFormGUI $form)
@inheritDoc
parseSortingValue($value, bool $link=true)
Returns sortable value for the specific field-types.
static getTempFilename(string $a_hash, string $a_field, string $a_name, string $a_type, ?string $a_index=null, ?string $a_sub_index=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilObjFile.
const MODE_OBJECT
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
This class represents a property form user interface.
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-...
getItemByPostVar(string $a_post_var)
global $DIC
Definition: feed.php:28
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193