ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCertificateGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
32
41{
42 public const string EDITOR_COMMAND = 'certificateEditor';
43
45 protected ilTree $tree;
46 protected ILIAS $ilias;
48 protected ilLanguage $lng;
49 protected int $ref_id;
52 private readonly WrapperFactory $httpWrapper;
53 private readonly Factory $refinery;
54 private IRSS $irss;
59 private readonly ilXlsFoParser $xlsFoParser;
63 private readonly FileUpload $fileUpload;
64 private readonly Filesystem $file_system;
65 private readonly string $certificatePath;
66 private readonly ilPageFormats $pageFormats;
67 private readonly ilLogger $logger;
69 private readonly ilDBInterface $database;
70
71 public function __construct(
72 private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
73 ilCertificatePlaceholderValues $placeholderValuesObject,
74 private readonly int $objectId,
75 string $certificatePath,
83 ?FileUpload $fileUpload = null,
84 private readonly ilSetting $settings = new ilSetting('certificate'),
86 ?Filesystem $tmp_file_system = null
87 ) {
88 global $DIC;
89
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->irss = $DIC->resourceStorage();
98 $this->stakeholder = new ilCertificateTemplateStakeholder($this->objectId);
99
100 $this->tree = $DIC['tree'];
101 $this->access = $DIC['ilAccess'];
102 $this->toolbar = $DIC['ilToolbar'];
103
104 $this->global_certificate_settings = new ilObjCertificateSettings();
105 $this->lng->loadLanguageModule('certificate');
106 $this->lng->loadLanguageModule('cert');
107 $this->lng->loadLanguageModule('trac');
108
109 $this->ref_id = (int) $DIC->http()->wrapper()->query()->retrieve('ref_id', $DIC->refinery()->kindlyTo()->int());
110
111 $this->logger = $DIC->logger()->cert();
112
113 $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
114 $this->objectId,
116 $this->lng,
117 $this->tpl,
118 $this->ctrl,
119 $this->access,
120 $this->toolbar,
121 $placeholderDescriptionObject,
122 $DIC->ui()->factory(),
123 $DIC->ui()->renderer()
124 );
125 $this->templateRepository = $templateRepository ?? new ilCertificateTemplateDatabaseRepository(
126 $DIC->database(),
127 $this->logger
128 );
129 $this->deleteAction = $deleteAction ?? new ilCertificateTemplateDeleteAction($this->templateRepository);
130 $this->pageFormats = $pageFormats ?? new ilPageFormats($DIC->language());
131 $this->xlsFoParser = $xlsFoParser ?? new ilXlsFoParser($DIC->settings(), $this->pageFormats);
132 $this->exportAction = $exportAction ?? new ilCertificateTemplateExportAction(
133 $this->objectId,
135 $this->templateRepository,
136 $this->irss
137 );
138 $this->previewAction = $previewAction ?? new ilCertificateTemplatePreviewAction(
139 $this->templateRepository,
140 $placeholderValuesObject,
141 $this->irss
142 );
143 $this->fileUpload = $fileUpload ?? $DIC->upload();
144 $this->file_system = $file_system ?? $DIC->filesystem()->web();
145 $this->database = $DIC->database();
146 }
147
158 public function executeCommand()
159 {
160 $cmd = $this->ctrl->getCmd(self::EDITOR_COMMAND);
161 $next_class = $this->ctrl->getNextClass($this);
162
163 $ret = null;
164
165 $cmd = $this->getCommand($cmd);
166 switch ($next_class) {
167 case 'ilpropertyformgui':
168 $form = $this->getEditorForm();
169 $this->ctrl->forwardCommand($form);
170
171 break;
172 default:
173 $ret = $this->$cmd();
174
175 break;
176 }
177
178 return $ret;
179 }
180
181 public function getCommand($cmd)
182 {
183 return $cmd;
184 }
185
186 public function certificateImport(): void
187 {
188 $this->certificateEditor();
189 }
190
191 public function certificatePreview(): void
192 {
193 try {
194 $this->previewAction->createPreviewPdf($this->objectId);
195 } catch (Exception) {
196 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'));
197 $this->certificateEditor();
198 }
199 }
200
207 public function certificateExportFO(): void
208 {
209 $this->exportAction->export();
210 }
211
212 public function certificateDelete(): void
213 {
214 // display confirmation message
215 $cgui = new ilConfirmationGUI();
216 $cgui->setFormAction($this->ctrl->getFormAction($this, self::EDITOR_COMMAND));
217 $cgui->setHeaderText($this->lng->txt('certificate_confirm_deletion_text'));
218 $cgui->setCancel($this->lng->txt('no'), self::EDITOR_COMMAND);
219 $cgui->setConfirm($this->lng->txt('yes'), 'certificateDeleteConfirm');
220
221 $this->tpl->setContent($cgui->getHTML());
222 }
223
227 public function certificateDeleteConfirm(): void
228 {
229 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
230 $templateId = $template->getId();
231
232 $this->deleteAction->delete($templateId, $this->objectId);
233 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
234 }
235
245 public function certificateSave(): void
246 {
247 global $DIC;
248
249 $form = $this->settingsFormFactory->createForm(
250 $this
251 );
252
253 $form->setValuesByPost();
254
255 $request = $DIC->http()->request();
256
257 $formFields = $request->getParsedBody();
258
259 $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
260
261 $this->saveCertificate($form, $formFields, $this->objectId);
262 }
263
273 public function certificateUpload(): void
274 {
275 $this->certificateEditor();
276 }
277
288 public function certificateEditor(): void
289 {
290 $form = $this->getEditorForm();
291 $enabledGlobalLearningProgress = ilObjUserTracking::_enabledLearningProgress();
292
293 $messageBoxHtml = '';
294 if ($enabledGlobalLearningProgress) {
295 $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
296 $mode = $objectLearningProgressSettings->getMode();
297
299 $object = ilObjectFactory::getInstanceByObjId($this->objectId);
300 if (ilLPObjSettings::LP_MODE_DEACTIVATED === $mode && $object->getType() !== 'crs') {
301 global $DIC;
302
303 $renderer = $DIC->ui()->renderer();
304 $messageBox = $DIC->ui()
305 ->factory()
306 ->messageBox()
307 ->info($this->lng->txt('learning_progress_deactivated'))
308 ;
309
310 $messageBoxHtml = $renderer->render($messageBox);
311 $form->clearCommandButtons();
312 }
313 }
314
315 $formHtml = $form->getHTML();
316
317 $this->tpl->setVariable('ADM_CONTENT', $messageBoxHtml . $formHtml);
318 }
319
329 {
330 $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
331
332 $form = $this->settingsFormFactory->createForm(
333 $this
334 );
335
336 $formFields = $this->createFormatArray($certificateTemplate);
337
338 $formFields['active'] = $certificateTemplate->isCurrentlyActive();
339
340 $form->setValuesByArray($formFields);
341
342 return $form;
343 }
344
345 private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId): void
346 {
347 $certificate_handler = new CertificateResourceHandler(
350 $this->irss,
351 $this->global_certificate_settings,
352 $this->stakeholder,
353 );
354 $current_template = $this->templateRepository->fetchPreviousCertificate($objId);
355 $currentVersion = $current_template->getVersion();
356 $nextVersion = $currentVersion + 1;
357 $current_background_rid = $this->irss->manageContainer()->find(
358 $current_template->getBackgroundImageIdentification()
359 );
360 $current_tile_image_rid = $this->irss->manageContainer()->find(
361 $current_template->getTileImageIdentification()
362 );
363 $old_background_image = $current_background_rid === null
364 ? $current_template->getBackgroundImagePath() :
365 '';
366 $old_tile_image = $current_tile_image_rid === null
367 ? $current_template->getTileImagePath() :
368 '';
369
370 $should_delete_background =
371 $this->httpWrapper->post()->retrieve(
372 'background_delete',
373 $this->refinery->byTrying([
374 $this->refinery->kindlyTo()->bool(),
375 $this->refinery->always(false)
376 ])
377 );
378 $should_delete_tile_image =
379 $this->httpWrapper->post()->retrieve(
380 'certificate_card_tile_image_delete',
381 $this->refinery->byTrying([
382 $this->refinery->kindlyTo()->bool(),
383 $this->refinery->always(false)
384 ])
385 );
386
387 $new_background_rid = $current_background_rid && !$should_delete_background ? $current_background_rid :
388 $this->global_certificate_settings->getBackgroundImageIdentification();
389 if (
390 is_string($new_background_rid) &&
391 is_string($this->global_certificate_settings->getBackgroundImageIdentification()) &&
392 $new_background_rid === $this->global_certificate_settings->getBackgroundImageIdentification()
393 ) {
394 if ($this->file_system->has($new_background_rid)) {
395 $new_background_rid = $this->irss->manage()->stream(
396 $this->file_system->readStream($new_background_rid),
397 $this->stakeholder
398 );
399 } else {
400 $old_background_image = $new_background_rid;
401 $new_background_rid = null;
402 }
403 }
404
405 $new_tile_rid = !$should_delete_tile_image ? $current_tile_image_rid : null;
406 if ($form->checkInput()) {
407 try {
408 $this->settingsFormFactory->save($form_fields);
409
410 $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
411
412 if ($this->fileUpload->hasUploads()) {
413 if (!$this->fileUpload->hasBeenProcessed()) {
414 $this->fileUpload->process();
415 }
416 $new_background = $form->getInput('background')['tmp_name'] ?? '';
417 $new_tile_image = $form->getInput('certificate_card_tile_image')['tmp_name'] ?? '';
418 $results = $this->fileUpload->getResults();
419
420 if ($new_background !== '') {
421 $new_background_rid = $this->irss->manage()->upload(
422 $results[$new_background],
423 $this->stakeholder
424 );
425 }
426
427 if ($new_tile_image !== '') {
428 $new_tile_rid = $this->irss->manage()->upload(
429 $results[$new_tile_image],
430 $this->stakeholder
431 );
432 }
433 }
434
435 $jsonEncodedTemplateValues = json_encode($templateValues, JSON_THROW_ON_ERROR);
436
437 if (isset($new_background_rid)) {
438 $old_background_image = '';
439 }
440 if (isset($new_tile_rid)) {
441 $old_tile_image = '';
442 }
443
444 $xslfo = $this->xlsFoParser->parse($form_fields);
445 $newHashValue = hash(
446 'sha256',
447 implode('', [
448 $xslfo,
449 isset($new_background_rid) ? $new_background_rid->serialize() : '',
450 $jsonEncodedTemplateValues,
451 isset($new_tile_rid) ? $new_background_rid->serialize() : '',
452 $old_background_image, $old_tile_image
453 ])
454 );
455
456 $active = (bool) ($form_fields['active'] ?? false);
457
458 if ($newHashValue !== $current_template->getCertificateHash()) {
459 $certificateTemplate = new ilCertificateTemplate(
460 $objId,
462 $xslfo,
463 $newHashValue,
464 $jsonEncodedTemplateValues,
465 $nextVersion,
467 time(),
468 $active,
469 $old_background_image,
470 $old_tile_image,
471 isset($new_background_rid) ? $new_background_rid->serialize() : '',
472 isset($new_tile_rid) ? $new_tile_rid->serialize() : '',
473 );
474 $this->templateRepository->save($certificateTemplate);
475
476 if ($current_background_rid instanceof ResourceIdentification) {
477 $certificate_handler->handleResourceChange($current_background_rid);
478 }
479 if ($current_tile_image_rid instanceof ResourceIdentification) {
480 $certificate_handler->handleResourceChange($current_tile_image_rid);
481 }
482
483 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
484 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
485 }
486
487 if (
488 $current_template->getId() !== null &&
489 $current_template->isCurrentlyActive() !== $active
490 ) {
491 $this->templateRepository->updateActivity($current_template, $active);
492 $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_change_active_status'), true);
493 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
494 }
495
496 $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_same_not_saved'), true);
497 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
498 } catch (Exception $e) {
499 $this->tpl->setOnScreenMessage(
500 'failure',
501 $e->getMessage()
502 );
503 $this->logger->error($e->getTraceAsString());
504 }
505 }
506
507 $form->setValuesByPost();
508
509 $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
510 }
511
512 private function createFormatArray(ilCertificateTemplate $certificateTemplate): array
513 {
514 if ('' === $certificateTemplate->getCertificateHash()) {
515 $format = $this->settings->get('pageformat', '');
516 $formats = $this->pageFormats->fetchPageFormats();
517
518 return [
519 'pageformat' => $format,
520 'pagewidth' => $formats['width'] ?? '',
521 'pageheight' => $formats['height'] ?? '',
522 'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
523 'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
524 'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
525 'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
526 'certificate_text' => $certificateTemplate->getCertificateContent()
527 ];
528 }
529
530 return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
531 }
532}
$renderer
Builds data types.
Definition: Factory.php:36
Indicates that a file is missing or not found.
Indicates general problems with the input or output operations.
Definition: IOException.php:28
GUI class to create PDF certificates.
certificateExportFO()
Exports the certificate.
readonly ilXlsFoParser $xlsFoParser
createFormatArray(ilCertificateTemplate $certificateTemplate)
readonly ilCertificateTemplateExportAction $exportAction
ilGlobalPageTemplate $tpl
ilCertificateTemplateStakeholder $stakeholder
readonly WrapperFactory $httpWrapper
readonly ilObjCertificateSettings $global_certificate_settings
__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, ?ilCertificateTemplatePreviewAction $previewAction=null, ?FileUpload $fileUpload=null, private readonly ilSetting $settings=new ilSetting('certificate'), ?Filesystem $file_system=null, ?Filesystem $tmp_file_system=null)
readonly ilCertificateTemplateRepository $templateRepository
readonly Factory $refinery
readonly ilCertificateTemplatePreviewAction $previewAction
readonly ilLogger $logger
readonly ilPageFormats $pageFormats
readonly Filesystem $file_system
saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId)
ilCertificateTemplateDatabaseRepository $certificate_repo
certificateSave()
Saves the certificate.
readonly ilDBInterface $database
certificateDeleteConfirm()
Deletes the certificate and all its data.
readonly string $certificatePath
readonly ilCertificateDeleteAction $deleteAction
readonly FileUpload $fileUpload
readonly ilCertificateFormRepository $settingsFormFactory
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
Component logger with individual log levels by component id.
Class ilObjCertificateSettings.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType(int $id, bool $reference=false)
final const DEFAULT_MARGIN_BODY_RIGHT
final const DEFAULT_MARGIN_BODY_BOTTOM
final const DEFAULT_MARGIN_BODY_TOP
final const DEFAULT_MARGIN_BODY_LEFT
This class represents a property form user interface.
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-...
ILIAS Setting Class.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
const ILIAS_VERSION_NUMERIC
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilDBInterface.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
Class ilObjForumAdministration.
$results
global $DIC
Definition: shib_login.php:26
$objId
Definition: xapitoken.php:57