ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailTemplateService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
25 {
26  public function __construct(
27  protected ilMailTemplateRepository $repository,
28  protected ilMustacheFactory $mustache_factory
29  ) {
30  }
31 
32  public function createNewTemplate(
33  string $context_id,
34  string $title,
35  string $subject,
36  string $message,
37  string $language
38  ): ilMailTemplate {
39  try {
40  $this->mustache_factory->getBasicEngine()->render($subject, []);
41  } catch (Exception) {
42  throw new TemplateSubjectSyntaxException('Invalid mail template for subject');
43  }
44 
45  try {
46  $this->mustache_factory->getBasicEngine()->render($message, []);
47  } catch (Exception) {
48  throw new TemplateMessageSyntaxException('Invalid mail template for message');
49  }
50 
51  $template = new ilMailTemplate();
52  $template->setContext($context_id);
53  $template->setTitle($title);
54  $template->setSubject($subject);
55  $template->setMessage($message);
56  $template->setLang($language);
57 
58  $this->repository->store($template);
59 
60  return $template;
61  }
62 
63  public function modifyExistingTemplate(
64  int $template_id,
65  string $context_id,
66  string $title,
67  string $subject,
68  string $message,
69  string $language
70  ): void {
71  try {
72  $this->mustache_factory->getBasicEngine()->render($subject, []);
73  } catch (Exception) {
74  throw new TemplateSubjectSyntaxException('Invalid mail template for subject');
75  }
76 
77  try {
78  $this->mustache_factory->getBasicEngine()->render($message, []);
79  } catch (Exception) {
80  throw new TemplateMessageSyntaxException('Invalid mail template for message');
81  }
82 
83  $template = $this->repository->findById($template_id);
84 
85  $template->setContext($context_id);
86  $template->setTitle($title);
87  $template->setSubject($subject);
88  $template->setMessage($message);
89  $template->setLang($language);
90 
91  $this->repository->store($template);
92  }
93 
94  public function loadTemplateForId(int $template_id): ilMailTemplate
95  {
96  return $this->repository->findById($template_id);
97  }
98 
99  public function loadTemplatesForContextId(string $context_id): array
100  {
101  return $this->repository->findByContextId($context_id);
102  }
103 
104  public function deleteTemplatesByIds(array $template_ids): void
105  {
106  $this->repository->deleteByIds($template_ids);
107  }
108 
109  public function listAllTemplatesAsArray(): array
110  {
111  $templates = $this->repository->getAll();
112 
113  return array_map(static fn(ilMailTemplate $template): array => $template->toArray(), $templates);
114  }
115 
116  public function unsetAsContextDefault(ilMailTemplate $template): void
117  {
118  $template->setAsDefault(false);
119 
120  $this->repository->store($template);
121  }
122 
123  public function setAsContextDefault(ilMailTemplate $template): void
124  {
125  $all_of_context = $this->repository->findByContextId($template->getContext());
126  foreach ($all_of_context as $other_template) {
127  $other_template->setAsDefault(false);
128 
129  if ($template->getTplId() === $other_template->getTplId()) {
130  $other_template->setAsDefault(true);
131  }
132 
133  $this->repository->store($other_template);
134  }
135  }
136 }
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
__construct(protected ilMailTemplateRepository $repository, protected ilMustacheFactory $mustache_factory)
deleteTemplatesByIds(array $template_ids)
loadTemplatesForContextId(string $context_id)
unsetAsContextDefault(ilMailTemplate $template)
modifyExistingTemplate(int $template_id, string $context_id, string $title, string $subject, string $message, string $language)
setAsDefault(bool $is_default)
$message
Definition: xapiexit.php:31
setAsContextDefault(ilMailTemplate $template)
createNewTemplate(string $context_id, string $title, string $subject, string $message, string $language)