ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
39 {
41  private readonly Filesystem $fileSystem;
42  private readonly WrapperFactory $httpWrapper;
43  private readonly Factory $refinery;
45  protected ilTree $tree;
46  protected ILIAS $ilias;
48  protected ilLanguage $lng;
49  protected int $ref_id;
54  private readonly ilXlsFoParser $xlsFoParser;
59  private readonly FileUpload $fileUpload;
60  private readonly string $certificatePath;
61  private readonly ilPageFormats $pageFormats;
62  private readonly Filesystem $tmp_file_system;
63  private readonly ilLogger $logger;
65 
66  public function __construct(
67  private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
68  ilCertificatePlaceholderValues $placeholderValuesObject,
69  private readonly int $objectId,
70  string $certificatePath,
71  ?ilCertificateFormRepository $settingsFormFactory = null,
72  ?ilCertificateDeleteAction $deleteAction = null,
73  ?ilCertificateTemplateRepository $templateRepository = null,
74  ?ilPageFormats $pageFormats = null,
75  ?ilXlsFoParser $xlsFoParser = null,
76  ?ilCertificateTemplateExportAction $exportAction = null,
77  ?ilCertificateBackgroundImageUpload $upload = null,
78  ?ilCertificateTemplatePreviewAction $previewAction = null,
79  ?FileUpload $fileUpload = null,
80  private readonly ilSetting $settings = new ilSetting('certificate'),
81  ?ilCertificateBackgroundImageDelete $backgroundImageDelete = null,
82  ?Filesystem $fileSystem = null,
83  ?ilCertificateBackgroundImageFileService $imageFileService = null,
84  ?Filesystem $tmp_file_system = null
85  ) {
86  global $DIC;
87 
88  $this->httpWrapper = $DIC->http()->wrapper();
89  $this->refinery = $DIC->refinery();
90  $this->lng = $DIC['lng'];
91  $this->tpl = $DIC['tpl'];
92  $this->ctrl = $DIC['ilCtrl'];
93  $this->ilias = $DIC['ilias'];
94 
95  $this->tree = $DIC['tree'];
96  $this->access = $DIC['ilAccess'];
97  $this->toolbar = $DIC['ilToolbar'];
98 
99  $this->global_certificate_settings = new ilObjCertificateSettings();
100 
101  $this->lng->loadLanguageModule('cert');
102  $this->lng->loadLanguageModule("trac");
103 
104  $this->ref_id = (int) $DIC->http()->wrapper()->query()->retrieve("ref_id", $DIC->refinery()->kindlyTo()->int());
105 
106  $this->logger = $DIC->logger()->cert();
107 
108  $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
109  $this->objectId,
110  $certificatePath,
111  $this->lng,
112  $this->tpl,
113  $this->ctrl,
114  $this->access,
115  $this->toolbar,
116  $placeholderDescriptionObject,
117  $DIC->ui()->factory(),
118  $DIC->ui()->renderer()
119  );
120  $this->templateRepository = $templateRepository ?? new ilCertificateTemplateDatabaseRepository(
121  $DIC->database(),
123  );
124  $this->deleteAction = $deleteAction ?? new ilCertificateTemplateDeleteAction($this->templateRepository);
125  $this->pageFormats = $pageFormats ?? new ilPageFormats($DIC->language());
126  $this->xlsFoParser = $xlsFoParser ?? new ilXlsFoParser($DIC->settings(), $this->pageFormats);
127  $this->backgroundImageUpload = $upload ?? new ilCertificateBackgroundImageUpload(
128  $DIC->upload(),
130  $DIC->language()
131  );
132  $this->exportAction = $exportAction ?? new ilCertificateTemplateExportAction(
133  $this->objectId,
134  $certificatePath,
135  $this->templateRepository,
136  $DIC->filesystem()->web()
137  );
138  $this->previewAction = $previewAction ?? new ilCertificateTemplatePreviewAction(
139  $this->templateRepository,
140  $placeholderValuesObject
141  );
142  $this->fileUpload = $fileUpload ?? $DIC->upload();
143  $this->certificatePath = $certificatePath;
144  $this->fileSystem = $fileSystem ?? $DIC->filesystem()->web();
145  $imageFileService ??= new ilCertificateBackgroundImageFileService(
146  $this->certificatePath,
147  $this->fileSystem
148  );
149  $this->backgroundImageDelete = $backgroundImageDelete ?? new ilCertificateBackgroundImageDelete(
150  $this->certificatePath,
151  $imageFileService
152  );
153  $this->tmp_file_system = $tmp_file_system ?? $DIC->filesystem()->temp();
154  ;
155  }
156 
167  public function executeCommand()
168  {
169  $cmd = $this->ctrl->getCmd();
170  $next_class = $this->ctrl->getNextClass($this);
171 
172  $ret = null;
173 
174  $cmd = $this->getCommand($cmd);
175  switch ($next_class) {
176  case 'ilpropertyformgui':
177  $form = $this->getEditorForm();
178  $this->ctrl->forwardCommand($form);
179  break;
180 
181  default:
182  $ret = $this->$cmd();
183  break;
184  }
185  return $ret;
186  }
187 
188  public function getCommand($cmd)
189  {
190  return $cmd;
191  }
192 
193  public function certificateImport(): void
194  {
195  $this->certificateEditor();
196  }
197 
198  public function certificatePreview(): void
199  {
200  try {
201  $this->previewAction->createPreviewPdf($this->objectId);
202  } catch (Exception) {
203  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'));
204  $this->certificateEditor();
205  }
206  }
207 
214  public function certificateExportFO(): void
215  {
216  $this->exportAction->export();
217  }
218 
228  public function certificateRemoveBackground(): void
229  {
230  $this->backgroundImageDelete->deleteBackgroundImage(null);
231  $this->certificateEditor();
232  }
233 
234  public function certificateDelete(): void
235  {
236  // display confirmation message
237  $cgui = new ilConfirmationGUI();
238  $cgui->setFormAction($this->ctrl->getFormAction($this, "certificateEditor"));
239  $cgui->setHeaderText($this->lng->txt("certificate_confirm_deletion_text"));
240  $cgui->setCancel($this->lng->txt("no"), "certificateEditor");
241  $cgui->setConfirm($this->lng->txt("yes"), "certificateDeleteConfirm");
242 
243  $this->tpl->setContent($cgui->getHTML());
244  }
245 
249  public function certificateDeleteConfirm(): void
250  {
251  $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
252  $templateId = $template->getId();
253 
254  $this->deleteAction->delete($templateId, $this->objectId);
255  $this->ctrl->redirect($this, "certificateEditor");
256  }
257 
267  public function certificateSave(): void
268  {
269  global $DIC;
270 
271  $form = $this->settingsFormFactory->createForm(
272  $this
273  );
274 
275  $form->setValuesByPost();
276 
277  $request = $DIC->http()->request();
278 
279  $formFields = $request->getParsedBody();
280 
281  $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
282 
283  $this->saveCertificate($form, $formFields, $this->objectId);
284  }
285 
295  public function certificateUpload(): void
296  {
297  $this->certificateEditor();
298  }
299 
308  private function getEditorForm(): ilPropertyFormGUI
309  {
310  $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
311 
312  $form = $this->settingsFormFactory->createForm(
313  $this
314  );
315 
316  $formFields = $this->createFormatArray($certificateTemplate);
317 
318  $formFields['active'] = $certificateTemplate->isCurrentlyActive();
319 
320  $form->setValuesByArray($formFields);
321 
322  return $form;
323  }
324 
335  public function certificateEditor(): void
336  {
337  $form = $this->getEditorForm();
338  $enabledGlobalLearningProgress = ilObjUserTracking::_enabledLearningProgress();
339 
340  $messageBoxHtml = '';
341  if ($enabledGlobalLearningProgress) {
342  $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
343  $mode = $objectLearningProgressSettings->getMode();
344 
346  $object = ilObjectFactory::getInstanceByObjId($this->objectId);
347  if (ilLPObjSettings::LP_MODE_DEACTIVATED === $mode && $object->getType() !== 'crs') {
348  global $DIC;
349 
350  $renderer = $DIC->ui()->renderer();
351  $messageBox = $DIC->ui()
352  ->factory()
353  ->messageBox()
354  ->info($this->lng->txt('learning_progress_deactivated'));
355 
356  $messageBoxHtml = $renderer->render($messageBox);
357  $form->clearCommandButtons();
358  }
359  }
360 
361  $formHtml = $form->getHTML();
362 
363  $this->tpl->setVariable("ADM_CONTENT", $messageBoxHtml . $formHtml);
364  }
365 
366  private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId): void
367  {
368  $previousCertificateTemplate = $this->templateRepository->fetchPreviousCertificate($objId);
369  $currentVersion = $previousCertificateTemplate->getVersion();
370  $nextVersion = $currentVersion + 1;
371 
372  $backgroundDelete = $this->httpWrapper->post()->has("background_delete") && $this->httpWrapper->post()->retrieve(
373  "background_delete",
374  $this->refinery->kindlyTo()->bool()
375  );
376  $certificateCardThumbnailImageDelete = $this->httpWrapper->post()->has("certificate_card_thumbnail_image_delete") && $this->httpWrapper->post()->retrieve(
377  "certificate_card_thumbnail_image_delete",
378  $this->refinery->kindlyTo()->bool()
379  );
380 
381  if ($backgroundDelete) {
382  $this->backgroundImageDelete->deleteBackgroundImage($currentVersion);
383  }
384 
385  if ($form->checkInput()) {
386  try {
387  $this->settingsFormFactory->save($form_fields);
388 
389  $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
390 
391  // handle the background upload
392  $backgroundImagePath = '';
393  $temporaryFileName = $_FILES['background']['tmp_name'];
394  if ($temporaryFileName !== '') {
395  try {
396  $backgroundImagePath = $this->backgroundImageUpload->uploadBackgroundImage(
397  $temporaryFileName,
398  $nextVersion,
399  $form->getInput('background')
400  );
401  } catch (ilException) {
402  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
403  }
404  if (!$this->fileSystem->has($backgroundImagePath)) {
405  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
406  $backgroundImagePath = '';
407  }
408  }
409  if ($backgroundImagePath === '') {
410  if ($backgroundDelete || $previousCertificateTemplate->getBackgroundImagePath() === '') {
411  $backgroundImagePath = $this->global_certificate_settings->getDefaultBackgroundImagePath(true);
412  } else {
413  $backgroundImagePath = $previousCertificateTemplate->getBackgroundImagePath();
414  }
415  }
416 
417  // handle the card thumbnail upload
418  $cardThumbnailImagePath = '';
419  $temporaryFileName = $_FILES['certificate_card_thumbnail_image']['tmp_name'];
420  if ($temporaryFileName !== '' && $this->fileUpload->hasUploads()) {
421  try {
422  if (!$this->fileUpload->hasBeenProcessed()) {
423  $this->fileUpload->process();
424  }
425 
426  $uploadResults = $this->fileUpload->getResults();
427  $pending_card_file = $form->getInput('certificate_card_thumbnail_image');
428  $cardThumbnailFileName = 'card_thumbnail_image_' . $nextVersion . '.svg';
429  if (isset($uploadResults[$temporaryFileName])) {
431  $result = $uploadResults[$temporaryFileName];
432  if ($result->isOK()) {
433  $this->fileUpload->moveOneFileTo(
434  $result,
435  $this->certificatePath,
436  Location::WEB,
437  $cardThumbnailFileName,
438  true
439  );
440 
441  $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
442  }
443  } elseif (!empty($pending_card_file)) {
444  $stream = $this->tmp_file_system->readStream(basename($pending_card_file['tmp_name']));
445  $this->fileSystem->writeStream(
446  $this->certificatePath . '/' . $cardThumbnailFileName,
447  $stream
448  );
449  $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
450  } else {
451  throw new ilException($this->lng->txt('upload_error_file_not_found'));
452  }
453  } catch (ilException) {
454  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
455  }
456  if (!$this->fileSystem->has($cardThumbnailImagePath)) {
457  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
458  $cardThumbnailImagePath = '';
459  }
460  }
461  if ($cardThumbnailImagePath === '' && !$certificateCardThumbnailImageDelete) {
462  $cardThumbnailImagePath = $previousCertificateTemplate->getThumbnailImagePath();
463  }
464 
465  $jsonEncodedTemplateValues = json_encode($templateValues, JSON_THROW_ON_ERROR);
466 
467  $xslfo = $this->xlsFoParser->parse($form_fields);
468 
469  $newHashValue = hash(
470  'sha256',
471  implode('', [
472  $xslfo,
473  $backgroundImagePath,
474  $jsonEncodedTemplateValues,
475  $cardThumbnailImagePath
476  ])
477  );
478 
479  $active = (bool) ($form_fields['active'] ?? false);
480 
481  if ($newHashValue !== $previousCertificateTemplate->getCertificateHash()) {
482  $certificateTemplate = new ilCertificateTemplate(
483  $objId,
484  ilObject::_lookupType($objId),
485  $xslfo,
486  $newHashValue,
487  $jsonEncodedTemplateValues,
488  $nextVersion,
490  time(),
491  $active,
492  $backgroundImagePath,
493  $cardThumbnailImagePath
494  );
495 
496  $this->templateRepository->save($certificateTemplate);
497  $this->tpl->setOnScreenMessage('success', $this->lng->txt("saved_successfully"), true);
498  $this->ctrl->redirect($this, "certificateEditor");
499  }
500 
501  if ($previousCertificateTemplate->getId() !== null && $previousCertificateTemplate->isCurrentlyActive() !== $active) {
502  $this->templateRepository->updateActivity($previousCertificateTemplate, $active);
503  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_change_active_status'), true);
504  $this->ctrl->redirect($this, "certificateEditor");
505  }
506 
507  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_same_not_saved'), true);
508  $this->ctrl->redirect($this, "certificateEditor");
509  } catch (Exception $e) {
510  $this->tpl->setOnScreenMessage(
511  'failure',
512  $e->getMessage()
513  );
514  $this->logger->error($e->getTraceAsString());
515  }
516  }
517 
518  $form->setValuesByPost();
519 
520  $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
521  }
522 
523  private function createFormatArray(ilCertificateTemplate $certificateTemplate): array
524  {
525  if ('' === $certificateTemplate->getCertificateHash()) {
526  $format = $this->settings->get('pageformat', '');
527  $formats = $this->pageFormats->fetchPageFormats();
528 
529  return [
530  'pageformat' => $format,
531  'pagewidth' => $formats['width'] ?? '',
532  'pageheight' => $formats['height'] ?? '',
533  'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
534  'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
535  'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
536  'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
537  'certificate_text' => $certificateTemplate->getCertificateContent()
538  ];
539  }
540  return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
541  }
542 }
certificateSave()
Saves the certificate.
final const DEFAULT_MARGIN_BODY_TOP
readonly ilPageFormats $pageFormats
final const DEFAULT_MARGIN_BODY_RIGHT
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
const ILIAS_VERSION_NUMERIC
getItemByPostVar(string $a_post_var)
Class ChatMainBarProvider .
$objId
Definition: xapitoken.php:57
createFormatArray(ilCertificateTemplate $certificateTemplate)
readonly WrapperFactory $httpWrapper
readonly ilCertificateFormRepository $settingsFormFactory
readonly Filesystem $fileSystem
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
readonly ilCertificateBackgroundImageDelete $backgroundImageDelete
readonly ilCertificateTemplateExportAction $exportAction
certificateDeleteConfirm()
Deletes the certificate and all its data.
global $DIC
Definition: feed.php:28
Class ilObjCertificateSettings.
__construct(private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilCertificatePlaceholderValues $placeholderValuesObject, private readonly int $objectId, string $certificatePath, ?ilCertificateFormRepository $settingsFormFactory=null, ?ilCertificateDeleteAction $deleteAction=null, ?ilCertificateTemplateRepository $templateRepository=null, ?ilPageFormats $pageFormats=null, ?ilXlsFoParser $xlsFoParser=null, ?ilCertificateTemplateExportAction $exportAction=null, ?ilCertificateBackgroundImageUpload $upload=null, ?ilCertificateTemplatePreviewAction $previewAction=null, ?FileUpload $fileUpload=null, private readonly ilSetting $settings=new ilSetting('certificate'), ?ilCertificateBackgroundImageDelete $backgroundImageDelete=null, ?Filesystem $fileSystem=null, ?ilCertificateBackgroundImageFileService $imageFileService=null, ?Filesystem $tmp_file_system=null)
readonly FileUpload $fileUpload
certificateExportFO()
Exports the certificate.
GUI class to create PDF certificates.
header include for all ilias files.
readonly string $certificatePath
Class FileUpload.
Definition: FileUpload.php:34
readonly ilCertificateTemplatePreviewAction $previewAction
readonly Filesystem $tmp_file_system
readonly ilCertificateBackgroundImageUpload $backgroundImageUpload
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
readonly ilLogger $logger
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly ilCertificateTemplateRepository $templateRepository
readonly ilXlsFoParser $xlsFoParser
final const DEFAULT_MARGIN_BODY_LEFT
readonly ilCertificateDeleteAction $deleteAction
static _lookupType(int $id, bool $reference=false)
readonly ilObjCertificateSettings $global_certificate_settings
final const DEFAULT_MARGIN_BODY_BOTTOM
readonly Factory $refinery
ilGlobalPageTemplate $tpl