ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailMemberSearchGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use Psr\Http\Message\ServerRequestInterface;
27
29{
30 private readonly ServerRequestInterface $httpRequest;
32 private readonly array $mail_roles;
34 private readonly ilCtrlInterface $ctrl;
36 private readonly ilLanguage $lng;
37 private readonly ilAccessHandler $access;
38 private readonly \ILIAS\UI\Factory $ui_factory;
39 private readonly Services $http;
40 private readonly Renderer $ui_renderer;
41 private readonly Factory $refinery;
42
47 public function __construct(
48 private readonly object $gui,
49 public int $ref_id,
50 private readonly ilAbstractMailMemberRoles $objMailMemberRoles
51 ) {
52 global $DIC;
53
54 $this->ctrl = $DIC['ilCtrl'];
55 $this->tpl = $DIC['tpl'];
56 $this->lng = $DIC['lng'];
57 $this->access = $DIC['ilAccess'];
58 $this->httpRequest = $DIC->http()->request();
59
60 $this->ui_factory = $DIC->ui()->factory();
61 $this->ui_renderer = $DIC->ui()->renderer();
62 $this->refinery = $DIC->refinery();
63 $this->http = $DIC->http();
64
65 $this->lng->loadLanguageModule('mail');
66 $this->lng->loadLanguageModule('search');
67 $this->mail_roles = $objMailMemberRoles->getMailRoles($ref_id);
68 }
69
70 private function handleSearchMembersActions(): void
71 {
72 $action = $this->http->wrapper()->query()->retrieve(
73 'contact_search_members_action',
74 $this->refinery->byTrying([
75 $this->refinery->kindlyTo()->string(),
76 $this->refinery->always('')
77 ])
78 );
79 match ($action) {
80 'sendMailToSelectedUsers' => $this->sendMailToSelectedUsers(),
81 default => $this->ctrl->redirect($this, 'showSelectableUsers'),
82 };
83 }
84
85 public function executeCommand(): bool
86 {
87 $cmd = $this->ctrl->getCmd();
88
89 $this->ctrl->setReturn($this, '');
90
91 switch ($cmd) {
92 case 'handleSearchMembersActions':
94 break;
95
96 case 'showSelectableUsers':
97 $this->showSelectableUsers();
98 break;
99
100 case 'nextMailForm':
101 $this->nextMailForm();
102 break;
103
104 case 'cancel':
106 break;
107
108 default:
109 if (isset($this->httpRequest->getQueryParams()['returned_from_mail']) &&
110 $this->httpRequest->getQueryParams()['returned_from_mail'] === '1') {
112 }
113 $this->showSearchForm();
114 break;
115 }
116
117 return true;
118 }
119
120 private function redirectToParentReferer(): void
121 {
122 $url = $this->getStoredReferer();
123 $this->unsetStoredReferer();
124 $this->ctrl->redirectToURL($url);
125 }
126
127 public function storeReferer(): void
128 {
129 $back_link = $this->ctrl->getParentReturn($this);
130
131 if (isset($this->httpRequest->getServerParams()['HTTP_REFERER'])) {
132 $referer = $this->httpRequest->getServerParams()['HTTP_REFERER'];
133 $urlParts = parse_url($referer);
134 if (is_array($urlParts) && isset($urlParts['path'])) {
135 $url = ltrim(basename($urlParts['path']), '/');
136 if (isset($urlParts['query'])) {
137 $url .= '?' . $urlParts['query'];
138 }
139 if ($url !== '') {
140 $back_link = $url;
141 }
142 }
143 }
144
145 ilSession::set('ilMailMemberSearchGUIReferer', $back_link);
146 }
147
148 private function getStoredReferer(): string
149 {
150 return (string) ilSession::get('ilMailMemberSearchGUIReferer');
151 }
152
153 private function unsetStoredReferer(): void
154 {
155 ilSession::set('ilMailMemberSearchGUIReferer', '');
156 }
157
158 protected function nextMailForm(): void
159 {
160 $form = $this->initMailToMembersForm();
161 $form = $form->withRequest($this->http->request());
162 if (!$form->getError()) {
163 $data = $form->getData();
164
165 if (isset($data['mail_member_type']) && $data['mail_member_type'][0] === 'mail_member_roles') {
166 if ($data['mail_member_type'][1]) {
167 $role_mail_boxes = [];
168 $roles = $data['mail_member_type'][1];
169 foreach ($roles as $role_id => $enabled) {
170 if ($enabled) {
171 $role_mail_boxes[] = $this->objMailMemberRoles->getMailboxRoleAddress((int) $role_id);
172 }
173 }
174
175 ilSession::set('mail_roles', $role_mail_boxes);
176
177 $this->ctrl->redirectToURL(
179 $this,
180 'showSearchForm',
182 [
184 'rcp_to' => implode(',', $role_mail_boxes),
185 'sig' => $this->gui->createMailSignature()
186 ],
187 $this->generateContextArray()
188 )
189 );
190 } else {
191 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
192 $this->showSearchForm($form);
193
194 return;
195 }
196 } else {
197 $this->showSelectableUsers();
198
199 return;
200 }
201 }
202
203 $this->showSearchForm($form);
204 }
205
206 protected function generateContextArray(): array
207 {
208 $contextParameters = [];
209
210 $type = ilObject::_lookupType($this->ref_id, true);
211 switch ($type) {
212 case 'grp':
213 case 'crs':
214 if ($this->access->checkAccess('write', '', $this->ref_id)) {
215 $contextParameters = [
216 'ref_id' => $this->ref_id,
217 'ts' => time(),
219 ilObject::_lookupObjId($this->ref_id),
221 ''
222 )
223 ];
224
225 if ('crs' === $type) {
227 }
228 }
229 break;
230
231 case 'sess':
232 if ($this->access->checkAccess('write', '', $this->ref_id)) {
233 $contextParameters = [
235 'ref_id' => $this->ref_id,
236 'ts' => time()
237 ];
238 }
239 break;
240 }
241
242 return $contextParameters;
243 }
244
245 protected function showSelectableUsers(): void
246 {
247 $this->tpl->loadStandardTemplate();
248 $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
249 $tbl = new MailMemberSearchTable($this->ref_id, $provider, $this->ctrl, $this->lng, $this->ui_factory, $this->http);
250
251 $this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
252 }
253
254 protected function sendMailToSelectedUsers(): void
255 {
256 $selected_user_ids = $this->http->wrapper()->query()->retrieve(
257 'contact_search_members_user_ids',
258 $this->refinery->byTrying([
259 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
260 $this->refinery->always([])
261 ])
262 );
263
264 if ((string) current($selected_user_ids) === 'ALL_OBJECTS') {
265 $selected_user_ids = [];
266 $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
267 $entries = $provider->getData();
268
269 foreach ($entries as $entry) {
270 $selected_user_ids[] = (int) $entry['user_id'];
271 }
272 } else {
273 $selected_user_ids = array_map(intval(...), $selected_user_ids);
274 }
275
276 $rcps = [];
277 foreach ($selected_user_ids as $usr_id) {
278 $rcps[] = ilObjUser::_lookupLogin($usr_id);
279 }
280
281 if (array_filter($rcps) === []) {
282 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
283 $this->showSelectableUsers();
284 return;
285 }
286
288
289 $this->ctrl->redirectToURL(
291 $this,
292 'members',
293 [],
294 [
296 'sig' => $this->gui->createMailSignature(),
297 ],
298 $this->generateContextArray()
299 )
300 );
301 }
302
303 protected function showSearchForm(?StandardForm $form = null): void
304 {
305 $this->storeReferer();
306
307 if (!$form instanceof StandardForm) {
308 $form = $this->initMailToMembersForm();
309 }
310 $this->tpl->setContent($this->ui_renderer->render($form));
311 }
312
313 protected function getObjParticipants(): ?ilParticipants
314 {
316 }
317
319 {
320 $this->objParticipants = $objParticipants;
321 }
322
323 protected function initMailToMembersForm(): StandardForm
324 {
325 $this->lng->loadLanguageModule('mail');
326
327 return $this->ui_factory->input()->container()->form()->standard(
328 $this->ctrl->getFormAction($this, 'nextMailForm'),
329 [
330 'mail_member_type' => $this->getMailRadioGroup()
331 ]
332 );
333 }
334
338 private function getMailRoles(): array
339 {
340 return $this->mail_roles;
341 }
342
343 protected function getMailRadioGroup(): ILIAS\UI\Component\Input\Field\SwitchableGroup
344 {
345 $mail_roles = $this->getMailRoles();
346
347 $sub_items = [];
348 $values = [];
349 foreach ($mail_roles as $role) {
350 $chk_role = $this->ui_factory->input()->field()->checkbox($role['form_option_title'], $role['mailbox']);
351 $sub_items[$role['role_id']] = $chk_role;
352 if (isset($role['default_checked']) && $role['default_checked']) {
353 $values[$role['role_id']] = true;
354 } else {
355 $values[$role['role_id']] = false;
356 }
357 }
358
359 return $this->ui_factory->input()->field()->switchableGroup(
360 [
361 'mail_sel_users' => $this->ui_factory->input()->field()->group([], $this->lng->txt('mail_sel_users')),
362 'mail_member_roles' => $this->ui_factory->input()->field()->group(
363 $sub_items,
364 $this->objMailMemberRoles->getRadioOptionTitle()
365 )
366 ],
367 $this->lng->txt('mail_sel_label')
368 )->withValue(
369 [
370 'mail_member_roles',
371 $values
372 ]
373 );
374 }
375
376 public function getUnsafeGetCommands(): array
377 {
378 return [
379 'handleSearchMembersActions'
380 ];
381 }
382
383 public function getSafePostCommands(): array
384 {
385 return [];
386 }
387}
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
Class ilAbstractMailMemberRoles.
static _lookupContainerSetting(int $a_id, string $a_keyword, ?string $a_default_value=null)
language handling
static getRedirectTarget( $gui, string $cmd, array $gui_params=[], array $mail_params=[], array $context_params=[])
final const string CONTEXT_KEY
static setRecipients(array $recipients, string $type='to')
final const string MAIL_FORM_TYPE_NEW
final const string MAIL_FORM_TYPE_ROLE
Class ilMailMemberSearchDataProvider.
__construct(private readonly object $gui, public int $ref_id, private readonly ilAbstractMailMemberRoles $objMailMemberRoles)
getSafePostCommands()
This method must return a list of safe POST commands.
readonly ilCtrlInterface $ctrl
showSearchForm(?StandardForm $form=null)
readonly ilGlobalTemplateInterface $tpl
getUnsafeGetCommands()
This method must return a list of unsafe GET commands.
readonly ilAccessHandler $access
readonly ILIAS UI Factory $ui_factory
setObjParticipants(ilParticipants $objParticipants)
readonly ServerRequestInterface $httpRequest
const string PROP_CONTEXT_SUBJECT_PREFIX
static _lookupLogin(int $a_user_id)
static _lookupType(int $id, bool $reference=false)
static _lookupObjId(int $ref_id)
Base class for course and group participants.
static get(string $a_var)
static set(string $a_var, $a_val)
Set a value.
This describes a standard form.
Definition: Standard.php:29
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
$provider
Definition: ltitoken.php:80
static http()
Fetches the global http state from ILIAS.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:68