ILIAS  release_8 Revision v8.24
class.ilObjCertificateSettings.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
31{
37
38 public function __construct(int $a_id = 0, bool $a_reference = true)
39 {
40 global $DIC;
41
42 parent::__construct($a_id, $a_reference);
43 $this->type = 'cert';
44 $this->cert_logger = $DIC->logger()->cert();
45 $this->uuid_factory = new Factory();
46 $this->certificate_settings = new ilSetting('certificate');
47 $this->certificate_repo = new ilCertificateTemplateDatabaseRepository($DIC->database());
48 $this->user_certificate_repo = new ilUserCertificateRepository($DIC->database());
49 }
50
57 public function uploadBackgroundImage(UploadResult $upload_result): bool
58 {
59 $image_temp_file_name = $upload_result->getPath();
60 if ($image_temp_file_name !== '') {
61 $extension = pathinfo($upload_result->getName(), PATHINFO_EXTENSION);
62 $image_path = $this->getBackgroundImageDefaultFolder();
63 $new_image_file_name = "background_{$this->uuid_factory->uuid4AsString()}.jpg";
64 $new_image_path = $image_path . $new_image_file_name;
65
66 if (!is_dir($image_path)) {
67 ilFileUtils::makeDirParents($image_path);
68 }
69 // upload the file
71 $image_temp_file_name,
72 basename($this->getDefaultBackgroundImageTempFilePath($extension)),
74 )) {
75 $this->cert_logger->error(sprintf(
76 "Could not upload certificate background image from '%s' to temporary file '%s' (name: '%s')",
77 $image_temp_file_name,
79 basename($this->getDefaultBackgroundImageTempFilePath($extension))
80 ));
81 return false;
82 }
83
84 if (!is_file($this->getDefaultBackgroundImageTempFilePath($extension))) {
85 $this->cert_logger->error(sprintf(
86 "Uploaded certificate background image could not be moved to temporary file '%s'",
88 ));
89 return false;
90 }
91
92 // convert the uploaded file to JPEG
95 $new_image_path,
96 'JPEG'
97 );
101 'JPEG',
102 '100'
103 );
104
105 if (!is_file($new_image_path) || !file_exists($new_image_path)) {
106 // Something went wrong converting the file. Use the original file and hope, that PDF can work with it.
107 $this->cert_logger->error(sprintf(
108 "Could not convert certificate background image from '%s' as JPEG to '%s', trying fallbacj ...",
109 $this->getDefaultBackgroundImageTempFilePath($extension),
110 $new_image_path
111 ));
113 $this->getDefaultBackgroundImageTempFilePath($extension),
114 $new_image_file_name,
115 $new_image_path
116 )) {
117 $this->cert_logger->error(sprintf(
118 "Could not upload certificate background image from '%s' to final file '%s' (name: '%s')",
119 $this->getDefaultBackgroundImageTempFilePath($extension),
120 $new_image_path,
121 $new_image_file_name
122 ));
123 return false;
124 }
125 }
126
127 if (
128 is_file($this->getDefaultBackgroundImageTempFilePath($extension))
129 && file_exists($this->getDefaultBackgroundImageTempFilePath($extension))
130 ) {
131 unlink($this->getDefaultBackgroundImageTempFilePath($extension));
132 }
133
134 if (file_exists($new_image_path) && (filesize($new_image_path) > 0)) {
135 $old_path = $this->getDefaultBackgroundImagePath();
136 $old_path_thumb = $this->getDefaultBackgroundImageThumbPath();
137 $old_relative_path = $this->getDefaultBackgroundImagePath(true);
138 $this->certificate_settings->set('defaultImageFileName', $new_image_file_name);
139 $new_relative_path = $this->getDefaultBackgroundImagePath(true);
140
141 $this->certificate_repo->updateDefaultBackgroundImagePaths($old_relative_path, $new_relative_path);
142
143 if (
144 !$this->certificate_repo->isBackgroundImageUsed($old_relative_path)
145 && !$this->user_certificate_repo->isBackgroundImageUsed($old_relative_path)
146 ) {
147 if (is_file($old_path) && file_exists($old_path)) {
148 unlink($old_path);
149 }
150
151 if (is_file($old_path_thumb) && file_exists($old_path_thumb)) {
152 unlink($old_path_thumb);
153 }
154 }
155 return true;
156 }
157
158 $this->cert_logger->error(sprintf(
159 "Final background image '%s' does not exist or is empty",
160 $new_image_path
161 ));
162 }
163
164 return false;
165 }
166
167 public function deleteBackgroundImage(): bool
168 {
169 $result = true;
170
171
172 if (
173 $this->certificate_settings->get('defaultImageFileName', '')
174 && !$this->certificate_repo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
175 && !$this->user_certificate_repo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
176 ) {
177 //No certificates exist using the currently configured file, deleting file possible.
178
179 if (is_file($this->getDefaultBackgroundImageThumbPath())) {
180 $result &= unlink($this->getDefaultBackgroundImageThumbPath());
181 }
182 if (is_file($this->getDefaultBackgroundImagePath())) {
183 $result &= unlink($this->getDefaultBackgroundImagePath());
184 }
185
187 if (is_file($this->getDefaultBackgroundImageTempFilePath($extension))) {
188 $result &= unlink($this->getDefaultBackgroundImageTempFilePath($extension));
189 }
190 }
191 }
192
193 $this->certificate_settings->set('defaultImageFileName', '');
194
197 return (bool) $result; // Don't remove the cast, otherwise $result will be 1 or 0
198 }
199
200 public function getBackgroundImageDefaultFolder(bool $relativePath = false): string
201 {
202 return ($relativePath ? '' : CLIENT_WEB_DIR) . '/certificates/default/';
203 }
204
205 public function getDefaultBackgroundImagePath(bool $relativePath = false): string
206 {
207 return $this->getBackgroundImageDefaultFolder($relativePath)
208 . $this->certificate_settings->get('defaultImageFileName', '');
209 }
210
211 public function getDefaultBackgroundImageThumbPath(bool $relativePath = false): string
212 {
214 }
215
216 private function getDefaultBackgroundImageTempFilePath(string $extension): string
217 {
218 return implode('', [
221 '.' . $extension
222 ]);
223 }
224
225 public function hasBackgroundImage(): bool
226 {
227 $filePath = $this->getDefaultBackgroundImagePath();
228
229 return is_file($filePath) && filesize($filePath) > 0;
230 }
231
232 public function getDefaultBackgroundImagePathWeb(): string
233 {
234 return str_replace(
238 );
239 }
240
241 public function getBackgroundImageThumbPathWeb(): string
242 {
243 return str_replace(
247 );
248 }
249}
static makeDirParents(string $a_dir)
Create a new directory and all parent directories.
static removeTrailingPathSeparators(string $path)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
Component logger with individual log levels by component id.
Class ilObjCertificateSettings.
ilCertificateTemplateDatabaseRepository $certificate_repo
ilUserCertificateRepository $user_certificate_repo
getDefaultBackgroundImageTempFilePath(string $extension)
getDefaultBackgroundImagePath(bool $relativePath=false)
uploadBackgroundImage(UploadResult $upload_result)
Uploads a background image for the certificate.
getBackgroundImageDefaultFolder(bool $relativePath=false)
getDefaultBackgroundImageThumbPath(bool $relativePath=false)
__construct(int $a_id=0, bool $a_reference=true)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
convert image
const CLIENT_WEB_DIR
Definition: constants.php:47
global $DIC
Definition: feed.php:28
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc