ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilObjectTileImage.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
12 {
16  protected $service;
17 
21  protected $obj_id;
22 
26  protected $web;
27 
31  protected $upload;
32 
36  protected $ext;
37 
42  {
43  $this->service = $service;
44  $this->obj_id = $obj_id;
45  $this->web = $service->filesystem()->web();
46  $this->upload = $service->upload();
47  $this->ext = ilContainer::_lookupContainerSetting($obj_id, 'tile_image');
48  }
49 
53  public function getExtension() : string
54  {
55  return $this->ext;
56  }
57 
61  public function copy(int $target_obj_id)
62  {
63  if (!$this->exists()) {
64  ilContainer::_deleteContainerSettings($target_obj_id, 'tile_image');
65  return;
66  }
67 
68  try {
69  $this->web->copy(
70  $this->getRelativePath(),
71  preg_replace(
72  '/(' . "tile_image_" . ')(\d*)\/(.*)$/',
73  '${1}' . $target_obj_id . '/${3}',
74  $this->getRelativePath()
75  )
76  );
77 
78  ilContainer::_writeContainerSetting($target_obj_id, 'tile_image', $this->getExtension());
79  } catch (\Exception $e) {
80  ilContainer::_deleteContainerSettings($target_obj_id, 'tile_image');
81  }
82  }
83 
87  public function delete()
88  {
89  if ($this->web->hasDir($this->getRelativeDirectory())) {
90  try {
91  $this->web->deleteDir($this->getRelativeDirectory());
92  } catch (\Exception $e) {
93  }
94  }
95 
96  ilContainer::_deleteContainerSettings($this->obj_id, 'tile_image');
97  }
98 
102  public function saveFromHttpRequest(string $tmpname)
103  {
104  $this->createDirectory();
105 
106  // remove old file
107  $file_name = $this->getRelativePath();
108  if ($this->web->has($file_name)) {
109  $this->web->delete($file_name);
110  }
111 
112  if ($this->upload->hasUploads()) {
113  if (!$this->upload->hasBeenProcessed()) {
114  $this->upload->process();
115  }
116 
118  $results = $this->upload->getResults();
119  if (isset($results[$tmpname])) {
120  $result = $results[$tmpname];
121  $this->ext = pathinfo($result->getName(), PATHINFO_EXTENSION);
122  $file_name = $this->getRelativePath();
124  $this->upload->moveOneFileTo(
125  $result,
126  $this->getRelativeDirectory(),
127  \ILIAS\FileUpload\Location::WEB,
128  $this->getFileName(),
129  true
130  );
131 
132 
133  $fullpath = CLIENT_WEB_DIR . "/" . $this->getRelativeDirectory() . "/" . $this->getFileName();
134  list($width, $height, $type, $attr) = getimagesize($fullpath);
135  $min = min($width, $height);
136  ilUtil::execConvert($fullpath . "[0] -geometry " . $min . "x" . $min . "^ -gravity center -extent " . $min . "x" . $min . " " . $fullpath);
137  }
138  }
139  }
140  $this->persistImageState($file_name);
141  }
142 
146  protected function persistImageState($filename)
147  {
148  $ext = pathinfo($filename, PATHINFO_EXTENSION);
149 
150  if ($this->web->has($filename)) {
151  ilContainer::_writeContainerSetting($this->obj_id, 'tile_image', $ext);
152  } else {
153  ilContainer::_deleteContainerSettings($this->obj_id, 'tile_image');
154  }
155  }
156 
160  protected function createDirectory()
161  {
162  $this->web->createDir($this->getRelativeDirectory());
163 
164  /*
165 
166  $rel_directory = $this->getRelDirectory();
167 
168  if (!$this->web->has(dirname($rel_directory))) {
169  $this->web->createDir(dirname($rel_directory));
170  }
171 
172  if (!$this->web->has($rel_directory)) {
173  $this->web->createDir($rel_directory);
174  }*/
175  }
176 
180  public function getRelativeDirectory()
181  {
182  return implode(DIRECTORY_SEPARATOR, [
183  "obj_data",
184  "tile_image",
185  "tile_image_" . $this->obj_id
186  ]);
187  }
188 
192  protected function getFileName()
193  {
194  return 'tile_image.' . $this->getExtension();
195  }
196 
200  protected function getRelativePath()
201  {
202  return implode(DIRECTORY_SEPARATOR, [
203  $this->getRelativeDirectory(),
204  $this->getFileName()
205  ]);
206  }
207 
211  public function exists() : bool
212  {
213  if (!\ilContainer::_lookupContainerSetting($this->obj_id, 'tile_image', 0)) {
214  return false;
215  }
216  return $this->web->has($this->getRelativePath());
217  }
218 
222  public function getFullPath() : string
223  {
224  // TODO: Currently there is no option to get the relative base directory of a filesystem
225  return implode(DIRECTORY_SEPARATOR, [
227  $this->getRelativePath()
228  ]);
229  }
230 
238  public function createFromImportDir($source_dir, $ext)
239  {
240  $target_dir = implode(DIRECTORY_SEPARATOR, [
242  $this->getRelativeDirectory()
243  ]);
244  ilUtil::rCopy($source_dir, $target_dir);
245  ilContainer::_writeContainerSetting($this->obj_id, 'tile_image', $ext);
246  }
247 }
__construct(ilObjectService $service, int $obj_id)
Constructor.
$result
$type
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
Class BaseForm.
exists()
Does tile image file exist?bool
upload()
Get filesystems.
getExtension()
Get extenstion.string
copy(int $target_obj_id)
Copy tile image to repository object.
createFromImportDir($source_dir, $ext)
static execConvert($args)
execute convert command
filesystem()
Get filesystems.
static _deleteContainerSettings($a_id, $a_keyword=null, $a_keyword_like=false)
$filename
Definition: buildRTE.php:89
saveFromHttpRequest(string $tmpname)
Save image from request.
$results
Definition: svg-scanner.php:47
getFullPath()
Get full path of the tile image file.string
static _writeContainerSetting($a_id, $a_keyword, $a_value)
static getWebspaceDir($mode="filesystem")
get webspace directory
static _lookupContainerSetting($a_id, $a_keyword, $a_default_value=null)
Lookup a container setting.