ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
Custom.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 
29 class Custom
30 {
31  private const ICON_BASENAME = 'icon_custom';
32 
33  public function __construct(
34  protected Filesystem $filesystem,
35  protected FileUpload $upload,
36  protected Configuration $config,
37  protected int $objId
38  ) {
39  }
40 
41  protected function getObjId(): int
42  {
43  return $this->objId;
44  }
45 
46  public function copy(int $targetObjId): void
47  {
48  if (!$this->exists()) {
49  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '0');
50  return;
51  }
52 
53  try {
54  $this->filesystem->copy(
55  $this->getRelativePath(),
56  preg_replace(
57  '/(' . $this->config->getSubDirectoryPrefix() . ')(\d*)\/(.*)$/',
58  '${1}' . $targetObjId . '/${3}',
59  $this->getRelativePath()
60  )
61  );
62 
63  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '1');
64  } catch (Exception $e) {
65  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '0');
66  }
67  }
68 
69  public function delete(): void
70  {
71  if ($this->filesystem->hasDir($this->getIconDirectory())) {
72  try {
73  $this->filesystem->deleteDir($this->getIconDirectory());
74  } catch (Exception $e) {
75  }
76  }
77 
78  \ilContainer::_deleteContainerSettings($this->getObjId(), 'icon_custom');
79  }
80 
84  public function getSupportedFileExtensions(): array
85  {
86  return $this->config->getSupportedFileExtensions();
87  }
88 
89  public function saveFromSourceFile(string $sourceFilePath): void
90  {
92 
93  $fileName = $this->getRelativePath();
94 
95  if ($this->filesystem->has($fileName)) {
96  $this->filesystem->delete($fileName);
97  }
98 
99  $this->filesystem->copy($sourceFilePath, $fileName);
100 
101  $this->persistIconState($fileName);
102  }
103 
104  public function saveFromTempFileName(string $tempfile_name): void
105  {
106  $this->createCustomIconDirectory();
107 
108  $relative_path = $this->getRelativePath();
109 
110  if ($this->filesystem->has($relative_path)) {
111  $this->filesystem->delete($relative_path);
112  }
113 
114  rename(\ilFileUtils::getDataDir() . '/temp/' . $tempfile_name, $this->getFullPath());
115  $this->filesystem->setVisibility($relative_path, 'public');
116 
117 
118  foreach ($this->config->getUploadPostProcessors() as $processor) {
119  $processor->process($relative_path);
120  }
121 
122  $this->persistIconState($relative_path);
123  }
124 
125  public function saveFromHttpRequest(): void
126  {
127  $this->createCustomIconDirectory();
128 
129  $fileName = $this->getRelativePath();
130 
131  if ($this->filesystem->has($fileName)) {
132  $this->filesystem->delete($fileName);
133  }
134 
135  if ($this->upload->hasUploads() && !$this->upload->hasBeenProcessed()) {
136  $this->upload->process();
137 
139  $result = array_values($this->upload->getResults())[0];
140  if ($result->isOK()) {
141  $this->upload->moveOneFileTo(
142  $result,
143  $this->getIconDirectory(),
145  $this->getIconFileName(),
146  true
147  );
148  }
149 
150  foreach ($this->config->getUploadPostProcessors() as $processor) {
151  $processor->process($fileName);
152  }
153  }
154 
155  $this->persistIconState($fileName);
156  }
157 
158  protected function persistIconState(string $fileName): void
159  {
160  if ($this->filesystem->has($fileName)) {
161  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '1');
162  } else {
163  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
164  }
165  }
166 
167  public function remove(): void
168  {
169  $fileName = $this->getRelativePath();
170 
171  if ($this->filesystem->has($fileName)) {
172  $this->filesystem->delete($fileName);
173  }
174 
175  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
176  }
177 
181  protected function createCustomIconDirectory(): void
182  {
183  $iconDirectory = $this->getIconDirectory();
184 
185  if (!$this->filesystem->has(dirname($iconDirectory))) {
186  $this->filesystem->createDir(dirname($iconDirectory));
187  }
188 
189  if (!$this->filesystem->has($iconDirectory)) {
190  $this->filesystem->createDir($iconDirectory);
191  }
192  }
193 
194  protected function getIconDirectory(): string
195  {
196  return implode(DIRECTORY_SEPARATOR, [
197  $this->config->getBaseDirectory(),
198  $this->config->getSubDirectoryPrefix() . $this->getObjId()
199  ]);
200  }
201 
202  protected function getIconFileName(): string
203  {
204  return self::ICON_BASENAME . '.' . $this->config->getTargetFileExtension();
205  }
206 
207  protected function getRelativePath(): string
208  {
209  return implode(DIRECTORY_SEPARATOR, [
210  $this->getIconDirectory(),
211  $this->getIconFileName()
212  ]);
213  }
214 
215  public function exists(): bool
216  {
217  if (!\ilContainer::_lookupContainerSetting($this->getObjId(), 'icon_custom', '0')) {
218  return false;
219  }
220 
221  return $this->filesystem->has($this->getRelativePath());
222  }
223 
224  public function getFullPath(): string
225  {
226  // TODO: Currently there is no option to get the relative base directory of a filesystem
227  return implode(DIRECTORY_SEPARATOR, [
229  $this->getRelativePath()
230  ]);
231  }
232 
233  public function createFromImportDir(string $source_dir): void
234  {
235  $target_dir = implode(DIRECTORY_SEPARATOR, [
237  $this->getIconDirectory()
238  ]);
239  \ilFileUtils::rCopy($source_dir, $target_dir);
240  $this->persistIconState($this->getRelativePath());
241  }
242 }
static getWebspaceDir(string $mode="filesystem")
get webspace directory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$objId
Definition: xapitoken.php:57
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
__construct(protected Filesystem $filesystem, protected FileUpload $upload, protected Configuration $config, protected int $objId)
Definition: Custom.php:33
Class FileUpload.
Definition: FileUpload.php:37
static getDataDir()
get data directory (outside webspace)
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
static _lookupContainerSetting(int $a_id, string $a_keyword, ?string $a_default_value=null)
static _deleteContainerSettings(int $a_id, string $a_keyword="", bool $a_keyword_like=false)
const WEB
The filesystem within the ilias web root.
Definition: Location.php:38