ILIAS  release_8 Revision v8.23
class.ilCertificateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 
39 {
43  private Factory $refinery;
45  protected ilTree $tree;
46  protected ILIAS $ilias;
48  protected ilLanguage $lng;
49  protected int $ref_id;
54  private int $objectId;
62  private string $certificatePath;
66  private ilLogger $logger;
68 
69  public function __construct(
70  ilCertificatePlaceholderDescription $placeholderDescriptionObject,
71  ilCertificatePlaceholderValues $placeholderValuesObject,
72  int $objectId,
73  string $certificatePath,
74  ?ilCertificateFormRepository $settingsFormFactory = null,
75  ?ilCertificateDeleteAction $deleteAction = null,
76  ?ilCertificateTemplateRepository $templateRepository = null,
77  ?ilPageFormats $pageFormats = null,
78  ?ilXlsFoParser $xlsFoParser = null,
79  ?ilCertificateTemplateExportAction $exportAction = null,
80  ?ilCertificateBackgroundImageUpload $upload = null,
81  ?ilCertificateTemplatePreviewAction $previewAction = null,
82  ?FileUpload $fileUpload = null,
83  ?ilSetting $settings = null,
84  ?ilCertificateBackgroundImageDelete $backgroundImageDelete = null,
85  ?Filesystem $fileSystem = null,
86  ?ilCertificateBackgroundImageFileService $imageFileService = null,
87  ?Filesystem $tmp_file_system = null
88  ) {
89  global $DIC;
90  $this->httpWrapper = $DIC->http()->wrapper();
91  $this->refinery = $DIC->refinery();
92  $this->lng = $DIC['lng'];
93  $this->tpl = $DIC['tpl'];
94  $this->ctrl = $DIC['ilCtrl'];
95  $this->ilias = $DIC['ilias'];
96 
97  $this->tree = $DIC['tree'];
98  $this->access = $DIC['ilAccess'];
99  $this->toolbar = $DIC['ilToolbar'];
100 
101  $this->global_certificate_settings = new ilObjCertificateSettings();
102 
103  $this->lng->loadLanguageModule('certificate');
104  $this->lng->loadLanguageModule('cert');
105  $this->lng->loadLanguageModule("trac");
106 
107  $this->ref_id = (int) $DIC->http()->wrapper()->query()->retrieve("ref_id", $DIC->refinery()->kindlyTo()->int());
108 
109  $this->placeholderDescriptionObject = $placeholderDescriptionObject;
110 
111  $this->objectId = $objectId;
112 
113  $this->logger = $DIC->logger()->cert();
114 
115  if (null === $settingsFormFactory) {
116  $settingsFormFactory = new ilCertificateSettingsFormRepository(
117  $this->objectId,
118  $certificatePath,
119  $this->lng,
120  $this->tpl,
121  $this->ctrl,
122  $this->access,
123  $this->toolbar,
124  $placeholderDescriptionObject
125  );
126  }
127  $this->settingsFormFactory = $settingsFormFactory;
128 
129  if (null === $templateRepository) {
130  $templateRepository = new ilCertificateTemplateDatabaseRepository($DIC->database(), $this->logger);
131  }
132  $this->templateRepository = $templateRepository;
133 
134  if (null === $deleteAction) {
135  $deleteAction = new ilCertificateTemplateDeleteAction($templateRepository);
136  }
137  $this->deleteAction = $deleteAction;
138 
139  if (null === $pageFormats) {
140  $pageFormats = new ilPageFormats($DIC->language());
141  }
142  $this->pageFormats = $pageFormats;
143 
144  if (null === $xlsFoParser) {
145  $xlsFoParser = new ilXlsFoParser($DIC->settings(), $pageFormats);
146  }
147  $this->xlsFoParser = $xlsFoParser;
148 
149  if (null === $upload) {
151  $DIC->upload(),
153  $DIC->language(),
155  );
156  }
157  $this->backgroundImageUpload = $upload;
158 
159  if (null === $exportAction) {
160  $exportAction = new ilCertificateTemplateExportAction(
161  $this->objectId,
162  $certificatePath,
163  $this->templateRepository,
164  $DIC->filesystem()->web()
165  );
166  }
167  $this->exportAction = $exportAction;
168 
169  if (null === $previewAction) {
170  $previewAction = new ilCertificateTemplatePreviewAction($templateRepository, $placeholderValuesObject);
171  }
172  $this->previewAction = $previewAction;
173 
174  if (null === $fileUpload) {
175  global $DIC;
176  $fileUpload = $DIC->upload();
177  }
178  $this->fileUpload = $fileUpload;
179 
180  $this->certificatePath = $certificatePath;
181 
182  if (null === $settings) {
183  $settings = new ilSetting('certificate');
184  }
185  $this->settings = $settings;
186 
187  if (null === $fileSystem) {
188  $fileSystem = $DIC->filesystem()->web();
189  }
190  $this->fileSystem = $fileSystem;
191 
192  if (null === $imageFileService) {
193  $imageFileService = new ilCertificateBackgroundImageFileService(
194  $this->certificatePath,
195  $this->fileSystem
196  );
197  }
198 
199  if (null === $backgroundImageDelete) {
200  $backgroundImageDelete = new ilCertificateBackgroundImageDelete(
201  $this->certificatePath,
202  $imageFileService
203  );
204  }
205  $this->backgroundImageDelete = $backgroundImageDelete;
206 
207  if (null === $tmp_file_system) {
208  $tmp_file_system = $DIC->filesystem()->temp();
209  }
210  $this->tmp_file_system = $tmp_file_system;
211  }
212 
223  public function executeCommand()
224  {
225  $cmd = $this->ctrl->getCmd();
226  $next_class = $this->ctrl->getNextClass($this);
227 
228  $ret = null;
229 
230  $cmd = $this->getCommand($cmd);
231  switch ($next_class) {
232  case 'ilpropertyformgui':
233  $form = $this->getEditorForm();
234  $this->ctrl->forwardCommand($form);
235  break;
236 
237  default:
238  $ret = $this->$cmd();
239  break;
240  }
241  return $ret;
242  }
243 
244  public function getCommand($cmd)
245  {
246  return $cmd;
247  }
248 
249  public function certificateImport(): void
250  {
251  $this->certificateEditor();
252  }
253 
254  public function certificatePreview(): void
255  {
256  try {
257  $this->previewAction->createPreviewPdf($this->objectId);
258  } catch (Exception $exception) {
259  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'));
260  $this->certificateEditor();
261  }
262  }
263 
270  public function certificateExportFO(): void
271  {
272  $this->exportAction->export();
273  }
274 
284  public function certificateRemoveBackground(): void
285  {
286  $this->backgroundImageDelete->deleteBackgroundImage(null);
287  $this->certificateEditor();
288  }
289 
290  public function certificateDelete(): void
291  {
292  // display confirmation message
293  $cgui = new ilConfirmationGUI();
294  $cgui->setFormAction($this->ctrl->getFormAction($this, "certificateEditor"));
295  $cgui->setHeaderText($this->lng->txt("certificate_confirm_deletion_text"));
296  $cgui->setCancel($this->lng->txt("no"), "certificateEditor");
297  $cgui->setConfirm($this->lng->txt("yes"), "certificateDeleteConfirm");
298 
299  $this->tpl->setContent($cgui->getHTML());
300  }
301 
305  public function certificateDeleteConfirm(): void
306  {
307  $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
308  $templateId = $template->getId();
309 
310  $this->deleteAction->delete($templateId, $this->objectId);
311  $this->ctrl->redirect($this, "certificateEditor");
312  }
313 
323  public function certificateSave(): void
324  {
325  global $DIC;
326 
327  $form = $this->settingsFormFactory->createForm(
328  $this
329  );
330 
331  $form->setValuesByPost();
332 
333  $request = $DIC->http()->request();
334 
335  $formFields = $request->getParsedBody();
336 
337  $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
338 
339  $this->saveCertificate($form, $formFields, $this->objectId);
340  }
341 
351  public function certificateUpload(): void
352  {
353  $this->certificateEditor();
354  }
355 
365  private function getEditorForm(): ilPropertyFormGUI
366  {
367  $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
368 
369  $form = $this->settingsFormFactory->createForm(
370  $this
371  );
372 
373  $formFields = $this->createFormatArray($certificateTemplate);
374 
375  $formFields['active'] = $certificateTemplate->isCurrentlyActive();
376 
377  $form->setValuesByArray($formFields);
378 
379  return $form;
380  }
381 
392  public function certificateEditor(): void
393  {
394  $form = $this->getEditorForm();
395  $enabledGlobalLearningProgress = ilObjUserTracking::_enabledLearningProgress();
396 
397  $messageBoxHtml = '';
398  if ($enabledGlobalLearningProgress) {
399  $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
400  $mode = $objectLearningProgressSettings->getMode();
401 
403  $object = ilObjectFactory::getInstanceByObjId($this->objectId);
404  if (ilLPObjSettings::LP_MODE_DEACTIVATED === $mode && $object->getType() !== 'crs') {
405  global $DIC;
406 
407  $renderer = $DIC->ui()->renderer();
408  $messageBox = $DIC->ui()
409  ->factory()
410  ->messageBox()
411  ->info($this->lng->txt('learning_progress_deactivated'));
412 
413  $messageBoxHtml = $renderer->render($messageBox);
414  $form->clearCommandButtons();
415  }
416  }
417 
418  $formHtml = $form->getHTML();
419 
420  $this->tpl->setVariable("ADM_CONTENT", $messageBoxHtml . $formHtml);
421  }
422 
423  private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, $objId): void
424  {
425  $previousCertificateTemplate = $this->templateRepository->fetchPreviousCertificate($objId);
426  $currentVersion = $previousCertificateTemplate->getVersion();
427  $nextVersion = $currentVersion + 1;
428 
429  $backgroundDelete = $this->httpWrapper->post()->has("background_delete") && $this->httpWrapper->post()->retrieve(
430  "background_delete",
431  $this->refinery->kindlyTo()->bool()
432  );
433  $certificateCardThumbnailImageDelete = $this->httpWrapper->post()->has("certificate_card_thumbnail_image_delete") && $this->httpWrapper->post()->retrieve(
434  "certificate_card_thumbnail_image_delete",
435  $this->refinery->kindlyTo()->bool()
436  );
437 
438  if ($backgroundDelete) {
439  $this->backgroundImageDelete->deleteBackgroundImage($currentVersion);
440  }
441 
442  if ($form->checkInput()) {
443  try {
444  $this->settingsFormFactory->save($form_fields);
445 
446  $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
447 
448  // handle the background upload
449  $backgroundImagePath = '';
450  $temporaryFileName = $_FILES['background']['tmp_name'];
451  if ($temporaryFileName !== '') {
452  try {
453  $backgroundImagePath = $this->backgroundImageUpload->uploadBackgroundImage(
454  $temporaryFileName,
455  $nextVersion,
456  $form->getInput('background')
457  );
458  } catch (ilException $exception) {
459  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
460  }
461  if (false === $this->fileSystem->has($backgroundImagePath)) {
462  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
463  $backgroundImagePath = '';
464  }
465  }
466  if ($backgroundImagePath === '') {
467  if ($backgroundDelete || $previousCertificateTemplate->getBackgroundImagePath() === '') {
468  $backgroundImagePath = $this->global_certificate_settings->getDefaultBackgroundImagePath(true);
469 
470  } else {
471  $backgroundImagePath = $previousCertificateTemplate->getBackgroundImagePath();
472  }
473  }
474 
475  // handle the card thumbnail upload
476  $cardThumbnailImagePath = '';
477  $temporaryFileName = $_FILES['certificate_card_thumbnail_image']['tmp_name'];
478  if ($temporaryFileName !== '' && $this->fileUpload->hasUploads()) {
479  try {
480  if (false === $this->fileUpload->hasBeenProcessed()) {
481  $this->fileUpload->process();
482  }
483 
484  $uploadResults = $this->fileUpload->getResults();
485  $pending_card_file = $form->getInput('certificate_card_thumbnail_image');
486  $cardThumbnailFileName = 'card_thumbnail_image_' . $nextVersion . '.svg';
487  if (isset($uploadResults[$temporaryFileName])) {
489  $result = $uploadResults[$temporaryFileName];
490  if ($result->isOK()) {
491  $this->fileUpload->moveOneFileTo(
492  $result,
493  $this->certificatePath,
494  Location::WEB,
495  $cardThumbnailFileName,
496  true
497  );
498 
499  $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
500  }
501  } elseif (!empty($pending_card_file)) {
502  $stream = $this->tmp_file_system->readStream(basename($pending_card_file['tmp_name']));
503  $this->fileSystem->writeStream(
504  $this->certificatePath . '/' . $cardThumbnailFileName,
505  $stream
506  );
507  $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
508  } else {
509  throw new ilException($this->lng->txt('upload_error_file_not_found'));
510  }
511  } catch (ilException $exception) {
512  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
513  }
514  if (false === $this->fileSystem->has($cardThumbnailImagePath)) {
515  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
516  $cardThumbnailImagePath = '';
517  }
518  }
519  if ($cardThumbnailImagePath === '' && !$certificateCardThumbnailImageDelete) {
520  $cardThumbnailImagePath = $previousCertificateTemplate->getThumbnailImagePath();
521  }
522 
523  $jsonEncodedTemplateValues = json_encode($templateValues, JSON_THROW_ON_ERROR);
524 
525  $xslfo = $this->xlsFoParser->parse($form_fields);
526 
527  $newHashValue = hash(
528  'sha256',
529  implode('', [
530  $xslfo,
531  $backgroundImagePath,
532  $jsonEncodedTemplateValues,
533  $cardThumbnailImagePath
534  ])
535  );
536 
537  $active = (bool) ($form_fields['active'] ?? false);
538 
539  if ($newHashValue !== $previousCertificateTemplate->getCertificateHash()) {
540  $certificateTemplate = new ilCertificateTemplate(
541  $objId,
543  $xslfo,
544  $newHashValue,
545  $jsonEncodedTemplateValues,
546  $nextVersion,
548  time(),
549  $active,
550  $backgroundImagePath,
551  $cardThumbnailImagePath
552  );
553 
554  $this->templateRepository->save($certificateTemplate);
555  $this->tpl->setOnScreenMessage('success', $this->lng->txt("saved_successfully"), true);
556  $this->ctrl->redirect($this, "certificateEditor");
557  }
558 
559  if ($previousCertificateTemplate->getId() !== null && $previousCertificateTemplate->isCurrentlyActive() !== $active) {
560  $this->templateRepository->updateActivity($previousCertificateTemplate, $active);
561  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_change_active_status'), true);
562  $this->ctrl->redirect($this, "certificateEditor");
563  }
564 
565  $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_same_not_saved'), true);
566  $this->ctrl->redirect($this, "certificateEditor");
567  } catch (Exception $e) {
568  $this->tpl->setOnScreenMessage(
569  'failure',
570  $e->getMessage()
571  );
572  $this->logger->error($e->getTraceAsString());
573  }
574  }
575 
576  $form->setValuesByPost();
577 
578  $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
579  }
580 
581  private function setTemplateContent(ilCertificateTemplate $certificate, ilPropertyFormGUI $form): void
582  {
583  $form_fields = $this->settingsFormFactory->fetchFormFieldData($certificate->getCertificateContent());
584  $form_fields['active'] = $certificate->isCurrentlyActive();
585 
586  $form->setValuesByArray($form_fields);
587 
588  $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
589  }
590 
591  private function createFormatArray(ilCertificateTemplate $certificateTemplate): array
592  {
593  if ('' === $certificateTemplate->getCertificateHash()) {
594  $format = $this->settings->get('pageformat', '');
595  $formats = $this->pageFormats->fetchPageFormats();
596 
597  return [
598  'pageformat' => $format,
599  'pagewidth' => $formats['width'] ?? '',
600  'pageheight' => $formats['height'] ?? '',
601  'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
602  'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
603  'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
604  'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
605  'certificate_text' => $certificateTemplate->getCertificateContent()
606  ];
607  }
608  return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
609  }
610 }
certificateSave()
Saves the certificate.
ilCertificateTemplateRepository $templateRepository
const ILIAS_VERSION_NUMERIC
getItemByPostVar(string $a_post_var)
Class ChatMainBarProvider .
$objId
Definition: xapitoken.php:57
createFormatArray(ilCertificateTemplate $certificateTemplate)
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-...
certificateDeleteConfirm()
Deletes the certificate and all its data.
global $DIC
Definition: feed.php:28
Class ilObjCertificateSettings.
ilCertificateBackgroundImageUpload $backgroundImageUpload
certificateExportFO()
Exports the certificate.
ilCertificateTemplatePreviewAction $previewAction
WrapperFactory $httpWrapper
__construct(ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilCertificatePlaceholderValues $placeholderValuesObject, 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, ?ilSetting $settings=null, ?ilCertificateBackgroundImageDelete $backgroundImageDelete=null, ?Filesystem $fileSystem=null, ?ilCertificateBackgroundImageFileService $imageFileService=null, ?Filesystem $tmp_file_system=null)
ilCertificateFormRepository $settingsFormFactory
$format
Definition: metadata.php:235
GUI class to create PDF certificates.
header include for all ilias files.
Class FileUpload.
Definition: FileUpload.php:34
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
const DEFAULT_MARGIN_BODY_BOTTOM
ilObjCertificateSettings $global_certificate_settings
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
ilCertificateDeleteAction $deleteAction
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilCertificateTemplateExportAction $exportAction
ilCertificateBackgroundImageDelete $backgroundImageDelete
setTemplateContent(ilCertificateTemplate $certificate, ilPropertyFormGUI $form)
ilCertificatePlaceholderDescription $placeholderDescriptionObject
static _lookupType(int $id, bool $reference=false)
ilGlobalPageTemplate $tpl
Class FlySystemFileAccessTest disabled disabled disabled.
$formats
Definition: date.php:77
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...