ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
Custom.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28
29class 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 {
107
108 $relative_path = $this->getRelativePath();
109
110 if ($this->filesystem->has($relative_path)) {
111 $this->filesystem->delete($relative_path);
112 }
113
114 $temp_icon = new CustomIconTempUploadPath(
115 $tempfile_name,
117 );
118
119 rename($temp_icon->getAbsolutePath(), $this->getFullPath());
120 $this->filesystem->setVisibility($relative_path, 'public');
121
122
123 foreach ($this->config->getUploadPostProcessors() as $processor) {
124 $processor->process($relative_path);
125 }
126
127 $this->persistIconState($relative_path);
128 }
129
130 public function saveFromHttpRequest(): void
131 {
133
134 $fileName = $this->getRelativePath();
135
136 if ($this->filesystem->has($fileName)) {
137 $this->filesystem->delete($fileName);
138 }
139
140 if ($this->upload->hasUploads() && !$this->upload->hasBeenProcessed()) {
141 $this->upload->process();
142
144 $result = array_values($this->upload->getResults())[0];
145 if ($result->isOK()) {
146 $this->upload->moveOneFileTo(
147 $result,
148 $this->getIconDirectory(),
150 $this->getIconFileName(),
151 true
152 );
153 }
154
155 foreach ($this->config->getUploadPostProcessors() as $processor) {
156 $processor->process($fileName);
157 }
158 }
159
160 $this->persistIconState($fileName);
161 }
162
163 protected function persistIconState(string $fileName): void
164 {
165 if ($this->filesystem->has($fileName)) {
166 \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '1');
167 } else {
168 \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
169 }
170 }
171
172 public function remove(): void
173 {
174 $fileName = $this->getRelativePath();
175
176 if ($this->filesystem->has($fileName)) {
177 $this->filesystem->delete($fileName);
178 }
179
180 \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
181 }
182
186 protected function createCustomIconDirectory(): void
187 {
188 $iconDirectory = $this->getIconDirectory();
189
190 if (!$this->filesystem->has(dirname($iconDirectory))) {
191 $this->filesystem->createDir(dirname($iconDirectory));
192 }
193
194 if (!$this->filesystem->has($iconDirectory)) {
195 $this->filesystem->createDir($iconDirectory);
196 }
197 }
198
199 protected function getIconDirectory(): string
200 {
201 return implode(DIRECTORY_SEPARATOR, [
202 $this->config->getBaseDirectory(),
203 $this->config->getSubDirectoryPrefix() . $this->getObjId()
204 ]);
205 }
206
207 protected function getIconFileName(): string
208 {
209 return self::ICON_BASENAME . '.' . $this->config->getTargetFileExtension();
210 }
211
212 protected function getRelativePath(): string
213 {
214 return implode(DIRECTORY_SEPARATOR, [
215 $this->getIconDirectory(),
216 $this->getIconFileName()
217 ]);
218 }
219
220 public function exists(): bool
221 {
222 if (!\ilContainer::_lookupContainerSetting($this->getObjId(), 'icon_custom', '0')) {
223 return false;
224 }
225
226 return $this->filesystem->has($this->getRelativePath());
227 }
228
229 public function getFullPath(): string
230 {
231 // TODO: Currently there is no option to get the relative base directory of a filesystem
232 return implode(DIRECTORY_SEPARATOR, [
234 $this->getRelativePath()
235 ]);
236 }
237
238 public function createFromImportDir(string $source_dir): void
239 {
240 $target_dir = implode(DIRECTORY_SEPARATOR, [
242 $this->getIconDirectory()
243 ]);
244 \ilFileUtils::rCopy($source_dir, $target_dir);
245 $this->persistIconState($this->getRelativePath());
246 }
247}
Indicates general problems with the input or output operations.
Definition: IOException.php:28
Resolves a user-supplied temp file identifier to an absolute path that is guaranteed to refer to a re...
__construct(protected Filesystem $filesystem, protected FileUpload $upload, protected Configuration $config, protected int $objId)
Definition: Custom.php:33
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)
static getWebspaceDir(string $mode="filesystem")
get webspace directory
static getDataDir()
get data directory (outside webspace)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
Interface Location.
Definition: Location.php:33
const WEB
The filesystem within the ilias web root.
Definition: Location.php:38
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
$objId
Definition: xapitoken.php:55