ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailTemplateContext.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
4
7
8require_once './Services/Language/classes/class.ilLanguageFactory.php';
9
16{
18 protected $language;
19
21 protected $envHelper;
22
24 protected $languageHelper;
25
27 protected $userHelper;
28
31
39 public function __construct(
42 ilMailUserHelper $usernameHelper = null,
44 ) {
45 if (null === $orgUnitUserService) {
47 }
48 $this->orgUnitUserService = $orgUnitUserService;
49
50 if (null === $envHelper) {
52 }
53 $this->envHelper = $envHelper;
54
55 if (null === $usernameHelper) {
56 $usernameHelper = new ilMailUserHelper();
57 }
58 $this->userHelper = $usernameHelper;
59
60 if (null === $languageHelper) {
62 }
63 $this->languageHelper = $languageHelper;
64 }
65
69 public function getLanguage() : ilLanguage
70 {
71 return $this->language ? $this->language : $this->languageHelper->getCurrentLanguage();
72 }
73
78 abstract public function getId() : string;
79
84 abstract public function getTitle() : string;
85
90 abstract public function getDescription() : string;
91
95 private function getGenericPlaceholders() : array
96 {
97 return [
98 'mail_salutation' => [
99 'placeholder' => 'MAIL_SALUTATION',
100 'label' => $this->getLanguage()->txt('mail_nacc_salutation')
101 ],
102 'first_name' => [
103 'placeholder' => 'FIRST_NAME',
104 'label' => $this->getLanguage()->txt('firstname')
105 ],
106 'last_name' => [
107 'placeholder' => 'LAST_NAME',
108 'label' => $this->getLanguage()->txt('lastname')
109 ],
110 'login' => [
111 'placeholder' => 'LOGIN',
112 'label' => $this->getLanguage()->txt('mail_nacc_login')
113 ],
114 'title' => [
115 'placeholder' => 'TITLE',
116 'label' => $this->getLanguage()->txt('mail_nacc_title'),
117 'supportsCondition' => true
118 ],
119 'firstname_last_name_superior' => [
120 'placeholder' => 'FIRSTNAME_LASTNAME_SUPERIOR',
121 'label' => $this->getLanguage()->txt('mail_firstname_last_name_superior')
122 ],
123 'ilias_url' => [
124 'placeholder' => 'ILIAS_URL',
125 'label' => $this->getLanguage()->txt('mail_nacc_ilias_url')
126 ],
127 'client_name' => [
128 'placeholder' => 'CLIENT_NAME',
129 'label' => $this->getLanguage()->txt('mail_nacc_client_name')
130 ],
131 ];
132 }
133
138 final public function getPlaceholders() : array
139 {
140 $placeholders = $this->getGenericPlaceholders();
141 $specific = $this->getSpecificPlaceholders();
142
143 return $placeholders + $specific;
144 }
145
150 abstract public function getSpecificPlaceholders() : array;
151
159 abstract public function resolveSpecificPlaceholder(
160 string $placeholder_id,
161 array $context_parameters,
162 ilObjUser $recipient = null,
163 bool $html_markup = false
164 ) : string;
165
173 public function resolvePlaceholder(
174 string $placeholder_id,
175 array $context_parameters,
176 ilObjUser $recipient = null,
177 bool $html_markup = false
178 ) : string {
179 if ($recipient !== null) {
180 $this->initLanguage($recipient);
181 }
182
183 $resolved = '';
184
185 switch (true) {
186 case ('mail_salutation' === $placeholder_id && $recipient !== null):
187 $resolved = $this->getLanguage()->txt('mail_salutation_n');
188 switch ($recipient->getGender()) {
189 case 'f':
190 $resolved = $this->getLanguage()->txt('mail_salutation_f');
191 break;
192
193 case 'm':
194 $resolved = $this->getLanguage()->txt('mail_salutation_m');
195 break;
196
197 case 'n':
198 $resolved = $this->getLanguage()->txt('mail_salutation_n');
199 break;
200 }
201 break;
202
203 case ('first_name' === $placeholder_id && $recipient !== null):
204 $resolved = $recipient->getFirstname();
205 break;
206
207 case ('last_name' === $placeholder_id && $recipient !== null):
208 $resolved = $recipient->getLastname();
209 break;
210
211 case ('login' === $placeholder_id && $recipient !== null):
212 $resolved = $recipient->getLogin();
213 break;
214
215 case ('title' === $placeholder_id && $recipient !== null):
216 $resolved = $recipient->getUTitle();
217 break;
218
219 case 'ilias_url' === $placeholder_id:
220 $resolved = $this->envHelper->getHttpPath() . '/login.php?client_id=' . $this->envHelper->getClientId();
221 break;
222
223 case 'client_name' === $placeholder_id:
224 $resolved = $this->envHelper->getClientId();
225 break;
226
227 case 'firstname_last_name_superior' === $placeholder_id && $recipient !== null:
228 $ouUsers = $this->orgUnitUserService->getUsers([$recipient->getId()], true);
229 foreach ($ouUsers as $ouUser) {
230 $superiors = $ouUser->getSuperiors();
231
232 $superiorUsrIds = array_map(function (ilOrgUnitUser $ouUser) {
233 return $ouUser->getUserId();
234 }, $superiors);
235
236 $usrIdByNameMap = $this->userHelper->getUsernameMapForIds($superiorUsrIds);
237
238 $resolved = implode(', ', $usrIdByNameMap);
239 break;
240 }
241 break;
242
243 case !in_array($placeholder_id, array_keys($this->getGenericPlaceholders())):
244 $datePresentationLanguage = ilDatePresentation::getLanguage();
246
247 $resolved = $this->resolveSpecificPlaceholder(
248 $placeholder_id,
249 $context_parameters,
250 $recipient,
251 $html_markup
252 );
253
254 ilDatePresentation::setLanguage($datePresentationLanguage);
255 break;
256 }
257
258 return (string) $resolved;
259 }
260
264 protected function initLanguage(ilObjUser $user) : void
265 {
266 $this->initLanguageByIso2Code((string) $user->getLanguage());
267 }
268
272 protected function initLanguageByIso2Code(string $isoCode) : void
273 {
274 $this->language = $this->languageHelper->getLanguageByIsoCode($isoCode);
275 $this->language->loadLanguageModule('mail');
276 }
277}
An exception for terminatinating execution or to throw for unit testing.
return true
Flag indicating whether or not HTTP headers will be sent when outputting captcha image/audio.
Class for date presentation.
static setLanguage($a_lng)
set language
language handling
Class ilMailEnvironmentHelper.
Class ilMailLanguageHelper.
Class ilMailTemplateContext.
getSpecificPlaceholders()
Return an array of placeholders.
getId()
Returns a unique (in the context of mail template contexts) id.
resolvePlaceholder(string $placeholder_id, array $context_parameters, ilObjUser $recipient=null, bool $html_markup=false)
getTitle()
Returns a translated title (depending on the current language) which is displayed in the user interfa...
getPlaceholders()
Return an array of placeholders.
__construct(OrgUnitUserService $orgUnitUserService=null, ilMailEnvironmentHelper $envHelper=null, ilMailUserHelper $usernameHelper=null, ilMailLanguageHelper $languageHelper=null)
ilMailTemplateContext constructor.
getDescription()
Returns a translated description (depending on the current language) which is displayed in the user i...
resolveSpecificPlaceholder(string $placeholder_id, array $context_parameters, ilObjUser $recipient=null, bool $html_markup=false)
Class ilMailUserHelper.
getLanguage()
returns a 2char-language-string @access public
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
language()
Definition: language.php:2