ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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,
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 
172  protected function showInsertTemplateForm(?ilPropertyFormGUI $form = null): void
173  {
174  if (!($form instanceof ilPropertyFormGUI)) {
175  $form = $this->getTemplateForm();
176  }
177 
178  $this->tpl->setContent($form->getHTML());
179  }
180 
181  protected function updateTemplate(): void
182  {
183  if (!$this->isEditingAllowed()) {
184  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
185  }
186 
187  $templateId = 0;
188  if ($this->http->wrapper()->post()->has('tpl_id')) {
189  $templateId = $this->http->wrapper()->post()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
190  }
191 
192  if (!is_numeric($templateId) || $templateId < 1) {
193  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
194  $this->showTemplates();
195  return;
196  }
197 
198  try {
199  $form = $this->getTemplateForm();
200  if (!$form->checkInput()) {
201  $form->setValuesByPost();
202  $this->showEditTemplateForm($form);
203  return;
204  }
205 
206  $genericContext = new ilMailTemplateGenericContext();
207  if ($form->getInput('context') === $genericContext->getId()) {
208  $form->getItemByPostVar('context')->setAlert(
209  $this->lng->txt('mail_template_no_valid_context')
210  );
211  $form->setValuesByPost();
212  $this->showEditTemplateForm($form);
213  return;
214  }
215 
216  try {
217  $this->service->modifyExistingTemplate(
218  (int) $templateId,
220  $form->getInput('title'),
221  $form->getInput('m_subject'),
222  $form->getInput('m_message'),
223  $form->getInput('lang')
224  );
225 
226  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
227  $this->ctrl->redirect($this, 'showTemplates');
228  } catch (OutOfBoundsException) {
229  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
230  } catch (\ILIAS\Mail\Templates\TemplateSubjectSyntaxException) {
231  $form->getItemByPostVar('m_subject')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
232  } catch (\ILIAS\Mail\Templates\TemplateMessageSyntaxException) {
233  $form->getItemByPostVar('m_message')->setAlert($this->lng->txt('mail_template_invalid_tpl_syntax'));
234  } catch (Exception) {
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) {
244  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
245  $this->showTemplates();
246  }
247  }
248 
249  protected function showEditTemplateForm(?ilPropertyFormGUI $form = null): void
250  {
251  if (!($form instanceof ilPropertyFormGUI)) {
252  $templateId = 0;
253  if ($this->http->wrapper()->query()->has('tpl_id')) {
254  $templateId = $this->http->wrapper()->query()->retrieve(
255  'tpl_id',
256  $this->refinery->kindlyTo()->int()
257  );
258  }
259 
260  if (!is_numeric($templateId) || $templateId < 1) {
261  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
262  $this->showTemplates();
263  return;
264  }
265 
266  try {
267  $template = $this->service->loadTemplateForId((int) $templateId);
268  $form = $this->getTemplateForm($template);
269  $this->populateFormWithTemplate($form, $template);
270  } catch (Exception) {
271  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
272  $this->showTemplates();
273  return;
274  }
275  }
276 
277  $this->tpl->setContent($form->getHTML());
278  }
279 
280  protected function populateFormWithTemplate(ilPropertyFormGUI $form, ilMailTemplate $template): void
281  {
282  $form->setValuesByArray([
283  'tpl_id' => $template->getTplId(),
284  'title' => $template->getTitle(),
285  'context' => $template->getContext(),
286  'lang' => $template->getLang(),
287  'm_subject' => $template->getSubject(),
288  'm_message' => $template->getMessage(),
289  ]);
290  }
291 
292  protected function confirmDeleteTemplate(): void
293  {
294  if (!$this->isEditingAllowed()) {
295  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
296  }
297 
298  $templateIds = [];
299  if ($this->http->wrapper()->post()->has('tpl_id')) {
300  $templateIds = $this->http->wrapper()->post()->retrieve(
301  'tpl_id',
302  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
303  );
304  }
305  if (count($templateIds) === 0 && $this->http->wrapper()->query()->has('tpl_id')) {
306  $templateIds = [$this->http->wrapper()->query()->retrieve(
307  'tpl_id',
308  $this->refinery->kindlyTo()->int()
309  )];
310  }
311 
312  if (0 === count($templateIds)) {
313  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
314  $this->showTemplates();
315  return;
316  }
317 
318  $confirm = new ilConfirmationGUI();
319  $confirm->setFormAction($this->ctrl->getFormAction($this, 'deleteTemplate'));
320 
321  $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entries'));
322  if (1 === count($templateIds)) {
323  $confirm->setHeaderText($this->lng->txt('mail_tpl_sure_delete_entry'));
324  }
325 
326  $confirm->setConfirm($this->lng->txt('confirm'), 'deleteTemplate');
327  $confirm->setCancel($this->lng->txt('cancel'), 'showTemplates');
328 
329  foreach ($templateIds as $templateId) {
330  $template = $this->service->loadTemplateForId($templateId);
331  $confirm->addItem('tpl_id[]', (string) $templateId, $template->getTitle());
332  }
333 
334  $this->tpl->setContent($confirm->getHTML());
335  }
336 
337  protected function deleteTemplate(): void
338  {
339  if (!$this->isEditingAllowed()) {
340  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
341  }
342 
343  $templateIds = [];
344  if ($this->http->wrapper()->post()->has('tpl_id')) {
345  $templateIds = $this->http->wrapper()->post()->retrieve(
346  'tpl_id',
347  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
348  );
349  }
350  if (count($templateIds) === 0) {
351  $templateId = 0;
352  if ($this->http->wrapper()->query()->has('tpl_id')) {
353  $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
354  }
355  $templateIds = [$templateId];
356  }
357 
358  if (0 === count($templateIds)) {
359  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('select_one'));
360  $this->showTemplates();
361  return;
362  }
363 
364  $this->service->deleteTemplatesByIds($templateIds);
365 
366  if (1 === count($templateIds)) {
367  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_s'), true);
368  } else {
369  $this->tpl->setOnScreenMessage('success', $this->lng->txt('mail_tpl_deleted_p'), true);
370  }
371  $this->ctrl->redirect($this, 'showTemplates');
372  }
373 
377  public function getAjaxPlaceholdersById(): void
378  {
379  $triggerValue = '';
380  if ($this->http->wrapper()->query()->has('triggerValue')) {
381  $triggerValue = $this->http->wrapper()->query()->retrieve(
382  'triggerValue',
383  $this->refinery->kindlyTo()->string()
384  );
385  }
386  $contextId = ilUtil::stripSlashes($triggerValue);
387 
388  $placeholders = new ilManualPlaceholderInputGUI(
389  $this->lng->txt('mail_form_placeholders_label'),
390  'm_placeholders',
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) {
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 
411  protected function getTemplateForm(?ilMailTemplate $template = null): ilPropertyFormGUI
412  {
413  $form = new ilPropertyFormGUI();
414 
415  $title = new ilTextInputGUI($this->lng->txt('mail_template_title'), 'title');
416  $title->setRequired(true);
417  $title->setDisabled(!$this->isEditingAllowed());
418  $form->addItem($title);
419 
420  $context = new ilRadioGroupInputGUI($this->lng->txt('mail_template_context'), 'context');
421  $context->setDisabled(!$this->isEditingAllowed());
423 
424  if (count($contexts) <= 1) {
425  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_no_context_available'), true);
426  $this->ctrl->redirect($this, 'showTemplates');
427  }
428 
429  $context_sort = [];
430  $context_options = [];
431  $generic_context = new ilMailTemplateGenericContext();
432  foreach ($contexts as $ctx) {
433  if ($ctx->getId() !== $generic_context->getId()) {
434  $context_options[$ctx->getId()] = $ctx;
435  $context_sort[$ctx->getId()] = $ctx->getTitle();
436  }
437  }
438  asort($context_sort);
439  $first = null;
440  foreach (array_keys($context_sort) as $id) {
441  $ctx = $context_options[$id];
442  $option = new ilRadioOption($ctx->getTitle(), $ctx->getId());
443  $option->setInfo($ctx->getDescription());
444  $context->addOption($option);
445 
446  if (!$first) {
447  $first = $id;
448  }
449  }
450  $context->setValue($first);
451  $context->setRequired(true);
452  $form->addItem($context);
453 
454  $hidden = new ilHiddenInputGUI('lang');
455  $hidden->setValue($this->lng->getLangKey());
456  $form->addItem($hidden);
457 
458  $subject = new ilTextInputGUI($this->lng->txt('subject'), 'm_subject');
459  $subject->setDisabled(!$this->isEditingAllowed());
460  $subject->setSize(50);
461  $form->addItem($subject);
462 
463  $message = new ilTextAreaInputGUI($this->lng->txt('message'), 'm_message');
464  $message->setDisabled(!$this->isEditingAllowed());
465  $message->setRequired(true);
466  $message->setCols(60);
467  $message->setRows(10);
468  $form->addItem($message);
469 
470  $placeholders = new ilManualPlaceholderInputGUI(
471  $this->lng->txt('mail_form_placeholders_label'),
472  'm_placeholders',
473  'm_message'
474  );
475  $placeholders->setDisabled(!$this->isEditingAllowed());
476  $placeholders->setInstructionText($this->lng->txt('mail_nacc_use_placeholder'));
477  try {
478  $placeholders->setAdviseText(sprintf($this->lng->txt('placeholders_advise'), '<br />'));
479  } catch (Throwable) {
480  $placeholders->setAdviseText($this->lng->txt('placeholders_advise'));
481  }
482  $placeholders->supportsRerenderSignal(
483  'context',
484  $this->ctrl->getLinkTarget($this, 'getAjaxPlaceholdersById', '', true)
485  );
486  if ($template === null) {
487  $context_id = $generic_context->getId();
488  } else {
489  $context_id = $template->getContext();
490  }
492  foreach ($context->getPlaceholders() as $value) {
493  $placeholders->addPlaceholder($value['placeholder'], $value['label']);
494  }
495  $form->addItem($placeholders);
496  if ($template instanceof ilMailTemplate && $template->getTplId() > 0) {
497  $id = new ilHiddenInputGUI('tpl_id');
498  $form->addItem($id);
499 
500  $form->setTitle($this->lng->txt('mail_edit_tpl'));
501  $form->setFormAction($this->ctrl->getFormAction($this, 'updateTemplate'));
502 
503  if ($this->isEditingAllowed()) {
504  $form->addCommandButton('updateTemplate', $this->lng->txt('save'));
505  }
506  } else {
507  $form->setTitle($this->lng->txt('mail_create_tpl'));
508  $form->setFormAction($this->ctrl->getFormAction($this, 'insertTemplate'));
509 
510  if ($this->isEditingAllowed()) {
511  $form->addCommandButton('insertTemplate', $this->lng->txt('save'));
512  }
513  }
514 
515  if ($this->isEditingAllowed()) {
516  $form->addCommandButton('showTemplates', $this->lng->txt('cancel'));
517  } else {
518  $form->addCommandButton('showTemplates', $this->lng->txt('back'));
519  }
520 
521  return $form;
522  }
523 
524  public function unsetAsContextDefault(): void
525  {
526  if (!$this->isEditingAllowed()) {
527  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
528  }
529 
530  $templateId = 0;
531  if ($this->http->wrapper()->query()->has('tpl_id')) {
532  $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
533  }
534 
535  if (!is_numeric($templateId) || $templateId < 1) {
536  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
537  $this->showTemplates();
538  return;
539  }
540 
541  try {
542  $template = $this->service->loadTemplateForId((int) $templateId);
543  $this->service->unsetAsContextDefault($template);
544  } catch (Exception) {
545  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
546  $this->showTemplates();
547  return;
548  }
549 
550  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
551  $this->ctrl->redirect($this, 'showTemplates');
552  }
553 
554  public function setAsContextDefault(): void
555  {
556  if (!$this->isEditingAllowed()) {
557  $this->error->raiseError($this->lng->txt('msg_no_perm_write'), $this->error->WARNING);
558  }
559 
560  $templateId = 0;
561  if ($this->http->wrapper()->query()->has('tpl_id')) {
562  $templateId = $this->http->wrapper()->query()->retrieve('tpl_id', $this->refinery->kindlyTo()->int());
563  }
564 
565  if (!is_numeric($templateId) || $templateId < 1) {
566  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
567  $this->showTemplates();
568  return;
569  }
570 
571  try {
572  $template = $this->service->loadTemplateForId((int) $templateId);
573  $this->service->setAsContextDefault($template);
574  } catch (Exception) {
575  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('mail_template_missing_id'));
576  $this->showTemplates();
577  return;
578  }
579 
580  $this->tpl->setOnScreenMessage('success', $this->lng->txt('saved_successfully'), true);
581  $this->ctrl->redirect($this, 'showTemplates');
582  }
583 }
This class represents an option in a radio group.
$context
Definition: webdav.php:31
getItemByPostVar(string $a_post_var)
Class ilMailTemplateGUI.
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="")
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.
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
global $DIC
Definition: shib_login.php:22
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.
__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)
$message
Definition: xapiexit.php:31
setDisabled(bool $a_disabled)
showEditTemplateForm(?ilPropertyFormGUI $form=null)
ilMailTemplateService $service