ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilCertificateTemplateImportAction Class Reference
+ Collaboration diagram for ilCertificateTemplateImportAction:

Public Member Functions

 __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)
 
 import (string $zipFile, string $filename, string $rootDir=CLIENT_WEB_DIR, string $iliasVerision=ILIAS_VERSION_NUMERIC, string $installationID=IL_INST_ID)
 

Private Member Functions

 createArchiveDirectory (string $installationID)
 Creates a directory for a zip archive containing multiple certificates. More...
 
 getBackgroundImageThumbnailPath ()
 

Private Attributes

readonly ilCertificateTemplateRepository $templateRepository
 
readonly ilCertificateObjectHelper $objectHelper
 
readonly ilCertificateUtilHelper $utilHelper
 
readonly ilCertificateBackgroundImageFileService $fileService
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilCertificateTemplateImportAction::__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 
)

Definition at line 36 of file class.ilCertificateTemplateImportAction.php.

References $DIC, $fileService, $objectHelper, $templateRepository, and $utilHelper.

47  {
48  if (null === $database) {
49  global $DIC;
50  $database = $DIC->database();
51  }
52 
53  if (null === $templateRepository) {
55  }
56  $this->templateRepository = $templateRepository;
57 
58  if (null === $objectHelper) {
60  }
61  $this->objectHelper = $objectHelper;
62 
63  if (null === $utilHelper) {
65  }
66  $this->utilHelper = $utilHelper;
67 
68  if (null === $fileService) {
70  $certificatePath,
71  $filesystem
72  );
73  }
74  $this->fileService = $fileService;
75  }
readonly ilCertificateBackgroundImageFileService $fileService
global $DIC
Definition: feed.php:28
readonly ilCertificateTemplateRepository $templateRepository
Just a wrapper class to create Unit Test for other classes.

Member Function Documentation

◆ createArchiveDirectory()

ilCertificateTemplateImportAction::createArchiveDirectory ( string  $installationID)
private

Creates a directory for a zip archive containing multiple certificates.

Returns
string The created archive directory
Exceptions
IOException

Definition at line 245 of file class.ilCertificateTemplateImportAction.php.

References ILIAS\Repository\filesystem().

Referenced by import().

245  : 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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBackgroundImageThumbnailPath()

ilCertificateTemplateImportAction::getBackgroundImageThumbnailPath ( )
private

Definition at line 259 of file class.ilCertificateTemplateImportAction.php.

Referenced by import().

259  : string
260  {
261  return $this->certificatePath . 'background.jpg.thumb.jpg';
262  }
+ Here is the caller graph for this function:

◆ import()

ilCertificateTemplateImportAction::import ( string  $zipFile,
string  $filename,
string  $rootDir = CLIENT_WEB_DIR,
string  $iliasVerision = ILIAS_VERSION_NUMERIC,
string  $installationID = IL_INST_ID 
)
Exceptions
FileAlreadyExistsException
FileNotFoundException
IOException
ilDatabaseException
ilException

Definition at line 84 of file class.ilCertificateTemplateImportAction.php.

References Vendor\Package\$e, createArchiveDirectory(), ILIAS\Repository\filesystem(), getBackgroundImageThumbnailPath(), and ILIAS\Repository\logger().

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  }
$filename
Definition: buildRTE.php:78
createArchiveDirectory(string $installationID)
Creates a directory for a zip archive containing multiple certificates.
+ Here is the call graph for this function:

Field Documentation

◆ $fileService

readonly ilCertificateBackgroundImageFileService ilCertificateTemplateImportAction::$fileService
private

Definition at line 34 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $objectHelper

readonly ilCertificateObjectHelper ilCertificateTemplateImportAction::$objectHelper
private

Definition at line 32 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $templateRepository

readonly ilCertificateTemplateRepository ilCertificateTemplateImportAction::$templateRepository
private

Definition at line 31 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().

◆ $utilHelper

readonly ilCertificateUtilHelper ilCertificateTemplateImportAction::$utilHelper
private

Definition at line 33 of file class.ilCertificateTemplateImportAction.php.

Referenced by __construct().


The documentation for this class was generated from the following file: