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