ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailTemplateService.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
29 
30  public function __construct(ilMailTemplateRepository $repository)
31  {
32  $this->repository = $repository;
33  }
34 
35  public function createNewTemplate(
36  string $contextId,
37  string $title,
38  string $subject,
39  string $message,
40  string $language
41  ): ilMailTemplate {
42  $template = new ilMailTemplate();
43  $template->setContext($contextId);
44  $template->setTitle($title);
45  $template->setSubject($subject);
46  $template->setMessage($message);
47  $template->setLang($language);
48 
49  $this->repository->store($template);
50 
51  return $template;
52  }
53 
54  public function modifyExistingTemplate(
55  int $templateId,
56  string $contextId,
57  string $title,
58  string $subject,
59  string $message,
60  string $language
61  ): void {
62  $template = $this->repository->findById($templateId);
63 
64  $template->setContext($contextId);
65  $template->setTitle($title);
66  $template->setSubject($subject);
67  $template->setMessage($message);
68  $template->setLang($language);
69 
70  $this->repository->store($template);
71  }
72 
73  public function loadTemplateForId(int $templateId): ilMailTemplate
74  {
75  return $this->repository->findById($templateId);
76  }
77 
82  public function loadTemplatesForContextId(string $contextId): array
83  {
84  return $this->repository->findByContextId($contextId);
85  }
86 
90  public function deleteTemplatesByIds(array $templateIds): void
91  {
92  $this->repository->deleteByIds($templateIds);
93  }
94 
98  public function listAllTemplatesAsArray(): array
99  {
100  $templates = $this->repository->getAll();
101 
102  return array_map(static function (ilMailTemplate $template): array {
103  return $template->toArray();
104  }, $templates);
105  }
106 
107  public function unsetAsContextDefault(ilMailTemplate $template): void
108  {
109  $template->setAsDefault(false);
110 
111  $this->repository->store($template);
112  }
113 
114  public function setAsContextDefault(ilMailTemplate $template): void
115  {
116  $allOfContext = $this->repository->findByContextId($template->getContext());
117  foreach ($allOfContext as $otherTemplate) {
118  $otherTemplate->setAsDefault(false);
119 
120  if ($template->getTplId() === $otherTemplate->getTplId()) {
121  $otherTemplate->setAsDefault(true);
122  }
123 
124  $this->repository->store($otherTemplate);
125  }
126  }
127 }
modifyExistingTemplate(int $templateId, string $contextId, string $title, string $subject, string $message, string $language)
loadTemplatesForContextId(string $contextId)
__construct(ilMailTemplateRepository $repository)
Class ilMailTemplateService.
setAsDefault(bool $isDefault)
Class ilMailTemplate.
createNewTemplate(string $contextId, string $title, string $subject, string $message, string $language)
unsetAsContextDefault(ilMailTemplate $template)
ilMailTemplateRepository $repository
Class ilMailTemplateRepository.
$message
Definition: xapiexit.php:32
deleteTemplatesByIds(array $templateIds)
setAsContextDefault(ilMailTemplate $template)