ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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, private readonly IRSS $irss, ?ilCertificateTemplateRepository $templateRepository=null, ?ilCertificateObjectHelper $objectHelper=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilDBInterface $database=null,)
 
 import (string $zipFile, string $filename, string $rootDir=CLIENT_WEB_DIR, string $iliasVersion=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...
 

Private Attributes

readonly ilCertificateTemplateRepository $templateRepository
 
readonly ilCertificateObjectHelper $objectHelper
 
readonly ilCertificateUtilHelper $utilHelper
 
readonly ilCertificateTemplateStakeholder $stakeholder
 

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,
private readonly IRSS  $irss,
?ilCertificateTemplateRepository  $templateRepository = null,
?ilCertificateObjectHelper  $objectHelper = null,
?ilCertificateUtilHelper  $utilHelper = null,
?ilDBInterface  $database = null 
)

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

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

49  {
50  global $DIC;
51  if (null === $database) {
52  $database = $DIC->database();
53  }
54 
55  if (null === $templateRepository) {
57  }
58  $this->templateRepository = $templateRepository;
59 
60  if (null === $objectHelper) {
62  }
63  $this->objectHelper = $objectHelper;
64 
65  if (null === $utilHelper) {
67  }
68  $this->utilHelper = $utilHelper;
69  $this->stakeholder = new ilCertificateTemplateStakeholder();
70  }
global $DIC
Definition: shib_login.php:25
readonly ilCertificateTemplateRepository $templateRepository

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 214 of file class.ilCertificateTemplateImportAction.php.

References ILIAS\Repository\filesystem().

Referenced by import().

214  : string
215  {
216  $type = $this->objectHelper->lookupType($this->objectId);
217  $certificateId = $this->objectId;
218 
219  $dir = $this->certificatePath . time() . '__' . $installationID . '__' . $type . '__' . $certificateId . '__certificate/';
220  if ($this->filesystem->hasDir($dir)) {
221  $this->filesystem->deleteDir($dir);
222  }
223  $this->filesystem->createDir($dir);
224 
225  return $dir;
226  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ import()

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

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

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

85  : bool {
86  $importPath = $this->createArchiveDirectory($installationID);
87 
88  $clean_up_import_dir = function () use (&$importPath) {
89  try {
90  if ($this->filesystem->hasDir($importPath)) {
91  $this->filesystem->deleteDir($importPath);
92  }
93  } catch (Throwable $e) {
94  $this->logger->error(sprintf("Can't clean up import directory: %s", $e->getMessage()));
95  $this->logger->error($e->getTraceAsString());
96  }
97  };
98 
99  $result = $this->utilHelper->moveUploadedFile($zipFile, $filename, $rootDir . $importPath . $filename);
100  if (!$result) {
101  $clean_up_import_dir();
102 
103  return false;
104  }
105 
106  $destination_dir = $rootDir . $importPath;
107  $unzip = $this->utilHelper->unzip(
108  $rootDir . $importPath . $filename,
109  $destination_dir,
110  true
111  );
112 
113  $unzipped = $unzip->extract();
114 
115  // Cleanup memory, otherwise there will be issues with NFS-based file systems after `listContents` has been called
116  unset($unzip);
117 
118  if (!$unzipped) {
119  $clean_up_import_dir();
120 
121  return false;
122  }
123 
124  if ($this->filesystem->has($importPath . $filename)) {
125  $this->filesystem->delete($importPath . $filename);
126  }
127 
128  $xmlFiles = 0;
129  $contents = $this->filesystem->listContents($importPath);
130  foreach ($contents as $file) {
131  if ($file->isFile() && str_contains($file->getPath(), '.xml')) {
132  $xmlFiles++;
133  }
134  }
135 
136  if (0 === $xmlFiles) {
137  return false;
138  }
139 
140  $certificate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
141 
142  $currentVersion = $certificate->getVersion();
143  $newVersion = $currentVersion + 1;
144  $xsl = $certificate->getCertificateContent();
145 
146  foreach ($contents as $file) {
147  if (!$file->isFile()) {
148  continue;
149  }
150 
151  if (str_contains($file->getPath(), '.xml')) {
152  $xsl = $this->filesystem->read($file->getPath());
153  // TODO: wth??
154  } elseif (str_contains($file->getPath(), '.jpg')) {
155  $background_rid = $this->irss->manage()->stream(
156  $this->filesystem->readStream($file->getPath()),
157  $this->stakeholder
158  );
159  } elseif (str_contains($file->getPath(), '.svg')) {
160  $card_thumbnail_rid = $this->irss->manage()->stream(
161  $this->filesystem->readStream($file->getPath()),
162  $this->stakeholder
163  );
164  }
165  }
166 
167  $jsonEncodedTemplateValues = json_encode(
168  $this->placeholderDescriptionObject->getPlaceholderDescriptions(),
169  JSON_THROW_ON_ERROR
170  );
171 
172  $newHashValue = hash(
173  'sha256',
174  implode('', [
175  $xsl,
176  isset($background_rid) ? $this->irss->manage()->getResource(
177  $background_rid
178  )->getStorageID() : '',
179  $jsonEncodedTemplateValues,
180  isset($card_thumbnail_rid) ? $this->irss->manage()->getResource(
181  $card_thumbnail_rid
182  )->getStorageID() : ''
183  ])
184  );
185 
186  $template = new ilCertificateTemplate(
187  $this->objectId,
188  $this->objectHelper->lookupType($this->objectId),
189  $xsl,
190  $newHashValue,
191  $jsonEncodedTemplateValues,
192  $newVersion,
193  $iliasVersion,
194  time(),
195  false,
196  '',
197  '',
198  isset($background_rid) ? $background_rid->serialize() : '',
199  isset($card_thumbnail_rid) ? $card_thumbnail_rid->serialize() : ''
200  );
201 
202  $this->templateRepository->save($template);
203 
204  $clean_up_import_dir();
205 
206  return true;
207  }
$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

◆ $objectHelper

readonly ilCertificateObjectHelper ilCertificateTemplateImportAction::$objectHelper
private

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

Referenced by __construct().

◆ $stakeholder

readonly ilCertificateTemplateStakeholder ilCertificateTemplateImportAction::$stakeholder
private

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

◆ $templateRepository

readonly ilCertificateTemplateRepository ilCertificateTemplateImportAction::$templateRepository
private

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

Referenced by __construct().

◆ $utilHelper

readonly ilCertificateUtilHelper ilCertificateTemplateImportAction::$utilHelper
private

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

Referenced by __construct().


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