ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCertificateGUI.php
Go to the documentation of this file.
1<?php
2 /*
3 +----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +----------------------------------------------------------------------------+
22*/
23
33{
38
42 private $fileSystem;
43
49 protected $ctrl;
50
56 protected $tree;
57
63 protected $ilias;
64
70 protected $tpl;
71
77 protected $lng;
78
84 protected $ref_id;
85
89 protected $access;
90
94 protected $toolbar;
95
100
105
110
114 private $objectId;
115
120
125
130
135
140
145
150
155
159 private $fileUpload;
160
165
169 private $settings;
170
175
178
179 public function __construct(
182 $objectId,
193 \ILIAS\FileUpload\FileUpload $fileUpload = null,
194 ilSetting $settings = null,
197 ilCertificateBackgroundImageFileService $imageFileService = null,
199 ) {
200 global $DIC;
201
202 $this->lng = $DIC['lng'];
203 $this->tpl = $DIC['tpl'];
204 $this->ctrl = $DIC['ilCtrl'];
205 $this->ilias = $DIC['ilias'];
206 $this->tree = $DIC['tree'];
207 $this->access = $DIC['ilAccess'];
208 $this->toolbar = $DIC['ilToolbar'];
209
210 $this->lng->loadLanguageModule('certificate');
211 $this->lng->loadLanguageModule('cert');
212 $this->lng->loadLanguageModule("trac");
213
214 $this->ref_id = (int) $_GET['ref_id'];
215
216 $this->placeholderDescriptionObject = $placeholderDescriptionObject;
217
218 $this->placeholderValuesObject = $placeholderValuesObject;
219
220 $this->objectId = $objectId;
221
222 $logger = $DIC->logger()->cert();
223
224 if (null === $settingsFormFactory) {
226 $this->objectId,
228 $this->lng,
229 $this->tpl,
230 $this->ctrl,
231 $this->access,
232 $this->toolbar,
234 );
235 }
236 $this->settingsFormFactory = $settingsFormFactory;
237
238 if (null === $templateRepository) {
239 $templateRepository = new ilCertificateTemplateRepository($DIC->database(), $logger);
240 }
241 $this->templateRepository = $templateRepository;
242
243 if (null === $deleteAction) {
245 }
246 $this->deleteAction = $deleteAction;
247
248 if (null === $formFieldParser) {
250 }
251 $this->formFieldParser = $formFieldParser;
252
253 if (null === $pageFormats) {
254 $pageFormats = new ilPageFormats($DIC->language());
255 }
256 $this->pageFormats = $pageFormats;
257
258 if (null === $xlsFoParser) {
259 $xlsFoParser = new ilXlsFoParser($DIC->settings(), $pageFormats);
260 }
261 $this->xlsFoParser = $xlsFoParser;
262
263 if (null === $upload) {
265 $DIC->upload(),
267 $DIC->language(),
268 $logger
269 );
270 }
271 $this->backgroundImageUpload = $upload;
272
273 if (null === $exportAction) {
275 $this->objectId,
277 $this->templateRepository,
278 $DIC->filesystem()->web()
279 );
280 }
281 $this->exportAction = $exportAction;
282
283 if (null === $previewAction) {
285 }
286 $this->previewAction = $previewAction;
287
288 if (null === $fileUpload) {
289 global $DIC;
290 $fileUpload = $DIC->upload();
291 }
292 $this->fileUpload = $fileUpload;
293
294 $this->certificatePath = $certificatePath;
295
296 if (null === $settings) {
297 $settings = new ilSetting('certificate');
298 }
299 $this->settings = $settings;
300
301 if (null === $fileSystem) {
302 $fileSystem = $DIC->filesystem()->web();
303 }
304 $this->fileSystem = $fileSystem;
305
306 if (null === $imageFileService) {
307 $imageFileService = new ilCertificateBackgroundImageFileService(
308 $this->certificatePath,
309 $this->fileSystem
310 );
311 }
312
313 if (null === $backgroundImageDelete) {
315 $this->certificatePath,
316 $imageFileService
317 );
318 }
319 $this->backgroundImageDelete = $backgroundImageDelete;
320
321 if (null === $tmp_file_system) {
322 $tmp_file_system = $DIC->filesystem()->temp();
323 }
324 $this->tmp_file_system = $tmp_file_system;
325 }
326
330 public function executeCommand()
331 {
332 $cmd = $this->ctrl->getCmd();
333 $next_class = $this->ctrl->getNextClass($this);
334
335 $cmd = $this->getCommand($cmd);
336 switch ($next_class) {
337 case 'ilpropertyformgui':
338 $form = $this->getEditorForm();
339 $this->ctrl->forwardCommand($form);
340 break;
341
342 default:
343 $ret = $this->$cmd();
344 break;
345 }
346 return $ret;
347 }
348
352 public function getCommand($cmd)
353 {
354 return $cmd;
355 }
356
360 public function certificateImport()
361 {
362 $this->certificateEditor();
363 }
364
368 public function certificatePreview()
369 {
370 try {
371 $this->previewAction->createPreviewPdf($this->objectId);
372 } catch (Exception $exception) {
373 ilUtil::sendFailure($this->lng->txt('error_creating_certificate_pdf', true));
374 $this->certificateEditor();
375 }
376 }
377
381 public function certificateExportFO()
382 {
383 $this->exportAction->export();
384 }
385
390 {
391 $this->backgroundImageDelete->deleteBackgroundImage('');
392 $this->certificateEditor();
393 }
394
398 public function certificateDelete()
399 {
400 // display confirmation message
401 $cgui = new ilConfirmationGUI();
402 $cgui->setFormAction($this->ctrl->getFormAction($this, "certificateEditor"));
403 $cgui->setHeaderText($this->lng->txt("certificate_confirm_deletion_text"));
404 $cgui->setCancel($this->lng->txt("no"), "certificateEditor");
405 $cgui->setConfirm($this->lng->txt("yes"), "certificateDeleteConfirm");
406
407 $this->tpl->setContent($cgui->getHTML());
408 }
409
413 public function certificateDeleteConfirm()
414 {
415 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
416 $templateId = $template->getId();
417
418 $this->deleteAction->delete($templateId, $this->objectId);
419 $this->ctrl->redirect($this, "certificateEditor");
420 }
421
425 public function certificateSave()
426 {
427 global $DIC;
428
429 $form = $this->settingsFormFactory->createForm(
430 $this
431 );
432
433 $form->setValuesByPost();
434
435 $request = $DIC->http()->request();
436
437 $formFields = $request->getParsedBody();
438
439 $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
440
441 $this->saveCertificate($form, $formFields, $this->objectId);
442 }
443
447 public function certificateUpload()
448 {
449 $this->certificateEditor();
450 }
451
461 private function getEditorForm() : \ilPropertyFormGUI
462 {
463 $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
464
465 $form = $this->settingsFormFactory->createForm(
466 $this
467 );
468
469 $formFields = $this->createFormatArray($certificateTemplate);
470
471 $formFields['active'] = $certificateTemplate->isCurrentlyActive();
472
473 $form->setValuesByArray($formFields);
474
475 return $form;
476 }
477
481 public function certificateEditor()
482 {
483 $form = $this->getEditorForm();
484 $enabledGlobalLearningProgress = \ilObjUserTracking::_enabledLearningProgress();
485
486 $messageBoxHtml = '';
487 if ($enabledGlobalLearningProgress) {
488 $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
489 $mode = $objectLearningProgressSettings->getMode();
490
492 $object = ilObjectFactory::getInstanceByObjId($this->objectId);
493 if (ilLPObjSettings::LP_MODE_DEACTIVATED == $mode && $object->getType() !== 'crs') {
494 global $DIC;
495
496 $renderer = $DIC->ui()->renderer();
497 $messageBox = $DIC->ui()
498 ->factory()
499 ->messageBox()
500 ->info($this->lng->txt('learning_progress_deactivated'));
501
502 $messageBoxHtml = $renderer->render($messageBox);
503 $form->clearCommandButtons();
504 }
505 }
506
507 $formHtml = $form->getHTML();
508
509 $this->tpl->setVariable("ADM_CONTENT", $messageBoxHtml . $formHtml);
510 }
511
517 private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, $objId)
518 {
519 $previousCertificateTemplate = $this->templateRepository->fetchPreviousCertificate($objId);
520 $currentVersion = $previousCertificateTemplate->getVersion();
521 $nextVersion = $currentVersion + 1;
522
523 if ($_POST["background_delete"]) {
524 $this->backgroundImageDelete->deleteBackgroundImage($currentVersion);
525 }
526
527 if ($form->checkInput()) {
528 try {
529 $this->settingsFormFactory->save($form_fields);
530
531 $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
532
533 // handle the background upload
534 $backgroundImagePath = '';
535 $temporaryFileName = $_FILES['background']['tmp_name'];
536 if (strlen($temporaryFileName)) {
537 try {
538 $backgroundImagePath = $this->backgroundImageUpload->uploadBackgroundImage(
539 $temporaryFileName,
540 $nextVersion,
541 $form->getInput('background')
542 );
543 } catch (ilException $exception) {
544 $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
545 }
546 if (false === $this->fileSystem->has($backgroundImagePath)) {
547 $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
548 $backgroundImagePath = '';
549 }
550 }
551 if ($backgroundImagePath === '') {
552 if ($_POST['background_delete'] || $previousCertificateTemplate->getBackgroundImagePath() === '') {
553 $globalBackgroundImagePath = ilObjCertificateSettingsAccess::getBackgroundImagePath(true);
554 $backgroundImagePath = str_replace('[CLIENT_WEB_DIR]', '', $globalBackgroundImagePath);
555 } else {
556 $backgroundImagePath = $previousCertificateTemplate->getBackgroundImagePath();
557 }
558 }
559
560 // handle the card thumbnail upload
561 $cardThumbnailImagePath = '';
562 $temporaryFileName = $_FILES['certificate_card_thumbnail_image']['tmp_name'];
563 if (strlen($temporaryFileName) && $this->fileUpload->hasUploads()) {
564 try {
565 if (false === $this->fileUpload->hasBeenProcessed()) {
566 $this->fileUpload->process();
567 }
568
570 $uploadResults = $this->fileUpload->getResults();
571 $pending_card_file = $form->getInput('certificate_card_thumbnail_image');
572 $cardThumbnailFileName = 'card_thumbnail_image_' . $nextVersion . '.svg';
573 if (isset($uploadResults[$temporaryFileName])) {
574 $result = $uploadResults[$temporaryFileName];
575 if ($result->getStatus() == \ILIAS\FileUpload\DTO\ProcessingStatus::OK) {
576 $this->fileUpload->moveOneFileTo(
577 $result,
578 $this->certificatePath,
579 \ILIAS\FileUpload\Location::WEB,
580 $cardThumbnailFileName,
581 true
582 );
583
584 $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
585 }
586 } elseif ($pending_card_file !== null && !empty($pending_card_file)) {
587 $stream = $this->tmp_file_system->readStream(basename($pending_card_file['tmp_name']));
588 $this->fileSystem->writeStream($this->certificatePath . '/' . $cardThumbnailFileName, $stream);
589 $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
590 } else {
591 throw new ilException($this->lng->txt('upload_error_file_not_found'));
592 }
593 } catch (ilException $exception) {
594 $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
595 }
596 if (false === $this->fileSystem->has($cardThumbnailImagePath)) {
597 $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
598 $cardThumbnailImagePath = '';
599 }
600 }
601 if ($cardThumbnailImagePath === '' && !$_POST['certificate_card_thumbnail_image_delete']) {
602 $cardThumbnailImagePath = $previousCertificateTemplate->getThumbnailImagePath();
603 }
604
605 $jsonEncodedTemplateValues = json_encode($templateValues);
606
607 $xslfo = $this->xlsFoParser->parse($form_fields);
608
609 $newHashValue = hash(
610 'sha256',
611 implode('', array(
612 $xslfo,
613 $backgroundImagePath,
614 $jsonEncodedTemplateValues,
615 $cardThumbnailImagePath
616 ))
617 );
618
619 $active = (bool) $form_fields['active'];
620
621 if ($newHashValue !== $previousCertificateTemplate->getCertificateHash()) {
622 $certificateTemplate = new ilCertificateTemplate(
623 $objId,
625 $xslfo,
626 $newHashValue,
627 $jsonEncodedTemplateValues,
628 $nextVersion,
630 time(),
631 $active,
632 $backgroundImagePath,
633 $cardThumbnailImagePath
634 );
635
636 $this->templateRepository->save($certificateTemplate);
637 ilUtil::sendSuccess($this->lng->txt("saved_successfully"), true);
638 $this->ctrl->redirect($this, "certificateEditor");
639 }
640
641 if ($previousCertificateTemplate->getId() !== null && $previousCertificateTemplate->isCurrentlyActive() !== $active) {
642 $this->templateRepository->updateActivity($previousCertificateTemplate, $active);
643 ilUtil::sendInfo($this->lng->txt('certificate_change_active_status'), true);
644 $this->ctrl->redirect($this, "certificateEditor");
645 }
646
647 ilUtil::sendInfo($this->lng->txt('certificate_same_not_saved'), true);
648 $this->ctrl->redirect($this, "certificateEditor");
649 } catch (Exception $e) {
650 ilUtil::sendFailure($e->getMessage());
651 }
652 }
653
654 $form->setValuesByPost();
655
656 $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
657 }
658
664 private function setTemplateContent(ilCertificateTemplate $certificate, ilPropertyFormGUI $form)
665 {
666 $form_fields = $this->settingsFormFactory->fetchFormFieldData($certificate->getCertificateContent());
667 $form_fields['active'] = $certificate->isCurrentlyActive();
668
669 $form->setValuesByArray($form_fields);
670
671 $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
672 }
673
678 private function createFormatArray(ilCertificateTemplate $certificateTemplate)
679 {
680 if ('' === $certificateTemplate->getCertificateHash()) {
681 $format = $this->settings->get('pageformat');
682 $formats = $this->pageFormats->fetchPageFormats();
683
684 $formFieldArray = array(
685 'pageformat' => $format,
686 'pagewidth' => $formats['width'],
687 'pageheight' => $formats['height'],
688 'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
689 'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
690 'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
691 'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
692 'certificate_text' => $certificateTemplate->getCertificateContent()
693 );
694
695 return $formFieldArray;
696 }
697 return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
698 }
699}
$result
$_GET["client_id"]
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
GUI class to create PDF certificates.
certificateExportFO()
Exports the certificate.
createFormatArray(ilCertificateTemplate $certificateTemplate)
certificatePreview()
Creates a certificate preview.
__construct(ilCertificatePlaceholderDescription $placeholderDescriptionObject, ilCertificatePlaceholderValues $placeholderValuesObject, $objectId, $certificatePath, ilCertificateFormRepository $settingsFormFactory=null, ilCertificateDeleteAction $deleteAction=null, ilCertificateTemplateRepository $templateRepository=null, ilPageFormats $pageFormats=null, ilXlsFoParser $xlsFoParser=null, ilFormFieldParser $formFieldParser=null, ilCertificateTemplateExportAction $exportAction=null, ilCertificateBackgroundImageUpload $upload=null, ilCertificateTemplatePreviewAction $previewAction=null, \ILIAS\FileUpload\FileUpload $fileUpload=null, ilSetting $settings=null, ilCertificateBackgroundImageDelete $backgroundImageDelete=null, \ILIAS\Filesystem\Filesystem $fileSystem=null, ilCertificateBackgroundImageFileService $imageFileService=null, \ILIAS\Filesystem\Filesystem $tmp_file_system=null)
certificateImport()
Import a certificate from a ZIP archive.
setTemplateContent(ilCertificateTemplate $certificate, ilPropertyFormGUI $form)
certificateDelete()
Deletes the certificate and all its data.
getCommand($cmd)
Retrieves the ilCtrl command.
executeCommand()
execute command
certificateUpload()
Uploads the certificate.
certificateSave()
Saves the certificate.
certificateDeleteConfirm()
Deletes the certificate and all its data.
certificateRemoveBackground()
Removes the background image of a certificate.
Confirmation screen class.
Base class for ILIAS Exception handling.
static getBackgroundImagePath($asRelative=false)
Returns the filesystem path of the background image.
static _enabledLearningProgress()
check wether learing progress is enabled or not
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupType($a_id, $a_reference=false)
lookup object type
const DEFAULT_MARGIN_BODY_BOTTOM
This class represents a property form user interface.
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
checkInput()
Check Post Input.
setValuesByPost()
Set form values from POST values.
getItemByPostVar($a_post_var)
Get Item by POST variable.
ILIAS Setting Class.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
$formats
Definition: date.php:77
const ILIAS_VERSION_NUMERIC
$format
Definition: metadata.php:218
Class FlySystemFileAccessTest.
Class ChatMainBarProvider \MainMenu\Provider.
redirection script todo: (a better solution should control the processing via a xml file)
$ret
Definition: parser.php:6
settings()
Definition: settings.php:2
$DIC
Definition: xapitoken.php:46
$objId
Definition: xapitoken.php:41