ILIAS  release_8 Revision v8.24
class.ilSessionFile.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
29{
31 protected ilDBInterface $db;
32 protected ilLanguage $lng;
33 protected int $event_id = 0;
34 protected int $file_id = 0;
35 protected string $file_name = "";
36 protected string $file_type = "";
37 protected int $file_size = 0;
38 protected string $tmp_name = "";
39 protected int $error_code = 0;
41
42 public function __construct(int $a_file_id = 0)
43 {
44 global $DIC;
45
46 $this->ilErr = $DIC['ilErr'];
47 $this->db = $DIC->database();
48 $this->lng = $DIC->language();
49
50 $this->file_id = $a_file_id;
51 $this->__read();
52 }
53
54 public function setFileId(int $a_id): void
55 {
56 $this->file_id = $a_id;
57 }
58
59 public function getFileId(): int
60 {
61 return $this->file_id;
62 }
63
64 public function getSessionId(): int
65 {
66 return $this->event_id;
67 }
68
69 public function setSessionId(int $a_event_id): void
70 {
71 $this->event_id = $a_event_id;
72 }
73
74 public function setFileName(string $a_name): void
75 {
76 $this->file_name = $a_name;
77 }
78
79 public function getFileName(): string
80 {
81 return $this->file_name;
82 }
83
84 public function setFileType(string $a_type): void
85 {
86 $this->file_type = $a_type;
87 }
88
89 public function getFileType(): string
90 {
91 return $this->file_type;
92 }
93
94 public function setFileSize(int $a_size): void
95 {
96 $this->file_size = $a_size;
97 }
98
99 public function getFileSize(): int
100 {
101 return $this->file_size;
102 }
103
104 public function setTemporaryName(string $a_name): void
105 {
106 $this->tmp_name = $a_name;
107 }
108
109 public function getTemporaryName(): string
110 {
111 return $this->tmp_name;
112 }
113
114 public function setErrorCode(int $a_code): void
115 {
116 $this->error_code = $a_code;
117 }
118
119 public function getErrorCode(): int
120 {
121 return $this->error_code;
122 }
123
124 public function getAbsolutePath(): string
125 {
126 return $this->fss_storage->getAbsolutePath() . "/" . $this->getFileId();
127 }
128
129 public function validate(): bool
130 {
131 switch ($this->getErrorCode()) {
132 case UPLOAD_ERR_INI_SIZE:
133 $this->ilErr->appendMessage($this->lng->txt('file_upload_ini_size'));
134 break;
135 case UPLOAD_ERR_FORM_SIZE:
136 $this->ilErr->appendMessage($this->lng->txt('file_upload_form_size'));
137 break;
138
139 case UPLOAD_ERR_PARTIAL:
140 $this->ilErr->appendMessage($this->lng->txt('file_upload_only_partial'));
141 break;
142
143 case UPLOAD_ERR_NO_TMP_DIR:
144 $this->ilErr->appendMessage($this->lng->txt('file_upload_no_tmp_dir'));
145 break;
146
147 case UPLOAD_ERR_OK:
148 case UPLOAD_ERR_NO_FILE:
149 default:
150 return true;
151 }
152
153 return false;
154 }
155
156 public function cloneFiles(int $a_target_event_id): void
157 {
158 $file = new ilSessionFile();
159 $file->setSessionId($a_target_event_id);
160 $file->setFileName($this->getFileName());
161 $file->setFileType($this->getFileType());
162 $file->setFileSize($this->getFileSize());
163 $file->create(false);
164
165 // Copy file
167 $source->copyFile($this->getAbsolutePath(), $file->getAbsolutePath());
168 }
169
170 public function create(bool $a_upload = true): bool
171 {
173
174 if ($this->getErrorCode() != 0) {
175 return false;
176 }
177
178 $next_id = $ilDB->nextId('event_file');
179 $query = "INSERT INTO event_file (file_id,event_id,file_name,file_size,file_type) " .
180 "VALUES( " .
181 $ilDB->quote($next_id, 'integer') . ", " .
182 $ilDB->quote($this->getSessionId(), 'integer') . ", " .
183 $ilDB->quote($this->getFileName(), 'text') . ", " .
184 $ilDB->quote($this->getFileSize(), 'integer') . ", " .
185 $ilDB->quote($this->getFileType(), 'text') . " " .
186 ")";
187
188 $res = $ilDB->manipulate($query);
189 $this->setFileId($next_id);
190
191 $this->fss_storage = new ilFSStorageSession($this->getSessionId());
192 $this->fss_storage->createDirectory();
193
194 if ($a_upload) {
195 // now create file
197 $this->getTemporaryName(),
198 $this->getFileName(),
199 $this->fss_storage->getAbsolutePath() . '/' . $this->getFileId()
200 );
201 }
202
203 return true;
204 }
205
206 public function delete(): bool
207 {
209
210 // Delete db entry
211 $query = "DELETE FROM event_file " .
212 "WHERE file_id = " . $ilDB->quote($this->getFileId(), 'integer') . " ";
213 $res = $ilDB->manipulate($query);
214
215 // Delete file
216 $this->fss_storage->deleteFile($this->getAbsolutePath());
217 return true;
218 }
219
220 public function _deleteByEvent(int $a_event_id): bool
221 {
223
224 // delete all event ids and delete assigned files
225 $query = "DELETE FROM event_file " .
226 "WHERE event_id = " . $ilDB->quote($a_event_id, 'integer');
227 $res = $ilDB->manipulate($query);
228
229 #$this->fss_storage->delete();
230 return true;
231 }
232
233 public static function _readFilesByEvent(int $a_event_id): array
234 {
235 global $DIC;
236
237 $ilDB = $DIC->database();
238
239 $query = "SELECT * FROM event_file " .
240 "WHERE event_id = " . $ilDB->quote($a_event_id, 'integer');
241
242 $res = $ilDB->query($query);
243 $files = [];
244 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
245 $files[] = new ilSessionFile($row->file_id);
246 }
247 return $files;
248 }
249
250 protected function __read(): bool
251 {
253
254 if (!$this->file_id) {
255 return true;
256 }
257
258 // read file data
259 $query = "SELECT * FROM event_file WHERE file_id = " . $ilDB->quote($this->file_id, 'integer');
260 $res = $this->db->query($query);
261 while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
262 $this->setFileName($row->file_name);
263 $this->setFileSize($row->file_size);
264 $this->setFileType($row->file_type);
265 $this->setSessionId($row->event_id);
266 }
267 $this->fss_storage = new ilFSStorageSession($this->getSessionId());
268 return true;
269 }
270}
Error Handling & global info handling uses PEAR error class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setFileSize(int $a_size)
ilDBInterface $db
__construct(int $a_file_id=0)
setErrorCode(int $a_code)
static _readFilesByEvent(int $a_event_id)
ilFSStorageSession $fss_storage
create(bool $a_upload=true)
setFileType(string $a_type)
_deleteByEvent(int $a_event_id)
setFileName(string $a_name)
setTemporaryName(string $a_name)
setSessionId(int $a_event_id)
cloneFiles(int $a_target_event_id)
ilErrorHandling $ilErr
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$source
Definition: metadata.php:93
$query