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