ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCertificateTemplateImportAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
30 {
35 
36  public function __construct(
37  private readonly int $objectId,
38  private readonly string $certificatePath,
39  private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
40  private readonly ilLogger $logger,
41  private readonly Filesystem $filesystem,
42  ?ilCertificateTemplateRepository $templateRepository = null,
43  ?ilCertificateObjectHelper $objectHelper = null,
44  ?ilCertificateUtilHelper $utilHelper = null,
45  ?ilDBInterface $database = null,
46  ?ilCertificateBackgroundImageFileService $fileService = null
47  ) {
48  if (null === $database) {
49  global $DIC;
50  $database = $DIC->database();
51  }
52 
53  if (null === $templateRepository) {
54  $templateRepository = new ilCertificateTemplateDatabaseRepository($database, $logger);
55  }
56  $this->templateRepository = $templateRepository;
57 
58  if (null === $objectHelper) {
59  $objectHelper = new ilCertificateObjectHelper();
60  }
61  $this->objectHelper = $objectHelper;
62 
63  if (null === $utilHelper) {
64  $utilHelper = new ilCertificateUtilHelper();
65  }
66  $this->utilHelper = $utilHelper;
67 
68  if (null === $fileService) {
69  $fileService = new ilCertificateBackgroundImageFileService(
70  $certificatePath,
71  $filesystem
72  );
73  }
74  $this->fileService = $fileService;
75  }
76 
84  public function import(
85  string $zipFile,
86  string $filename,
87  string $rootDir = CLIENT_WEB_DIR,
88  string $iliasVerision = ILIAS_VERSION_NUMERIC,
89  string $installationID = IL_INST_ID
90  ): bool {
91  $importPath = $this->createArchiveDirectory($installationID);
92 
93  $clean_up_import_dir = function () use (&$importPath) {
94  try {
95  if ($this->filesystem->hasDir($importPath)) {
96  $this->filesystem->deleteDir($importPath);
97  }
98  } catch (Throwable $e) {
99  $this->logger->error(sprintf("Can't clean up import directory: %s", $e->getMessage()));
100  $this->logger->error($e->getTraceAsString());
101  }
102  };
103 
104  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
105  if (!$result) {
106  $clean_up_import_dir();
107  return false;
108  }
109 
110  $destination_dir = $rootDir . $importPath;
111  $unzip = $this->utilHelper->unzip(
112  $rootDir . $importPath . $filename,
113  $destination_dir,
114  true
115  );
116 
117  $unzipped = $unzip->extract();
118 
119  // Cleanup memory, otherwise there will be issues with NFS-based file systems after `listContents` has been called
120  unset($unzip);
121 
122  if (!$unzipped) {
123  $clean_up_import_dir();
124  return false;
125  }
126 
127  if ($this->filesystem->has($importPath . $filename)) {
128  $this->filesystem->delete($importPath . $filename);
129  }
130 
131  $xmlFiles = 0;
132  $contents = $this->filesystem->listContents($importPath);
133  foreach ($contents as $file) {
134  if ($file->isFile() && str_contains($file->getPath(), '.xml')) {
135  $xmlFiles++;
136  }
137  }
138 
139  if (0 === $xmlFiles) {
140  return false;
141  }
142 
143  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
144 
145  $currentVersion = $certificate->getVersion();
146  $newVersion = $currentVersion + 1;
147  $backgroundImagePath = $certificate->getBackgroundImagePath();
148  $cardThumbnailImagePath = $certificate->getThumbnailImagePath();
149  $xsl = $certificate->getCertificateContent();
150 
151  foreach ($contents as $file) {
152  if (!$file->isFile()) {
153  continue;
154  }
155 
156  if (str_contains($file->getPath(), '.xml')) {
157  $xsl = $this->filesystem->read($file->getPath());
158  // as long as we cannot make RPC calls in a given directory, we have
159  // to add the complete path to every url
160  $xsl = preg_replace_callback(
161  "/url\([']{0,1}(.*?)[']{0,1}\)/",
162  function (array $matches) use ($rootDir): string {
163  $basePath = rtrim(dirname($this->fileService->getBackgroundImageDirectory($rootDir)), '/');
164  $fileName = basename($matches[1]);
165 
166  if ('[BACKGROUND_IMAGE]' === $fileName) {
167  $basePath = '';
168  } elseif ($basePath !== '') {
169  $basePath .= '/';
170  }
171 
172  return 'url(' . $basePath . $fileName . ')';
173  },
174  $xsl
175  );
176  } elseif (str_contains($file->getPath(), '.jpg')) {
177  $newBackgroundImageName = 'background_' . $newVersion . '.jpg';
178  $newPath = $this->certificatePath . $newBackgroundImageName;
179  $this->filesystem->copy($file->getPath(), $newPath);
180 
181  $backgroundImagePath = $this->certificatePath . $newBackgroundImageName;
182  // upload of the background image, create a thumbnail
183 
184  $backgroundImageThumbPath = $this->getBackgroundImageThumbnailPath();
185 
186  $thumbnailImagePath = $rootDir . $backgroundImageThumbPath;
187 
188  $originalImagePath = $rootDir . $newPath;
189  $this->utilHelper->convertImage(
190  $originalImagePath,
191  $thumbnailImagePath,
192  '100'
193  );
194  } elseif (str_contains($file->getPath(), '.svg')) {
195  $newCardThumbnailName = 'thumbnail_' . $newVersion . '.svg';
196  $newPath = $this->certificatePath . $newCardThumbnailName;
197 
198  $this->filesystem->copy($file->getPath(), $newPath);
199 
200  $cardThumbnailImagePath = $this->certificatePath . $newCardThumbnailName;
201  }
202  }
203 
204  $jsonEncodedTemplateValues = json_encode(
205  $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
206  JSON_THROW_ON_ERROR
207  );
208 
209  $newHashValue = hash(
210  'sha256',
211  implode('', [
212  $xsl,
213  $backgroundImagePath,
214  $jsonEncodedTemplateValues,
215  $cardThumbnailImagePath
216  ])
217  );
218 
219  $template = new ilCertificateTemplate(
220  $this->objectId,
221  $this->objectHelper->lookupType($this->objectId),
222  $xsl,
223  $newHashValue,
224  $jsonEncodedTemplateValues,
225  $newVersion,
226  $iliasVerision,
227  time(),
228  false,
229  $backgroundImagePath,
230  $cardThumbnailImagePath
231  );
232 
233  $this->templateRepository->save($template);
234 
235  $clean_up_import_dir();
236 
237  return true;
238  }
239 
245  private function createArchiveDirectory(string $installationID): string
246  {
247  $type = $this->objectHelper->lookupType($this->objectId);
248  $certificateId = $this->objectId;
249 
250  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
251  if ($this->filesystem->hasDir($dir)) {
252  $this->filesystem->deleteDir($dir);
253  }
254  $this->filesystem->createDir($dir);
255 
256  return $dir;
257  }
258 
259  private function getBackgroundImageThumbnailPath(): string
260  {
261  return $this->certificatePath . 'background.jpg.thumb.jpg';
262  }
263 }
const IL_INST_ID
Definition: constants.php:40
const ILIAS_VERSION_NUMERIC
__construct(private readonly int $objectId, private readonly string $certificatePath, private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject, private readonly ilLogger $logger, private readonly Filesystem $filesystem, ?ilCertificateTemplateRepository $templateRepository=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilDBInterface $database=null, ?ilCertificateBackgroundImageFileService $fileService=null)
readonly ilCertificateBackgroundImageFileService $fileService
global $DIC
Definition: feed.php:28
const CLIENT_WEB_DIR
Definition: constants.php:47
$filename
Definition: buildRTE.php:78
readonly ilCertificateTemplateRepository $templateRepository
Just a wrapper class to create Unit Test for other classes.
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.