ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
ilCertificateTemplatePreviewAction.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 {
28  private ilLogger $logger;
29  private ilObjUser $user;
34  private string $rootDirectory;
36 
37  public function __construct(
38  ilCertificateTemplateRepository $templateRepository,
39  ilCertificatePlaceholderValues $placeholderValuesObject,
40  ?ilLogger $logger = null,
41  ?ilObjUser $user = null,
42  ?ilCertificateUtilHelper $utilHelper = null,
43  ?ilCertificateMathJaxHelper $mathJaxHelper = null,
44  ?ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper = null,
45  ?ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper = null,
46  string $rootDirectory = CLIENT_WEB_DIR,
47  ?ilCertificatePdfFileNameFactory $pdfFileNameFactory = 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) {
65  $utilHelper = new ilCertificateUtilHelper();
66  }
67  $this->utilHelper = $utilHelper;
68 
69  if (null === $mathJaxHelper) {
70  $mathJaxHelper = new ilCertificateMathJaxHelper();
71  }
72  $this->mathJaxHelper = $mathJaxHelper;
73 
74  if (null === $userDefinedFieldsHelper) {
75  $userDefinedFieldsHelper = new ilCertificateUserDefinedFieldsHelper();
76  }
77  $this->userDefinedFieldsHelper = $userDefinedFieldsHelper;
78 
79  if (null === $rpcClientFactoryHelper) {
80  $rpcClientFactoryHelper = new ilCertificateRpcClientFactoryHelper();
81  }
82  $this->rpcClientFactoryHelper = $rpcClientFactoryHelper;
83 
84  if (null === $pdfFileNameFactory) {
85  $pdfFileNameFactory = new ilCertificatePdfFileNameFactory($DIC->language());
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
__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)
exchangeCertificateVariables(string $certificate_text, ilCertificateTemplate $template, int $objectId)
Exchanges the variables in the certificate text with given values.
global $DIC
Definition: feed.php:28
ilCertificateRpcClientFactoryHelper $rpcClientFactoryHelper
string $key
Consumer key/client ID value.
Definition: System.php:193
const CLIENT_WEB_DIR
Definition: constants.php:47
Just a wrapper class to create Unit Test for other classes.
ilCertificateUserDefinedFieldsHelper $userDefinedFieldsHelper