ILIAS  release_8 Revision v8.24
class.ilMailTemplateGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
24use ILIAS\Refinery\Factory as Refinery;
25
33{
37 protected ilLanguage $lng;
44 protected Refinery $refinery;
47
48 public function __construct(
52 ilLanguage $lng = null,
57 Factory $uiFactory = null,
58 Renderer $uiRenderer = null,
59 ilMailTemplateService $templateService = null
60 ) {
61 global $DIC;
62 $this->parentObject = $parentObject;
63 $this->tpl = $tpl ?? $DIC->ui()->mainTemplate();
64 $this->ctrl = $ctrl ?? $DIC->ctrl();
65 $this->lng = $lng ?? $DIC->language();
66 $this->toolbar = $toolbar ?? $DIC->toolbar();
67 $this->rbacsystem = $rbacsystem ?? $DIC->rbac()->system();
68 $this->error = $error ?? $DIC['ilErr'];
69 $this->http = $http ?? $DIC->http();
70 $this->refinery = $DIC->refinery();
71 $this->uiFactory = $uiFactory ?? $DIC->ui()->factory();
72 $this->uiRenderer = $uiRenderer ?? $DIC->ui()->renderer();
73 $this->service = $templateService ?? $DIC['mail.texttemplates.service'];
74
75 $this->lng->loadLanguageModule('meta');
76 }
77
78 private function isEditingAllowed(): bool
79 {
80 return $this->rbacsystem->checkAccess('write', $this->parentObject->getRefId());
81 }
82
83 public function executeCommand(): void
84 {
85 $next_class = $this->ctrl->getNextClass($this);
86 $cmd = $this->ctrl->getCmd();
87
88 switch ($next_class) {
89 default:
90 if (!$cmd || !method_exists($this, $cmd)) {
91 $cmd = 'showTemplates';
92 }
93
94 $this->$cmd();
95 break;
96 }
97 }
98
99 protected function showTemplates(): void
100 {
102 if (count($contexts) <= 1) {
103 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'));
104 } elseif ($this->isEditingAllowed()) {
105 $create_tpl_button = ilLinkButton::getInstance();
106 $create_tpl_button->setCaption('mail_new_template');
107 $create_tpl_button->setUrl($this->ctrl->getLinkTarget($this, 'showInsertTemplateForm'));
108 $this->toolbar->addButtonInstance($create_tpl_button);
109 }
110
111 $tbl = new ilMailTemplateTableGUI(
112 $this,
113 'showTemplates',
114 $this->uiFactory,
115 $this->uiRenderer,
116 !$this->isEditingAllowed()
117 );
118 $tbl->setData($this->service->listAllTemplatesAsArray());
119
120 $this->tpl->setContent($tbl->getHTML());
121 }
122
126 protected function insertTemplate(): void
127 {
128 if (!$this->isEditingAllowed()) {
129 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
130 }
131
132 $form = $this->getTemplateForm();
133
134 if (!$form->checkInput()) {
136 $this->showInsertTemplateForm($form);
137 return;
138 }
139
140 $generic_context = new ilMailTemplateGenericContext();
141 if ($form->getInput('context') === $generic_context->getId()) {
142 $form->getItemByPostVar('context')->setAlert(
143 $this->lng->txt('mail_template_no_valid_context')
144 );
145 $form->setValuesByPost();
146 $this->showInsertTemplateForm($form);
147 return;
148 }
149
150 try {
151 $this->service->createNewTemplate(
153 $form->getInput('title'),
154 $form->getInput('m_subject'),
155 $form->getInput('m_message'),
156 $form->getInput('lang')
157 );
158
159 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
160 $this->ctrl->redirect($this, 'showTemplates');
161 } catch (Exception $e) {
162 $form->getItemByPostVar('context')->setAlert(
163 $this->lng->txt('mail_template_no_valid_context')
164 );
165 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
166 }
167
168 $form->setValuesByPost();
169 $this->showInsertTemplateForm($form);
170 }
171
176 protected function showInsertTemplateForm(ilPropertyFormGUI $form = null): void
177 {
178 if (!($form instanceof ilPropertyFormGUI)) {
179 $form = $this->getTemplateForm();
180 }
181
182 $this->tpl->setContent($form->getHTML());
183 }
184
185 protected function updateTemplate(): void
186 {
187 if (!$this->isEditingAllowed()) {
188 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
189 }
190
191 $templateId = 0;
192 if ($this->http->wrapper()->post()->has('tpl_id')) {
193 $templateId = $this->http->wrapper()->post()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
194 }
195
196 if (!is_numeric($templateId) || $templateId < 1) {
197 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
198 $this->showTemplates();
199 return;
200 }
201
202 try {
203 $form = $this->getTemplateForm();
204 if (!$form->checkInput()) {
206 $this->showEditTemplateForm($form);
207 return;
208 }
209
210 $genericContext = new ilMailTemplateGenericContext();
211 if ($form->getInput('context') === $genericContext->getId()) {
212 $form->getItemByPostVar('context')->setAlert(
213 $this->lng->txt('mail_template_no_valid_context')
214 );
215 $form->setValuesByPost();
216 $this->showEditTemplateForm($form);
217 return;
218 }
219
220 try {
221 $this->service->modifyExistingTemplate(
222 (int) $templateId,
224 $form->getInput('title'),
225 $form->getInput('m_subject'),
226 $form->getInput('m_message'),
227 $form->getInput('lang')
228 );
229
230 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
231 $this->ctrl->redirect($this, 'showTemplates');
232 } catch (OutOfBoundsException $e) {
233 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
234 } catch (Exception $e) {
235 $form->getItemByPostVar('context')->setAlert(
236 $this->lng->txt('mail_template_no_valid_context')
237 );
238 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
239 }
240
241 $form->setValuesByPost();
242 $this->showEditTemplateForm($form);
243 } catch (Exception $e) {
244 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
245 $this->showTemplates();
246 return;
247 }
248 }
249
250 protected function showEditTemplateForm(ilPropertyFormGUI $form = null): void
251 {
252 if (!($form instanceof ilPropertyFormGUI)) {
253 $templateId = 0;
254 if ($this->http->wrapper()->query()->has('tpl_id')) {
255 $templateId = $this->http->wrapper()->query()->retrieve(
256 'tpl_id',
257 $this->refinery->kindlyTo()->int()
258 );
259 }
260
261 if (!is_numeric($templateId) || $templateId < 1) {
262 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
263 $this->showTemplates();
264 return;
265 }
266
267 try {
268 $template = $this->service->loadTemplateForId((int) $templateId);
269 $form = $this->getTemplateForm($template);
270 $this->populateFormWithTemplate($form, $template);
271 } catch (Exception $e) {
272 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
273 $this->showTemplates();
274 return;
275 }
276 }
277
278 $this->tpl->setContent($form->getHTML());
279 }
280
282 {
284 'tpl_id' => $template->getTplId(),
285 'title' => $template->getTitle(),
286 'context' => $template->getContext(),
287 'lang' => $template->getLang(),
288 'm_subject' => $template->getSubject(),
289 'm_message' => $template->getMessage(),
290 ]);
291 }
292
293 protected function confirmDeleteTemplate(): void
294 {
295 if (!$this->isEditingAllowed()) {
296 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
297 }
298
299 $templateIds = [];
300 if ($this->http->wrapper()->post()->has('tpl_id')) {
301 $templateIds = $this->http->wrapper()->post()->retrieve(
302 'tpl_id',
303 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
304 );
305 }
306 if (count($templateIds) === 0 && $this->http->wrapper()->query()->has('tpl_id')) {
307 $templateIds = [$this->http->wrapper()->query()->retrieve(
308 'tpl_id',
309 $this->refinery->kindlyTo()->int()
310 )];
311 }
312
313 if (0 === count($templateIds)) {
314 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
315 $this->showTemplates();
316 return;
317 }
318
319 $confirm = new ilConfirmationGUI();
320 $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
321
322 $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entries'));
323 if (1 === count($templateIds)) {
324 $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entry'));
325 }
326
327 $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
328 $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
329
330 foreach ($templateIds as $templateId) {
331 $template = $this->service->loadTemplateForId($templateId);
332 $confirm->addItem('tpl_id[]', (string) $templateId, $template->getTitle());
333 }
334
335 $this->tpl->setContent($confirm->getHTML());
336 }
337
338 protected function deleteTemplate(): void
339 {
340 if (!$this->isEditingAllowed()) {
341 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
342 }
343
344 $templateIds = [];
345 if ($this->http->wrapper()->post()->has('tpl_id')) {
346 $templateIds = $this->http->wrapper()->post()->retrieve(
347 'tpl_id',
348 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
349 );
350 }
351 if (count($templateIds) === 0) {
352 $templateId = 0;
353 if ($this->http->wrapper()->query()->has('tpl_id')) {
354 $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
355 }
356 $templateIds = [$templateId];
357 }
358
359 if (0 === count($templateIds)) {
360 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
361 $this->showTemplates();
362 return;
363 }
364
365 $this->service->deleteTemplatesByIds($templateIds);
366
367 if (1 === count($templateIds)) {
368 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_s'), true);
369 } else {
370 $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_p'), true);
371 }
372 $this->ctrl->redirect($this, 'showTemplates');
373 }
374
378 public function getAjaxPlaceholdersById(): void
379 {
380 $triggerValue = '';
381 if ($this->http->wrapper()->query()->has('triggerValue')) {
382 $triggerValue = $this->http->wrapper()->query()->retrieve(
383 'triggerValue',
384 $this->refinery->kindlyTo()->string()
385 );
386 }
387 $contextId = ilUtil::stripSlashes($triggerValue);
388
389 $placeholders = new ilManualPlaceholderInputGUI(
390 $this->lng->txt('mail_form_placeholders_label'),
391 'm_message'
392 );
393 $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
394 try {
395 $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
396 } catch (Throwable $e) {
397 $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
398 }
399
401 foreach ($context->getPlaceholders() as $value) {
402 $placeholders->addPlaceholder($value['placeholder'], $value['label']);
403 }
404
405 $placeholders->render(true);
406 }
407
413 protected function getTemplateForm(ilMailTemplate $template = null): ilPropertyFormGUI
414 {
415 $form = new ilPropertyFormGUI();
416
417 $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
418 $title->setRequired(true);
419 $title->setDisabled(!$this->isEditingAllowed());
420 $form->addItem($title);
421
422 $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
423 $context->setDisabled(!$this->isEditingAllowed());
425
426 if (count($contexts) <= 1) {
427 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'), true);
428 $this->ctrl->redirect($this, 'showTemplates');
429 }
430
431 $context_sort = [];
432 $context_options = [];
433 $generic_context = new ilMailTemplateGenericContext();
434 foreach ($contexts as $ctx) {
435 if ($ctx->getId() !== $generic_context->getId()) {
436 $context_options[$ctx->getId()] = $ctx;
437 $context_sort[$ctx->getId()] = $ctx->getTitle();
438 }
439 }
440 asort($context_sort);
441 $first = null;
442 foreach ($context_sort as $id => $title) {
443 $ctx = $context_options[$id];
444 $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
445 $option->setInfo($ctx->getDescription());
446 $context->addOption($option);
447
448 if (!$first) {
449 $first = $id;
450 }
451 }
452 $context->setValue($first);
453 $context->setRequired(true);
455
456 $hidden = new ilHiddenInputGUI('lang');
457 $hidden->setValue($this->lng->getLangKey());
458 $form->addItem($hidden);
459
460 $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
461 $subject->setDisabled(!$this->isEditingAllowed());
462 $subject->setSize(50);
463 $form->addItem($subject);
464
465 $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
466 $message->setDisabled(!$this->isEditingAllowed());
467 $message->setRequired(true);
468 $message->setCols(60);
469 $message->setRows(10);
470 $form->addItem($message);
471
472 $placeholders = new ilManualPlaceholderInputGUI(
473 $this->lng->txt('mail_form_placeholders_label'),
474 'm_message'
475 );
476 $placeholders->setDisabled(!$this->isEditingAllowed());
477 $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
478 try {
479 $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
480 } catch (Throwable $e) {
481 $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
482 }
483 $placeholders->supportsRerenderSignal(
484 'context',
485 $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true)
486 );
487 if ($template === null) {
488 $context_id = $generic_context->getId();
489 } else {
490 $context_id = $template->getContext();
491 }
493 foreach ($context->getPlaceholders() as $key => $value) {
494 $placeholders->addPlaceholder($value['placeholder'], $value['label']);
495 }
496 $form->addItem($placeholders);
497 if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
498 $id = new ilHiddenInputGUI('tpl_id');
499 $form->addItem($id);
500
501 $form->setTitle($this->lng->txt('mail_edit_tpl'));
502 $form->setFormAction($this->ctrl->getFormAction($this, 'updateTemplate'));
503
504 if ($this->isEditingAllowed()) {
505 $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
506 }
507 } else {
508 $form->setTitle($this->lng->txt('mail_create_tpl'));
509 $form->setFormAction($this->ctrl->getFormAction($this, 'insertTemplate'));
510
511 if ($this->isEditingAllowed()) {
512 $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
513 }
514 }
515
516 if ($this->isEditingAllowed()) {
517 $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
518 } else {
519 $form->addCommandButton('showTemplates', $this->lng->txt('back'));
520 }
521
522 return $form;
523 }
524
525 public function unsetAsContextDefault(): void
526 {
527 if (!$this->isEditingAllowed()) {
528 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
529 }
530
531 $templateId = 0;
532 if ($this->http->wrapper()->query()->has('tpl_id')) {
533 $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
534 }
535
536 if (!is_numeric($templateId) || $templateId < 1) {
537 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
538 $this->showTemplates();
539 return;
540 }
541
542 try {
543 $template = $this->service->loadTemplateForId((int) $templateId);
544 $this->service->unsetAsContextDefault($template);
545 } catch (Exception $e) {
546 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
547 $this->showTemplates();
548 return;
549 }
550
551 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
552 $this->ctrl->redirect($this, 'showTemplates');
553 }
554
555 public function setAsContextDefault(): void
556 {
557 if (!$this->isEditingAllowed()) {
558 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
559 }
560
561 $templateId = 0;
562 if ($this->http->wrapper()->query()->has('tpl_id')) {
563 $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
564 }
565
566 if (!is_numeric($templateId) || $templateId < 1) {
567 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
568 $this->showTemplates();
569 return;
570 }
571
572 try {
573 $template = $this->service->loadTemplateForId((int) $templateId);
574 $this->service->setAsContextDefault($template);
575 } catch (Exception $e) {
576 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
577 $this->showTemplates();
578 return;
579 }
580
581 $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
582 $this->ctrl->redirect($this, 'showTemplates');
583 }
584}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
error(string $a_errmsg)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Error Handling & global info handling uses PEAR error class.
setFormAction(string $a_formaction)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
static getTemplateContexts(?array $a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
Class ilMailTemplateGUI.
ilGlobalTemplateInterface $tpl
getTemplateForm(ilMailTemplate $template=null)
__construct(ilObject $parentObject, ilGlobalTemplateInterface $tpl=null, ilCtrlInterface $ctrl=null, ilLanguage $lng=null, ilToolbarGUI $toolbar=null, ilRbacSystem $rbacsystem=null, ilErrorHandling $error=null, GlobalHttpState $http=null, Factory $uiFactory=null, Renderer $uiRenderer=null, ilMailTemplateService $templateService=null)
showInsertTemplateForm(ilPropertyFormGUI $form=null)
showEditTemplateForm(ilPropertyFormGUI $form=null)
ilMailTemplateService $service
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
Class ilMailTemplateService.
Class ilMailTemplateTableGUI.
Class ilMailTemplate.
Class ilManualPlaceholderInputGUI.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a property form user interface.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
This class represents a text area property in a property form.
This class represents a text property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
global $DIC
Definition: feed.php:28
Interface GlobalHttpState.
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
$context
Definition: webdav.php:29
$message
Definition: xapiexit.php:32