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