ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailTemplateService.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
10 {
12  protected $repository;
13 
19  {
20  $this->repository = $repository;
21  }
22 
31  public function createNewTemplate(
32  string $contextId,
33  string $title,
34  string $subject,
35  string $message,
36  string $language
37  ) : \ilMailTemplate {
38  $template = new \ilMailTemplate();
39  $template->setContext($contextId);
40  $template->setTitle($title);
41  $template->setSubject($subject);
42  $template->setMessage($message);
43  $template->setLang($language);
44 
45  $this->repository->store($template);
46 
47  return $template;
48  }
49 
58  public function modifyExistingTemplate(
59  int $templateId,
60  string $contextId,
61  string $title,
62  string $subject,
63  string $message,
64  string $language
65  ) {
66  $template = $this->repository->findById($templateId);
67 
68  $template->setContext($contextId);
69  $template->setTitle($title);
70  $template->setSubject($subject);
71  $template->setMessage($message);
72  $template->setLang($language);
73 
74  $this->repository->store($template);
75  }
76 
81  public function loadTemplateForId(int $templateId) : \ilMailTemplate
82  {
83  return $this->repository->findById($templateId);
84  }
85 
90  public function loadTemplatesForContextId(string $contextId) : array
91  {
92  return $this->repository->findByContextId($contextId);
93  }
94 
98  public function deleteTemplatesByIds(array $templateIds)
99  {
100  $this->repository->deleteByIds($templateIds);
101  }
102 
106  public function listAllTemplatesAsArray() : array
107  {
108  $templates = $this->repository->getAll();
109 
110  $templates = array_map(function (\ilMailTemplate $template) {
111  return $template->toArray();
112  }, $templates);
113 
114  return $templates;
115  }
116 
121  {
122  $template->setAsDefault(false);
123 
124  $this->repository->store($template);
125  }
126 
131  {
132  $allOfContext = $this->repository->findByContextId($template->getContext());
133  foreach ($allOfContext as $otherTemplate) {
134  $otherTemplate->setAsDefault(false);
135 
136  if ((int) $template->getTplId() === (int) $otherTemplate->getTplId()) {
137  $otherTemplate->setAsDefault(true);
138  }
139 
140  $this->repository->store($otherTemplate);
141  }
142  }
143 }
unsetAsContextDefault(\ilMailTemplate $template)
$template
modifyExistingTemplate(int $templateId, string $contextId, string $title, string $subject, string $message, string $language)
loadTemplatesForContextId(string $contextId)
Class ilMailTemplateService.
setAsDefault(bool $isDefault)
Class ilMailTemplate.
__construct(\ilMailTemplateRepository $repository)
ilMailTemplateService constructor.
createNewTemplate(string $contextId, string $title, string $subject, string $message, string $language)
catch(Exception $e) $message
repository()
Definition: repository.php:5
setAsContextDefault(\ilMailTemplate $template)
Class ilMailTemplateRepository.
deleteTemplatesByIds(array $templateIds)