ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilMailTemplateGUI.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
4
8
16{
18 protected $form;
19
21 protected $tpl;
22
24 protected $ctrl;
25
27 protected $lng;
28
30 protected $toolbar;
31
33 protected $rbacsystem;
34
36 protected $parentObject;
37
39 protected $error;
40
42 protected $service;
43
45 protected $http;
46
48 protected $uiFactory;
49
51 protected $uiRenderer;
52
67 public function __construct(
70 ilCtrl $ctrl = null,
71 ilLanguage $lng = null,
75 HTTPServices $http = null,
76 Factory $uiFactory = null,
77 Renderer $uiRenderer = null,
78 ilMailTemplateService $templateService = null
79 ) {
80 global $DIC;
81
82 $this->parentObject = $parentObject;
83
84 if ($tpl === null) {
85 $tpl = $DIC->ui()->mainTemplate();
86 }
87 $this->tpl = $tpl;
88
89 if ($ctrl === null) {
90 $ctrl = $DIC->ctrl();
91 }
92 $this->ctrl = $ctrl;
93
94 if ($lng === null) {
95 $lng = $DIC->language();
96 }
97 $this->lng = $lng;
98
99 if ($toolbar === null) {
100 $toolbar = $DIC->toolbar();
101 }
102 $this->toolbar = $toolbar;
103
104 if ($rbacsystem === null) {
105 $rbacsystem = $DIC->rbac()->system();
106 }
107 $this->rbacsystem = $rbacsystem;
108
109 if ($error === null) {
110 $error = $DIC['ilErr'];
111 }
112 $this->error = $error;
113
114 if ($http === null) {
115 $http = $DIC->http();
116 }
117 $this->http = $http;
118
119 if ($uiFactory === null) {
120 $uiFactory = $DIC->ui()->factory();
121 }
122 $this->uiFactory = $uiFactory;
123
124 if ($uiRenderer === null) {
125 $uiRenderer = $DIC->ui()->renderer();
126 }
127 $this->uiRenderer = $uiRenderer;
128
129 if (null === $templateService) {
130 $templateService = $DIC['mail.texttemplates.service'];
131 }
132 $this->service = $templateService;
133
134 $this->lng->loadLanguageModule('meta');
135 }
136
140 private function isEditingAllowed() : bool
141 {
142 return $this->rbacsystem->checkAccess('write', $this->parentObject->getRefId());
143 }
144
148 public function executeCommand() : void
149 {
150 $next_class = $this->ctrl->getNextClass($this);
151 $cmd = $this->ctrl->getCmd();
152
153 switch ($next_class) {
154 default:
155 if (!$cmd || !method_exists($this, $cmd)) {
156 $cmd = 'showTemplates';
157 }
158
159 $this->$cmd();
160 break;
161 }
162 }
163
167 protected function showTemplates() : void
168 {
170 if (count($contexts) <= 1) {
171 ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'));
172 } elseif ($this->isEditingAllowed()) {
173 $create_tpl_button = ilLinkButton::getInstance();
174 $create_tpl_button->setCaption('mail_new_template');
175 $create_tpl_button->setUrl($this->ctrl->getLinkTarget($this, 'showInsertTemplateForm'));
176 $this->toolbar->addButtonInstance($create_tpl_button);
177 }
178
179 $tbl = new ilMailTemplateTableGUI(
180 $this,
181 'showTemplates',
182 $this->uiFactory,
183 $this->uiRenderer,
184 !$this->isEditingAllowed()
185 );
186 $tbl->setData($this->service->listAllTemplatesAsArray());
187
188 $this->tpl->setContent($tbl->getHTML());
189 }
190
194 protected function insertTemplate() : void
195 {
196 if (!$this->isEditingAllowed()) {
197 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
198 }
199
200 $form = $this->getTemplateForm();
201
202 if (!$form->checkInput()) {
203 $form->setValuesByPost();
205 return;
206 }
207
208 $generic_context = new ilMailTemplateGenericContext();
209 if ($form->getInput('context') === $generic_context->getId()) {
210 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
211 $form->setValuesByPost();
213 return;
214 }
215
216 try {
217 $this->service->createNewTemplate(
218 (string) ilMailTemplateContextService::getTemplateContextById($form->getInput('context'))->getId(),
219 (string) $form->getInput('title'),
220 (string) $form->getInput('m_subject'),
221 (string) $form->getInput('m_message'),
222 (string) $form->getInput('lang')
223 );
224
225 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
226 $this->ctrl->redirect($this, 'showTemplates');
227 } catch (Exception $e) {
228 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
229 ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
230 }
231
232 $form->setValuesByPost();
234 }
235
240 protected function showInsertTemplateForm(ilPropertyFormGUI $form = null) : void
241 {
242 if (!($form instanceof ilPropertyFormGUI)) {
243 $form = $this->getTemplateForm();
244 }
245
246 $this->tpl->setContent($form->getHTML());
247 }
248
252 protected function updateTemplate() : void
253 {
254 if (!$this->isEditingAllowed()) {
255 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
256 }
257
258 $templateId = $this->http->request()->getParsedBody()['tpl_id'] ?? 0;
259
260 if (!is_numeric($templateId) || $templateId < 1) {
261 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
262 $this->showTemplates();
263 return;
264 }
265
266 try {
267 $form = $this->getTemplateForm();
268 if (!$form->checkInput()) {
269 $form->setValuesByPost();
271 return;
272 }
273
274 $genericContext = new ilMailTemplateGenericContext();
275 if ($form->getInput('context') === $genericContext->getId()) {
276 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
277 $form->setValuesByPost();
279 return;
280 }
281
282 try {
283 $this->service->modifyExistingTemplate(
284 (int) $templateId,
285 (string) ilMailTemplateContextService::getTemplateContextById($form->getInput('context'))->getId(),
286 (string) $form->getInput('title'),
287 (string) $form->getInput('m_subject'),
288 (string) $form->getInput('m_message'),
289 (string) $form->getInput('lang')
290 );
291
292 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
293 $this->ctrl->redirect($this, 'showTemplates');
294 } catch (OutOfBoundsException $e) {
295 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
296 } catch (Exception $e) {
297 $form->getItemByPostVar('context')->setAlert($this->lng->txt('mail_template_no_valid_context'));
298 ilUtil::sendFailure($this->lng->txt('form_input_not_valid'));
299 }
300
301 $form->setValuesByPost();
303 } catch (Exception $e) {
304 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
305 $this->showTemplates();
306 return;
307 }
308 }
309
313 protected function showEditTemplateForm(ilPropertyFormGUI $form = null) : void
314 {
315 if (!($form instanceof ilPropertyFormGUI)) {
316 $templateId = $this->http->request()->getQueryParams()['tpl_id'] ?? 0;
317
318 if (!is_numeric($templateId) || $templateId < 1) {
319 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
320 $this->showTemplates();
321 return;
322 }
323
324 try {
325 $template = $this->service->loadTemplateForId((int) $templateId);
326 $form = $this->getTemplateForm($template);
327 $this->populateFormWithTemplate($form, $template);
328 } catch (Exception $e) {
329 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
330 $this->showTemplates();
331 return;
332 }
333 }
334
335 $this->tpl->setContent($form->getHTML());
336 }
337
343 {
344 $form->setValuesByArray(array(
345 'tpl_id' => $template->getTplId(),
346 'title' => $template->getTitle(),
347 'context' => $template->getContext(),
348 'lang' => $template->getLang(),
349 'm_subject' => $template->getSubject(),
350 'm_message' => $template->getMessage(),
351 ));
352 }
353
357 protected function confirmDeleteTemplate() : void
358 {
359 if (!$this->isEditingAllowed()) {
360 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
361 }
362
363 $templateIds = $this->http->request()->getParsedBody()['tpl_id'] ?? array();
364 if (is_array($templateIds) && count($templateIds) > 0) {
365 $templateIds = array_filter(array_map('intval', $templateIds));
366 } else {
367 $templateId = $this->http->request()->getQueryParams()['tpl_id'] ?? '';
368 if (is_numeric($templateId) && $templateId > 0) {
369 $templateIds = array_filter(array((int) $templateId));
370 } else {
371 $templateIds = array();
372 }
373 }
374
375 if (0 === count($templateIds)) {
376 ilUtil::sendFailure($this->lng->txt('select_one'));
377 $this->showTemplates();
378 return;
379 }
380
381 $confirm = new ilConfirmationGUI();
382 $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
383
384 $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entry'));
385 if (1 === count($templateIds)) {
386 $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entries'));
387 }
388
389 $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
390 $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
391
392 foreach ($templateIds as $templateId) {
393 $template = $this->service->loadTemplateForId((int) $templateId);
394 $confirm->addItem('tpl_id[]', $templateId, $template->getTitle());
395 }
396
397 $this->tpl->setContent($confirm->getHTML());
398 }
399
403 protected function deleteTemplate() : void
404 {
405 if (!$this->isEditingAllowed()) {
406 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
407 }
408
409 $templateIds = $this->http->request()->getParsedBody()['tpl_id'] ?? array();
410 if (is_array($templateIds) && count($templateIds) > 0) {
411 $templateIds = array_filter(array_map('intval', $templateIds));
412 } else {
413 $templateId = $this->http->request()->getQueryParams()['tpl_id'] ?? '';
414 if (is_numeric($templateId) && $templateId > 0) {
415 $templateIds = array_filter(array((int) $templateId));
416 } else {
417 $templateIds = array();
418 }
419 }
420
421 if (0 === count($templateIds)) {
422 ilUtil::sendFailure($this->lng->txt('select_one'));
423 $this->showTemplates();
424 return;
425 }
426
427 $this->service->deleteTemplatesByIds($templateIds);
428
429 if (1 === count($templateIds)) {
430 ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_s'), true);
431 } else {
432 ilUtil::sendSuccess($this->lng->txt('mail_tpl_deleted_p'), true);
433 }
434 $this->ctrl->redirect($this, 'showTemplates');
435 }
436
440 public function getAjaxPlaceholdersById() : void
441 {
442 $triggerValue = $this->http->request()->getQueryParams()['triggerValue'] ?? '';
443 $contextId = ilUtil::stripSlashes($triggerValue);
444
445 $placeholders = new ilManualPlaceholderInputGUI('m_message');
446 $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
447 $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
448
450 foreach ($context->getPlaceholders() as $key => $value) {
451 $placeholders->addPlaceholder($value['placeholder'], $value['label']);
452 }
453
454 $placeholders->render(true);
455 exit();
456 }
457
463 protected function getTemplateForm(ilMailTemplate $template = null) : ilPropertyFormGUI
464 {
465 $form = new ilPropertyFormGUI();
466
467 $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
468 $title->setRequired(true);
469 $title->setDisabled(!$this->isEditingAllowed());
470 $form->addItem($title);
471
472 $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
473 $context->setDisabled(!$this->isEditingAllowed());
475
476 if (count($contexts) <= 1) {
477 ilUtil::sendFailure($this->lng->txt('mail_template_no_context_available'), true);
478 $this->ctrl->redirect($this, 'showTemplates');
479 }
480
481 $context_sort = array();
482 $context_options = array();
483 $generic_context = new ilMailTemplateGenericContext();
484 foreach ($contexts as $ctx) {
485 if ($ctx->getId() != $generic_context->getId()) {
486 $context_options[$ctx->getId()] = $ctx;
487 $context_sort[$ctx->getId()] = $ctx->getTitle();
488 }
489 }
490 asort($context_sort);
491 $first = null;
492 foreach ($context_sort as $id => $title) {
493 $ctx = $context_options[$id];
494 $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
495 $option->setInfo($ctx->getDescription());
496 $context->addOption($option);
497
498 if (!$first) {
499 $first = $id;
500 }
501 }
502 $context->setValue($first);
503 $context->setRequired(true);
504 $form->addItem($context);
505
506 $hidden = new ilHiddenInputGUI('lang');
507 $hidden->setValue($this->lng->getLangKey());
508 $form->addItem($hidden);
509
510 $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
511 $subject->setDisabled(!$this->isEditingAllowed());
512 $subject->setSize(50);
513 $form->addItem($subject);
514
515 $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
516 $message->setDisabled(!$this->isEditingAllowed());
517 $message->setRequired(true);
518 $message->setCols(60);
519 $message->setRows(10);
520 $form->addItem($message);
521
522 $placeholders = new ilManualPlaceholderInputGUI('m_message');
523 $placeholders->setDisabled(!$this->isEditingAllowed());
524 $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
525 $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
526 $placeholders->supportsRerenderSignal(
527 'context',
528 $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true, false)
529 );
530 if ($template === null) {
531 $context_id = $generic_context->getId();
532 } else {
533 $context_id = $template->getContext();
534 }
536 foreach ($context->getPlaceholders() as $key => $value) {
537 $placeholders->addPlaceholder($value['placeholder'], $value['label']);
538 }
539 $form->addItem($placeholders);
540 if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
541 $id = new ilHiddenInputGUI('tpl_id');
542 $form->addItem($id);
543
544 $form->setTitle($this->lng->txt('mail_edit_tpl'));
545 $form->setFormAction($this->ctrl->getFormaction($this, 'updateTemplate'));
546
547 if ($this->isEditingAllowed()) {
548 $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
549 }
550 } else {
551 $form->setTitle($this->lng->txt('mail_create_tpl'));
552 $form->setFormAction($this->ctrl->getFormaction($this, 'insertTemplate'));
553
554 if ($this->isEditingAllowed()) {
555 $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
556 }
557 }
558
559 if ($this->isEditingAllowed()) {
560 $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
561 } else {
562 $form->addCommandButton('showTemplates', $this->lng->txt('back'));
563 }
564
565 return $form;
566 }
567
571 public function unsetAsContextDefault() : void
572 {
573 if (!$this->isEditingAllowed()) {
574 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
575 }
576
577 $templateId = $this->http->request()->getQueryParams()['tpl_id'] ?? 0;
578
579 if (!is_numeric($templateId) || $templateId < 1) {
580 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
581 $this->showTemplates();
582 return;
583 }
584
585 try {
586 $template = $this->service->loadTemplateForId((int) $templateId);
587 $this->service->unsetAsContextDefault($template);
588 } catch (Exception $e) {
589 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
590 $this->showTemplates();
591 return;
592 }
593
594 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
595 $this->ctrl->redirect($this, 'showTemplates');
596 }
597
601 public function setAsContextDefault()
602 {
603 if (!$this->isEditingAllowed()) {
604 $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
605 }
606
607 $templateId = $this->http->request()->getQueryParams()['tpl_id'] ?? 0;
608
609 if (!is_numeric($templateId) || $templateId < 1) {
610 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
611 $this->showTemplates();
612 return;
613 }
614
615 try {
616 $template = $this->service->loadTemplateForId((int) $templateId);
617 $this->service->setAsContextDefault($template);
618 } catch (Exception $e) {
619 ilUtil::sendFailure($this->lng->txt('mail_template_missing_id'));
620 $this->showTemplates();
621 return;
622 }
623
624 ilUtil::sendSuccess($this->lng->txt('saved_successfully'), true);
625 $this->ctrl->redirect($this, 'showTemplates');
626 }
627}
An exception for terminatinating execution or to throw for unit testing.
Provides an interface to the ILIAS HTTP services.
error($a_errmsg)
set error message @access public
Confirmation screen class.
This class provides processing control methods.
Class ilGlobalPageTemplate.
This class represents a hidden form property in a property form.
language handling
static getInstance()
Factory.
static getTemplateContexts($a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
Class ilMailTemplateGUI.
__construct(ilObject $parentObject, ilGlobalPageTemplate $tpl=null, ilCtrl $ctrl=null, ilLanguage $lng=null, ilToolbarGUI $toolbar=null, ilRbacSystem $rbacsystem=null, ilErrorHandling $error=null, HTTPServices $http=null, Factory $uiFactory=null, Renderer $uiRenderer=null, ilMailTemplateService $templateService=null)
ilMailTemplateGUI constructor.
getTemplateForm(ilMailTemplate $template=null)
showInsertTemplateForm(ilPropertyFormGUI $form=null)
showEditTemplateForm(ilPropertyFormGUI $form=null)
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
Class ilMailTemplateService.
Class ilMailTemplateTableGUI.
Class ilMailTemplate.
Class ilManualPlaceholderInputGUI.
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.
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.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
This is how the factory for UI elements looks.
Definition: Factory.php:18
An entity that renders components to a string output.
Definition: Renderer.php:15
exit
Definition: login.php:29
static http()
Fetches the global http state from ILIAS.
$context
Definition: webdav.php:26
$message
Definition: xapiexit.php:14
$DIC
Definition: xapitoken.php:46