ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilCertificateTemplatePreviewAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
26  private readonly ilObjUser $user;
32 
33  public function __construct(
34  private readonly ilCertificateTemplateRepository $templateRepository,
35  private readonly ilCertificatePlaceholderValues $placeholderValuesObject,
36  private readonly string $rootDirectory = CLIENT_WEB_DIR,
37  ?ilObjUser $user = null,
38  ?ilCertificateUtilHelper $utilHelper = null,
39  ?ilCertificateMathJaxHelper $mathJaxHelper = null,
40  ?ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper = null,
41  ?ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper = null,
42  ?ilCertificatePdfFileNameFactory $pdfFileNameFactory = null
43  ) {
44  global $DIC;
45 
46  if (null === $user) {
47  $user = $DIC->user();
48  }
49  $this->user = $user;
50 
51  if (null === $utilHelper) {
52  $utilHelper = new ilCertificateUtilHelper();
53  }
54  $this->utilHelper = $utilHelper;
55 
56  if (null === $mathJaxHelper) {
57  $mathJaxHelper = new ilCertificateMathJaxHelper();
58  }
59  $this->mathJaxHelper = $mathJaxHelper;
60 
61  if (null === $userDefinedFieldsHelper) {
62  $userDefinedFieldsHelper = new ilCertificateUserDefinedFieldsHelper();
63  }
64  $this->userDefinedFieldsHelper = $userDefinedFieldsHelper;
65 
66  if (null === $rpcClientFactoryHelper) {
67  $rpcClientFactoryHelper = new ilCertificateRpcClientFactoryHelper();
68  }
69  $this->rpcClientFactoryHelper = $rpcClientFactoryHelper;
70 
71  if (null === $pdfFileNameFactory) {
72  $pdfFileNameFactory = new ilCertificatePdfFileNameFactory($DIC->language());
73  }
74  $this->pdfFileNameFactory = $pdfFileNameFactory;
75  }
76 
80  public function createPreviewPdf(int $objectId): void
81  {
82  $template = $this->templateRepository->fetchCurrentlyUsedCertificate($objectId);
83 
84  $xslfo = $template->getCertificateContent();
85 
86  $xslfo = $this->exchangeCertificateVariables($xslfo, $template, $objectId);
87 
88  // render tex as fo graphics
89  $xlsfo = $this->mathJaxHelper->fillXlsFoContent($xslfo);
90 
91  $pdf_base64 = $this->rpcClientFactoryHelper
92  ->ilFO2PDF('RPCTransformationHandler', $xlsfo);
93 
94  $pdfPresentation = new ilUserCertificatePresentation(
95  $template->getObjId(),
96  $template->getObjType(),
97  null,
98  '',
99  ''
100  );
101 
102  $this->utilHelper->deliverData(
103  $pdf_base64->scalar,
104  $this->pdfFileNameFactory->create($pdfPresentation),
105  'application/pdf'
106  );
107  }
108 
115  string $certificate_text,
116  ilCertificateTemplate $template,
117  int $objectId
118  ): string {
119  $insert_tags = $this->placeholderValuesObject->getPlaceholderValuesForPreview($this->user->getId(), $objectId);
120 
121  foreach ($this->getCustomCertificateFields() as $value) {
122  $insert_tags[$value['ph']] = $this->utilHelper->prepareFormOutput($value['name']);
123  }
124 
125  foreach ($insert_tags as $placeholderVariable => $value) {
126  $certificate_text = str_replace('[' . $placeholderVariable . ']', $value, $certificate_text);
127  }
128 
129  $certificate_text = str_replace(
130  '[CLIENT_WEB_DIR]',
131  'file://' . $this->rootDirectory,
132  $certificate_text
133  );
134 
135  $backgroundImagePath = $template->getBackgroundImagePath();
136 
137  return str_replace(
138  '[BACKGROUND_IMAGE]',
139  'file://' . $this->rootDirectory . $backgroundImagePath,
140  $certificate_text
141  );
142  }
143 
147  private function getCustomCertificateFields(): array
148  {
149  $user_field_definitions = $this->userDefinedFieldsHelper->createInstance();
150  $fds = $user_field_definitions->getDefinitions();
151 
152  $fields = [];
153  foreach ($fds as $f) {
154  if ($f['certificate']) {
155  $fields[$f['field_id']] = [
156  'name' => $f['field_name'],
157  'ph' => '[#' . str_replace(' ', '_', strtoupper($f['field_name'])) . ']'
158  ];
159  }
160  }
161 
162  return $fields;
163  }
164 }
exchangeCertificateVariables(string $certificate_text, ilCertificateTemplate $template, int $objectId)
Exchanges the variables in the certificate text with given values.
readonly ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper
readonly ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper
global $DIC
Definition: feed.php:28
readonly ilCertificatePdfFileNameFactory $pdfFileNameFactory
const CLIENT_WEB_DIR
Definition: constants.php:47
Just a wrapper class to create Unit Test for other classes.
__construct(private readonly ilCertificateTemplateRepository $templateRepository, private readonly ilCertificatePlaceholderValues $placeholderValuesObject, private readonly string $rootDirectory=CLIENT_WEB_DIR, ?ilObjUser $user=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateMathJaxHelper $mathJaxHelper=null, ?ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper=null, ?ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper=null, ?ilCertificatePdfFileNameFactory $pdfFileNameFactory=null)
readonly ilCertificateMathJaxHelper $mathJaxHelper