ILIAS  trunk Revision v12.0_alpha-1329-g1094ddb0c33
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 }
197
198 $this->ctrl->redirect($this, 'showSelectableUsers');
199 }
200
201 $this->showSearchForm($form);
202 }
203
204 protected function generateContextArray(): array
205 {
206 $contextParameters = [];
207
208 $type = ilObject::_lookupType($this->ref_id, true);
209 switch ($type) {
210 case 'grp':
211 case 'crs':
212 if ($this->access->checkAccess('write', '', $this->ref_id)) {
213 $contextParameters = [
214 'ref_id' => $this->ref_id,
215 'ts' => time(),
217 ilObject::_lookupObjId($this->ref_id),
219 ''
220 )
221 ];
222
223 if ('crs' === $type) {
225 }
226 }
227 break;
228
229 case 'sess':
230 if ($this->access->checkAccess('write', '', $this->ref_id)) {
231 $contextParameters = [
233 'ref_id' => $this->ref_id,
234 'ts' => time()
235 ];
236 }
237 break;
238 }
239
240 return $contextParameters;
241 }
242
243 protected function showSelectableUsers(): void
244 {
245 $this->tpl->loadStandardTemplate();
246 $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
247 $tbl = new MailMemberSearchTable($this->ref_id, $provider, $this->ctrl, $this->lng, $this->ui_factory, $this->http);
248
249 $this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
250 }
251
252 protected function sendMailToSelectedUsers(): void
253 {
254 $selected_user_ids = $this->http->wrapper()->query()->retrieve(
255 'contact_search_members_user_ids',
256 $this->refinery->byTrying([
257 $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
258 $this->refinery->always([])
259 ])
260 );
261
262 if ((string) current($selected_user_ids) === 'ALL_OBJECTS') {
263 $selected_user_ids = [];
264 $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
265 $entries = $provider->getData();
266
267 foreach ($entries as $entry) {
268 $selected_user_ids[] = (int) $entry['user_id'];
269 }
270 } else {
271 $selected_user_ids = array_map(intval(...), $selected_user_ids);
272 }
273
274 $rcps = [];
275 foreach ($selected_user_ids as $usr_id) {
276 $rcps[] = ilObjUser::_lookupLogin($usr_id);
277 }
278
279 if (array_filter($rcps) === []) {
280 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
281 $this->showSelectableUsers();
282 return;
283 }
284
286
287 $this->ctrl->redirectToURL(
289 $this,
290 'members',
291 [],
292 [
294 'sig' => $this->gui->createMailSignature(),
295 ],
296 $this->generateContextArray()
297 )
298 );
299 }
300
301 protected function showSearchForm(?StandardForm $form = null): void
302 {
303 $this->storeReferer();
304
305 if (!$form instanceof StandardForm) {
306 $form = $this->initMailToMembersForm();
307 }
308 $this->tpl->setContent($this->ui_renderer->render($form));
309 }
310
311 protected function getObjParticipants(): ?ilParticipants
312 {
314 }
315
317 {
318 $this->objParticipants = $objParticipants;
319 }
320
321 protected function initMailToMembersForm(): StandardForm
322 {
323 $this->lng->loadLanguageModule('mail');
324
325 return $this->ui_factory->input()->container()->form()->standard(
326 $this->ctrl->getFormAction($this, 'nextMailForm'),
327 [
328 'mail_member_type' => $this->getMailRadioGroup()
329 ]
330 );
331 }
332
336 private function getMailRoles(): array
337 {
338 return $this->mail_roles;
339 }
340
341 protected function getMailRadioGroup(): ILIAS\UI\Component\Input\Field\SwitchableGroup
342 {
343 $mail_roles = $this->getMailRoles();
344
345 $sub_items = [];
346 $values = [];
347 foreach ($mail_roles as $role) {
348 $chk_role = $this->ui_factory->input()->field()->checkbox($role['form_option_title'], $role['mailbox']);
349 $sub_items[$role['role_id']] = $chk_role;
350 if (isset($role['default_checked']) && $role['default_checked']) {
351 $values[$role['role_id']] = true;
352 } else {
353 $values[$role['role_id']] = false;
354 }
355 }
356
357 return $this->ui_factory->input()->field()->switchableGroup(
358 [
359 'mail_sel_users' => $this->ui_factory->input()->field()->group([], $this->lng->txt('mail_sel_users')),
360 'mail_member_roles' => $this->ui_factory->input()->field()->group(
361 $sub_items,
362 $this->objMailMemberRoles->getRadioOptionTitle()
363 )
364 ],
365 $this->lng->txt('mail_sel_label')
366 )->withValue(
367 [
368 'mail_member_roles',
369 $values
370 ]
371 );
372 }
373
374 public function getUnsafeGetCommands(): array
375 {
376 return [
377 'handleSearchMembersActions'
378 ];
379 }
380
381 public function getSafePostCommands(): array
382 {
383 return [];
384 }
385}
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:30
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:70