ILIAS  release_7 Revision v7.30-3-g800a261c036
ilCertificateTemplatePreviewAction.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
8{
13
18
22 private $logger;
23
27 private $user;
28
32 private $utilHelper;
33
38
43
48
53
58
71 public function __construct(
74 ilLogger $logger = null,
75 ilObjUser $user = null,
82 ) {
83 global $DIC;
84
85 $this->templateRepository = $templateRepository;
86 $this->placeholderValuesObject = $placeholderValuesObject;
87
88 if (null === $logger) {
89 $logger = $DIC->logger()->cert();
90 }
91 $this->logger = $logger;
92
93 if (null === $user) {
94 $user = $DIC->user();
95 }
96 $this->user = $user;
97
98 if (null === $utilHelper) {
100 }
101 $this->utilHelper = $utilHelper;
102
103 if (null === $mathJaxHelper) {
105 }
106 $this->mathJaxHelper = $mathJaxHelper;
107
108 if (null === $userDefinedFieldsHelper) {
110 }
111 $this->userDefinedFieldsHelper = $userDefinedFieldsHelper;
112
113 if (null === $rpcClientFactoryHelper) {
115 }
116 $this->rpcClientFactoryHelper = $rpcClientFactoryHelper;
117
118 if (null === $pdfFileNameFactory) {
120 }
121 $this->pdfFileNameFactory = $pdfFileNameFactory;
122
123 $this->rootDirectory = $rootDirectory;
124 }
125
132 public function createPreviewPdf(int $objectId)
133 {
134 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($objectId);
135
136 $xslfo = $template->getCertificateContent();
137
138 $xslfo = $this->exchangeCertificateVariables($xslfo, $template, $objectId);
139
140 try {
141 // render tex as fo graphics
142 $xlsfo = $this->mathJaxHelper->fillXlsFoContent($xslfo);
143
144 $pdf_base64 = $this->rpcClientFactoryHelper
145 ->ilFO2PDF('RPCTransformationHandler', $xlsfo);
146
147 $pdfPresentation = new ilUserCertificatePresentation(
148 $template->getObjId(),
149 $template->getObjType(),
150 null,
151 '',
152 ''
153 );
154
155 $this->utilHelper->deliverData(
156 $pdf_base64->scalar,
157 $this->pdfFileNameFactory->create($pdfPresentation),
158 'application/pdf'
159 );
160 } catch (Exception $e) {
161 throw $e;
162 }
163 }
164
174 string $certificate_text,
175 ilCertificateTemplate $template,
176 int $objectId
177 ) {
178 $insert_tags = $this->placeholderValuesObject->getPlaceholderValuesForPreview($this->user->getId(), $objectId);
179
180 foreach ($this->getCustomCertificateFields() as $key => $value) {
181 $insert_tags[$value['ph']] = $this->utilHelper->prepareFormOutput($value['name']);
182 }
183
184 foreach ($insert_tags as $placeholderVariable => $value) {
185 $certificate_text = str_replace('[' . $placeholderVariable . ']', $value, $certificate_text);
186 }
187
188 $certificate_text = str_replace(
189 '[CLIENT_WEB_DIR]',
190 'file://' . $this->rootDirectory,
191 $certificate_text
192 );
193
194 $backgroundImagePath = $template->getBackgroundImagePath();
195
196 $certificate_text = str_replace(
197 '[BACKGROUND_IMAGE]',
198 'file://' . $this->rootDirectory . $backgroundImagePath,
199 $certificate_text
200 );
201
202 return $certificate_text;
203 }
204
210 private function getCustomCertificateFields()
211 {
212 $user_field_definitions = $this->userDefinedFieldsHelper->createInstance();
213 $fds = $user_field_definitions->getDefinitions();
214
215 $fields = array();
216 foreach ($fds as $f) {
217 if ($f['certificate']) {
218 $fields[$f['field_id']] = array(
219 'name' => $f['field_name'],
220 'ph' => '[#' . str_replace(' ', '_', strtoupper($f['field_name'])) . ']');
221 }
222 }
223
224 return $fields;
225 }
226}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
exchangeCertificateVariables(string $certificate_text, ilCertificateTemplate $template, int $objectId)
Exchanges the variables in the certificate text with given values.
getCustomCertificateFields()
Get custom certificate fields.
__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.
const CLIENT_WEB_DIR
Definition: constants.php:45
global $DIC
Definition: goto.php:24