ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 string $certificatePath;
65 private readonly ilPageFormats $pageFormats;
66 private readonly ilLogger $logger;
68 private readonly ilDBInterface $database;
69
70 public function __construct(
71 private readonly ilCertificatePlaceholderDescription $placeholderDescriptionObject,
72 ilCertificatePlaceholderValues $placeholderValuesObject,
73 private readonly int $objectId,
74 string $certificatePath,
82 ?FileUpload $fileUpload = null,
83 private readonly ilSetting $settings = new ilSetting('certificate'),
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->irss = $DIC->resourceStorage();
96 $this->stakeholder = new ilCertificateTemplateStakeholder($this->objectId);
97
98 $this->tree = $DIC['tree'];
99 $this->access = $DIC['ilAccess'];
100 $this->toolbar = $DIC['ilToolbar'];
101
102 $this->global_certificate_settings = new ilObjCertificateSettings();
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->logger = $DIC->logger()->cert();
110
111 $this->settingsFormFactory = $settingsFormFactory ?? new ilCertificateSettingsFormRepository(
112 $this->objectId,
114 $this->lng,
115 $this->tpl,
116 $this->ctrl,
117 $this->access,
118 $this->toolbar,
119 $placeholderDescriptionObject,
120 $DIC->ui()->factory(),
121 $DIC->ui()->renderer()
122 );
123 $this->templateRepository = $templateRepository ?? new ilCertificateTemplateDatabaseRepository(
124 $DIC->database(),
125 $this->logger
126 );
127 $this->deleteAction = $deleteAction ?? new ilCertificateTemplateDeleteAction($this->templateRepository);
128 $this->pageFormats = $pageFormats ?? new ilPageFormats($DIC->language());
129 $this->xlsFoParser = $xlsFoParser ?? new ilXlsFoParser($DIC->settings(), $this->pageFormats);
130 $this->exportAction = $exportAction ?? new ilCertificateTemplateExportAction(
131 $this->objectId,
133 $this->templateRepository,
134 $this->irss
135 );
136 $this->previewAction = $previewAction ?? new ilCertificateTemplatePreviewAction(
137 $this->templateRepository,
138 $placeholderValuesObject,
139 $this->irss
140 );
141 $this->fileUpload = $fileUpload ?? $DIC->upload();
142 $this->database = $DIC->database();
143 }
144
155 public function executeCommand()
156 {
157 $cmd = $this->ctrl->getCmd(self::EDITOR_COMMAND);
158 $next_class = $this->ctrl->getNextClass($this);
159
160 $ret = null;
161
162 $cmd = $this->getCommand($cmd);
163 switch ($next_class) {
164 case 'ilpropertyformgui':
165 $form = $this->getEditorForm();
166 $this->ctrl->forwardCommand($form);
167
168 break;
169 default:
170 $ret = $this->$cmd();
171
172 break;
173 }
174
175 return $ret;
176 }
177
178 public function getCommand($cmd)
179 {
180 return $cmd;
181 }
182
183 public function certificateImport(): void
184 {
185 $this->certificateEditor();
186 }
187
188 public function certificatePreview(): void
189 {
190 try {
191 $this->previewAction->createPreviewPdf($this->objectId);
192 } catch (Exception) {
193 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'));
194 $this->certificateEditor();
195 }
196 }
197
204 public function certificateExportFO(): void
205 {
206 $this->exportAction->export();
207 }
208
209 public function certificateDelete(): void
210 {
211 // display confirmation message
212 $cgui = new ilConfirmationGUI();
213 $cgui->setFormAction($this->ctrl->getFormAction($this, self::EDITOR_COMMAND));
214 $cgui->setHeaderText($this->lng->txt('certificate_confirm_deletion_text'));
215 $cgui->setCancel($this->lng->txt('no'), self::EDITOR_COMMAND);
216 $cgui->setConfirm($this->lng->txt('yes'), 'certificateDeleteConfirm');
217
218 $this->tpl->setContent($cgui->getHTML());
219 }
220
224 public function certificateDeleteConfirm(): void
225 {
226 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
227 $templateId = $template->getId();
228
229 $this->deleteAction->delete($templateId, $this->objectId);
230 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
231 }
232
242 public function certificateSave(): void
243 {
244 global $DIC;
245
246 $form = $this->settingsFormFactory->createForm(
247 $this
248 );
249
250 $form->setValuesByPost();
251
252 $request = $DIC->http()->request();
253
254 $formFields = $request->getParsedBody();
255
256 $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
257
258 $this->saveCertificate($form, $formFields, $this->objectId);
259 }
260
270 public function certificateUpload(): void
271 {
272 $this->certificateEditor();
273 }
274
285 public function certificateEditor(): void
286 {
287 $form = $this->getEditorForm();
288 $enabledGlobalLearningProgress = ilObjUserTracking::_enabledLearningProgress();
289
290 $messageBoxHtml = '';
291 if ($enabledGlobalLearningProgress) {
292 $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
293 $mode = $objectLearningProgressSettings->getMode();
294
296 $object = ilObjectFactory::getInstanceByObjId($this->objectId);
297 if (ilLPObjSettings::LP_MODE_DEACTIVATED === $mode && $object->getType() !== 'crs') {
298 global $DIC;
299
300 $renderer = $DIC->ui()->renderer();
301 $messageBox = $DIC->ui()
302 ->factory()
303 ->messageBox()
304 ->info($this->lng->txt('learning_progress_deactivated'))
305 ;
306
307 $messageBoxHtml = $renderer->render($messageBox);
308 $form->clearCommandButtons();
309 }
310 }
311
312 $formHtml = $form->getHTML();
313
314 $this->tpl->setVariable('ADM_CONTENT', $messageBoxHtml . $formHtml);
315 }
316
326 {
327 $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
328
329 $form = $this->settingsFormFactory->createForm(
330 $this
331 );
332
333 $formFields = $this->createFormatArray($certificateTemplate);
334
335 $formFields['active'] = $certificateTemplate->isCurrentlyActive();
336
337 $form->setValuesByArray($formFields);
338
339 return $form;
340 }
341
342 private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, int $objId): void
343 {
344 $certificate_handler = new CertificateResourceHandler(
347 $this->irss,
348 $this->global_certificate_settings,
349 $this->stakeholder,
350 );
351 $current_template = $this->templateRepository->fetchPreviousCertificate($objId);
352 $currentVersion = $current_template->getVersion();
353 $nextVersion = $currentVersion + 1;
354 $current_background_rid = $this->irss->manageContainer()->find(
355 $current_template->getBackgroundImageIdentification()
356 );
357 $current_tile_image_rid = $this->irss->manageContainer()->find(
358 $current_template->getTileImageIdentification()
359 );
360
361 $should_delete_background =
362 $this->httpWrapper->post()->retrieve(
363 'background_delete',
364 $this->refinery->byTrying([
365 $this->refinery->kindlyTo()->bool(),
366 $this->refinery->always(false)
367 ])
368 );
369 $should_delete_tile_image =
370 $this->httpWrapper->post()->retrieve(
371 'certificate_card_tile_image_delete',
372 $this->refinery->byTrying([
373 $this->refinery->kindlyTo()->bool(),
374 $this->refinery->always(false)
375 ])
376 );
377
378 $new_background_rid = $current_background_rid && !$should_delete_background ? $current_background_rid :
379 $this->global_certificate_settings->getBackgroundImageIdentification();
380
381 $new_tile_rid = !$should_delete_tile_image ? $current_tile_image_rid : null;
382 if ($form->checkInput()) {
383 try {
384 $this->settingsFormFactory->save($form_fields);
385
386 $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
387
388 if ($this->fileUpload->hasUploads()) {
389 if (!$this->fileUpload->hasBeenProcessed()) {
390 $this->fileUpload->process();
391 }
392 $new_background = $form->getInput('background')['tmp_name'] ?? '';
393 $new_tile_image = $form->getInput('certificate_card_tile_image')['tmp_name'] ?? '';
394 $results = $this->fileUpload->getResults();
395
396 if ($new_background !== '') {
397 $new_background_rid = $this->irss->manage()->upload(
398 $results[$new_background],
399 $this->stakeholder
400 );
401 }
402
403 if ($new_tile_image !== '') {
404 $new_tile_rid = $this->irss->manage()->upload(
405 $results[$new_tile_image],
406 $this->stakeholder
407 );
408 }
409 }
410
411 $jsonEncodedTemplateValues = json_encode($templateValues, JSON_THROW_ON_ERROR);
412
413 $xslfo = $this->xlsFoParser->parse($form_fields);
414 $newHashValue = hash(
415 'sha256',
416 implode('', [
417 $xslfo,
418 isset($new_background_rid) ? $new_background_rid->serialize() : '',
419 $jsonEncodedTemplateValues,
420 isset($new_tile_rid) ? $new_background_rid->serialize() : ''
421 ])
422 );
423
424 $active = (bool) ($form_fields['active'] ?? false);
425
426 if ($newHashValue !== $current_template->getCertificateHash()) {
427 $certificateTemplate = new ilCertificateTemplate(
428 $objId,
430 $xslfo,
431 $newHashValue,
432 $jsonEncodedTemplateValues,
433 $nextVersion,
435 time(),
436 $active,
437 isset($new_background_rid) ? $new_background_rid->serialize() : '',
438 isset($new_tile_rid) ? $new_tile_rid->serialize() : '',
439 );
440 $this->templateRepository->save($certificateTemplate);
441
442 if ($current_background_rid instanceof ResourceIdentification) {
443 $certificate_handler->handleResourceChange($current_background_rid);
444 }
445 if ($current_tile_image_rid instanceof ResourceIdentification) {
446 $certificate_handler->handleResourceChange($current_tile_image_rid);
447 }
448
449 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
450 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
451 }
452
453 if (
454 $current_template->getId() !== null &&
455 $current_template->isCurrentlyActive() !== $active
456 ) {
457 $this->templateRepository->updateActivity($current_template, $active);
458 $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_change_active_status'), true);
459 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
460 }
461
462 $this->tpl->setOnScreenMessage('info', $this->lng->txt('certificate_same_not_saved'), true);
463 $this->ctrl->redirect($this, self::EDITOR_COMMAND);
464 } catch (Exception $e) {
465 $this->tpl->setOnScreenMessage(
466 'failure',
467 $e->getMessage()
468 );
469 $this->logger->error($e->getTraceAsString());
470 }
471 }
472
473 $form->setValuesByPost();
474
475 $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
476 }
477
478 private function createFormatArray(ilCertificateTemplate $certificateTemplate): array
479 {
480 if ('' === $certificateTemplate->getCertificateHash()) {
481 $format = $this->settings->get('pageformat', '');
482 $formats = $this->pageFormats->fetchPageFormats();
483
484 return [
485 'pageformat' => $format,
486 'pagewidth' => $formats['width'] ?? '',
487 'pageheight' => $formats['height'] ?? '',
488 'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
489 'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
490 'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
491 'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
492 'certificate_text' => $certificateTemplate->getCertificateContent()
493 ];
494 }
495
496 return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
497 }
498}
$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
__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 $tmp_file_system=null)
ilGlobalPageTemplate $tpl
ilCertificateTemplateStakeholder $stakeholder
readonly WrapperFactory $httpWrapper
readonly ilObjCertificateSettings $global_certificate_settings
readonly ilCertificateTemplateRepository $templateRepository
readonly Factory $refinery
readonly ilCertificateTemplatePreviewAction $previewAction
readonly ilLogger $logger
readonly ilPageFormats $pageFormats
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:55