ILIAS  release_8 Revision v8.23
class.ilCertificateCloneAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
31  private ilLogger $logger;
37  private string $webDirectory;
38  private string $global_certificate_path;
40 
41  public function __construct(
42  ilDBInterface $database,
43  ilCertificatePathFactory $pathFactory,
44  ilCertificateTemplateRepository $templateRepository,
45  ?Filesystem $fileSystem = null,
46  ?ilLogger $logger = null,
47  ?ilCertificateObjectHelper $objectHelper = null,
48  ?ilObjCertificateSettings $global_certificate_settings = null,
49  string $webDirectory = CLIENT_WEB_DIR,
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) {
69  $objectHelper = new ilCertificateObjectHelper();
70  }
71  $this->objectHelper = $objectHelper;
72 
73  if (!$global_certificate_settings) {
74  $global_certificate_settings = new ilObjCertificateSettings();
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 }
__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)
const ILIAS_VERSION_NUMERIC
global $DIC
Definition: feed.php:28
Class ilObjCertificateSettings.
ilCertificateTemplateRepository $templateRepository
const CLIENT_WEB_DIR
Definition: constants.php:47
getDefaultBackgroundImagePath(bool $relativePath=false)
getBackgroundImageThumbPath(string $certificatePath)
ilCertificateObjectHelper $objectHelper
ilObjCertificateSettings $global_certificate_settings
Class FlySystemFileAccessTest disabled disabled disabled.