ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailTemplateGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
31 {
35  protected ilLanguage $lng;
41  protected Refinery $refinery;
42  protected Factory $ui_factory;
44 
45  public function __construct(
46  protected ilObject $parent_object,
48  ?ilCtrlInterface $ctrl = null,
49  ?ilLanguage $lng = null,
50  ?ilToolbarGUI $toolbar = null,
51  ?ilRbacSystem $rbacsystem = null,
52  ?ilErrorHandling $error = null,
53  ?GlobalHttpState $http = null,
54  ?Factory $ui_factory = null,
55  ?Renderer $ui_renderer = null,
56  ?ilMailTemplateService $template_service = null
57  ) {
58  global $DIC;
59  $this->tpl = $tpl ?? $DIC->ui()->mainTemplate();
60  $this->ctrl = $ctrl ?? $DIC->ctrl();
61  $this->lng = $lng ?? $DIC->language();
62  $this->toolbar = $toolbar ?? $DIC->toolbar();
63  $this->rbacsystem = $rbacsystem ?? $DIC->rbac()->system();
64  $this->error = $error ?? $DIC['ilErr'];
65  $this->http = $http ?? $DIC->http();
66  $this->refinery = $DIC->refinery();
67  $this->ui_factory = $ui_factory ?? $DIC->ui()->factory();
68  $this->ui_renderer = $ui_renderer ?? $DIC->ui()->renderer();
69  $this->service = $template_service ?? $DIC->mail()->textTemplates();
70 
71  $this->lng->loadLanguageModule('meta');
72  }
73 
74  private function isEditingAllowed(): bool
75  {
76  return $this->rbacsystem->checkAccess('write', $this->parent_object->getRefId());
77  }
78 
79  public function executeCommand(): void
80  {
81  $cmd = $this->ctrl->getCmd();
82 
83  if ($this->http->wrapper()->query()->has('mail_template_table_action')) {
84  $cmd = $this->http->wrapper()->query()->retrieve(
85  'mail_template_table_action',
86  $this->refinery->kindlyTo()->string()
87  );
88  }
89  if (!$cmd || !method_exists($this, $cmd)) {
90  $cmd = 'showTemplates';
91  }
92  $this->$cmd();
93  }
94 
95  protected function showTemplates(): void
96  {
98  if (count($contexts) <= 1) {
99  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'));
100  } elseif ($this->isEditingAllowed()) {
101  $this->toolbar->addComponent($this->ui_factory->button()->standard(
102  $this->lng->txt('mail_new_template'),
103  $this->ctrl->getLinkTarget($this, 'showInsertTemplateForm')
104  ));
105  }
106 
107  $tbl = new ilMailTemplateTable(
108  $this->http->request(),
109  $this->lng,
111  new DataFactory(),
113  !$this->isEditingAllowed()
114  );
115 
116  $this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
117  }
118 
119  protected function insertTemplate(): void
120  {
121  if (!$this->isEditingAllowed()) {
122  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
123  }
124 
125  $form = $this->getTemplateForm();
126 
127  if (!$form->checkInput()) {
128  $form->setValuesByPost();
129  $this->showInsertTemplateForm($form);
130  return;
131  }
132 
133  $generic_context = new ilMailTemplateGenericContext();
134  if ($form->getInput('context') === $generic_context->getId()) {
135  $form->getItemByPostVar('context')->setAlert(
136  $this->lng->txt('mail_template_no_valid_context')
137  );
138  $form->setValuesByPost();
139  $this->showInsertTemplateForm($form);
140  return;
141  }
142 
143  try {
144  $this->service->createNewTemplate(
146  $form->getInput('title'),
147  $form->getInput('m_subject'),
148  $form->getInput('m_message'),
149  $form->getInput('lang')
150  );
151 
152  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
153  $this->ctrl->redirect($this, 'showTemplates');
154  } catch (\ILIAS\Mail\Templates\TemplateSubjectSyntaxException) {
155  $form->getItemByPostVar('m_subject')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
156  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
157  } catch (\ILIAS\Mail\Templates\TemplateMessageSyntaxException) {
158  $form->getItemByPostVar('m_message')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
159  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
160  } catch (Exception) {
161  $form->getItemByPostVar('context')->setAlert(
162  $this->lng->txt('mail_template_no_valid_context')
163  );
164  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
165  }
166 
167  $form->setValuesByPost();
168  $this->showInsertTemplateForm($form);
169  }
170 
171  protected function showInsertTemplateForm(?ilPropertyFormGUI $form = null): void
172  {
173  if (!($form instanceof ilPropertyFormGUI)) {
174  $form = $this->getTemplateForm();
175  }
176 
177  $this->tpl->setContent($form->getHTML());
178  }
179 
180  protected function updateTemplate(): void
181  {
182  if (!$this->isEditingAllowed()) {
183  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
184  }
185 
186  $template_id = 0;
187  if ($this->http->wrapper()->post()->has('tpl_id')) {
188  $template_id = $this->http->wrapper()->post()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
189  }
190 
191  if (!is_numeric($template_id) || $template_id < 1) {
192  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
193  $this->showTemplates();
194  return;
195  }
196 
197  try {
198  $form = $this->getTemplateForm();
199  if (!$form->checkInput()) {
200  $form->setValuesByPost();
201  $this->showEditTemplateForm($form);
202  return;
203  }
204 
205  $generic_context = new ilMailTemplateGenericContext();
206  if ($form->getInput('context') === $generic_context->getId()) {
207  $form->getItemByPostVar('context')->setAlert(
208  $this->lng->txt('mail_template_no_valid_context')
209  );
210  $form->setValuesByPost();
211  $this->showEditTemplateForm($form);
212  return;
213  }
214 
215  try {
216  $this->service->modifyExistingTemplate(
217  (int) $template_id,
219  $form->getInput('title'),
220  $form->getInput('m_subject'),
221  $form->getInput('m_message'),
222  $form->getInput('lang')
223  );
224 
225  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
226  $this->ctrl->redirect($this, 'showTemplates');
227  } catch (OutOfBoundsException) {
228  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
229  } catch (\ILIAS\Mail\Templates\TemplateSubjectSyntaxException) {
230  $form->getItemByPostVar('m_subject')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
231  } catch (\ILIAS\Mail\Templates\TemplateMessageSyntaxException) {
232  $form->getItemByPostVar('m_message')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
233  } catch (Exception) {
234  $form->getItemByPostVar('context')->setAlert(
235  $this->lng->txt('mail_template_no_valid_context')
236  );
237  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('form_input_not_valid'));
238  }
239 
240  $form->setValuesByPost();
241  $this->showEditTemplateForm($form);
242  } catch (Exception) {
243  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
244  $this->showTemplates();
245  }
246  }
247 
248  protected function showEditTemplateForm(?ilPropertyFormGUI $form = null): void
249  {
250  if (!($form instanceof ilPropertyFormGUI)) {
251  $template_id = 0;
252  if ($this->http->wrapper()->query()->has('mail_template_tpl_ids')) {
253  $template_id = $this->http->wrapper()->query()->retrieve(
254  'mail_template_tpl_ids',
255  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
256  )[0];
257  }
258 
259  if (!is_numeric($template_id) || $template_id < 1) {
260  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
261  $this->showTemplates();
262  return;
263  }
264 
265  try {
266  $template = $this->service->loadTemplateForId((int) $template_id);
267  $form = $this->getTemplateForm($template);
268  $this->populateFormWithTemplate($form, $template);
269  } catch (Exception) {
270  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
271  $this->showTemplates();
272  return;
273  }
274  }
275 
276  $this->tpl->setContent($form->getHTML());
277  }
278 
279  protected function populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template): void
280  {
281  $form->setValuesByArray([
282  'tpl_id' => $template->getTplId(),
283  'title' => $template->getTitle(),
284  'context' => $template->getContext(),
285  'lang' => $template->getLang(),
286  'm_subject' => $template->getSubject(),
287  'm_message' => $template->getMessage(),
288  ]);
289  }
290 
291  protected function confirmDeleteTemplate(): void
292  {
293  if (!$this->isEditingAllowed()) {
294  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
295  }
296 
297  $template_ids = [];
298  if ($this->http->wrapper()->query()->has('mail_template_tpl_ids')) {
299  $template_ids = $this->http->wrapper()->query()->retrieve(
300  'mail_template_tpl_ids',
301  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
302  );
303  if ($template_ids === ['ALL_OBJECTS']) {
304  $template_ids = array_map(
305  static fn(array $template): int => (int) ($template['tpl_id'] ?? 0),
306  $this->service->listAllTemplatesAsArray()
307  );
308  } else {
309  $template_ids = $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
310  ->transform($template_ids);
311  }
312  }
313 
314  if (count($template_ids) === 0) {
315  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
316  $this->showTemplates();
317  return;
318  }
319 
320  $confirm = new ilConfirmationGUI();
321  $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
322 
323  $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entries'));
324  if (count($template_ids) === 1) {
325  $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entry'));
326  }
327 
328  $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
329  $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
330 
331  foreach ($template_ids as $template_id) {
332  $template = $this->service->loadTemplateForId($template_id);
333  $confirm->addItem('tpl_id[]', (string) $template_id, $template->getTitle());
334  }
335 
336  $this->tpl->setContent($confirm->getHTML());
337  }
338 
339  protected function deleteTemplate(): void
340  {
341  if (!$this->isEditingAllowed()) {
342  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
343  }
344 
345  $template_ids = [];
346  if ($this->http->wrapper()->post()->has('tpl_id')) {
347  $template_ids = $this->http->wrapper()->post()->retrieve(
348  'tpl_id',
349  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
350  );
351  }
352  if (count($template_ids) === 0) {
353  $template_id = 0;
354  if ($this->http->wrapper()->query()->has('tpl_id')) {
355  $template_id = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
356  }
357  $template_ids = [$template_id];
358  }
359 
360  if (count($template_ids) === 0) {
361  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
362  $this->showTemplates();
363  return;
364  }
365 
366  $this->service->deleteTemplatesByIds($template_ids);
367 
368  if (count($template_ids) === 1) {
369  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_s'), true);
370  } else {
371  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_p'), true);
372  }
373  $this->ctrl->redirect($this, 'showTemplates');
374  }
375 
376  public function getAjaxPlaceholdersById(): void
377  {
378  $trigger_value = '';
379  if ($this->http->wrapper()->query()->has('triggerValue')) {
380  $trigger_value = $this->http->wrapper()->query()->retrieve(
381  'triggerValue',
382  $this->refinery->kindlyTo()->string()
383  );
384  }
385  $context_id = ilUtil::stripSlashes($trigger_value);
386 
387  $placeholders = new ilManualPlaceholderInputGUI(
388  $this->lng->txt('mail_form_placeholders_label'),
389  'm_placeholders',
390  'm_message'
391  );
392  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
393  try {
394  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
395  } catch (Throwable) {
396  $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
397  }
398 
400  foreach ($context->getPlaceholders() as $value) {
401  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
402  }
403 
404  $placeholders->render(true);
405  }
406 
407  protected function getTemplateForm(?ilMailTemplate $template = null): ilPropertyFormGUI
408  {
409  $form = new ilPropertyFormGUI();
410 
411  $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
412  $title->setRequired(true);
413  $title->setDisabled(!$this->isEditingAllowed());
414  $form->addItem($title);
415 
416  $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
417  $context->setDisabled(!$this->isEditingAllowed());
419 
420  if (count($contexts) <= 1) {
421  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'), true);
422  $this->ctrl->redirect($this, 'showTemplates');
423  }
424 
425  $context_sort = [];
426  $context_options = [];
427  $generic_context = new ilMailTemplateGenericContext();
428  foreach ($contexts as $ctx) {
429  if ($ctx->getId() !== $generic_context->getId()) {
430  $context_options[$ctx->getId()] = $ctx;
431  $context_sort[$ctx->getId()] = $ctx->getTitle();
432  }
433  }
434  asort($context_sort);
435  $first = null;
436  foreach (array_keys($context_sort) as $id) {
437  $ctx = $context_options[$id];
438  $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
439  $option->setInfo($ctx->getDescription());
440  $context->addOption($option);
441 
442  if (!$first) {
443  $first = $id;
444  }
445  }
446  $context->setValue($first);
447  $context->setRequired(true);
448  $form->addItem($context);
449 
450  $hidden = new ilHiddenInputGUI('lang');
451  $hidden->setValue($this->lng->getLangKey());
452  $form->addItem($hidden);
453 
454  $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
455  $subject->setDisabled(!$this->isEditingAllowed());
456  $subject->setSize(50);
457  $form->addItem($subject);
458 
459  $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
460  $message->setDisabled(!$this->isEditingAllowed());
461  $message->setRequired(true);
462  $message->setCols(60);
463  $message->setRows(10);
464  $form->addItem($message);
465 
466  $placeholders = new ilManualPlaceholderInputGUI(
467  $this->lng->txt('mail_form_placeholders_label'),
468  'm_placeholders',
469  'm_message'
470  );
471  $placeholders->setDisabled(!$this->isEditingAllowed());
472  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
473  try {
474  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
475  } catch (Throwable) {
476  $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
477  }
478  $placeholders->supportsRerenderSignal(
479  'context',
480  $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true)
481  );
482  if ($template === null) {
483  $context_id = $generic_context->getId();
484  } else {
485  $context_id = $template->getContext();
486  }
488  foreach ($context->getPlaceholders() as $value) {
489  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
490  }
491  $form->addItem($placeholders);
492  if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
493  $id = new ilHiddenInputGUI('tpl_id');
494  $form->addItem($id);
495 
496  $form->setTitle($this->lng->txt('mail_edit_tpl'));
497  $form->setFormAction($this->ctrl->getFormAction($this, 'updateTemplate'));
498 
499  if ($this->isEditingAllowed()) {
500  $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
501  }
502  } else {
503  $form->setTitle($this->lng->txt('mail_create_tpl'));
504  $form->setFormAction($this->ctrl->getFormAction($this, 'insertTemplate'));
505 
506  if ($this->isEditingAllowed()) {
507  $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
508  }
509  }
510 
511  if ($this->isEditingAllowed()) {
512  $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
513  } else {
514  $form->addCommandButton('showTemplates', $this->lng->txt('back'));
515  }
516 
517  return $form;
518  }
519 
520  public function unsetAsContextDefault(): void
521  {
522  if (!$this->isEditingAllowed()) {
523  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
524  }
525 
526  $template_id = 0;
527  if ($this->http->wrapper()->query()->has('mail_template_tpl_ids')) {
528  $template_id = $this->http->wrapper()->query()->retrieve(
529  'mail_template_tpl_ids',
530  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
531  )[0];
532  }
533 
534  if (!is_numeric($template_id) || $template_id < 1) {
535  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
536  $this->showTemplates();
537  return;
538  }
539 
540  try {
541  $template = $this->service->loadTemplateForId((int) $template_id);
542  $this->service->unsetAsContextDefault($template);
543  } catch (Exception) {
544  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
545  $this->showTemplates();
546  return;
547  }
548 
549  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
550  $this->ctrl->redirect($this, 'showTemplates');
551  }
552 
553  public function setAsContextDefault(): void
554  {
555  if (!$this->isEditingAllowed()) {
556  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
557  }
558 
559  $template_id = 0;
560  if ($this->http->wrapper()->query()->has('mail_template_tpl_ids')) {
561  $template_id = $this->http->wrapper()->query()->retrieve(
562  'mail_template_tpl_ids',
563  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
564  )[0];
565  }
566 
567  if (!is_numeric($template_id) || $template_id < 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) $template_id);
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 class represents an option in a radio group.
$context
Definition: webdav.php:31
getItemByPostVar(string $a_post_var)
ilMailTemplateGUI: ilObjMailGUI
Interface Observer Contains several chained tasks and infos about them.
setInfo(string $a_info)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
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-...
__construct(protected ilObject $parent_object, ?ilGlobalTemplateInterface $tpl=null, ?ilCtrlInterface $ctrl=null, ?ilLanguage $lng=null, ?ilToolbarGUI $toolbar=null, ?ilRbacSystem $rbacsystem=null, ?ilErrorHandling $error=null, ?GlobalHttpState $http=null, ?Factory $ui_factory=null, ?Renderer $ui_renderer=null, ?ilMailTemplateService $template_service=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
ilGlobalTemplateInterface $tpl
static getTemplateContexts(?array $a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id. ...
This class represents a hidden form property in a property form.
static http()
Fetches the global http state from ILIAS.
This class represents a property in a property form.
populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template)
showInsertTemplateForm(?ilPropertyFormGUI $form=null)
setFormAction(string $a_formaction)
This is how the factory for UI elements looks.
Definition: Factory.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:26
RFC 822 Email address list validation Utility.
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:23
This class represents a text area property in a property form.
getTemplateForm(?ilMailTemplate $template=null)
$message
Definition: xapiexit.php:31
setDisabled(bool $a_disabled)
showEditTemplateForm(?ilPropertyFormGUI $form=null)
ilMailTemplateService $service