ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFileDataForumDraftsRCImplementation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
25{
26 public const FORUM_PATH_RCID = 'RCID';
27
28 private readonly \ILIAS\ResourceStorage\Services $irss;
29 private readonly \ILIAS\FileUpload\FileUpload $upload;
31 private array $collection_cache = [];
33 private array $posting_cache = [];
35
36 public function __construct(
37 private readonly int $obj_id = 0,
38 private int $draft_id = 0
39 ) {
40 global $DIC;
41 $this->irss = $DIC->resourceStorage();
42 $this->upload = $DIC->upload();
43 $this->stakeholder = new ilForumPostingFileStakeholder();
44 }
45
46 private function getCurrentDraft(bool $use_cache = true): ilForumPostDraft
47 {
48 return $this->getDraftById($this->draft_id, $use_cache);
49 }
50
51 private function getDraftById(int $draft_id, bool $use_cache = true): ilForumPostDraft
52 {
53 if ($use_cache && isset($this->posting_cache[$draft_id])) {
54 return $this->posting_cache[$draft_id];
55 }
56
57 return $this->posting_cache[$draft_id] = ilForumPostDraft::newInstanceByDraftId($draft_id);
58 }
59
61 {
62 return $this->collection_cache[$this->draft_id] ?? ($this->collection_cache[$this->draft_id] = $this->irss->collection(
63 )->get(
64 $this->irss->collection()->id(
65 $this->getCurrentDraft()->getRCID()
66 )
67 ));
68 }
69
70 private function getResourceIdByHash(string $hash): ?ResourceIdentification
71 {
72 foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
73 $revision = $this->irss->manage()->getCurrentRevision($identification);
74 if ($revision->getTitle() === $hash) {
75 return $identification;
76 }
77 }
78
79 return null;
80 }
81
82 public function getObjId(): int
83 {
84 return $this->obj_id;
85 }
86
87 public function getPosId(): int
88 {
89 return $this->draft_id;
90 }
91
92 public function setPosId(int $posting_id): void
93 {
94 $this->draft_id = $posting_id;
95 }
96
97 public function getForumPath(): string
98 {
100 }
101
105 public function getFilesOfPost(): array
106 {
107 $files = [];
108 foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
109 $revision = $this->irss->manage()->getCurrentRevision($identification);
110 $info = $revision->getInformation();
111 $files[$revision->getTitle()] = [
112 'path' => $this->irss->consume()->stream($identification)->getStream()->getMetadata('uri'),
113 'md5' => $revision->getTitle(),
114 'name' => $info->getTitle(),
115 'size' => $info->getSize(),
116 'ctime' => $info->getCreationDate()->format('Y-m-d H:i:s')
117 ];
118 }
119
120 return $files;
121 }
122
123 public function moveFilesOfPost(int $new_frm_id = 0): bool
124 {
125 // nothing to do here since collections are related to the post
126 return true;
127 }
128
129 public function ilClone(int $new_obj_id, int $new_posting_id): bool
130 {
131 $current_collection_id = $this->getCurrentCollection()->getIdentification();
132 $new_collection_id = $this->irss->collection()->clone($current_collection_id);
133 $new_draft = $this->getDraftById($new_posting_id);
134 $new_draft->setRCID($new_collection_id->serialize());
135 $new_draft->update();
136
137 return true;
138 }
139
140 public function delete(?array $posting_ids_to_delete = null): bool
141 {
142 // Each element of $posting_ids_to_delete represents a "Draft Id", NOT a "Posting Id"
143 if ($posting_ids_to_delete === null) {
144 return true;
145 }
146
147 foreach ($posting_ids_to_delete as $draft_id) {
148 $this->irss->collection()->remove(
149 $this->irss->collection()->id(
150 $this->getDraftById($draft_id)->getRCID()
151 ),
153 true
154 );
155 }
156
157 return true;
158 }
159
160 public function storeUploadedFiles(): bool
161 {
162 if (!$this->upload->hasBeenProcessed()) {
163 $this->upload->process();
164 }
165 $collection = $this->getCurrentCollection();
166
167 foreach ($this->upload->getResults() as $result) {
168 if (!$result->isOK()) {
169 continue;
170 }
171 $rid = $this->irss->manage()->upload(
172 $result,
173 $this->stakeholder,
174 md5($result->getName())
175 );
176 $collection->add($rid);
177 }
178 $this->irss->collection()->store($collection);
179 $draft = $this->getCurrentDraft(false);
180 $draft->setRCID($collection->getIdentification()->serialize());
181 $draft->update();
182
183 return true;
184 }
185
186 public function unlinkFile(string $filename): bool
187 {
188 throw new DomainException('Not implemented');
189 }
190
194 public function getFileDataByMD5Filename(string $hashed_filename): ?array
195 {
196 foreach ($this->getCurrentCollection()->getResourceIdentifications() as $identification) {
197 $revision = $this->irss->manage()->getCurrentRevision($identification);
198 if ($revision->getTitle() === $hashed_filename) {
199 $info = $revision->getInformation();
200 return [
201 'path' => '',
202 'filename' => $info->getTitle(),
203 'clean_filename' => $info->getTitle()
204 ];
205 }
206 }
207
208 return null;
209 }
210
214 public function unlinkFilesByMD5Filenames($hashed_filename_or_filenames): bool
215 {
216 $hashes = is_array($hashed_filename_or_filenames)
217 ? $hashed_filename_or_filenames
218 : [$hashed_filename_or_filenames];
219
220 foreach ($hashes as $hash) {
221 $identification = $this->getResourceIdByHash($hash);
222 if ($identification !== null) {
223 $this->irss->manage()->remove($identification, $this->stakeholder);
224 }
225 }
226
227 return true;
228 }
229
230 public function deliverFile(string $file): void
231 {
232 $rid = $this->getResourceIdByHash($file);
233 if ($rid !== null) {
234 $this->irss->consume()->download($rid)->run();
235 }
236 }
237
238 public function deliverZipFile(): bool
239 {
240 // https://mantis.ilias.de/view.php?id=39910
242 $this->getCurrentDraft()->getPostSubject() . '.zip'
243 );
244 $rcid = $this->getCurrentCollection()->getIdentification();
245
246 $this->irss
247 ->consume()
248 ->downloadCollection($rcid, $zip_filename)
249 ->useRevisionTitlesForFileNames(false)
250 ->run();
251
252 return true;
253 }
254}
$filename
Definition: buildRTE.php:78
static returnASCIIFileName(string $original_filename)
Converts a UTF-8 filename to ASCII.
Definition: Delivery.php:513
return true
__construct(private readonly int $obj_id=0, private int $draft_id=0)
Class ilForumPostDraft.
static newInstanceByDraftId(int $draft_id)
$info
Definition: entry_point.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26