ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMailTemplateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 
33 {
37  protected ilLanguage $lng;
43  protected Refinery $refinery;
44  protected Factory $uiFactory;
45  protected Renderer $uiRenderer;
46 
47  public function __construct(
48  protected ilObject $parentObject,
49  ilGlobalTemplateInterface $tpl = null,
50  ilCtrlInterface $ctrl = null,
51  ilLanguage $lng = null,
52  ilToolbarGUI $toolbar = null,
53  ilRbacSystem $rbacsystem = null,
54  ilErrorHandling $error = null,
55  GlobalHttpState $http = null,
56  Factory $uiFactory = null,
57  Renderer $uiRenderer = null,
58  ilMailTemplateService $templateService = null
59  ) {
60  global $DIC;
61  $this->tpl = $tpl ?? $DIC->ui()->mainTemplate();
62  $this->ctrl = $ctrl ?? $DIC->ctrl();
63  $this->lng = $lng ?? $DIC->language();
64  $this->toolbar = $toolbar ?? $DIC->toolbar();
65  $this->rbacsystem = $rbacsystem ?? $DIC->rbac()->system();
66  $this->error = $error ?? $DIC['ilErr'];
67  $this->http = $http ?? $DIC->http();
68  $this->refinery = $DIC->refinery();
69  $this->uiFactory = $uiFactory ?? $DIC->ui()->factory();
70  $this->uiRenderer = $uiRenderer ?? $DIC->ui()->renderer();
71  $this->service = $templateService ?? $DIC->mail()->textTemplates();
72 
73  $this->lng->loadLanguageModule('meta');
74  }
75 
76  private function isEditingAllowed(): bool
77  {
78  return $this->rbacsystem->checkAccess('write', $this->parentObject->getRefId());
79  }
80 
81  public function executeCommand(): void
82  {
83  $cmd = $this->ctrl->getCmd();
84  if (!$cmd || !method_exists($this, $cmd)) {
85  $cmd = 'showTemplates';
86  }
87  $this->$cmd();
88  }
89 
90  protected function showTemplates(): void
91  {
93  if (count($contexts) <= 1) {
94  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'));
95  } elseif ($this->isEditingAllowed()) {
96  $this->toolbar->addComponent($this->uiFactory->button()->standard(
97  $this->lng->txt('mail_new_template'),
98  $this->ctrl->getLinkTarget($this, 'showInsertTemplateForm')
99  ));
100  }
101 
102  $tbl = new ilMailTemplateTableGUI(
103  $this,
104  'showTemplates',
105  $this->uiFactory,
106  $this->uiRenderer,
107  !$this->isEditingAllowed()
108  );
109  $tbl->setData($this->service->listAllTemplatesAsArray());
110 
111  $this->tpl->setContent($tbl->getHTML());
112  }
113 
117  protected function insertTemplate(): void
118  {
119  if (!$this->isEditingAllowed()) {
120  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
121  }
122 
123  $form = $this->getTemplateForm();
124 
125  if (!$form->checkInput()) {
126  $form->setValuesByPost();
127  $this->showInsertTemplateForm($form);
128  return;
129  }
130 
131  $generic_context = new ilMailTemplateGenericContext();
132  if ($form->getInput('context') === $generic_context->getId()) {
133  $form->getItemByPostVar('context')->setAlert(
134  $this->lng->txt('mail_template_no_valid_context')
135  );
136  $form->setValuesByPost();
137  $this->showInsertTemplateForm($form);
138  return;
139  }
140 
141  try {
142  $this->service->createNewTemplate(
144  $form->getInput('title'),
145  $form->getInput('m_subject'),
146  $form->getInput('m_message'),
147  $form->getInput('lang')
148  );
149 
150  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
151  $this->ctrl->redirect($this, 'showTemplates');
152  } catch (\ILIAS\Mail\Templates\TemplateSubjectSyntaxException) {
153  $form->getItemByPostVar('m_subject')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
154  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
155  } catch (\ILIAS\Mail\Templates\TemplateMessageSyntaxException) {
156  $form->getItemByPostVar('m_message')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
157  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
158  } catch (Exception) {
159  $form->getItemByPostVar('context')->setAlert(
160  $this->lng->txt('mail_template_no_valid_context')
161  );
162  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
163  }
164 
165  $form->setValuesByPost();
166  $this->showInsertTemplateForm($form);
167  }
168 
173  protected function showInsertTemplateForm(ilPropertyFormGUI $form = null): void
174  {
175  if (!($form instanceof ilPropertyFormGUI)) {
176  $form = $this->getTemplateForm();
177  }
178 
179  $this->tpl->setContent($form->getHTML());
180  }
181 
182  protected function updateTemplate(): void
183  {
184  if (!$this->isEditingAllowed()) {
185  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
186  }
187 
188  $templateId = 0;
189  if ($this->http->wrapper()->post()->has('tpl_id')) {
190  $templateId = $this->http->wrapper()->post()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
191  }
192 
193  if (!is_numeric($templateId) || $templateId < 1) {
194  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
195  $this->showTemplates();
196  return;
197  }
198 
199  try {
200  $form = $this->getTemplateForm();
201  if (!$form->checkInput()) {
202  $form->setValuesByPost();
203  $this->showEditTemplateForm($form);
204  return;
205  }
206 
207  $genericContext = new ilMailTemplateGenericContext();
208  if ($form->getInput('context') === $genericContext->getId()) {
209  $form->getItemByPostVar('context')->setAlert(
210  $this->lng->txt('mail_template_no_valid_context')
211  );
212  $form->setValuesByPost();
213  $this->showEditTemplateForm($form);
214  return;
215  }
216 
217  try {
218  $this->service->modifyExistingTemplate(
219  (int) $templateId,
221  $form->getInput('title'),
222  $form->getInput('m_subject'),
223  $form->getInput('m_message'),
224  $form->getInput('lang')
225  );
226 
227  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
228  $this->ctrl->redirect($this, 'showTemplates');
229  } catch (OutOfBoundsException) {
230  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
231  } catch (\ILIAS\Mail\Templates\TemplateSubjectSyntaxException) {
232  $form->getItemByPostVar('m_subject')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
233  } catch (\ILIAS\Mail\Templates\TemplateMessageSyntaxException) {
234  $form->getItemByPostVar('m_message')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
235  } catch (Exception) {
236  $form->getItemByPostVar('context')->setAlert(
237  $this->lng->txt('mail_template_no_valid_context')
238  );
239  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
240  }
241 
242  $form->setValuesByPost();
243  $this->showEditTemplateForm($form);
244  } catch (Exception) {
245  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
246  $this->showTemplates();
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) {
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 
281  protected function populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template): void
282  {
283  $form->setValuesByArray([
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_placeholders',
392  'm_message'
393  );
394  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
395  try {
396  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
397  } catch (Throwable) {
398  $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
399  }
400 
402  foreach ($context->getPlaceholders() as $value) {
403  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
404  }
405 
406  $placeholders->render(true);
407  }
408 
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 (array_keys($context_sort) as $id) {
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);
454  $form->addItem($context);
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_placeholders',
475  'm_message'
476  );
477  $placeholders->setDisabled(!$this->isEditingAllowed());
478  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
479  try {
480  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
481  } catch (Throwable) {
482  $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
483  }
484  $placeholders->supportsRerenderSignal(
485  'context',
486  $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true)
487  );
488  if ($template === null) {
489  $context_id = $generic_context->getId();
490  } else {
491  $context_id = $template->getContext();
492  }
494  foreach ($context->getPlaceholders() as $value) {
495  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
496  }
497  $form->addItem($placeholders);
498  if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
499  $id = new ilHiddenInputGUI('tpl_id');
500  $form->addItem($id);
501 
502  $form->setTitle($this->lng->txt('mail_edit_tpl'));
503  $form->setFormAction($this->ctrl->getFormAction($this, 'updateTemplate'));
504 
505  if ($this->isEditingAllowed()) {
506  $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
507  }
508  } else {
509  $form->setTitle($this->lng->txt('mail_create_tpl'));
510  $form->setFormAction($this->ctrl->getFormAction($this, 'insertTemplate'));
511 
512  if ($this->isEditingAllowed()) {
513  $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
514  }
515  }
516 
517  if ($this->isEditingAllowed()) {
518  $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
519  } else {
520  $form->addCommandButton('showTemplates', $this->lng->txt('back'));
521  }
522 
523  return $form;
524  }
525 
526  public function unsetAsContextDefault(): void
527  {
528  if (!$this->isEditingAllowed()) {
529  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
530  }
531 
532  $templateId = 0;
533  if ($this->http->wrapper()->query()->has('tpl_id')) {
534  $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
535  }
536 
537  if (!is_numeric($templateId) || $templateId < 1) {
538  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
539  $this->showTemplates();
540  return;
541  }
542 
543  try {
544  $template = $this->service->loadTemplateForId((int) $templateId);
545  $this->service->unsetAsContextDefault($template);
546  } catch (Exception) {
547  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
548  $this->showTemplates();
549  return;
550  }
551 
552  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
553  $this->ctrl->redirect($this, 'showTemplates');
554  }
555 
556  public function setAsContextDefault(): void
557  {
558  if (!$this->isEditingAllowed()) {
559  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
560  }
561 
562  $templateId = 0;
563  if ($this->http->wrapper()->query()->has('tpl_id')) {
564  $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
565  }
566 
567  if (!is_numeric($templateId) || $templateId < 1) {
568  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
569  $this->showTemplates();
570  return;
571  }
572 
573  try {
574  $template = $this->service->loadTemplateForId((int) $templateId);
575  $this->service->setAsContextDefault($template);
576  } catch (Exception) {
577  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
578  $this->showTemplates();
579  return;
580  }
581 
582  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
583  $this->ctrl->redirect($this, 'showTemplates');
584  }
585 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$context
Definition: webdav.php:31
showEditTemplateForm(ilPropertyFormGUI $form=null)
getItemByPostVar(string $a_post_var)
Class ilMailTemplateGUI.
Interface Observer Contains several chained tasks and infos about them.
setInfo(string $a_info)
__construct(protected 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)
getTemplateForm(ilMailTemplate $template=null)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
Class ilMailTemplate.
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-...
Class ilMailTemplateTableGUI.
Class ilManualPlaceholderInputGUI.
ilGlobalTemplateInterface $tpl
static getTemplateContexts(?array $a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id. ...
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.
This class represents a property in a property form.
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
setFormAction(string $a_formaction)
This is how the factory for UI elements looks.
Definition: Factory.php:37
global $DIC
Definition: shib_login.php:25
RFC 822 Email address list validation Utility.
showInsertTemplateForm(ilPropertyFormGUI $form=null)
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
This class represents a text area property in a property form.
setDisabled(bool $a_disabled)
ilMailTemplateService $service