ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilObjCertificateSettings.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
32 {
34  private \ILIAS\Filesystem\Util\Convert\LegacyImages $file_converter;
39 
40  public function __construct(int $a_id = 0, bool $a_reference = true)
41  {
42  global $DIC;
43 
44  parent::__construct($a_id, $a_reference);
45  $this->type = 'cert';
46  $this->cert_logger = $DIC->logger()->cert();
47  $this->file_converter = $DIC->fileConverters()->legacyImages();
48  $this->uuid_factory = new Factory();
49  $this->certificate_settings = new ilSetting('certificate');
50  $this->certificate_repo = new ilCertificateTemplateDatabaseRepository($DIC->database());
51  $this->user_certificate_repo = new ilUserCertificateRepository($DIC->database());
52  }
53 
60  public function uploadBackgroundImage(UploadResult $upload_result): bool
61  {
62  $image_temp_file_name = $upload_result->getPath();
63  if ($image_temp_file_name !== '') {
64  $extension = pathinfo($upload_result->getName(), PATHINFO_EXTENSION);
65  $image_path = $this->getBackgroundImageDefaultFolder();
66  $new_image_file_name = "background_{$this->uuid_factory->uuid4AsString()}.jpg";
67  $new_image_path = $image_path . $new_image_file_name;
68 
69  if (!is_dir($image_path)) {
70  ilFileUtils::makeDirParents($image_path);
71  }
72  // upload the file
74  $image_temp_file_name,
75  basename($this->getDefaultBackgroundImageTempFilePath($extension)),
76  $this->getDefaultBackgroundImageTempFilePath($extension)
77  )) {
78  $this->cert_logger->error(sprintf(
79  "Could not upload certificate background image from '%s' to temporary file '%s' (name: '%s')",
80  $image_temp_file_name,
81  $this->getDefaultBackgroundImageTempFilePath($extension),
82  basename($this->getDefaultBackgroundImageTempFilePath($extension))
83  ));
84  return false;
85  }
86 
87  if (!is_file($this->getDefaultBackgroundImageTempFilePath($extension))) {
88  $this->cert_logger->error(sprintf(
89  "Uploaded certificate background image could not be moved to temporary file '%s'",
90  $this->getDefaultBackgroundImageTempFilePath($extension)
91  ));
92  return false;
93  }
94 
95  // convert the uploaded file to JPEG
96  $this->file_converter->convertToFormat(
97  $this->getDefaultBackgroundImageTempFilePath($extension),
98  $new_image_path,
99  ImageOutputOptions::FORMAT_JPG
100  );
101 
102  $this->file_converter->croppedSquare(
103  $this->getDefaultBackgroundImageTempFilePath($extension),
105  100,
106  ImageOutputOptions::FORMAT_JPG
107  );
108 
109  if (!is_file($new_image_path) || !file_exists($new_image_path)) {
110  // Something went wrong converting the file. Use the original file and hope, that PDF can work with it.
111  $this->cert_logger->error(sprintf(
112  "Could not convert certificate background image from '%s' as JPEG to '%s', trying fallback ...",
113  $this->getDefaultBackgroundImageTempFilePath($extension),
114  $new_image_path
115  ));
117  $this->getDefaultBackgroundImageTempFilePath($extension),
118  $new_image_file_name,
119  $new_image_path
120  )) {
121  $this->cert_logger->error(sprintf(
122  "Could not upload certificate background image from '%s' to final file '%s' (name: '%s')",
123  $this->getDefaultBackgroundImageTempFilePath($extension),
124  $new_image_path,
125  $new_image_file_name
126  ));
127  return false;
128  }
129  }
130 
131  if (
132  is_file($this->getDefaultBackgroundImageTempFilePath($extension))
133  && file_exists($this->getDefaultBackgroundImageTempFilePath($extension))
134  ) {
135  unlink($this->getDefaultBackgroundImageTempFilePath($extension));
136  }
137 
138  if (file_exists($new_image_path) && (filesize($new_image_path) > 0)) {
139  $old_path = $this->getDefaultBackgroundImagePath();
140  $old_path_thumb = $this->getDefaultBackgroundImageThumbPath();
141  $old_relative_path = $this->getDefaultBackgroundImagePath(true);
142  $this->certificate_settings->set('defaultImageFileName', $new_image_file_name);
143  $new_relative_path = $this->getDefaultBackgroundImagePath(true);
144 
145  $this->certificate_repo->updateDefaultBackgroundImagePaths($old_relative_path, $new_relative_path);
146 
147  if (
148  !$this->certificate_repo->isBackgroundImageUsed($old_relative_path)
149  && !$this->user_certificate_repo->isBackgroundImageUsed($old_relative_path)
150  ) {
151  if (is_file($old_path) && file_exists($old_path)) {
152  unlink($old_path);
153  }
154 
155  if (is_file($old_path_thumb) && file_exists($old_path_thumb)) {
156  unlink($old_path_thumb);
157  }
158  }
159  return true;
160  }
161 
162  $this->cert_logger->error(sprintf(
163  "Final background image '%s' does not exist or is empty",
164  $new_image_path
165  ));
166  }
167 
168  return false;
169  }
170 
171  public function deleteBackgroundImage(): bool
172  {
173  $result = true;
174 
175 
176  if (
177  $this->certificate_settings->get('defaultImageFileName', '')
178  && !$this->certificate_repo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
179  && !$this->user_certificate_repo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
180  ) {
181  //No certificates exist using the currently configured file, deleting file possible.
182 
183  if (is_file($this->getDefaultBackgroundImageThumbPath())) {
184  $result &= unlink($this->getDefaultBackgroundImageThumbPath());
185  }
186  if (is_file($this->getDefaultBackgroundImagePath())) {
187  $result &= unlink($this->getDefaultBackgroundImagePath());
188  }
189 
191  if (is_file($this->getDefaultBackgroundImageTempFilePath($extension))) {
192  $result &= unlink($this->getDefaultBackgroundImageTempFilePath($extension));
193  }
194  }
195  }
196 
197  $this->certificate_settings->set('defaultImageFileName', '');
198 
201  return (bool) $result; // Don't remove the cast, otherwise $result will be 1 or 0
202  }
203 
204  public function getBackgroundImageDefaultFolder(bool $relativePath = false): string
205  {
206  return ($relativePath ? '' : CLIENT_WEB_DIR) . '/certificates/default/';
207  }
208 
209  public function getDefaultBackgroundImagePath(bool $relativePath = false): string
210  {
211  return $this->getBackgroundImageDefaultFolder($relativePath)
212  . $this->certificate_settings->get('defaultImageFileName', '');
213  }
214 
215  public function getDefaultBackgroundImageThumbPath(bool $relativePath = false): string
216  {
218  }
219 
220  private function getDefaultBackgroundImageTempFilePath(string $extension): string
221  {
222  return implode('', [
225  '.' . $extension
226  ]);
227  }
228 
229  public function hasBackgroundImage(): bool
230  {
231  $filePath = $this->getDefaultBackgroundImagePath();
232 
233  return is_file($filePath) && filesize($filePath) > 0;
234  }
235 
236  public function getDefaultBackgroundImagePathWeb(): string
237  {
238  return str_replace(
239  ilFileUtils::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH),
242  );
243  }
244 
245  public function getBackgroundImageThumbPathWeb(): string
246  {
247  return str_replace(
248  ilFileUtils::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH),
251  );
252  }
253 }
uploadBackgroundImage(UploadResult $upload_result)
Uploads a background image for the certificate.
getDefaultBackgroundImageThumbPath(bool $relativePath=false)
getBackgroundImageDefaultFolder(bool $relativePath=false)
ILIAS Filesystem Util Convert LegacyImages $file_converter
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
getDefaultBackgroundImageTempFilePath(string $extension)
static removeTrailingPathSeparators(string $path)
global $DIC
Definition: feed.php:28
Class ilObjCertificateSettings.
ilUserCertificateRepository $user_certificate_repo
__construct(VocabulariesInterface $vocabularies)
ilCertificateTemplateDatabaseRepository $certificate_repo
const CLIENT_WEB_DIR
Definition: constants.php:47
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
getDefaultBackgroundImagePath(bool $relativePath=false)
__construct(int $a_id=0, bool $a_reference=true)