ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ilObjCertificateSettingsGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25
35{
36 public const TAB_CERTIFICATES = 'certificates';
37 public const CMD_CERTIFICATES_OVERVIEW = 'certificatesOverview';
38 public const CMD_DOWNLOAD_CERTIFICATE = 'downloadCertificate';
39 private IRSS $irss;
42
44 protected ?ilObject $object;
45 private readonly ilLogger $logger;
49
50 public function __construct(
51 $data,
52 int $id = 0,
53 bool $call_by_reference = true,
54 bool $prepare_output = true,
57 ) {
58 global $DIC;
59
61
62 $this->httpState = $DIC->http();
63 $this->upload = $DIC->upload();
64 $this->logger = $DIC->logger()->root();
65 $this->type = 'cert';
66 $this->lng->loadLanguageModule('certificate');
67 $this->lng->loadLanguageModule('cert');
68 $this->lng->loadLanguageModule('trac');
69
70 $this->irss = $DIC->resourceStorage();
71
72 $this->card_thumbnail_definition = new FitToSquare(
73 true,
74 100
75 );
76
77 $this->user_certificate_repo = $user_certificate_repo ?: new ilUserCertificateRepository();
78 $this->certificate_active_validator = $certificate_active_validator ?: new ilCertificateActiveValidator();
79 }
80
81 public function executeCommand(): void
82 {
83 $next_class = $this->ctrl->getNextClass($this) ?? '';
84 $cmd = $this->ctrl->getCmd();
85
86 $this->prepareOutput();
87
88 if (!$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
89 $this->error->raiseError($this->lng->txt('no_permission'), $this->error->WARNING);
90 }
91
92 switch (strtolower($next_class)) {
93 case strtolower(ilPermissionGUI::class):
94 $this->tabs_gui->setTabActive('perm_settings');
95 $perm_gui = new ilPermissionGUI($this);
96 $this->ctrl->forwardCommand($perm_gui);
97
98 break;
99 default:
100 if (!$cmd || $cmd === 'view') {
101 $cmd = 'settings';
102 }
103
104 $this->$cmd();
105
106 break;
107 }
108 }
109
110 public function getAdminTabs(): void
111 {
112 if ($this->certificate_active_validator->validate() && $this->rbac_system->checkAccess('read', $this->object->getRefId())) {
113 $this->tabs_gui->addTab(
114 self::TAB_CERTIFICATES,
115 $this->lng->txt('overview'),
116 $this->ctrl->getLinkTarget($this, self::CMD_CERTIFICATES_OVERVIEW)
117 );
118 }
119
120 if ($this->rbac_system->checkAccess('read', $this->object->getRefId())) {
121 $this->tabs_gui->addTarget(
122 'settings',
123 $this->ctrl->getLinkTarget($this, 'settings'),
124 ['settings', 'view']
125 );
126 }
127
128 if ($this->rbac_system->checkAccess('edit_permission', $this->object->getRefId())) {
129 $this->tabs_gui->addTarget(
130 'perm_settings',
131 $this->ctrl->getLinkTargetByClass(ilPermissionGUI::class, 'perm'),
132 [],
133 strtolower(ilPermissionGUI::class)
134 );
135 }
136 }
137
138 public function settings(): void
139 {
140 $this->tabs_gui->setTabActive('settings');
141 $form_settings = new ilSetting('certificate');
142
143 $form = new ilPropertyFormGUI();
144 $form->setFormAction($this->ctrl->getFormAction($this));
145 $form->setTitle($this->lng->txt('certificate_settings'));
146
147 $active = new ilCheckboxInputGUI($this->lng->txt('active'), 'active');
148 $active->setChecked((bool) $form_settings->get('active', '0'));
149 $form->addItem($active);
150
151 $info = new ilNonEditableValueGUI($this->lng->txt('info'), 'info');
152 $info->setValue($this->lng->txt('certificate_usage'));
153 $form->addItem($info);
154
155 $bgimage = new ilImageFileInputGUI($this->lng->txt('certificate_background_image'), 'background');
156 $bgimage->setRequired(false);
157
158 if (strcmp($this->ctrl->getCmd(), 'save') === 0) {
159 $backgroundDelete = $this->httpState->wrapper()->post()->has('background_delete') && $this->httpState->wrapper()->post()->retrieve(
160 'background_delete',
161 $this->refinery->kindlyTo()->bool()
162 );
163 if ($backgroundDelete) {
164 $this->object->deleteBackgroundImage();
165 }
166 }
167
168 if (
169 $this->upload->hasUploads() &&
170 $this->httpState->request()->getMethod() === 'POST' &&
171 $bgimage->checkInput()
172 ) {
173 if (!$this->upload->hasBeenProcessed()) {
174 $this->upload->process();
175 }
176
177 if (is_array($this->upload->getResults()) && $this->upload->getResults() !== []) {
178 $results = $this->upload->getResults();
179 $file = array_pop($results);
180 if ($file->isOK()) {
181 $result = $this->object->uploadBackgroundImage($file);
182 if ($result === false) {
183 $bgimage->setAlert($this->lng->txt('certificate_error_upload_bgimage'));
184 }
185 }
186 }
187 }
188
189 if ($this->object->hasBackgroundImage()) {
190 $rid = $this->object->getBackgroundImageIdentification();
191 if ($rid instanceof ResourceIdentification) {
192 $background_flavour = $this->irss->flavours()->get(
193 $rid,
194 $this->card_thumbnail_definition
195 );
196 $flavour_urls = $this->irss->consume()->flavourUrls($background_flavour);
197 foreach ($flavour_urls->getURLs(true) as $url) {
199 $bgimage->setImage($url);
200 }
201 }
202 }
203 $bgimage->setInfo($this->lng->txt('default_background_info'));
204 $form->addItem($bgimage);
205 $format = new ilSelectInputGUI($this->lng->txt('certificate_page_format'), 'pageformat');
206 $defaultformats = [
207 'a4' => $this->lng->txt('certificate_a4'), // (297 mm x 210 mm)
208 'a4landscape' => $this->lng->txt('certificate_a4_landscape'), // (210 mm x 297 mm)',
209 'a5' => $this->lng->txt('certificate_a5'), // (210 mm x 148.5 mm)
210 'a5landscape' => $this->lng->txt('certificate_a5_landscape'), // (148.5 mm x 210 mm)
211 'letter' => $this->lng->txt('certificate_letter'), // (11 inch x 8.5 inch)
212 'letterlandscape' => $this->lng->txt('certificate_letter_landscape') // (11 inch x 8.5 inch)
213 ];
214 $format->setOptions($defaultformats);
215 $format->setValue($form_settings->get('pageformat', ''));
216 $format->setInfo($this->lng->txt('certificate_page_format_info'));
217 $form->addItem($format);
218
219 if ($this->rbac_system->checkAccess('write', $this->object->getRefId())) {
220 $form->addCommandButton('save', $this->lng->txt('save'));
221 }
222
226 $form,
227 $this
228 );
229 }
230
231 $persistentCertificateMode = new ilRadioGroupInputGUI(
232 $this->lng->txt('persistent_certificate_mode'),
233 'persistent_certificate_mode'
234 );
235 $persistentCertificateMode->setRequired(true);
236
237 $cronJobMode = new ilRadioOption(
238 $this->lng->txt('persistent_certificate_mode_cron'),
239 'persistent_certificate_mode_cron'
240 );
241 $cronJobMode->setInfo($this->lng->txt('persistent_certificate_mode_cron_info'));
242
243 $instantMode = new ilRadioOption(
244 $this->lng->txt('persistent_certificate_mode_instant'),
245 'persistent_certificate_mode_instant'
246 );
247 $instantMode->setInfo($this->lng->txt('persistent_certificate_mode_instant_info'));
248
249 $persistentCertificateMode->addOption($cronJobMode);
250 $persistentCertificateMode->addOption($instantMode);
251
252 $persistentCertificateMode->setValue($form_settings->get(
253 'persistent_certificate_mode',
254 'persistent_certificate_mode_cron'
255 ));
256
257 $form->addItem($persistentCertificateMode);
258
259 $this->tpl->setContent($form->getHTML());
260 }
261
262 public function certificatesOverview(): void
263 {
264 if (
265 !$this->certificate_active_validator->validate() ||
266 !$this->rbac_system->checkAccess('read', $this->object->getRefId())
267 ) {
268 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->WARNING);
269 }
270
271 $this->tabs_gui->activateTab(self::TAB_CERTIFICATES);
272
273 $table = new CertificateOverviewTable();
274
275 $this->tpl->setContent($table->render());
276 }
277
278 public function downloadCertificate(): void
279 {
280 if (!$this->certificate_active_validator->validate() ||
281 !$this->rbac_system->checkAccess('read', $this->object->getRefId())) {
282 $this->error->raiseError($this->lng->txt('permission_denied'), $this->error->WARNING);
283 }
284
285 $certificate_id = $this->httpState->wrapper()->query()->retrieve(
286 'cert_overview_id',
287 $this->refinery->byTrying([
288 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int()),
289 $this->refinery->always([]),
290 ])
291 );
292
293 if ($certificate_id === []) {
294 $this->ctrl->redirect($this, self::CMD_CERTIFICATES_OVERVIEW);
295 }
296
297 // Only one download at a time is possible
298 $certificate_id = $certificate_id[array_key_first($certificate_id)];
299
300 try {
301 $certificate = $this->user_certificate_repo->fetchCertificate($certificate_id);
302 if (!$certificate->isCurrentlyActive()) {
303 throw new Exception('Certificate is not active');
304 }
305
306 $pdf_action = (new ilCertificatePdfAction(
307 (new ilPdfGenerator($this->user_certificate_repo))->withLogger($this->logger),
309 $this->lng->txt('error_creating_certificate_pdf')
310 ))->withLogger($this->logger);
311 $pdf_action->downloadPdf($certificate->getUserId(), $certificate->getObjId());
312 $this->ctrl->redirect($this, self::CMD_CERTIFICATES_OVERVIEW);
313 } catch (Exception $ex) {
314 $this->logger->error('Fetching user certificate for download failed. Ex.: ' . $ex->getMessage());
315 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('error_creating_certificate_pdf'), true);
316 $this->ctrl->redirect($this, self::CMD_CERTIFICATES_OVERVIEW);
317 }
318 }
319
320 public function save(): void
321 {
322 $form_settings = new ilSetting('certificate');
323
324 $mode = $this->httpState->wrapper()->post()->retrieve(
325 'persistent_certificate_mode',
326 $this->refinery->kindlyTo()->string()
327 );
328 $previousMode = $form_settings->get('persistent_certificate_mode', 'persistent_certificate_mode_cron');
329 if ($mode !== $previousMode && $mode === 'persistent_certificate_mode_instant') {
330 $cron = new ilCertificateCron();
331 $cron->init();
332 $cron->run();
333 }
334
335 $form_settings->set(
336 'pageformat',
337 $this->httpState->wrapper()->post()->retrieve('pageformat', $this->refinery->kindlyTo()->string())
338 );
339 $form_settings->set(
340 'active',
341 (string) ($this->httpState->wrapper()->post()->has('active') && $this->httpState->wrapper()->post()->retrieve(
342 'active',
343 $this->refinery->kindlyTo()->bool()
344 ))
345 );
346 $form_settings->set('persistent_certificate_mode', $mode);
347
348 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
349 $this->settings();
350 }
351}
error(string $a_errmsg)
static addFieldsToForm(int $a_form_id, ilPropertyFormGUI $a_form, ilObjectGUI $a_parent_gui)
Just a wrapper class to create Unit Test for other classes.
This class represents a checkbox property in a property form.
This class represents an image file property in a property form.
Component logger with individual log levels by component id.
This class represents a non editable value in a property form.
__construct( $data, int $id=0, bool $call_by_reference=true, bool $prepare_output=true, ?ilUserCertificateRepository $user_certificate_repo=null, ?ilCertificateActiveValidator $certificate_active_validator=null,)
readonly ilCertificateActiveValidator $certificate_active_validator
readonly ilUserCertificateRepository $user_certificate_repo
getAdminTabs()
administration tabs show only permissions and trash folder
Class ilObjectGUI Basic methods of all Output classes.
prepareOutput(bool $show_sub_objects=true)
Class ilObject Basic functions for all objects.
This class represents a property form user interface.
This class represents a property in a property form.
This class represents an option in a radio group.
This class represents a selection list property in a property form.
ILIAS Setting Class.
$cron
Definition: cron.php:26
$info
Definition: entry_point.php:21
Interface GlobalHttpState.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$results
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68