ILIAS  release_8 Revision v8.24
ilCertificateTemplatePreviewAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
34 private string $rootDirectory;
36
37 public function __construct(
40 ?ilLogger $logger = null,
41 ?ilObjUser $user = null,
48 ) {
49 global $DIC;
50
51 $this->templateRepository = $templateRepository;
52 $this->placeholderValuesObject = $placeholderValuesObject;
53
54 if (null === $logger) {
55 $logger = $DIC->logger()->cert();
56 }
57 $this->logger = $logger;
58
59 if (null === $user) {
60 $user = $DIC->user();
61 }
62 $this->user = $user;
63
64 if (null === $utilHelper) {
66 }
67 $this->utilHelper = $utilHelper;
68
69 if (null === $mathJaxHelper) {
71 }
72 $this->mathJaxHelper = $mathJaxHelper;
73
74 if (null === $userDefinedFieldsHelper) {
76 }
77 $this->userDefinedFieldsHelper = $userDefinedFieldsHelper;
78
79 if (null === $rpcClientFactoryHelper) {
81 }
82 $this->rpcClientFactoryHelper = $rpcClientFactoryHelper;
83
84 if (null === $pdfFileNameFactory) {
86 }
87 $this->pdfFileNameFactory = $pdfFileNameFactory;
88
89 $this->rootDirectory = $rootDirectory;
90 }
91
96 public function createPreviewPdf(int $objectId): void
97 {
98 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($objectId);
99
100 $xslfo = $template->getCertificateContent();
101
102 $xslfo = $this->exchangeCertificateVariables($xslfo, $template, $objectId);
103
104 // render tex as fo graphics
105 $xlsfo = $this->mathJaxHelper->fillXlsFoContent($xslfo);
106
107 $pdf_base64 = $this->rpcClientFactoryHelper
108 ->ilFO2PDF('RPCTransformationHandler', $xlsfo);
109
110 $pdfPresentation = new ilUserCertificatePresentation(
111 $template->getObjId(),
112 $template->getObjType(),
113 null,
114 '',
115 ''
116 );
117
118 $this->utilHelper->deliverData(
119 $pdf_base64->scalar,
120 $this->pdfFileNameFactory->create($pdfPresentation),
121 'application/pdf'
122 );
123 }
124
133 string $certificate_text,
134 ilCertificateTemplate $template,
135 int $objectId
136 ): string {
137 $insert_tags = $this->placeholderValuesObject->getPlaceholderValuesForPreview($this->user->getId(), $objectId);
138
139 foreach ($this->getCustomCertificateFields() as $key => $value) {
140 $insert_tags[$value['ph']] = $this->utilHelper->prepareFormOutput($value['name']);
141 }
142
143 foreach ($insert_tags as $placeholderVariable => $value) {
144 $certificate_text = str_replace('[' . $placeholderVariable . ']', $value, $certificate_text);
145 }
146
147 $certificate_text = str_replace(
148 '[CLIENT_WEB_DIR]',
149 'file://' . $this->rootDirectory,
150 $certificate_text
151 );
152
153 $backgroundImagePath = $template->getBackgroundImagePath();
154
155 return str_replace(
156 '[BACKGROUND_IMAGE]',
157 'file://' . $this->rootDirectory . $backgroundImagePath,
158 $certificate_text
159 );
160 }
161
162 private function getCustomCertificateFields(): array
163 {
164 $user_field_definitions = $this->userDefinedFieldsHelper->createInstance();
165 $fds = $user_field_definitions->getDefinitions();
166
167 $fields = [];
168 foreach ($fds as $f) {
169 if ($f['certificate']) {
170 $fields[$f['field_id']] = [
171 'name' => $f['field_name'],
172 'ph' => '[#' . str_replace(' ', '_', strtoupper($f['field_name'])) . ']'
173 ];
174 }
175 }
176
177 return $fields;
178 }
179}
ilCertificatePlaceholderValues $placeholderValuesObject
exchangeCertificateVariables(string $certificate_text, ilCertificateTemplate $template, int $objectId)
Exchanges the variables in the certificate text with given values.
ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper
ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper
__construct(ilCertificateTemplateRepository $templateRepository, ilCertificatePlaceholderValues $placeholderValuesObject, ?ilLogger $logger=null, ?ilObjUser $user=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateMathJaxHelper $mathJaxHelper=null, ?ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper=null, ?ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper=null, string $rootDirectory=CLIENT_WEB_DIR, ?ilCertificatePdfFileNameFactory $pdfFileNameFactory=null)
Just a wrapper class to create Unit Test for other classes.
Component logger with individual log levels by component id.
User class.
const CLIENT_WEB_DIR
Definition: constants.php:47
global $DIC
Definition: feed.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193