ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMailMemberSearchGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
28 {
31  private readonly array $mail_roles;
33  private readonly ilCtrlInterface $ctrl;
34  private readonly ilGlobalTemplateInterface $tpl;
35  private readonly ilLanguage $lng;
36  private readonly ilAccessHandler $access;
37  private readonly \ILIAS\UI\Factory $ui_factory;
38  private readonly Services $http;
39  private readonly Renderer $ui_renderer;
40  private readonly Factory $refinery;
41 
46  public function __construct(
47  private readonly object $gui,
48  public int $ref_id,
49  private readonly ilAbstractMailMemberRoles $objMailMemberRoles
50  ) {
51  global $DIC;
52 
53  $this->ctrl = $DIC['ilCtrl'];
54  $this->tpl = $DIC['tpl'];
55  $this->lng = $DIC['lng'];
56  $this->access = $DIC['ilAccess'];
57  $this->httpRequest = $DIC->http()->request();
58 
59  $this->ui_factory = $DIC->ui()->factory();
60  $this->ui_renderer = $DIC->ui()->renderer();
61  $this->refinery = $DIC->refinery();
62  $this->http = $DIC->http();
63 
64  $this->lng->loadLanguageModule('mail');
65  $this->lng->loadLanguageModule('search');
66  $this->mail_roles = $objMailMemberRoles->getMailRoles($ref_id);
67  }
68 
69  private function handleSearchMembersActions(): void
70  {
71  $action = $this->http->wrapper()->query()->retrieve(
72  'contact_search_members_action',
73  $this->refinery->byTrying([
74  $this->refinery->kindlyTo()->string(),
75  $this->refinery->always('')
76  ])
77  );
78  match ($action) {
79  'sendMailToSelectedUsers' => $this->sendMailToSelectedUsers(),
80  default => $this->ctrl->redirect($this, 'showSelectableUsers'),
81  };
82  }
83 
84  public function executeCommand(): bool
85  {
86  $cmd = $this->ctrl->getCmd();
87 
88  $this->ctrl->setReturn($this, '');
89 
90  switch ($cmd) {
91  case 'handleSearchMembersActions':
93  break;
94 
95  case 'showSelectableUsers':
96  $this->showSelectableUsers();
97  break;
98 
99  case 'nextMailForm':
100  $this->nextMailForm();
101  break;
102 
103  case 'cancel':
104  $this->redirectToParentReferer();
105  break;
106 
107  default:
108  if (isset($this->httpRequest->getQueryParams()['returned_from_mail']) &&
109  $this->httpRequest->getQueryParams()['returned_from_mail'] === '1') {
110  $this->redirectToParentReferer();
111  }
112  $this->showSearchForm();
113  break;
114  }
115 
116  return true;
117  }
118 
119  private function redirectToParentReferer(): void
120  {
121  $url = $this->getStoredReferer();
122  $this->unsetStoredReferer();
123  $this->ctrl->redirectToURL($url);
124  }
125 
126  public function storeReferer(): void
127  {
128  $back_link = $this->ctrl->getParentReturn($this);
129 
130  if (isset($this->httpRequest->getServerParams()['HTTP_REFERER'])) {
131  $referer = $this->httpRequest->getServerParams()['HTTP_REFERER'];
132  $urlParts = parse_url($referer);
133  if (is_array($urlParts) && isset($urlParts['path'])) {
134  $url = ltrim(basename($urlParts['path']), '/');
135  if (isset($urlParts['query'])) {
136  $url .= '?' . $urlParts['query'];
137  }
138  if ($url !== '') {
139  $back_link = $url;
140  }
141  }
142  }
143 
144  ilSession::set('ilMailMemberSearchGUIReferer', $back_link);
145  }
146 
147  private function getStoredReferer(): string
148  {
149  return (string) ilSession::get('ilMailMemberSearchGUIReferer');
150  }
151 
152  private function unsetStoredReferer(): void
153  {
154  ilSession::set('ilMailMemberSearchGUIReferer', '');
155  }
156 
157  protected function nextMailForm(): void
158  {
159  $form = $this->initMailToMembersForm();
160  if ($form->checkInput()) {
161  if ($form->getInput('mail_member_type') === 'mail_member_roles') {
162  if (is_array($form->getInput('roles')) && $form->getInput('roles') !== []) {
163  $role_mail_boxes = [];
164  $roles = $form->getInput('roles');
165  foreach ($roles as $role_id) {
166  $mailbox = $this->objMailMemberRoles->getMailboxRoleAddress((int) $role_id);
167  $role_mail_boxes[] = $mailbox;
168  }
169 
170  ilSession::set('mail_roles', $role_mail_boxes);
171 
172  $this->ctrl->redirectToURL(
174  $this,
175  'showSearchForm',
177  [
179  'rcp_to' => implode(',', $role_mail_boxes),
180  'sig' => $this->gui->createMailSignature()
181  ],
182  $this->generateContextArray()
183  )
184  );
185  } else {
186  $form->setValuesByPost();
187  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
188  $this->showSearchForm();
189  return;
190  }
191  } else {
192  $this->showSelectableUsers();
193  return;
194  }
195  }
196 
197  $form->setValuesByPost();
198  $this->showSearchForm();
199  }
200 
201  protected function generateContextArray(): array
202  {
203  $contextParameters = [];
204 
205  $type = ilObject::_lookupType($this->ref_id, true);
206  switch ($type) {
207  case 'grp':
208  case 'crs':
209  if ($this->access->checkAccess('write', '', $this->ref_id)) {
210  $contextParameters = [
211  'ref_id' => $this->ref_id,
212  'ts' => time(),
214  ilObject::_lookupObjId($this->ref_id),
216  ''
217  )
218  ];
219 
220  if ('crs' === $type) {
222  }
223  }
224  break;
225 
226  case 'sess':
227  if ($this->access->checkAccess('write', '', $this->ref_id)) {
228  $contextParameters = [
230  'ref_id' => $this->ref_id,
231  'ts' => time()
232  ];
233  }
234  break;
235  }
236 
237  return $contextParameters;
238  }
239 
240  protected function showSelectableUsers(): void
241  {
242  $this->tpl->loadStandardTemplate();
243  $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
244  $tbl = new MailMemberSearchTable($this->ref_id, $provider, $this->ctrl, $this->lng, $this->ui_factory, $this->http);
245 
246  $this->tpl->setContent($this->ui_renderer->render($tbl->getComponent()));
247  }
248 
249  protected function sendMailToSelectedUsers(): void
250  {
251  $selected_user_ids = $this->http->wrapper()->query()->retrieve(
252  'contact_search_members_user_ids',
253  $this->refinery->byTrying([
254  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string()),
255  $this->refinery->always([])
256  ])
257  );
258 
259  if ((string) current($selected_user_ids) === 'ALL_OBJECTS') {
260  $selected_user_ids = [];
261  $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
262  $entries = $provider->getData();
263 
264  foreach ($entries as $entry) {
265  $selected_user_ids[] = (int) $entry['user_id'];
266  }
267  } else {
268  $selected_user_ids = array_map(intval(...), $selected_user_ids);
269  }
270 
271  $rcps = [];
272  foreach ($selected_user_ids as $usr_id) {
273  $rcps[] = ilObjUser::_lookupLogin($usr_id);
274  }
275 
276  if (array_filter($rcps) === []) {
277  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
278  $this->showSelectableUsers();
279  return;
280  }
281 
283 
284  $this->ctrl->redirectToURL(
286  $this,
287  'members',
288  [],
289  [
291  'sig' => $this->gui->createMailSignature(),
292  ],
293  $this->generateContextArray()
294  )
295  );
296  }
297 
298  protected function showSearchForm(): void
299  {
300  $this->storeReferer();
301 
302  $form = $this->initMailToMembersForm();
303  $this->tpl->setContent($form->getHTML());
304  }
305 
306  protected function getObjParticipants(): ?ilParticipants
307  {
308  return $this->objParticipants;
309  }
310 
311  public function setObjParticipants(ilParticipants $objParticipants): void
312  {
313  $this->objParticipants = $objParticipants;
314  }
315 
317  {
318  $this->lng->loadLanguageModule('mail');
319 
320  $form = new ilPropertyFormGUI();
321  $form->setTitle($this->lng->txt('mail_members'));
322 
323  $form->setFormAction($this->ctrl->getFormAction($this, 'nextMailForm'));
324 
325  $radio_grp = $this->getMailRadioGroup();
326 
327  $form->addItem($radio_grp);
328  $form->addCommandButton('nextMailForm', $this->lng->txt('mail_members_search_continue'));
329  $form->addCommandButton('cancel', $this->lng->txt('cancel'));
330 
331  return $form;
332  }
333 
337  private function getMailRoles(): array
338  {
339  return $this->mail_roles;
340  }
341 
343  {
344  $mail_roles = $this->getMailRoles();
345 
346  $radio_grp = new ilRadioGroupInputGUI($this->lng->txt('mail_sel_label'), 'mail_member_type');
347 
348  $radio_sel_users = new ilRadioOption($this->lng->txt('mail_sel_users'), 'mail_sel_users');
349 
350  $radio_roles = new ilRadioOption($this->objMailMemberRoles->getRadioOptionTitle(), 'mail_member_roles');
351  foreach ($mail_roles as $role) {
352  $chk_role = new ilCheckboxInputGUI($role['form_option_title'], 'roles[' . $role['role_id'] . ']');
353 
354  if (isset($role['default_checked']) && $role['default_checked']) {
355  $chk_role->setChecked(true);
356  }
357  $chk_role->setValue((string) $role['role_id']);
358  $chk_role->setInfo($role['mailbox']);
359  $radio_roles->addSubItem($chk_role);
360  }
361 
362  $radio_grp->setValue('mail_member_roles');
363 
364  $radio_grp->addOption($radio_sel_users);
365  $radio_grp->addOption($radio_roles);
366 
367  return $radio_grp;
368  }
369 }
static get(string $a_var)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
readonly ilCtrlInterface $ctrl
const PROP_CONTEXT_SUBJECT_PREFIX
$url
Definition: shib_logout.php:63
readonly ilAccessHandler $access
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
static _lookupObjId(int $ref_id)
$provider
Definition: ltitoken.php:80
static http()
Fetches the global http state from ILIAS.
$ref_id
Definition: ltiauth.php:66
This class represents a property in a property form.
readonly ILIAS UI Factory $ui_factory
readonly ilGlobalTemplateInterface $tpl
global $DIC
Definition: shib_login.php:25
setObjParticipants(ilParticipants $objParticipants)
Base class for course and group participants.
static setRecipients(array $recipients, string $type='to')
final const MAIL_FORM_TYPE_NEW
readonly ServerRequestInterface $httpRequest
static getRedirectTarget( $gui, string $cmd, array $gui_params=[], array $mail_params=[], array $context_params=[])
static _lookupType(int $id, bool $reference=false)
final const MAIL_FORM_TYPE_ROLE
Class ilAbstractMailMemberRoles.
static set(string $a_var, $a_val)
Set a value.
__construct(private readonly object $gui, public int $ref_id, private readonly ilAbstractMailMemberRoles $objMailMemberRoles)
static _lookupLogin(int $a_user_id)