ILIAS  release_8 Revision v8.24
class.ilCertificateCloneAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
30{
37 private string $webDirectory;
40
41 public function __construct(
45 ?Filesystem $fileSystem = null,
46 ?ilLogger $logger = null,
50 string $global_certificate_path = null
51 ) {
52 $this->database = $database;
53 $this->pathFactory = $pathFactory;
54 $this->templateRepository = $templateRepository;
55
56 if (null === $logger) {
57 global $DIC;
58 $logger = $DIC->logger()->cert();
59 }
60 $this->logger = $logger;
61
62 if (null === $fileSystem) {
63 global $DIC;
64 $fileSystem = $DIC->filesystem()->web();
65 }
66 $this->fileSystem = $fileSystem;
67
68 if (null === $objectHelper) {
70 }
71 $this->objectHelper = $objectHelper;
72
75 }
76 $this->global_certificate_settings = $global_certificate_settings;
77
78 if (null === $global_certificate_path) {
79 $global_certificate_path = $this->global_certificate_settings->getDefaultBackgroundImagePath(true);
80 }
81 $this->global_certificate_path = $global_certificate_path;
82
83
84 $this->webDirectory = $webDirectory;
85 }
86
98 public function cloneCertificate(
99 ilObject $oldObject,
100 ilObject $newObject,
101 string $iliasVersion = ILIAS_VERSION_NUMERIC,
102 string $webDir = CLIENT_WEB_DIR
103 ): void {
104 $oldType = $oldObject->getType();
105 $newType = $newObject->getType();
106
107 if ($oldType !== $newType) {
108 throw new ilException(sprintf(
109 'The types "%s" and "%s" for cloning does not match',
110 $oldType,
111 $newType
112 ));
113 }
114
115 $certificatePath = $this->pathFactory->create($newObject);
116
117 $templates = $this->templateRepository->fetchCertificateTemplatesByObjId($oldObject->getId());
118
120 foreach ($templates as $template) {
121 $backgroundImagePath = $template->getBackgroundImagePath();
122 $backgroundImageFile = basename($backgroundImagePath);
123 $backgroundImageThumbnail = dirname($backgroundImagePath) . '/background.jpg.thumb.jpg';
124
125 $newBackgroundImage = '';
126 $newBackgroundImageThumbnail = '';
127 if ($this->global_certificate_path !== $backgroundImagePath) {
128 if ($this->fileSystem->has($backgroundImagePath) &&
129 !$this->fileSystem->hasDir($backgroundImagePath)
130 ) {
131 $newBackgroundImage = $certificatePath . $backgroundImageFile;
132 $newBackgroundImageThumbnail = str_replace(
133 $webDir,
134 '',
135 $this->getBackgroundImageThumbPath($certificatePath)
136 );
137 if ($this->fileSystem->has($newBackgroundImage) &&
138 !$this->fileSystem->hasDir($newBackgroundImage)
139 ) {
140 $this->fileSystem->delete($newBackgroundImage);
141 }
142
143 $this->fileSystem->copy(
144 $backgroundImagePath,
145 $newBackgroundImage
146 );
147 }
148
149 if (
150 $newBackgroundImageThumbnail !== '' &&
151 $this->fileSystem->has($backgroundImageThumbnail) &&
152 !$this->fileSystem->hasDir($backgroundImageThumbnail)
153 ) {
154 if ($this->fileSystem->has($newBackgroundImageThumbnail) &&
155 !$this->fileSystem->hasDir($newBackgroundImageThumbnail)
156 ) {
157 $this->fileSystem->delete($newBackgroundImageThumbnail);
158 }
159
160 $this->fileSystem->copy(
161 $backgroundImageThumbnail,
162 $newBackgroundImageThumbnail
163 );
164 }
165 } else {
166 $newBackgroundImage = $this->global_certificate_path;
167 }
168
169 $newCardThumbImage = '';
170 $cardThumbImagePath = $template->getThumbnailImagePath();
171
172 if ($this->fileSystem->has($cardThumbImagePath) && !$this->fileSystem->hasDir($cardThumbImagePath)) {
173 $newCardThumbImage = $certificatePath . basename($cardThumbImagePath);
174 if ($this->fileSystem->has($newCardThumbImage) && !$this->fileSystem->hasDir($newCardThumbImage)) {
175 $this->fileSystem->delete($newCardThumbImage);
176 }
177 $this->fileSystem->copy(
178 $cardThumbImagePath,
179 $newCardThumbImage
180 );
181 }
182
183 $newTemplate = new ilCertificateTemplate(
184 $newObject->getId(),
185 $this->objectHelper->lookupType($newObject->getId()),
186 $template->getCertificateContent(),
187 $template->getCertificateHash(),
188 $template->getTemplateValues(),
189 $template->getVersion(),
190 $iliasVersion,
191 time(),
192 $template->isCurrentlyActive(),
193 $newBackgroundImage,
194 $newCardThumbImage
195 );
196
197 $this->templateRepository->save($newTemplate);
198 }
199
200 // #10271
201 if ($this->isActive($oldObject->getId())) {
202 $this->database->replace(
203 'il_certificate',
204 ['obj_id' => ['integer', $newObject->getId()]],
205 []
206 );
207 }
208 }
209
210 private function isActive(int $objectId): bool
211 {
212 $sql = 'SELECT 1 FROM il_certificate WHERE obj_id = ' . $this->database->quote($objectId, 'integer');
213
214 if ($row = $this->database->fetchAssoc($this->database->query($sql))) {
215 return true;
216 }
217
218 return false;
219 }
220
221 private function getBackgroundImageName(): string
222 {
223 return "background.jpg";
224 }
225
226 private function getBackgroundImageThumbPath(string $certificatePath): string
227 {
228 return $this->webDirectory . $certificatePath . $this->getBackgroundImageName() . ".thumb.jpg";
229 }
230}
getBackgroundImageThumbPath(string $certificatePath)
ilObjCertificateSettings $global_certificate_settings
ilCertificateObjectHelper $objectHelper
ilCertificateTemplateRepository $templateRepository
__construct(ilDBInterface $database, ilCertificatePathFactory $pathFactory, ilCertificateTemplateRepository $templateRepository, ?Filesystem $fileSystem=null, ?ilLogger $logger=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilObjCertificateSettings $global_certificate_settings=null, string $webDirectory=CLIENT_WEB_DIR, string $global_certificate_path=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Component logger with individual log levels by component id.
Class ilObjCertificateSettings.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const CLIENT_WEB_DIR
Definition: constants.php:47
global $DIC
Definition: feed.php:28
const ILIAS_VERSION_NUMERIC
Interface Filesystem.
Definition: Filesystem.php:40
Interface ilDBInterface.
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...