ILIAS  release_8 Revision v8.23
class.ilContactGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 {
29  public const CONTACTS_VIEW_GALLERY = 1;
30  public const CONTACTS_VIEW_TABLE = 2;
31  private \ILIAS\HTTP\GlobalHttpState $http;
35  private ?array $postUsrId = null;
36 
39  protected ilLanguage $lng;
40  protected ilTabsGUI $tabs_gui;
41  protected ilHelpGUI $help;
43  protected ilFormatMail $umail;
44  protected ilObjUser $user;
47  protected bool $has_sub_tabs = false;
49  protected \ILIAS\UI\Factory $ui_factory;
50  protected \ILIAS\UI\Renderer $ui_renderer;
51 
52  public function __construct()
53  {
54  global $DIC;
55 
56  $this->tpl = $DIC['tpl'];
57  $this->ctrl = $DIC['ilCtrl'];
58  $this->lng = $DIC['lng'];
59  $this->tabs_gui = $DIC['ilTabs'];
60  $this->help = $DIC['ilHelp'];
61  $this->toolbar = $DIC['ilToolbar'];
62  $this->user = $DIC['ilUser'];
63  $this->error = $DIC['ilErr'];
64  $this->rbacsystem = $DIC['rbacsystem'];
65  $this->http = $DIC->http();
66  $this->refinery = $DIC->refinery();
67  $this->ui_factory = $DIC->ui()->factory();
68  $this->ui_renderer = $DIC->ui()->renderer();
69 
70  $this->ctrl->saveParameter($this, "mobj_id");
71 
72  $this->umail = new ilFormatMail($this->user->getId());
73  $this->lng->loadLanguageModule('buddysystem');
74  }
75 
76  public function executeCommand(): bool
77  {
78  $this->showSubTabs();
79 
80  $forward_class = $this->ctrl->getNextClass($this);
81 
82  $this->umail->persistToStage($this->user->getId(), [], '', '', '', '', '', false);
83 
84  switch (strtolower($forward_class)) {
85  case strtolower(ilMailSearchCoursesGUI::class):
86  $this->activateTab('mail_my_courses');
87 
88  $this->ctrl->setReturn($this, "showContacts");
89  $this->ctrl->forwardCommand(new ilMailSearchCoursesGUI());
90  break;
91 
92  case strtolower(ilMailSearchGroupsGUI::class):
93  $this->activateTab('mail_my_groups');
94 
95  $this->ctrl->setReturn($this, "showContacts");
96  $this->ctrl->forwardCommand(new ilMailSearchGroupsGUI());
97  break;
98 
99  case strtolower(ilMailingListsGUI::class):
100  $this->activateTab('mail_my_mailing_lists');
101 
102  $this->ctrl->setReturn($this, "showContacts");
103  $this->ctrl->forwardCommand(new ilMailingListsGUI());
104  break;
105 
106  case strtolower(ilUsersGalleryGUI::class):
107  if (!ilBuddySystem::getInstance()->isEnabled()) {
108  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
109  }
110 
111  $this->tabs_gui->activateSubTab('buddy_view_gallery');
112  $this->activateTab('my_contacts');
113  $this->ctrl->forwardCommand(new ilUsersGalleryGUI(new ilUsersGalleryContacts()));
114  $this->tpl->printToStdout();
115  break;
116 
117  case strtolower(ilPublicUserProfileGUI::class):
118  $profile_gui = new ilPublicUserProfileGUI(
119  $this->http->wrapper()->query()->retrieve('user', $this->refinery->kindlyTo()->int())
120  );
121  $profile_gui->setBackUrl($this->ctrl->getLinkTarget($this, 'showContacts'));
122  $this->ctrl->forwardCommand($profile_gui);
123  $this->tpl->printToStdout();
124  break;
125 
126  default:
127  $this->activateTab('mail_my_entries');
128 
129  if (!($cmd = $this->ctrl->getCmd())) {
130  if (ilBuddySystem::getInstance()->isEnabled()) {
131  $cmd = 'showContacts';
132  } else {
133  $this->ctrl->redirectByClass(ilMailSearchCoursesGUI::class);
134  }
135  }
136 
137  $this->$cmd();
138  break;
139  }
140  return true;
141  }
142 
143 
144  private function showSubTabs(): void
145  {
146  $galleryCmdClasses = array_map('strtolower', [ilUsersGalleryGUI::class, self::class]);
147  if ($this->tabs_gui->hasTabs()) {
148  if (ilBuddySystem::getInstance()->isEnabled()) {
149  $this->tabs_gui->addSubTab(
150  'my_contacts',
151  $this->lng->txt('my_contacts'),
152  $this->ctrl->getLinkTarget($this)
153  );
154 
155  if (in_array(strtolower($this->ctrl->getCmdClass()), $galleryCmdClasses, true)) {
156  $view_selection = new ilSelectInputGUI('', 'contacts_view');
157  $view_selection->setOptions([
158  (string) self::CONTACTS_VIEW_TABLE => $this->lng->txt('buddy_view_table'),
159  (string) self::CONTACTS_VIEW_GALLERY => $this->lng->txt('buddy_view_gallery')
160  ]);
161  $view_selection->setValue(
162  strtolower($this->ctrl->getCmdClass()) === strtolower(ilUsersGalleryGUI::class)
163  ? (string) self::CONTACTS_VIEW_GALLERY
164  : (string) self::CONTACTS_VIEW_TABLE
165  );
166  $this->toolbar->addInputItem($view_selection);
167 
168  $contact_view_btn = ilSubmitButton::getInstance();
169  $contact_view_btn->setCaption('show');
170  $contact_view_btn->setCommand('changeContactsView');
171  $this->toolbar->addButtonInstance($contact_view_btn);
172  $this->toolbar->setFormAction($this->ctrl->getFormAction($this, 'changeContactsView'));
173  }
174 
175  if (
176  count(ilBuddyList::getInstanceByGlobalUser()->getLinkedRelations()) > 0 ||
177  (new ilMailingLists($this->user))->hasAny()
178  ) {
179  $this->tabs_gui->addSubTab(
180  'mail_my_mailing_lists',
181  $this->lng->txt('mail_my_mailing_lists'),
182  $this->ctrl->getLinkTargetByClass(ilMailingListsGUI::class)
183  );
184  }
185  }
186 
187  $this->tabs_gui->addSubTab(
188  'mail_my_courses',
189  $this->lng->txt('mail_my_courses'),
190  $this->ctrl->getLinkTargetByClass(ilMailSearchCoursesGUI::class)
191  );
192  $this->tabs_gui->addSubTab(
193  'mail_my_groups',
194  $this->lng->txt('mail_my_groups'),
195  $this->ctrl->getLinkTargetByClass(ilMailSearchGroupsGUI::class)
196  );
197  $this->has_sub_tabs = true;
198  } else {
199  $this->tpl->setTitleIcon(ilUtil::getImagePath('icon_cadm.svg'));
200 
201  $this->help->setScreenIdComponent('contacts');
202 
203  if (ilBuddySystem::getInstance()->isEnabled()) {
204  $this->tabs_gui->addTab(
205  'my_contacts',
206  $this->lng->txt('my_contacts'),
207  $this->ctrl->getLinkTarget($this)
208  );
209 
210  if (in_array(strtolower($this->ctrl->getCmdClass()), $galleryCmdClasses, true)) {
211  $this->tabs_gui->addSubTab(
212  'buddy_view_table',
213  $this->lng->txt('buddy_view_table'),
214  $this->ctrl->getLinkTarget($this)
215  );
216  $this->tabs_gui->addSubTab(
217  'buddy_view_gallery',
218  $this->lng->txt('buddy_view_gallery'),
219  $this->ctrl->getLinkTargetByClass(ilUsersGalleryGUI::class)
220  );
221  }
222 
223  if (
224  count(ilBuddyList::getInstanceByGlobalUser()->getLinkedRelations()) > 0 ||
225  (new ilMailingLists($this->user))->hasAny()
226  ) {
227  $this->tabs_gui->addTab(
228  'mail_my_mailing_lists',
229  $this->lng->txt('mail_my_mailing_lists'),
230  $this->ctrl->getLinkTargetByClass(ilMailingListsGUI::class)
231  );
232  }
233  }
234 
235  $this->tabs_gui->addTab(
236  'mail_my_courses',
237  $this->lng->txt('mail_my_courses'),
238  $this->ctrl->getLinkTargetByClass(ilMailSearchCoursesGUI::class)
239  );
240  $this->tabs_gui->addTab(
241  'mail_my_groups',
242  $this->lng->txt('mail_my_groups'),
243  $this->ctrl->getLinkTargetByClass(ilMailSearchGroupsGUI::class)
244  );
245  }
246  }
247 
248  protected function activateTab(string $a_id): void
249  {
250  if ($this->has_sub_tabs) {
251  $this->tabs_gui->activateSubTab($a_id);
252  } else {
253  $this->tabs_gui->activateTab($a_id);
254  }
255  }
256 
260  protected function changeContactsView(): void
261  {
262  if (!ilBuddySystem::getInstance()->isEnabled()) {
263  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
264  }
265 
266  if ($this->http->wrapper()->post()->has('contacts_view')) {
267  switch ($this->http->wrapper()->post()->retrieve('contacts_view', $this->refinery->kindlyTo()->int())) {
268  case self::CONTACTS_VIEW_GALLERY:
269  $this->ctrl->redirectByClass(ilUsersGalleryGUI::class);
270 
271  // no break
272  case self::CONTACTS_VIEW_TABLE:
273  $this->ctrl->redirect($this);
274  }
275  }
276 
277  $this->ctrl->redirect($this);
278  }
279 
280 
281  protected function applyContactsTableFilter(): void
282  {
283  if (!ilBuddySystem::getInstance()->isEnabled()) {
284  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
285  }
286 
287  $table = new ilBuddySystemRelationsTableGUI($this, 'showContacts');
288 
289  $table->resetOffset();
290  $table->writeFilterToSession();
291 
292  $this->showContacts();
293  }
294 
295 
296  protected function resetContactsTableFilter(): void
297  {
298  if (!ilBuddySystem::getInstance()->isEnabled()) {
299  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
300  }
301 
302  $table = new ilBuddySystemRelationsTableGUI($this, 'showContacts');
303 
304  $table->resetOffset();
305  $table->resetFilter();
306 
307  $this->showContacts();
308  }
309 
310 
311  protected function showContacts(): void
312  {
313  if (!ilBuddySystem::getInstance()->isEnabled()) {
314  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
315  }
316 
317  $content = [];
318 
319  $this->tabs_gui->activateSubTab('buddy_view_table');
320  $this->activateTab('my_contacts');
321 
322  if ($this->http->wrapper()->query()->has('inv_room_ref_id') &&
323  $this->http->wrapper()->query()->has('inv_room_scope') &&
324  $this->http->wrapper()->query()->has('inv_usr_ids')) {
325  $inv_room_ref_id = $this->http->wrapper()->query()->retrieve(
326  'inv_room_ref_id',
327  $this->refinery->kindlyTo()->int()
328  );
329  $inv_room_scope = $this->http->wrapper()->query()->retrieve(
330  'inv_room_scope',
331  $this->refinery->kindlyTo()->int()
332  );
333  $inv_usr_ids = $this->http->wrapper()->query()->retrieve(
334  'inv_usr_ids',
335  $this->refinery->in()->series([
336  $this->refinery->kindlyTo()->string(),
337  $this->refinery->custom()->transformation(fn (string $value): array => explode(',', $value)),
338  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
339  ])
340  );
341 
342  $userlist = [];
343  foreach ($inv_usr_ids as $inv_usr_id) {
344  $login = ilObjUser::_lookupLogin($inv_usr_id);
345  $userlist[] = $login;
346  }
347 
348  if ($userlist !== []) {
349  $url = $inv_room_scope !== 0 ? ilLink::_getStaticLink($inv_room_ref_id, 'chtr', true, '_' . $inv_room_scope) : ilLink::_getStaticLink($inv_room_ref_id, 'chtr');
350 
351  $content[] = $this->ui_factory->messageBox()->success(
352  $this->lng->txt('chat_users_have_been_invited') . $this->ui_renderer->render(
353  $this->ui_factory->listing()->unordered($userlist)
354  )
355  )->withButtons([
356  $this->ui_factory->button()->standard($this->lng->txt('goto_invitation_chat'), $url)
357  ]);
358  }
359  }
360 
361  $table = new ilBuddySystemRelationsTableGUI($this, 'showContacts');
362  $table->populate();
363  $content[] = $this->ui_factory->legacy($table->getHTML());
364 
365  $this->tpl->setContent($this->ui_renderer->render($content));
366  $this->tpl->printToStdout();
367  }
368 
369  private function showContactRequests(): void
370  {
371  if (!ilBuddySystem::getInstance()->isEnabled()) {
372  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
373  }
374 
375  $table = new ilBuddySystemRelationsTableGUI($this, 'showContacts');
376 
377  $table->resetOffset();
378  $table->resetFilter();
379 
380  $table->applyFilterValue(
382  ilBuddySystemRequestedRelationState::class . '_p'
383  );
384 
385  $this->showContacts();
386  }
387 
388  protected function mailToUsers(): void
389  {
390  if (!$this->rbacsystem->checkAccess('internal_mail', ilMailGlobalServices::getMailObjectRefId())) {
391  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
392  }
393 
394  try {
395  $usr_ids = $this->http->wrapper()->post()->retrieve(
396  'usr_id',
397  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
398  );
399 
400  // TODO: Replace this with some kind of 'ArrayLengthConstraint'
401  if ($usr_ids === []) {
402  throw new LengthException('mail_select_one_entry');
403  }
404  } catch (Exception $e) {
405  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one_entry'));
406  $this->showContacts();
407  return;
408  }
409 
410  $logins = [];
411  $mail_data = $this->umail->retrieveFromStage();
412  foreach ($usr_ids as $usr_id) {
413  $login = ilObjUser::_lookupLogin($usr_id);
414  if (!$this->umail->existsRecipient($login, (string) $mail_data['rcp_to'])) {
415  $logins[] = $login;
416  }
417  }
418  $logins = array_filter($logins);
419 
420  if ($logins !== []) {
421  $mail_data = $this->umail->appendSearchResult($logins, 'to');
422  $this->umail->persistToStage(
423  (int) $mail_data['user_id'],
424  $mail_data['attachments'],
425  $mail_data['rcp_to'],
426  $mail_data['rcp_cc'],
427  $mail_data['rcp_bcc'],
428  $mail_data['m_subject'],
429  $mail_data['m_message'],
430  $mail_data['use_placeholders'],
431  $mail_data['tpl_ctx_id'],
432  $mail_data['tpl_ctx_params']
433  );
434  }
435 
436  $this->ctrl->redirectToURL('ilias.php?baseClass=ilMailGUI&type=search_res');
437  }
438 
443  public function submitInvitation(): void
444  {
445  $usr_ids = [];
446  try {
447  $usr_ids = $this->refinery->kindlyTo()->listOf(
448  $this->refinery->kindlyTo()->int()
449  )->transform(explode(',', $this->http->wrapper()->post()->retrieve(
450  'usr_id',
451  $this->refinery->kindlyTo()->string()
452  )));
453 
454  // TODO: Replace this with some kind of 'ArrayLengthConstraint'
455  if ($usr_ids === []) {
456  throw new LengthException('select_one');
457  }
458  } catch (Exception $e) {
459  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'), true);
460  $this->ctrl->redirect($this);
461  }
462 
463  if (!$this->http->wrapper()->post()->has('room_id')) {
464  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
465  $this->postUsrId = $usr_ids;
466  $this->inviteToChat();
467  return;
468  }
469 
470  // get selected chatroom from POST-String, format: "room_id,scope"
471  $room_ids = $this->refinery->kindlyTo()->listOf(
472  $this->refinery->kindlyTo()->int()
473  )->transform(explode(',', $this->http->wrapper()->post()->retrieve(
474  'room_id',
475  $this->refinery->kindlyTo()->string()
476  )));
477 
478  $room_id = (int) $room_ids[0];
479 
480  $scope = 0;
481  if (count($room_ids) > 1) {
482  $scope = (int) $room_ids[1];
483  }
484 
485  $room = ilChatroom::byRoomId($room_id, true);
486  $no_access = [];
487  $no_login = [];
488  $valid_users = [];
489  $valid_user_to_login_map = [];
490 
491  foreach ($usr_ids as $usr_id) {
492  $login = ilObjUser::_lookupLogin($usr_id);
493  if ($login === '') {
494  $no_login[$usr_id] = $usr_id;
495  continue;
496  }
497 
498  $ref_id = $room->getRefIdByRoomId($room_id);
499 
500  if (
501  !ilChatroom::checkPermissionsOfUser((int) $usr_id, 'read', $ref_id) ||
502  $room->isUserBanned((int) $usr_id)
503  ) {
504  $no_access[$usr_id] = $login;
505  } else {
506  $valid_users[$usr_id] = $usr_id;
507  $valid_user_to_login_map[$usr_id] = $login;
508  }
509  }
510 
511  if (count($no_access) || count($no_login)) {
512  $message = '';
513 
514  if (count($no_access) > 0) {
515  $message .= $this->lng->txt('chat_users_without_permission') . ':<br>';
516  $list = '';
517 
518  foreach ($no_access as $usr_id => $login) {
519  $list .= '<li>' . $login . '</li>';
520  }
521 
522  $message .= '<ul>';
523  $message .= $list;
524  $message .= '</ul>';
525  }
526 
527  if (count($no_login) > 0) {
528  $message .= $this->lng->txt('chat_users_without_login') . ':<br>';
529  $list = '';
530 
531  foreach ($no_login as $usr_id) {
532  $list .= '<li>' . $usr_id . '</li>';
533  }
534 
535  $message .= '<ul>';
536  $message .= $list;
537  $message .= '</ul>';
538  }
539 
540  $this->tpl->setOnScreenMessage('failure', $message);
541  $this->postUsrId = $usr_ids;
542  $this->inviteToChat();
543  return;
544  }
545 
546  $ref_id = $room->getRefIdByRoomId($room_id);
547 
548  $url = $scope !== 0 ? ilLink::_getStaticLink($ref_id, 'chtr', true, '_' . $scope) : ilLink::_getStaticLink($ref_id, 'chtr');
549 
550  foreach ($valid_users as $id) {
551  $room->inviteUserToPrivateRoom((int) $id, $scope);
552  $room->sendInvitationNotification(
553  null,
554  $this->user->getId(),
555  (int) $id,
556  $scope,
557  $url
558  );
559  }
560 
561  $this->ctrl->setParameter($this, 'inv_room_ref_id', $ref_id);
562  $this->ctrl->setParameter($this, 'inv_room_scope', (int) $scope);
563  $this->ctrl->setParameter($this, 'inv_usr_ids', implode(',', $valid_users));
564 
565  $this->ctrl->redirect($this);
566  }
567 
571  protected function inviteToChat(): void
572  {
573  $this->tabs_gui->activateSubTab('buddy_view_table');
574  $this->activateTab('my_contacts');
575 
576  $this->lng->loadLanguageModule('chatroom');
577 
578  $usr_ids = $this->postUsrId;
579  if (!is_array($usr_ids)) {
580  try {
581  $usr_ids = $this->http->wrapper()->post()->retrieve(
582  'usr_id',
583  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
584  );
585  } catch (Exception $e) {
586  $usr_ids = [];
587  }
588  }
589 
590  if (!is_array($usr_ids) || [] === $usr_ids) {
591  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'), true);
592  $this->ctrl->redirect($this);
593  }
594 
595  $usr_ids = $this->refinery->kindlyTo()->listOf(
596  $this->refinery->kindlyTo()->int()
597  )->transform($usr_ids);
598 
599  $ilChatroom = new ilChatroom();
600  $chat_rooms = $ilChatroom->getAccessibleRoomIdByTitleMap($this->user->getId());
601  $subrooms = [];
602 
603  foreach ($chat_rooms as $room_id => $title) {
604  $subrooms[] = $ilChatroom->getPrivateSubRooms($room_id, $this->user->getId());
605  }
606 
607  $form = new ilPropertyFormGUI();
608  $form->setTitle($this->lng->txt('mail_invite_users_to_chat'));
609 
610  $psel = new ilSelectInputGUI($this->lng->txt('chat_select_room'), 'room_id');
611  $options = [];
612 
613  asort($chat_rooms);
614  foreach ($chat_rooms as $room_id => $room) {
615  $ref_id = $room_id;
616 
617  if ($ilChatroom->isUserBanned($this->user->getId())) {
618  continue;
619  }
620 
621  $options[$ref_id] = $room;
622 
623  foreach ($subrooms as $subroom) {
624  foreach ($subroom as $sub_id => $parent_id) {
625  if ($parent_id === $ref_id) {
626  $title = ilChatroom::lookupPrivateRoomTitle($sub_id);
627  $options[$ref_id . ',' . $sub_id] = '+&nbsp;' . $title;
628  }
629  }
630  }
631  }
632 
633  $psel->setOptions($options);
634  $form->addItem($psel);
635  $phidden = new ilHiddenInputGUI('usr_id');
636  $phidden->setValue(implode(',', $usr_ids));
637  $form->addItem($phidden);
638  $form->addCommandButton('submitInvitation', $this->lng->txt('submit'));
639  $form->addCommandButton('showContacts', $this->lng->txt('cancel'));
640  $form->setFormAction($this->ctrl->getFormAction($this, 'showContacts'));
641 
642  $this->tpl->setTitle($this->lng->txt('mail_invite_users_to_chat'));
643  $this->tpl->setContent($form->getHTML());
644  $this->tpl->printToStdout();
645  }
646 }
$scope
Definition: ltiregstart.php:53
changeContactsView()
This method is used to switch the contacts view between gallery and table in the mail system...
ILIAS UI Factory $ui_factory
static getInstanceByGlobalUser()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
Help GUI class.
static lookupPrivateRoomTitle(int $proom_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
inviteToChat()
Send chat invitations to selected Users.
setOptions(array $a_options)
ilCtrlInterface $ctrl
global $DIC
Definition: feed.php:28
ilGlobalTemplateInterface $tpl
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:67
static http()
Fetches the global http state from ILIAS.
ILIAS HTTP GlobalHttpState $http
static checkPermissionsOfUser(int $usr_id, $permissions, int $ref_id)
Checks user permissions in question for a given user id in relation to a given ref_id.
activateTab(string $a_id)
ilFormatMail $umail
submitInvitation()
Last step of chat invitations check access for every selected user and send invitation.
ilToolbarGUI $toolbar
static byRoomId(int $room_id, bool $initObject=false)
ILIAS Refinery Factory $refinery
Class ilChatroom.
ilRbacSystem $rbacsystem
Error Handling & global info handling uses PEAR error class.
ILIAS UI Renderer $ui_renderer
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$message
Definition: xapiexit.php:32
$url
ilErrorHandling $error
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupLogin(int $a_user_id)