ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
24 include_once("./Services/Certificate/classes/class.ilCertificate.php");
25 
35 {
39  private $fileSystem;
40 
45  protected $certifcateObject;
46 
52  protected $ctrl;
53 
59  protected $tree;
60 
66  protected $ilias;
67 
73  protected $tpl;
74 
80  protected $lng;
81 
87  protected $ref_id;
88 
92  protected $access;
93 
97  protected $toolbar;
98 
103 
108 
113 
117  private $objectId;
118 
123 
128 
132  private $xlsFoParser;
133 
137  private $deleteAction;
138 
142  private $exportAction;
143 
148 
152  private $previewAction;
153 
158 
162  private $fileUpload;
163 
168 
172  private $settings;
173 
177  private $pageFormats;
178 
199  public function __construct(
200  ilCertificateAdapter $adapter,
203  $objectId,
212  ilCertificateBackgroundImageUpload $upload = null,
214  \ILIAS\FileUpload\FileUpload $fileUpload = null,
215  ilSetting $settings = null,
217  ) {
218  global $DIC;
219 
220 
221  $this->certifcateObject = new ilCertificate(
222  $adapter,
223  $placeholderDescriptionObject,
224  $placeholderValuesObject,
225  $objectId,
227  );
228 
229  $this->lng = $DIC['lng'];
230  $this->tpl = $DIC['tpl'];
231  $this->ctrl = $DIC['ilCtrl'];
232  $this->ilias = $DIC['ilias'];
233  $this->tree = $DIC['tree'];
234  $this->tree = $DIC['tree'];
235  $this->access = $DIC['ilAccess'];
236  $this->toolbar = $DIC['ilToolbar'];
237 
238  $this->lng->loadLanguageModule('certificate');
239  $this->lng->loadLanguageModule('cert');
240  $this->lng->loadLanguageModule("trac");
241 
242  $this->ref_id = (int) $_GET['ref_id'];
243 
244  $this->placeholderDescriptionObject = $placeholderDescriptionObject;
245 
246  $this->placeholderValuesObject = $placeholderValuesObject;
247 
248  $this->objectId = $objectId;
249 
250  $logger = $DIC->logger()->cert();
251 
252  if (null === $settingsFormFactory) {
254  $this->objectId,
256  $this->lng,
257  $this->tpl,
258  $this->ctrl,
259  $this->access,
260  $this->toolbar,
261  $placeholderDescriptionObject
262  );
263  }
264  $this->settingsFormFactory = $settingsFormFactory;
265 
266  if (null === $templateRepository) {
267  $templateRepository = new ilCertificateTemplateRepository($DIC->database(), $logger);
268  }
269  $this->templateRepository = $templateRepository;
270 
271  if (null === $deleteAction) {
273  }
274  $this->deleteAction = $deleteAction;
275 
276  if (null === $formFieldParser) {
278  }
279  $this->formFieldParser = $formFieldParser;
280 
281  if (null === $pageFormats) {
282  $pageFormats = new ilPageFormats($DIC->language());
283  }
284  $this->pageFormats = $pageFormats;
285 
286  if (null === $xlsFoParser) {
287  $xlsFoParser = new ilXlsFoParser($DIC->settings(), $pageFormats);
288  }
289  $this->xlsFoParser = $xlsFoParser;
290 
291  if (null === $upload) {
293  $DIC->upload(),
295  $DIC->language(),
296  $logger
297  );
298  }
299  $this->backgroundImageUpload = $upload;
300 
301  if (null === $exportAction) {
303  $this->objectId,
305  $this->templateRepository,
306  $DIC->filesystem()->web()
307  );
308  }
309  $this->exportAction = $exportAction;
310 
311  if (null === $previewAction) {
313  }
314  $this->previewAction = $previewAction;
315 
316  if (null === $fileUpload) {
317  global $DIC;
318  $fileUpload = $DIC->upload();
319  }
320  $this->fileUpload = $fileUpload;
321 
322  $this->certificatePath = $certificatePath;
323 
324  if (null === $settings) {
325  $settings = new ilSetting('certificate');
326  }
327  $this->settings = $settings;
328 
329  if (null === $fileSystem) {
330  $fileSystem = $DIC->filesystem()->web();
331  }
332  $this->fileSystem = $fileSystem;
333  }
334 
338  public function executeCommand()
339  {
340  $cmd = $this->ctrl->getCmd();
341  $next_class = $this->ctrl->getNextClass($this);
342 
343  $cmd = $this->getCommand($cmd);
344  switch ($next_class) {
345  case 'ilpropertyformgui':
346  $form = $this->getEditorForm();
347  $this->ctrl->forwardCommand($form);
348  break;
349 
350  default:
351  $ret = $this->$cmd();
352  break;
353  }
354  return $ret;
355  }
356 
360  public function getCommand($cmd)
361  {
362  return $cmd;
363  }
364 
368  public function certificateImport()
369  {
370  $this->certificateEditor();
371  }
372 
376  public function certificatePreview()
377  {
378  try {
379  $this->previewAction->createPreviewPdf($this->objectId);
380  } catch (Exception $exception) {
381  ilUtil::sendFailure($this->lng->txt('error_creating_certificate_pdf', true));
382  $this->certificateEditor();
383  }
384  }
385 
389  public function certificateExportFO()
390  {
391  $this->exportAction->export();
392  }
393 
397  public function certificateRemoveBackground()
398  {
399  $this->certifcateObject->deleteBackgroundImage();
400  $this->certificateEditor();
401  }
402 
406  public function certificateDelete()
407  {
408  // display confirmation message
409  $cgui = new ilConfirmationGUI();
410  $cgui->setFormAction($this->ctrl->getFormAction($this, "certificateEditor"));
411  $cgui->setHeaderText($this->lng->txt("certificate_confirm_deletion_text"));
412  $cgui->setCancel($this->lng->txt("no"), "certificateEditor");
413  $cgui->setConfirm($this->lng->txt("yes"), "certificateDeleteConfirm");
414 
415  $this->tpl->setContent($cgui->getHTML());
416  }
417 
421  public function certificateDeleteConfirm()
422  {
423  $template = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
424  $templateId = $template->getId();
425 
426  $this->deleteAction->delete($templateId, $this->objectId);
427  $this->ctrl->redirect($this, "certificateEditor");
428  }
429 
433  public function certificateSave()
434  {
435  global $DIC;
436 
437  $form = $this->settingsFormFactory->createForm(
438  $this,
439  $this->certifcateObject
440  );
441 
442  $form->setValuesByPost();
443 
444  $request = $DIC->http()->request();
445 
446  $formFields = $request->getParsedBody();
447 
448  $this->tpl->setVariable('ADM_CONTENT', $form->getHTML());
449 
450  $this->saveCertificate($form, $formFields, $this->objectId);
451  }
452 
456  public function certificateUpload()
457  {
458  $this->certificateEditor();
459  }
460 
470  private function getEditorForm() : \ilPropertyFormGUI
471  {
472  $certificateTemplate = $this->templateRepository->fetchCurrentlyUsedCertificate($this->objectId);
473 
474  $form = $this->settingsFormFactory->createForm(
475  $this,
476  $this->certifcateObject
477  );
478 
479  $formFields = $this->createFormatArray($certificateTemplate);
480 
481  $formFields['active'] = $certificateTemplate->isCurrentlyActive();
482 
483  $form->setValuesByArray($formFields);
484 
485  return $form;
486  }
487 
491  public function certificateEditor()
492  {
493  $form = $this->getEditorForm();
494  $enabledGlobalLearningProgress = \ilObjUserTracking::_enabledLearningProgress();
495 
496  $messageBoxHtml = '';
497  if ($enabledGlobalLearningProgress) {
498  $objectLearningProgressSettings = new ilLPObjSettings($this->objectId);
499  $mode = $objectLearningProgressSettings->getMode();
500 
502  $object = ilObjectFactory::getInstanceByObjId($this->objectId);
503  if (ilLPObjSettings::LP_MODE_DEACTIVATED == $mode && $object->getType() !== 'crs') {
504  global $DIC;
505 
506  $renderer = $DIC->ui()->renderer();
507  $messageBox = $DIC->ui()
508  ->factory()
509  ->messageBox()
510  ->info($this->lng->txt('learning_progress_deactivated'));
511 
512  $messageBoxHtml = $renderer->render($messageBox);
513  $form->clearCommandButtons();
514  }
515  }
516 
517  $formHtml = $form->getHTML();
518 
519  $this->tpl->setVariable("ADM_CONTENT", $messageBoxHtml . $formHtml);
520  }
521 
527  private function saveCertificate(ilPropertyFormGUI $form, array $form_fields, $objId)
528  {
529  $previousCertificateTemplate = $this->templateRepository->fetchPreviousCertificate($objId);
530  $currentVersion = $previousCertificateTemplate->getVersion();
531  $nextVersion = $currentVersion + 1;
532 
533  if ($_POST["background_delete"]) {
534  $this->certifcateObject->deleteBackgroundImage($currentVersion);
535  }
536 
537  if ($form->checkInput()) {
538  try {
539  $this->settingsFormFactory->save($form_fields);
540 
541  $templateValues = $this->placeholderDescriptionObject->getPlaceholderDescriptions();
542 
543  // handle the background upload
544  $backgroundImagePath = '';
545  $temporaryFileName = $_FILES['background']['tmp_name'];
546  if (strlen($temporaryFileName)) {
547  try {
548  $backgroundImagePath = $this->backgroundImageUpload->uploadBackgroundImage($temporaryFileName, $nextVersion);
549  } catch (ilException $exception) {
550  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
551  }
552  if (false === $this->fileSystem->has($backgroundImagePath)) {
553  $form->getItemByPostVar('background')->setAlert($this->lng->txt("certificate_error_upload_bgimage"));
554  $backgroundImagePath = '';
555  }
556  }
557  if ($backgroundImagePath === '') {
558  if ($_POST['background_delete'] || $previousCertificateTemplate->getBackgroundImagePath() === '') {
559  $globalBackgroundImagePath = ilObjCertificateSettingsAccess::getBackgroundImagePath(true);
560  $backgroundImagePath = str_replace('[CLIENT_WEB_DIR]', '', $globalBackgroundImagePath);
561  } else {
562  $backgroundImagePath = $previousCertificateTemplate->getBackgroundImagePath();
563  }
564  }
565 
566  // handle the card thumbnail upload
567  $cardThumbnailImagePath = '';
568  $temporaryFileName = $_FILES['certificate_card_thumbnail_image']['tmp_name'];
569  if (strlen($temporaryFileName) && $this->fileUpload->hasUploads()) {
570  try {
571  if (false === $this->fileUpload->hasBeenProcessed()) {
572  $this->fileUpload->process();
573  }
574 
576  $uploadResults = $this->fileUpload->getResults();
577  $result = $uploadResults[$temporaryFileName];
579  $cardThumbnailFileName = 'card_thumbnail_image_' . $nextVersion . '.svg';
580 
581  $this->fileUpload->moveOneFileTo(
582  $result,
583  $this->certificatePath,
584  \ILIAS\FileUpload\Location::WEB,
585  $cardThumbnailFileName,
586  true
587  );
588 
589  $cardThumbnailImagePath = $this->certificatePath . $cardThumbnailFileName;
590  }
591  } catch (ilException $exception) {
592  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
593  }
594  if (false === $this->fileSystem->has($cardThumbnailImagePath)) {
595  $form->getItemByPostVar('certificate_card_thumbnail_image')->setAlert($this->lng->txt("certificate_error_upload_ctimage"));
596  $cardThumbnailImagePath = '';
597  }
598  }
599  if ($cardThumbnailImagePath === '' && !$_POST['certificate_card_thumbnail_image_delete']) {
600  $cardThumbnailImagePath = $previousCertificateTemplate->getThumbnailImagePath();
601  }
602 
603  $jsonEncodedTemplateValues = json_encode($templateValues);
604 
605  $xslfo = $this->xlsFoParser->parse($form_fields);
606 
607  $newHashValue = hash(
608  'sha256',
609  implode('', array(
610  $xslfo,
611  $backgroundImagePath,
612  $jsonEncodedTemplateValues,
613  $cardThumbnailImagePath
614  ))
615  );
616 
617  $active = (bool) $form_fields['active'];
618 
619  if ($newHashValue !== $previousCertificateTemplate->getCertificateHash()) {
620  $certificateTemplate = new ilCertificateTemplate(
621  $objId,
622  ilObject::_lookupType($objId),
623  $xslfo,
624  $newHashValue,
625  $jsonEncodedTemplateValues,
626  $nextVersion,
628  time(),
629  $active,
630  $backgroundImagePath,
631  $cardThumbnailImagePath
632  );
633 
634  $this->templateRepository->save($certificateTemplate);
635  ilUtil::sendSuccess($this->lng->txt("saved_successfully"), true);
636  $this->ctrl->redirect($this, "certificateEditor");
637  }
638 
639  if ($previousCertificateTemplate->getId() !== null && $previousCertificateTemplate->isCurrentlyActive() !== $active) {
640  $this->templateRepository->updateActivity($previousCertificateTemplate, $active);
641  ilUtil::sendInfo($this->lng->txt('certificate_change_active_status'), true);
642  $this->ctrl->redirect($this, "certificateEditor");
643  }
644 
645  ilUtil::sendInfo($this->lng->txt('certificate_same_not_saved'), true);
646  $this->ctrl->redirect($this, "certificateEditor");
647  } catch (Exception $e) {
648  ilUtil::sendFailure($e->getMessage());
649  }
650  }
651 
652  $form->setValuesByPost();
653 
654  $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
655  }
656 
663  {
664  $form_fields = $this->settingsFormFactory->fetchFormFieldData($certificate->getCertificateContent());
665  $form_fields['active'] = $certificate->isCurrentlyActive();
666 
667  $form->setValuesByArray($form_fields);
668 
669  $this->tpl->setVariable("ADM_CONTENT", $form->getHTML());
670  }
671 
676  private function createFormatArray(ilCertificateTemplate $certificateTemplate)
677  {
678  if ('' === $certificateTemplate->getCertificateHash()) {
679  $format = $this->settings->get('pageformat');
680  $formats = $this->pageFormats->fetchPageFormats();
681 
682  $formFieldArray = array(
683  'pageformat' => $format,
684  'pagewidth' => $formats['width'],
685  'pageheight' => $formats['height'],
686  'margin_body_top' => ilPageFormats::DEFAULT_MARGIN_BODY_TOP,
687  'margin_body_right' => ilPageFormats::DEFAULT_MARGIN_BODY_RIGHT,
688  'margin_body_bottom' => ilPageFormats::DEFAULT_MARGIN_BODY_BOTTOM,
689  'margin_body_left' => ilPageFormats::DEFAULT_MARGIN_BODY_LEFT,
690  'certificate_text' => $certificateTemplate->getCertificateContent()
691  );
692 
693  return $formFieldArray;
694  }
695  return $this->settingsFormFactory->fetchFormFieldData($certificateTemplate->getCertificateContent());
696  }
697 }
getCommand($cmd)
Retrieves the ilCtrl command.
certificateSave()
Saves the certificate.
settings()
Definition: settings.php:2
$format
Definition: metadata.php:141
getItemByPostVar($a_post_var)
Get Item by POST variable.
$result
const ILIAS_VERSION_NUMERIC
certificatePreview()
Creates a certificate preview.
$template
This class represents a property form user interface.
foreach($paths as $path) $request
Definition: asyncclient.php:32
global $DIC
Definition: saml.php:7
$_GET["client_id"]
Class BaseForm.
static getBackgroundImagePath($asRelative=false)
Returns the filesystem path of the background image.
executeCommand()
execute command
createFormatArray(ilCertificateTemplate $certificateTemplate)
certificateDeleteConfirm()
Deletes the certificate and all its data.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
static _enabledLearningProgress()
check wether learing progress is enabled or not
certificateExportFO()
Exports the certificate.
if(isset($_POST['submit'])) $form
certificateImport()
Import a certificate from a ZIP archive.
certificateUpload()
Uploads the certificate.
GUI class to create PDF certificates.
checkInput()
Check Post Input.
redirection script todo: (a better solution should control the processing via a xml file) ...
__construct(ilCertificateAdapter $adapter, 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, \ILIAS\Filesystem\Filesystem $fileSystem=null)
ilCertificateGUI constructor
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
certificateRemoveBackground()
Removes the background image of a certificate.
const DEFAULT_MARGIN_BODY_BOTTOM
setValuesByPost()
Set form values from POST values.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
Adapter class to provide certificate data for the certificate generator.
$ret
Definition: parser.php:6
setTemplateContent(ilCertificateTemplate $certificate, ilPropertyFormGUI $form)
Create PDF certificates.
certificateDelete()
Deletes the certificate and all its data.
hash(StreamInterface $stream, $algo, $rawOutput=false)
Calculate a hash of a Stream.
Definition: functions.php:406
$_POST["username"]
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
Class FlySystemFileAccessTest.
$formats
Definition: date.php:77
Confirmation screen class.