ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 private const FILE_TMP_NAME = 'tmp_name';
31 private const FILE_NAME = "name";
32 private 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 $file = $value;
50
51 // Some general Request Information
52 $has_record_id = $this->http->wrapper()->query()->has('record_id');
53 $is_confirmed = $this->http->wrapper()->post()->has('save_confirmed');
54 $has_save_confirmation = ($this->getRecord()->getTable()->getSaveConfirmation() && !$has_record_id);
55
56 if (
57 is_array($file)
58 && isset($file[self::FILE_TMP_NAME])
59 && $file[self::FILE_TMP_NAME] !== ""
60 && (!$has_save_confirmation || $is_confirmed)
61 ) {
62 if ($has_save_confirmation) {
63 $ilfilehash = $this->http->wrapper()->post()->retrieve(
64 'ilfilehash',
65 $this->refinery->kindlyTo()->string()
66 );
67
69 $ilfilehash,
70 'field_' . $this->getField()->getId(),
71 $file[self::FILE_NAME],
72 $file[self::FILE_TYPE]
73 );
74
75 $file_stream = ILIAS\Filesystem\Stream\Streams::ofResource(fopen($move_file, 'rb'));
76 } else {
77 $move_file = $file[self::FILE_TMP_NAME];
78
79 if (false === $this->upload->hasBeenProcessed()) {
80 $this->upload->process();
81 }
82
83 if (false === $this->upload->hasUploads()) {
84 throw new ilException($this->lng->txt('upload_error_file_not_found'));
85 }
86
87 $file_stream = Streams::ofResource(fopen($move_file, 'rb'));
88 }
89
90 $file_title = $file[self::FILE_NAME] ?? basename($move_file);
91
92 // Storing the File to the IRSS
93 $existing_value = $this->getValue();
94 if (
95 is_string($existing_value)
96 && ($rid = $this->irss->manage()->find($existing_value)) !== null
97 ) {
98 // Append to existing RID
99 $this->irss->manage()->replaceWithStream(
100 $rid,
101 $file_stream,
102 $this->stakeholder,
103 $file_title
104 );
105 } else {
106 // Create new RID
107 $rid = $this->irss->manage()->stream(
108 $file_stream,
109 $this->stakeholder,
110 $file_title
111 );
112 }
113
114 return $rid->serialize();
115 } else {
116 // handover for save-confirmation
117 if (is_array($file) && isset($file[self::FILE_TMP_NAME]) && $file[self::FILE_TMP_NAME] != "") {
118 return $file;
119 }
120 }
121 return $this->getValue();
122 }
123
124 public function setValueFromForm(ilPropertyFormGUI $form): void
125 {
126 if ($this->value !== null && $form->getItemByPostVar("field_" . $this->getField()->getId())->getDeletionFlag()) {
127 $this->removeData();
128 $this->setValue(null, true);
129 $this->doUpdate();
130 }
131 parent::setValueFromForm($form);
132 }
133
134 public function delete(): void
135 {
136 if ($this->value !== null) {
137 $this->removeData();
138 }
139 parent::delete();
140 }
141
142 protected function removeData(): void
143 {
144 $this->irss->manage()->remove($this->irss->manage()->find($this->value), $this->stakeholder);
145 }
146
147 public function parseExportValue($value)
148 {
149 return $this->valueToFileTitle($value);
150 }
151
152 public function parseSortingValue($value, bool $link = true)
153 {
154 return $this->valueToFileTitle($value);
155 }
156
157 public function afterClone(): void
158 {
162
163 if (!$record_field->getValue()) {
164 return;
165 }
166 $current = $this->valueToCurrentRevision($record_field->getValue());
167 if ($current !== null) {
168 $new_rid = $this->irss->manage()->clone($current->getIdentification());
169 $this->setValue($new_rid->serialize());
170 $this->doUpdate();
171 }
172 }
173}
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.
doUpdate()
Update object in database.
static getRecordFieldCache(object $record, object $field)
static getCloneOf(int $id, string $type)
@noinspection AutoloadingIssuesInspection
parseValue($value)
Function to parse incoming data from form input value $value.
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)
Base class for ILIAS Exception handling.
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