ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilObjectCustomIconImpl.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
6 
12 {
13  const ICON_BASENAME = 'icon_custom';
14 
16  protected $webDirectory;
17 
19  protected $upload;
20 
22  protected $config;
23 
25  protected $objId;
26 
35  {
36  $this->objId = $objId;
37 
38  $this->webDirectory = $webDirectory;
39  $this->upload = $uploadService;
40  $this->config = $config;
41  }
42 
46  protected function getObjId() : int
47  {
48  return $this->objId;
49  }
50 
54  public function copy(int $targetObjId)
55  {
56  if (!$this->exists()) {
57  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', 0);
58  return;
59  }
60 
61  try {
62  $this->webDirectory->copy(
63  $this->getRelativePath(),
64  preg_replace(
65  '/(' . $this->config->getSubDirectoryPrefix() . ')(\d*)\/(.*)$/',
66  '${1}' . $targetObjId . '/${3}',
67  $this->getRelativePath()
68  )
69  );
70 
71  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', 1);
72  } catch (\Exception $e) {
73  \ilContainer::_writeContainerSetting($targetObjId, 'icon_custom', 0);
74  }
75  }
76 
80  public function delete()
81  {
82  if ($this->webDirectory->hasDir($this->getIconDirectory())) {
83  try {
84  $this->webDirectory->deleteDir($this->getIconDirectory());
85  } catch (\Exception $e) {
86  }
87  }
88 
89  \ilContainer::_deleteContainerSettings($this->getObjId(), 'icon_custom');
90  }
91 
95  public function getSupportedFileExtensions() : array
96  {
97  return $this->config->getSupportedFileExtensions();
98  }
99 
103  public function saveFromSourceFile(string $sourceFilePath)
104  {
105  $this->createCustomIconDirectory();
106 
107  $fileName = $this->getRelativePath();
108 
109  if ($this->webDirectory->has($fileName)) {
110  $this->webDirectory->delete($fileName);
111  }
112 
113  $this->webDirectory->copy($sourceFilePath, $fileName);
114 
115  $this->persistIconState($fileName);
116  }
117 
121  public function saveFromHttpRequest()
122  {
123  $this->createCustomIconDirectory();
124 
125  $fileName = $this->getRelativePath();
126 
127  if ($this->webDirectory->has($fileName)) {
128  $this->webDirectory->delete($fileName);
129  }
130 
131  if ($this->upload->hasUploads() && !$this->upload->hasBeenProcessed()) {
132  $this->upload->process();
133 
135  $result = array_values($this->upload->getResults())[0];
137  $this->upload->moveOneFileTo(
138  $result,
139  $this->getIconDirectory(),
140  \ILIAS\FileUpload\Location::WEB,
141  $this->getIconFileName(),
142  true
143  );
144  }
145 
146  foreach ($this->config->getUploadPostProcessors() as $processor) {
147  $processor->process($fileName);
148  }
149  }
150 
151  $this->persistIconState($fileName);
152  }
153 
157  protected function persistIconState(string $fileName)
158  {
159  if ($this->webDirectory->has($fileName)) {
160  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', 1);
161  } else {
162  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', 0);
163  }
164  }
165 
169  public function remove()
170  {
171  $fileName = $this->getRelativePath();
172 
173  if ($this->webDirectory->has($fileName)) {
174  $this->webDirectory->delete($fileName);
175  }
176 
177  \ilContainer::_writeContainerSetting($this->getObjId(), 'icon_custom', 0);
178  }
179 
183  protected function createCustomIconDirectory()
184  {
185  $iconDirectory = $this->getIconDirectory();
186 
187  if (!$this->webDirectory->has(dirname($iconDirectory))) {
188  $this->webDirectory->createDir(dirname($iconDirectory));
189  }
190 
191  if (!$this->webDirectory->has($iconDirectory)) {
192  $this->webDirectory->createDir($iconDirectory);
193  }
194  }
195 
199  protected function getIconDirectory() : string
200  {
201  return implode(DIRECTORY_SEPARATOR, [
202  $this->config->getBaseDirectory(),
203  $this->config->getSubDirectoryPrefix() . $this->getObjId()
204  ]);
205  }
206 
210  protected function getIconFileName() : string
211  {
212  return self::ICON_BASENAME . '.' . $this->config->getTargetFileExtension();
213  }
214 
218  protected function getRelativePath() : string
219  {
220  return implode(DIRECTORY_SEPARATOR, [
221  $this->getIconDirectory(),
222  $this->getIconFileName()
223  ]);
224  }
225 
229  public function exists() : bool
230  {
231  if (!\ilContainer::_lookupContainerSetting($this->getObjId(), 'icon_custom', 0)) {
232  return false;
233  }
234 
235  return $this->webDirectory->has($this->getRelativePath());
236  }
237 
241  public function getFullPath() : string
242  {
243  // TODO: Currently there is no option to get the relative base directory of a filesystem
244  return implode(DIRECTORY_SEPARATOR, [
246  $this->getRelativePath()
247  ]);
248  }
249 
257  public function createFromImportDir($source_dir)
258  {
259  $target_dir = implode(DIRECTORY_SEPARATOR, [
261  $this->getIconDirectory()
262  ]);
263  ilUtil::rCopy($source_dir, $target_dir);
264  $this->persistIconState($this->getRelativePath());
265  }
266 }
$result
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
Class BaseForm.
static _deleteContainerSettings($a_id, $a_keyword=null, $a_keyword_like=false)
Class FileUpload.
Definition: FileUpload.php:21
Interface ilObjectCustomIcon.
static _writeContainerSetting($a_id, $a_keyword, $a_value)
saveFromSourceFile(string $sourceFilePath)
static getWebspaceDir($mode="filesystem")
get webspace directory
__construct(Filesystem $webDirectory, FileUpload $uploadService, \ilCustomIconObjectConfiguration $config, int $objId)
ilObjectCustomIconImpl constructor.
Class FlySystemFileAccessTest.
static _lookupContainerSetting($a_id, $a_keyword, $a_default_value=null)
Lookup a container setting.
Class ilObjectCustomIconImpl TODO: Inject database persistence in future instead of using ...