ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilObjCertificateSettings.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
25
26require_once './Services/Object/classes/class.ilObject.php';
27
37{
39 private $cert_logger;
40
44 private $uuidFactory;
45
50
55
60
67 public function __construct($a_id = 0, $a_reference = true)
68 {
69 global $DIC;
70
71 $this->type = 'cert';
72 parent::__construct($a_id, $a_reference);
73 $this->cert_logger = $DIC->logger()->cert();
74 $this->uuidFactory = new Factory();
75 $this->certificateSettings = new ilSetting('certificate');
76 $this->certificateRepo = new ilCertificateTemplateRepository($DIC->database());
77 $this->userCertificateRepo = new ilUserCertificateRepository($DIC->database());
78 }
79
89 public function uploadBackgroundImage(string $imageTempFileName) : bool
90 {
91 if (!empty($imageTempFileName)) {
92 $imagePath = $this->getBackgroundImageDefaultFolder();
93 $newImageFileName = "background_{$this->uuidFactory->uuid4AsString()}.jpg";
94 $newImagePath = $imagePath . $newImageFileName;
95
96 if (!file_exists($imagePath)) {
97 ilUtil::makeDirParents($imagePath);
98 }
99 // upload the file
101 $imageTempFileName,
102 basename($this->getDefaultBackgroundImageTempFilePath()),
104 )) {
105 $this->cert_logger->error(sprintf(
106 "Could not upload certificate background image from '%s' to temporary file '%s' (name: '%s')",
107 $imageTempFileName,
110 ));
111 return false;
112 }
113
114 // convert the uploaded file to JPEG
117 $newImagePath,
118 'JPEG'
119 );
123 'JPEG',
124 100
125 );
126
127 if (!is_file($newImagePath) || !file_exists($newImagePath)) {
128 // Something went wrong converting the file. Use the original file and hope, that PDF can work with it.
129 $this->cert_logger->error(sprintf(
130 "Could not convert certificate background image from '%s' as JPEG to '%s', trying fallbacj ...",
132 $newImagePath
133 ));
134 if (!rename(
136 $newImagePath
137 )) {
138 $this->cert_logger->error(sprintf(
139 "Could not upload certificate background image from '%s' to final file '%s' (name: '%s')",
141 $newImagePath,
142 $newImageFileName
143 ));
144 return false;
145 }
146 }
147
148 if (
150 && file_exists($this->getDefaultBackgroundImageTempFilePath())
151 ) {
153 }
154
155 if (file_exists($newImagePath) && (filesize($newImagePath) > 0)) {
156 $oldPath = $this->getDefaultBackgroundImagePath();
157 $oldPathThumb = $this->getDefaultBackgroundImageThumbPath();
158
159 $oldRelativePath = $this->getDefaultBackgroundImagePath(true);
160 $this->certificateSettings->set('defaultImageFileName', $newImageFileName);
161 $newRelativePath = $this->getDefaultBackgroundImagePath(true);
162
163 $this->certificateRepo->updateDefaultBackgroundImagePaths($oldRelativePath, $newRelativePath);
164
165 if (
166 !$this->certificateRepo->isBackgroundImageUsed($oldRelativePath)
167 && !$this->userCertificateRepo->isBackgroundImageUsed($oldRelativePath)
168 ) {
169 if (is_file($oldPath) && file_exists($oldPath)) {
170 unlink($oldPath);
171 }
172
173 if (is_file($oldPathThumb) && file_exists($oldPathThumb)) {
174 unlink($oldPathThumb);
175 }
176 }
177
178
179 return true;
180 }
181
182 $this->cert_logger->error(sprintf(
183 "Final background image '%s' does not exist or is empty",
184 $newImagePath
185 ));
186 }
187
188 return false;
189 }
190
196 public function deleteBackgroundImage() : bool
197 {
198 $result = true;
199
200
201 if (
202 $this->certificateSettings->get('defaultImageFileName', '')
203 && !$this->certificateRepo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
204 && !$this->userCertificateRepo->isBackgroundImageUsed($this->getDefaultBackgroundImagePath(true))
205 ) {
206 //No certificates exist using the currently configured file, deleting file possible.
207
208 if (is_file($this->getDefaultBackgroundImageThumbPath())) {
210 }
211 if (is_file($this->getDefaultBackgroundImagePath())) {
212 $result &= unlink($this->getDefaultBackgroundImagePath());
213 }
214 if (is_file($this->getDefaultBackgroundImageTempFilePath())) {
216 }
217 }
218
219 $this->certificateSettings->set('defaultImageFileName', '');
220
221 return $result;
222 }
223
224 public function getBackgroundImageDefaultFolder(bool $relativePath = false) : string
225 {
226 return ($relativePath ? '' : CLIENT_WEB_DIR) . '/certificates/default/';
227 }
228
234 public function getDefaultBackgroundImagePath(bool $relativePath = false) : string
235 {
236 return $this->getBackgroundImageDefaultFolder($relativePath)
237 . $this->certificateSettings->get('defaultImageFileName', '');
238 }
239
245 public function getDefaultBackgroundImageThumbPath(bool $relativePath = false) : string
246 {
248 }
249
255 private function getDefaultBackgroundImageTempFilePath() : string
256 {
258 }
259
263 public function hasBackgroundImage() : bool
264 {
265 $filePath = $this->getDefaultBackgroundImagePath();
266 return is_file($filePath) && filesize($filePath) > 0;
267 }
268
273 public function getDefaultBackgroundImagePathWeb() : string
274 {
275 return str_replace(
276 ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH),
279 );
280 }
281
286 public function getBackgroundImageThumbPathWeb() : string
287 {
288 return str_replace(
289 ilUtil::removeTrailingPathSeparators(ILIAS_ABSOLUTE_PATH),
292 );
293 }
294}
$result
An exception for terminatinating execution or to throw for unit testing.
Class ilObjCertificateSettings.
getDefaultBackgroundImagePath(bool $relativePath=false)
Returns the filesystem path of the background image.
getDefaultBackgroundImagePathWeb()
Returns the web path of the background image.
getBackgroundImageDefaultFolder(bool $relativePath=false)
__construct($a_id=0, $a_reference=true)
Constructor @access public.
getDefaultBackgroundImageThumbPath(bool $relativePath=false)
Returns the filesystem path of the background image thumbnail.
getDefaultBackgroundImageTempFilePath()
Returns the filesystem path of the background image temp file during upload.
deleteBackgroundImage()
Deletes the background image of a certificate.
uploadBackgroundImage(string $imageTempFileName)
Uploads a background image for the certificate.
getBackgroundImageThumbPathWeb()
Returns the web path of the background image thumbnail.
Class ilObject Basic functions for all objects.
ILIAS Setting Class.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static convertImage( $a_from, $a_to, $a_target_format="", $a_geometry="", $a_background_color="")
convert image
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static removeTrailingPathSeparators($path)
const CLIENT_WEB_DIR
Definition: constants.php:45
global $DIC
Definition: goto.php:24
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc