ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCertificateTemplatePreviewAction.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24
29{
30 private readonly ilObjUser $user;
31 private readonly Language $lng;
36
37 public function __construct(
38 private readonly ilCertificateTemplateRepository $templateRepository,
39 private readonly ilCertificatePlaceholderValues $placeholderValuesObject,
40 private readonly IRSS $irss,
41 private readonly string $rootDirectory = CLIENT_WEB_DIR,
42 ?ilObjUser $user = null,
43 ?Language $lng = null,
48 ) {
49 global $DIC;
50 if (null === $user) {
51 $user = $DIC->user();
52 }
53 $this->user = $user;
54
55 if (null === $lng) {
56 $lng = $DIC->language();
57 }
58 $this->lng = $lng;
59
60 if (null === $utilHelper) {
62 }
63 $this->utilHelper = $utilHelper;
64
65 if (null === $userDefinedFieldsHelper) {
67 }
68 $this->userDefinedFieldsHelper = $userDefinedFieldsHelper;
69
70 if (null === $rpcClientFactoryHelper) {
72 }
73 $this->rpcClientFactoryHelper = $rpcClientFactoryHelper;
74
75 if (null === $pdfFileNameFactory) {
77 }
78 $this->pdfFileNameFactory = $pdfFileNameFactory;
79 }
80
84 public function createPreviewPdf(int $objectId): void
85 {
86 $template = $this->templateRepository->fetchCurrentlyUsedCertificate($objectId);
87
88 $xslfo = $template->getCertificateContent();
89
90 $xslfo = $this->exchangeCertificateVariables($xslfo, $template, $objectId);
91
92 $pdf_base64 = $this->rpcClientFactoryHelper
93 ->ilFO2PDF('RPCTransformationHandler', $xslfo)
94 ;
95
96 $pdfPresentation = new ilUserCertificatePresentation(
97 $template->getObjId(),
98 $template->getObjType(),
99 null,
100 '',
101 ''
102 );
103
104 $this->utilHelper->deliverData(
105 $pdf_base64->scalar,
106 $this->pdfFileNameFactory->create($pdfPresentation),
107 'application/pdf'
108 );
109 }
110
117 string $certificate_text,
118 ilCertificateTemplate $template,
119 int $objectId
120 ): string {
121 $insert_tags = $this->placeholderValuesObject->getPlaceholderValuesForPreview($this->user->getId(), $objectId);
122
123 foreach ($this->getCustomCertificateFields() as $value) {
124 $insert_tags[$value['ph']] = $this->utilHelper->prepareFormOutput($value['name']);
125 }
126
127 foreach ($insert_tags as $placeholderVariable => $value) {
128 $certificate_text = str_replace('[' . $placeholderVariable . ']', $value, $certificate_text);
129 }
130
131 $certificate_text = str_replace(
132 '[CLIENT_WEB_DIR]',
133 'file://' . $this->rootDirectory,
134 $certificate_text
135 );
136
137 $identification = $this->irss->manage()->find($template->getBackgroundImageIdentification());
138 if ($identification instanceof ResourceIdentification) {
139 $backgroundImagePath = $this->irss->consume()->src($identification)->getSrc(true);
140 }
141
142 return str_replace(
143 '[BACKGROUND_IMAGE]',
144 $backgroundImagePath ?? '',
145 $certificate_text
146 );
147 }
148
152 private function getCustomCertificateFields(): array
153 {
154 $fds = $this->userDefinedFieldsHelper->getFields();
155
156 $fields = [];
157 foreach ($fds as $f) {
158 $fields[$f->getIdentifier()] = [
159 'name' => $f->getLabel($this->lng),
160 'ph' => '[#' . str_replace(' ', '_', strtoupper($f->getLabel($this->lng))) . ']'
161 ];
162 }
163
164 return $fields;
165 }
166}
readonly ilCertificatePdfFileNameFactory $pdfFileNameFactory
exchangeCertificateVariables(string $certificate_text, ilCertificateTemplate $template, int $objectId)
Exchanges the variables in the certificate text with given values.
readonly ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper
readonly ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper
__construct(private readonly ilCertificateTemplateRepository $templateRepository, private readonly ilCertificatePlaceholderValues $placeholderValuesObject, private readonly IRSS $irss, private readonly string $rootDirectory=CLIENT_WEB_DIR, ?ilObjUser $user=null, ?Language $lng=null, ?ilCertificateUtilHelper $utilHelper=null, ?ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper=null, ?ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper=null, ?ilCertificatePdfFileNameFactory $pdfFileNameFactory=null)
Just a wrapper class to create Unit Test for other classes.
User class.
const CLIENT_WEB_DIR
Definition: constants.php:47
global $DIC
Definition: shib_login.php:26