ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMailMemberSearchGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 {
30  private readonly ServerRequestInterface $httpRequest;
32  private readonly array $mail_roles;
34  private readonly ilCtrlInterface $ctrl;
35  private readonly ilGlobalTemplateInterface $tpl;
36  private readonly ilLanguage $lng;
37  private readonly ilAccessHandler $access;
38 
44  public function __construct(private readonly object $gui, public int $ref_id, private readonly ilAbstractMailMemberRoles $objMailMemberRoles)
45  {
46  global $DIC;
47 
48  $this->ctrl = $DIC['ilCtrl'];
49  $this->tpl = $DIC['tpl'];
50  $this->lng = $DIC['lng'];
51  $this->access = $DIC['ilAccess'];
52  $this->httpRequest = $DIC->http()->request();
53 
54  $this->lng->loadLanguageModule('mail');
55  $this->lng->loadLanguageModule('search');
56  $this->mail_roles = $objMailMemberRoles->getMailRoles($ref_id);
57  }
58 
59 
60  public function executeCommand(): bool
61  {
62  $cmd = $this->ctrl->getCmd();
63 
64  $this->ctrl->setReturn($this, '');
65 
66  switch ($cmd) {
67  case 'sendMailToSelectedUsers':
68  $this->sendMailToSelectedUsers();
69  break;
70 
71  case 'showSelectableUsers':
72  $this->showSelectableUsers();
73  break;
74 
75  case 'nextMailForm':
76  $this->nextMailForm();
77  break;
78 
79  case 'cancel':
80  $this->redirectToParentReferer();
81  break;
82 
83  default:
84  if (isset($this->httpRequest->getQueryParams()['returned_from_mail']) && $this->httpRequest->getQueryParams()['returned_from_mail'] === '1') {
85  $this->redirectToParentReferer();
86  }
87  $this->showSearchForm();
88  break;
89  }
90 
91  return true;
92  }
93 
94  private function redirectToParentReferer(): void
95  {
96  $url = $this->getStoredReferer();
97  $this->unsetStoredReferer();
98  $this->ctrl->redirectToURL($url);
99  }
100 
101  public function storeReferer(): void
102  {
103  $back_link = $this->ctrl->getParentReturn($this);
104 
105  if (isset($this->httpRequest->getServerParams()['HTTP_REFERER'])) {
106  $referer = $this->httpRequest->getServerParams()['HTTP_REFERER'];
107  $urlParts = parse_url($referer);
108 
109  if (isset($urlParts['path'])) {
110  $url = ltrim(basename($urlParts['path']), '/');
111  if (isset($urlParts['query'])) {
112  $url .= '?' . $urlParts['query'];
113  }
114  if ($url !== '') {
115  $back_link = $url;
116  }
117  }
118  }
119 
120  ilSession::set('ilMailMemberSearchGUIReferer', $back_link);
121  }
122 
123  private function getStoredReferer(): string
124  {
125  return (string) ilSession::get('ilMailMemberSearchGUIReferer');
126  }
127 
128  private function unsetStoredReferer(): void
129  {
130  ilSession::set('ilMailMemberSearchGUIReferer', '');
131  }
132 
133  protected function nextMailForm(): void
134  {
135  $form = $this->initMailToMembersForm();
136  if ($form->checkInput()) {
137  if ($form->getInput('mail_member_type') === 'mail_member_roles') {
138  if (is_array($form->getInput('roles')) && $form->getInput('roles') !== []) {
139  $role_mail_boxes = [];
140  $roles = $form->getInput('roles');
141  foreach ($roles as $role_id) {
142  $mailbox = $this->objMailMemberRoles->getMailboxRoleAddress((int) $role_id);
143  $role_mail_boxes[] = $mailbox;
144  }
145 
146  ilSession::set('mail_roles', $role_mail_boxes);
147 
148  $this->ctrl->redirectToURL(ilMailFormCall::getRedirectTarget(
149  $this,
150  'showSearchForm',
152  [
154  'rcp_to' => implode(',', $role_mail_boxes),
155  'sig' => $this->gui->createMailSignature()
156  ],
157  $this->generateContextArray()
158  ));
159  } else {
160  $form->setValuesByPost();
161  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('no_checkbox'));
162  $this->showSearchForm();
163  return;
164  }
165  } else {
166  $this->showSelectableUsers();
167  return;
168  }
169  }
170 
171  $form->setValuesByPost();
172  $this->showSearchForm();
173  }
174 
175  protected function generateContextArray(): array
176  {
177  $contextParameters = [];
178 
179  $type = ilObject::_lookupType($this->ref_id, true);
180  switch ($type) {
181  case 'grp':
182  case 'crs':
183  if ($this->access->checkAccess('write', '', $this->ref_id)) {
184  $contextParameters = [
185  'ref_id' => $this->ref_id,
186  'ts' => time(),
188  ilObject::_lookupObjId($this->ref_id),
190  ''
191  )
192  ];
193 
194  if ('crs' === $type) {
196  }
197  }
198  break;
199 
200  case 'sess':
201  if ($this->access->checkAccess('write', '', $this->ref_id)) {
202  $contextParameters = [
204  'ref_id' => $this->ref_id,
205  'ts' => time()
206  ];
207  }
208  break;
209  }
210 
211  return $contextParameters;
212  }
213 
214  protected function showSelectableUsers(): void
215  {
216  $this->tpl->loadStandardTemplate();
217  $tbl = new ilMailMemberSearchTableGUI($this, 'showSelectableUsers');
218  $provider = new ilMailMemberSearchDataProvider($this->getObjParticipants(), $this->ref_id);
219  $tbl->setData($provider->getData());
220 
221  $this->tpl->setContent($tbl->getHTML());
222  }
223 
224 
225  protected function sendMailToSelectedUsers(): void
226  {
227  if (!isset($this->httpRequest->getParsedBody()['user_ids']) || !is_array($this->httpRequest->getParsedBody()['user_ids']) || [] === $this->httpRequest->getParsedBody()['user_ids']) {
228  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("no_checkbox"));
229  $this->showSelectableUsers();
230  return;
231  }
232 
233  $rcps = [];
234  foreach ($this->httpRequest->getParsedBody()['user_ids'] as $usr_id) {
235  $rcps[] = ilObjUser::_lookupLogin((int) $usr_id);
236  }
237 
238  if (array_filter($rcps) === []) {
239  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("no_checkbox"));
240  $this->showSelectableUsers();
241  return;
242  }
243 
245 
246  $this->ctrl->redirectToURL(ilMailFormCall::getRedirectTarget(
247  $this,
248  'members',
249  [],
250  [
252  'sig' => $this->gui->createMailSignature(),
253  ],
254  $this->generateContextArray()
255  ));
256  }
257 
258  protected function showSearchForm(): void
259  {
260  $this->storeReferer();
261 
262  $form = $this->initMailToMembersForm();
263  $this->tpl->setContent($form->getHTML());
264  }
265 
266 
267  protected function getObjParticipants(): ?ilParticipants
268  {
269  return $this->objParticipants;
270  }
271 
272  public function setObjParticipants(ilParticipants $objParticipants): void
273  {
274  $this->objParticipants = $objParticipants;
275  }
276 
277 
279  {
280  $this->lng->loadLanguageModule('mail');
281 
282  $form = new ilPropertyFormGUI();
283  $form->setTitle($this->lng->txt('mail_members'));
284 
285  $form->setFormAction($this->ctrl->getFormAction($this, 'nextMailForm'));
286 
287  $radio_grp = $this->getMailRadioGroup();
288 
289  $form->addItem($radio_grp);
290  $form->addCommandButton('nextMailForm', $this->lng->txt('mail_members_search_continue'));
291  $form->addCommandButton('cancel', $this->lng->txt('cancel'));
292 
293  return $form;
294  }
295 
299  private function getMailRoles(): array
300  {
301  return $this->mail_roles;
302  }
303 
304 
306  {
307  $mail_roles = $this->getMailRoles();
308 
309  $radio_grp = new ilRadioGroupInputGUI($this->lng->txt('mail_sel_label'), 'mail_member_type');
310 
311  $radio_sel_users = new ilRadioOption($this->lng->txt('mail_sel_users'), 'mail_sel_users');
312 
313  $radio_roles = new ilRadioOption($this->objMailMemberRoles->getRadioOptionTitle(), 'mail_member_roles');
314  foreach ($mail_roles as $role) {
315  $chk_role = new ilCheckboxInputGUI($role['form_option_title'], 'roles[' . $role['role_id'] . ']');
316 
317  if (isset($role['default_checked']) && $role['default_checked']) {
318  $chk_role->setChecked(true);
319  }
320  $chk_role->setValue((string) $role['role_id']);
321  $chk_role->setInfo($role['mailbox']);
322  $radio_roles->addSubItem($chk_role);
323  }
324 
325  $radio_grp->setValue('mail_member_roles');
326 
327  $radio_grp->addOption($radio_sel_users);
328  $radio_grp->addOption($radio_roles);
329 
330  return $radio_grp;
331  }
332 }
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
Class ilMailMemberSearchGUI.
readonly ilAccessHandler $access
static _lookupContainerSetting(int $a_id, string $a_keyword, string $a_default_value=null)
static _lookupObjId(int $ref_id)
global $DIC
Definition: feed.php:28
$provider
Definition: ltitoken.php:83
$ref_id
Definition: ltiauth.php:67
This class represents a property in a property form.
readonly ilGlobalTemplateInterface $tpl
$url
Definition: ltiregstart.php:35
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=[])
Class ilMailMemberSearchDataProvider.
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)
ilMailMemberSearchGUI constructor.
static _lookupLogin(int $a_user_id)