ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjectCustomIconImpl.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
26 
32 {
33  private const ICON_BASENAME = 'icon_custom';
34 
36  protected FileUpload $upload;
38  protected int $objId;
39 
40  public function __construct(
41  Filesystem $webDirectory,
42  FileUpload $uploadService,
44  int $objId
45  ) {
46  $this->objId = $objId;
47 
48  $this->webDirectory = $webDirectory;
49  $this->upload = $uploadService;
50  $this->config = $config;
51  }
52 
53  protected function getObjId(): int
54  {
55  return $this->objId;
56  }
57 
58  public function copy(int $targetObjId): void
59  {
60  if (!$this->exists()) {
61  ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '0');
62  return;
63  }
64 
65  try {
66  $this->webDirectory->copy(
67  $this->getRelativePath(),
68  preg_replace(
69  '/(' . $this->config->getSubDirectoryPrefix() . ')(\d*)\/(.*)$/',
70  '${1}' . $targetObjId . '/${3}',
71  $this->getRelativePath()
72  )
73  );
74 
75  ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '1');
76  } catch (Exception $e) {
77  ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', '0');
78  }
79  }
80 
81  public function delete(): void
82  {
83  if ($this->webDirectory->hasDir($this->getIconDirectory())) {
84  try {
85  $this->webDirectory->deleteDir($this->getIconDirectory());
86  } catch (Exception $e) {
87  }
88  }
89 
90  ilContainer::_deleteContainerSettings($this->getObjId(), 'icon_custom');
91  }
92 
96  public function getSupportedFileExtensions(): array
97  {
98  return $this->config->getSupportedFileExtensions();
99  }
100 
101  public function saveFromSourceFile(string $sourceFilePath): void
102  {
103  $this->createCustomIconDirectory();
104 
105  $fileName = $this->getRelativePath();
106 
107  if ($this->webDirectory->has($fileName)) {
108  $this->webDirectory->delete($fileName);
109  }
110 
111  $this->webDirectory->copy($sourceFilePath, $fileName);
112 
113  $this->persistIconState($fileName);
114  }
115 
116  public function saveFromHttpRequest(): void
117  {
118  $this->createCustomIconDirectory();
119 
120  $fileName = $this->getRelativePath();
121 
122  if ($this->webDirectory->has($fileName)) {
123  $this->webDirectory->delete($fileName);
124  }
125 
126  if ($this->upload->hasUploads()) {
127  if (!$this->upload->hasBeenProcessed()) {
128  $this->upload->process();
129  }
130 
132  $result = array_values($this->upload->getResults())[0];
133  if ($result->isOK()) {
134  $this->upload->moveOneFileTo(
135  $result,
136  $this->getIconDirectory(),
137  Location::WEB,
138  $this->getIconFileName(),
139  true
140  );
141  }
142 
143  foreach ($this->config->getUploadPostProcessors() as $processor) {
144  $processor->process($fileName);
145  }
146  }
147 
148  $this->persistIconState($fileName);
149  }
150 
151  protected function persistIconState(string $fileName): void
152  {
153  if ($this->webDirectory->has($fileName)) {
154  ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '1');
155  } else {
156  ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
157  }
158  }
159 
160  public function remove(): void
161  {
162  $fileName = $this->getRelativePath();
163 
164  if ($this->webDirectory->has($fileName)) {
165  $this->webDirectory->delete($fileName);
166  }
167 
168  ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', '0');
169  }
170 
174  protected function createCustomIconDirectory(): void
175  {
176  $iconDirectory = $this->getIconDirectory();
177 
178  if (!$this->webDirectory->has(dirname($iconDirectory))) {
179  $this->webDirectory->createDir(dirname($iconDirectory));
180  }
181 
182  if (!$this->webDirectory->has($iconDirectory)) {
183  $this->webDirectory->createDir($iconDirectory);
184  }
185  }
186 
187  protected function getIconDirectory(): string
188  {
189  return implode(DIRECTORY_SEPARATOR, [
190  $this->config->getBaseDirectory(),
191  $this->config->getSubDirectoryPrefix() . $this->getObjId()
192  ]);
193  }
194 
195  protected function getIconFileName(): string
196  {
197  return self::ICON_BASENAME . '.' . $this->config->getTargetFileExtension();
198  }
199 
200  protected function getRelativePath(): string
201  {
202  return implode(DIRECTORY_SEPARATOR, [
203  $this->getIconDirectory(),
204  $this->getIconFileName()
205  ]);
206  }
207 
208  public function exists(): bool
209  {
210  if (!ilContainer::_lookupContainerSetting($this->getObjId(), 'icon_custom', '0')) {
211  return false;
212  }
213 
214  return $this->webDirectory->has($this->getRelativePath());
215  }
216 
217  public function getFullPath(): string
218  {
219  // TODO: Currently there is no option to get the relative base directory of a filesystem
220  return implode(DIRECTORY_SEPARATOR, [
222  $this->getRelativePath()
223  ]);
224  }
225 
226  public function createFromImportDir(string $source_dir): void
227  {
228  $target_dir = implode(DIRECTORY_SEPARATOR, [
230  $this->getIconDirectory()
231  ]);
232  ilFileUtils::rCopy($source_dir, $target_dir);
233  $this->persistIconState($this->getRelativePath());
234  }
235 }
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...
__construct(Filesystem $webDirectory, FileUpload $uploadService, ilCustomIconObjectConfiguration $config, int $objId)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
Class FileUpload.
Definition: FileUpload.php:34
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
saveFromSourceFile(string $sourceFilePath)
ilCustomIconObjectConfiguration $config
Class FlySystemFileAccessTest disabled disabled disabled.
static _deleteContainerSettings(int $a_id, string $a_keyword="", bool $a_keyword_like=false)
Class ilObjectCustomIconImpl TODO: Inject database persistence in future instead of using ...