ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMailSearchObjectGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
24 abstract class ilMailSearchObjectGUI
25 {
27  protected Refinery $refinery;
28  protected ?string $view = null;
31  protected ilLanguage $lng;
32  protected ilObjUser $user;
36  protected ilTree $tree;
38  protected ilFormatMail $umail;
39  protected bool $mailing_allowed;
43  protected $wsp_access_handler = null;
44  protected ?int $wsp_node_id = null;
45 
51  public function __construct($wsp_access_handler = null, ?int $wsp_node_id = null)
52  {
53  global $DIC;
54 
55  $this->tpl = $DIC['tpl'];
56  $this->ctrl = $DIC['ilCtrl'];
57  $this->lng = $DIC['lng'];
58  $this->user = $DIC['ilUser'];
59  $this->error = $DIC['ilErr'];
60  $this->rbacsystem = $DIC['rbacsystem'];
61  $this->rbacreview = $DIC['rbacreview'];
62  $this->tree = $DIC['tree'];
63  $this->cache = $DIC['ilObjDataCache'];
64  $this->http = $DIC->http();
65  $this->refinery = $DIC->refinery();
66 
67  $this->wsp_access_handler = $wsp_access_handler;
68  $this->wsp_node_id = $wsp_node_id;
69 
70  $this->ctrl->saveParameter($this, 'mobj_id');
71  $this->ctrl->saveParameter($this, 'ref');
72 
73  $mail = new ilMail($this->user->getId());
74  $this->mailing_allowed = $this->rbacsystem->checkAccess('internal_mail', $mail->getMailObjectReferenceId());
75 
76  $this->umail = new ilFormatMail($this->user->getId());
77  }
78 
79  private function isDefaultRequestContext(): bool
80  {
81  return (
82  !$this->http->wrapper()->query()->has('ref') ||
83  $this->http->wrapper()->query()->retrieve('ref', $this->refinery->kindlyTo()->string()) !== 'wsp'
84  );
85  }
86 
87  private function getContext(): string
88  {
89  $context = 'mail';
90  if ($this->http->wrapper()->query()->has('ref')) {
91  $context = $this->http->wrapper()->query()->retrieve('ref', $this->refinery->kindlyTo()->string());
92  }
93 
94  return $context;
95  }
96 
97  private function isLocalRoleTitle(string $title): bool
98  {
99  foreach ($this->getLocalDefaultRolePrefixes() as $local_role_prefix) {
100  if (strpos($title, $local_role_prefix) === 0) {
101  return true;
102  }
103  }
104 
105  return false;
106  }
107 
108  abstract protected function getObjectType(): string;
109 
113  abstract protected function getLocalDefaultRolePrefixes(): array;
114 
115  protected function getRequestValue(string $key, \ILIAS\Refinery\Transformation $trafo, $default = null)
116  {
117  $value = $default;
118  if ($this->http->wrapper()->query()->has($key)) {
119  $value = $this->http->wrapper()->query()->retrieve($key, $trafo);
120  }
121 
122  if ($this->http->wrapper()->post()->has($key)) {
123  $value = $this->http->wrapper()->post()->retrieve($key, $trafo);
124  }
125 
126  return $value;
127  }
128 
132  protected function addPermission(array $a_obj_ids): void
133  {
134  $existing = $this->wsp_access_handler->getPermissions($this->wsp_node_id);
135  $added = false;
136  foreach ($a_obj_ids as $object_id) {
137  if (!in_array($object_id, $existing, true)) {
138  $added = $this->wsp_access_handler->addPermission($this->wsp_node_id, $object_id);
139  }
140  }
141 
142  if ($added) {
143  $this->tpl->setOnScreenMessage('success', $this->lng->txt('wsp_share_success'), true);
144  }
145  $this->ctrl->redirectByClass(ilWorkspaceAccessGUI::class, 'share');
146  }
147 
148  protected function share(): void
149  {
150  $view = '';
151  if ($this->http->wrapper()->query()->has('view')) {
152  $view = $this->http->wrapper()->query()->retrieve('view', $this->refinery->kindlyTo()->string());
153  }
154 
155  if ($view === 'myobjects') {
156  $obj_ids = [];
157  if ($this->http->wrapper()->query()->has('search_' . $this->getObjectType())) {
158  $obj_ids = [
159  $this->http->wrapper()->query()->retrieve(
160  'search_' . $this->getObjectType(),
161  $this->refinery->kindlyTo()->int()
162  )
163  ];
164  } elseif ($this->http->wrapper()->post()->has('search_' . $this->getObjectType())) {
165  $obj_ids = $this->http->wrapper()->post()->retrieve(
166  'search_' . $this->getObjectType(),
167  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
168  );
169  }
170 
171  if ($obj_ids !== []) {
172  $this->addPermission($obj_ids);
173  } else {
174  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_course'));
175  $this->showMyObjects();
176  }
177  } elseif ($view === $this->getObjectType() . '_members') {
178  $usr_ids = [];
179  if ($this->http->wrapper()->query()->has('search_members')) {
180  $usr_ids = [
181  $this->http->wrapper()->query()->retrieve(
182  'search_members',
183  $this->refinery->kindlyTo()->int()
184  )
185  ];
186  } elseif ($this->http->wrapper()->post()->has('search_members')) {
187  $usr_ids = $this->http->wrapper()->post()->retrieve(
188  'search_members',
189  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
190  );
191  }
192 
193  if ($usr_ids !== []) {
194  $this->addPermission($usr_ids);
195  } else {
196  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one_entry'));
197  $this->showMembers();
198  }
199  } else {
200  $this->showMyObjects();
201  }
202  }
203 
204  protected function mail(): void
205  {
206  $view = '';
207  if ($this->http->wrapper()->query()->has('view')) {
208  $view = $this->http->wrapper()->query()->retrieve('view', $this->refinery->kindlyTo()->string());
209  }
210 
211  if ($view === 'myobjects') {
212  $obj_ids = [];
213  if ($this->http->wrapper()->query()->has('search_' . $this->getObjectType())) {
214  $obj_ids = [
215  $this->http->wrapper()->query()->retrieve(
216  'search_' . $this->getObjectType(),
217  $this->refinery->kindlyTo()->int()
218  )
219  ];
220  } elseif ($this->http->wrapper()->post()->has('search_' . $this->getObjectType())) {
221  $obj_ids = $this->http->wrapper()->post()->retrieve(
222  'search_' . $this->getObjectType(),
223  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
224  );
225  }
226 
227  if ($obj_ids !== []) {
228  $this->mailObjects();
229  } else {
230  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_course'));
231  $this->showMyObjects();
232  }
233  } elseif ($view === $this->getObjectType() . '_members') {
234  $usr_ids = [];
235  if ($this->http->wrapper()->query()->has('search_members')) {
236  $usr_ids = [
237  $this->http->wrapper()->query()->retrieve(
238  'search_members',
239  $this->refinery->kindlyTo()->int()
240  )
241  ];
242  } elseif ($this->http->wrapper()->post()->has('search_members')) {
243  $usr_ids = $this->http->wrapper()->post()->retrieve(
244  'search_members',
245  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
246  );
247  }
248 
249  if ($usr_ids !== []) {
250  $this->mailMembers();
251  } else {
252  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_one_entry'));
253  $this->showMembers();
254  }
255  } else {
256  $this->showMyObjects();
257  }
258  }
259 
260  protected function mailObjects(): void
261  {
262  $members = [];
263  $mail_data = $this->umail->retrieveFromStage();
264 
265  $obj_ids = [];
266  if ($this->http->wrapper()->query()->has('search_' . $this->getObjectType())) {
267  $obj_ids = [
268  $this->http->wrapper()->query()->retrieve(
269  'search_' . $this->getObjectType(),
270  $this->refinery->kindlyTo()->int()
271  )
272  ];
273  } elseif ($this->http->wrapper()->post()->has('search_' . $this->getObjectType())) {
274  $obj_ids = $this->http->wrapper()->post()->retrieve(
275  'search_' . $this->getObjectType(),
276  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
277  );
278  }
279 
280  foreach ($obj_ids as $obj_id) {
281  $ref_ids = ilObject::_getAllReferences($obj_id);
282  foreach ($ref_ids as $ref_id) {
283  $can_send_mails = ilParticipants::canSendMailToMembers(
284  $ref_id,
285  $this->user->getId(),
287  );
288  if (!$can_send_mails) {
289  continue;
290  }
291 
292  $roles = $this->rbacreview->getAssignableChildRoles($ref_id);
293  foreach ($roles as $role) {
294  if ($this->isLocalRoleTitle($role['title'])) {
295  $recipient = (new ilRoleMailboxAddress($role['obj_id']))->value();
296  if (!$this->umail->existsRecipient($recipient, (string) $mail_data['rcp_to'])) {
297  $members[] = $recipient;
298  }
299  }
300  }
301  }
302  }
303 
304  $mail_data = $members !== [] ? $this->umail->appendSearchResult(array_unique($members), 'to') : $this->umail->retrieveFromStage();
305 
306  $this->umail->persistToStage(
307  (int) $mail_data['user_id'],
308  $mail_data['attachments'],
309  $mail_data['rcp_to'],
310  $mail_data['rcp_cc'],
311  $mail_data['rcp_bcc'],
312  $mail_data['m_subject'],
313  $mail_data['m_message'],
314  $mail_data['use_placeholders'],
315  $mail_data['tpl_ctx_id'],
316  $mail_data['tpl_ctx_params']
317  );
318 
319  $this->ctrl->redirectToURL('ilias.php?baseClass=ilMailGUI&type=search_res');
320  }
321 
322  public function mailMembers(): void
323  {
324  $members = [];
325  $usr_ids = [];
326  if ($this->http->wrapper()->query()->has('search_members')) {
327  $usr_ids = [
328  $this->http->wrapper()->query()->retrieve(
329  'search_members',
330  $this->refinery->kindlyTo()->int()
331  )
332  ];
333  } elseif ($this->http->wrapper()->post()->has('search_members')) {
334  $usr_ids = $this->http->wrapper()->post()->retrieve(
335  'search_members',
336  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())
337  );
338  }
339 
340  $mail_data = $this->umail->retrieveFromStage();
341  foreach ($usr_ids as $usr_id) {
342  $login = ilObjUser::_lookupLogin($usr_id);
343  if (!$this->umail->existsRecipient($login, (string) $mail_data['rcp_to'])) {
344  $members[] = $login;
345  }
346  }
347  $mail_data = $this->umail->appendSearchResult(array_unique($members), 'to');
348 
349  $this->umail->persistToStage(
350  (int) $mail_data['user_id'],
351  $mail_data['attachments'],
352  $mail_data['rcp_to'],
353  $mail_data['rcp_cc'],
354  $mail_data['rcp_bcc'],
355  $mail_data['m_subject'],
356  $mail_data['m_message'],
357  $mail_data['use_placeholders'],
358  $mail_data['tpl_ctx_id'],
359  $mail_data['tpl_ctx_params']
360  );
361 
362  $this->ctrl->redirectToURL('ilias.php?baseClass=ilMailGUI&type=search_res');
363  }
364 
365  public function cancel(): void
366  {
367  $view = '';
368  if ($this->http->wrapper()->query()->has('view')) {
369  $view = $this->http->wrapper()->query()->retrieve('view', $this->refinery->kindlyTo()->string());
370  }
371 
372  if ($view === 'myobjects' && $this->isDefaultRequestContext()) {
373  $this->ctrl->returnToParent($this);
374  } else {
375  $this->showMyObjects();
376  }
377  }
378 
379  public function showMembers(): void
380  {
381  $obj_ids = [];
382  if ($this->http->wrapper()->query()->has('search_' . $this->getObjectType())) {
383  $obj_ids = $this->refinery->kindlyTo()->listOf(
384  $this->refinery->kindlyTo()->int()
385  )->transform(explode(',', $this->http->wrapper()->query()->retrieve(
386  'search_' . $this->getObjectType(),
387  $this->refinery->kindlyTo()->string()
388  )));
389  } elseif ($this->http->wrapper()->post()->has('search_' . $this->getObjectType())) {
390  $obj_ids = $this->http->wrapper()->post()->retrieve(
391  'search_' . $this->getObjectType(),
392  $this->refinery->kindlyTo()->listOf(
393  $this->refinery->kindlyTo()->int()
394  )
395  );
396  } elseif (ilSession::get('search_' . $this->getObjectType())) {
397  $obj_ids = $this->refinery->kindlyTo()->listOf(
398  $this->refinery->kindlyTo()->int()
399  )->transform(explode(',', ilSession::get('search_' . $this->getObjectType())));
400  ilSession::set('search_' . $this->getObjectType(), '');
401  }
402 
403  if ($obj_ids === []) {
404  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_select_course'));
405  $this->showMyObjects();
406  return;
407  }
408 
409  foreach ($obj_ids as $obj_id) {
411  $object = ilObjectFactory::getInstanceByObjId($obj_id);
412  if (!$object->getShowMembers()) {
413  $this->tpl->setOnScreenMessage('info', $this->lng->txt('mail_crs_list_members_not_available_for_at_least_one_crs'));
414  $this->showMyObjects();
415  return;
416  }
417  }
418 
419  $this->tpl->setTitle($this->lng->txt('mail_addressbook'));
420 
421  $this->ctrl->setParameter($this, 'view', $this->getObjectType() . '_members');
422  if ($obj_ids !== []) {
423  $this->ctrl->setParameter($this, 'search_' . $this->getObjectType(), implode(',', $obj_ids));
424  }
425  $this->tpl->setVariable('ACTION', $this->ctrl->getFormAction($this));
426  $this->ctrl->clearParameters($this);
427 
428  $this->lng->loadLanguageModule($this->getObjectType());
429 
430  $context = $this->getContext();
431 
433  $this,
434  $this->getObjectType(),
435  $context,
436  $obj_ids
437  );
438  $tableData = [];
439 
440  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
441  foreach ($obj_ids as $obj_id) {
442  $members_obj = ilParticipants::getInstanceByObjId($obj_id);
443  $usr_ids = array_map('intval', ilUtil::_sortIds($members_obj->getParticipants(), 'usr_data', 'lastname', 'usr_id'));
444  foreach ($usr_ids as $usr_id) {
445  $user = new ilObjUser($usr_id);
446  if (!$user->getActive()) {
447  continue;
448  }
449 
450  $fullname = '';
451  if (in_array(ilObjUser::_lookupPref($user->getId(), 'public_profile'), ['g', 'y'])) {
452  $fullname = $user->getLastname() . ', ' . $user->getFirstname();
453  }
454 
455  $rowData = [
456  'members_id' => $user->getId(),
457  'members_login' => $user->getLogin(),
458  'members_name' => $fullname,
459  'members_crs_grp' => $this->cache->lookupTitle((int) $obj_id),
460  'search_' . $this->getObjectType() => $obj_id
461  ];
462 
463  if ('mail' === $context && ilBuddySystem::getInstance()->isEnabled()) {
464  $relation = ilBuddyList::getInstanceByGlobalUser()->getRelationByUserId($user->getId());
465  $state_name = ilStr::convertUpperCamelCaseToUnderscoreCase($relation->getState()->getName());
466  $rowData['status'] = '';
467  if ($user->getId() !== $this->user->getId()) {
468  if ($relation->isOwnedByActor()) {
469  $rowData['status'] = $this->lng->txt('buddy_bs_state_' . $state_name . '_a');
470  } else {
471  $rowData['status'] = $this->lng->txt('buddy_bs_state_' . $state_name . '_p');
472  }
473  }
474  }
475 
476  $tableData[] = $rowData;
477  }
478  }
479  $table->setData($tableData);
480 
481  if (count($tableData)) {
482  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $this->lng->txt('marked_entries'));
483  }
484 
485  $searchTpl->setVariable('TABLE', $table->getHTML());
486  $this->tpl->setContent($searchTpl->get());
487 
488  if ($this->isDefaultRequestContext()) {
489  $this->tpl->printToStdout();
490  }
491  }
492 
493  abstract protected function doesExposeMembers(ilObject $object): bool;
494 
495  public function showMyObjects(): void
496  {
497  $this->tpl->setTitle($this->lng->txt('mail_addressbook'));
498 
499  $searchTpl = new ilTemplate('tpl.mail_search_template.html', true, true, 'Services/Contact');
500 
501  $this->lng->loadLanguageModule('crs');
502 
503  $table = new ilMailSearchObjectsTableGUI(
504  $this,
505  $this->getObjectType(),
506  $this->getContext()
507  );
508  $table->setId('search_' . $this->getObjectType() . '_tbl');
509 
510  $objs_ids = ilParticipants::_getMembershipByType($this->user->getId(), [$this->getObjectType()]);
511  $counter = 0;
512  $tableData = [];
513  if ($objs_ids !== []) {
514  $num_courses_hidden_members = 0;
515  foreach ($objs_ids as $obj_id) {
517  $object = ilObjectFactory::getInstanceByObjId($obj_id);
518 
519  $ref_ids = array_keys(ilObject::_getAllReferences($object->getId()));
520  $ref_id = $ref_ids[0];
521  $object->setRefId($ref_id);
522 
523  $has_untrashed_references = ilObject::_hasUntrashedReference($object->getId());
524  $can_send_mails = ilParticipants::canSendMailToMembers(
525  $object->getRefId(),
526  $this->user->getId(),
528  );
529 
530  if ($has_untrashed_references && ($can_send_mails || $this->doesExposeMembers($object))) {
531  $member_list_enabled = $object->getShowMembers();
532  $participants = ilParticipants::getInstanceByObjId($object->getId());
533  $usr_ids = $participants->getParticipants();
534 
535  foreach ($usr_ids as $key => $usr_id) {
536  $is_active = ilObjUser::_lookupActive($usr_id);
537  if (!$is_active) {
538  unset($usr_ids[$key]);
539  }
540  }
541  $usr_ids = array_values($usr_ids);
542 
543  $hiddenMembers = false;
544  if (!$member_list_enabled) {
545  ++$num_courses_hidden_members;
546  $hiddenMembers = true;
547  }
548 
549  $path_arr = $this->tree->getPathFull($object->getRefId(), $this->tree->getRootId());
550  $path = '';
551  foreach ($path_arr as $data) {
552  if ($path !== '') {
553  $path .= ' -> ';
554  }
555  $path .= $data['title'];
556  }
557 
558  $current_selection_list = new ilAdvancedSelectionListGUI();
559  $current_selection_list->setListTitle($this->lng->txt('actions'));
560  $current_selection_list->setId('act_' . $counter);
561 
562  $this->ctrl->setParameter($this, 'search_' . $this->getObjectType(), $object->getId());
563  $this->ctrl->setParameter($this, 'view', 'myobjects');
564 
565  if ($this->isDefaultRequestContext()) {
566  if ($this->mailing_allowed && $can_send_mails) {
567  $current_selection_list->addItem(
568  $this->lng->txt('mail_members'),
569  '',
570  $this->ctrl->getLinkTarget($this, 'mail')
571  );
572  }
573  } else {
574  $current_selection_list->addItem(
575  $this->lng->txt('wsp_share_with_members'),
576  '',
577  $this->ctrl->getLinkTarget($this, 'share')
578  );
579  }
580  $current_selection_list->addItem(
581  $this->lng->txt('mail_list_members'),
582  '',
583  $this->ctrl->getLinkTarget($this, 'showMembers')
584  );
585 
586  $this->ctrl->clearParameters($this);
587 
588  $rowData = [
589  'OBJECT_ID' => $object->getId(),
590  'OBJECT_NAME' => $object->getTitle(),
591  'OBJECT_NO_MEMBERS' => count($usr_ids),
592  'OBJECT_PATH' => $path,
593  'COMMAND_SELECTION_LIST' => $current_selection_list->getHTML(),
594  'hidden_members' => $hiddenMembers,
595  ];
596  $counter++;
597  $tableData[] = $rowData;
598  }
599  }
600 
601  if ($num_courses_hidden_members > 0) {
602  $searchTpl->setCurrentBlock('caption_block');
603  $searchTpl->setVariable('TXT_LIST_MEMBERS_NOT_AVAILABLE', $this->lng->txt('mail_crs_list_members_not_available'));
604  $searchTpl->parseCurrentBlock();
605  }
606  }
607 
608  $searchTpl->setVariable('TXT_MARKED_ENTRIES', $this->lng->txt('marked_entries'));
609 
610  $table->setData($tableData);
611  $searchTpl->setVariable('TABLE', $table->getHTML());
612  $this->tpl->setContent($searchTpl->get());
613 
614  if ($this->isDefaultRequestContext()) {
615  $this->tpl->printToStdout();
616  }
617  }
618 
619  public function executeCommand(): bool
620  {
621  $forward_class = $this->ctrl->getNextClass($this);
622  switch (strtolower($forward_class)) {
623  case strtolower(ilBuddySystemGUI::class):
624  if (!ilBuddySystem::getInstance()->isEnabled()) {
625  $this->error->raiseError($this->lng->txt('msg_no_perm_read'), $this->error->MESSAGE);
626  }
627 
628  $this->ctrl->saveParameter($this, 'search_' . $this->getObjectType());
629 
630  $this->ctrl->setReturn($this, 'showMembers');
631  $this->ctrl->forwardCommand(new ilBuddySystemGUI());
632  break;
633 
634  default:
635  if (!($cmd = $this->ctrl->getCmd())) {
636  $cmd = 'showMyObjects';
637  }
638 
639  $this->$cmd();
640  break;
641  }
642 
643  return true;
644  }
645 }
static canSendMailToMembers( $ref_id_or_instance, ?int $usr_id=null, ?int $mail_obj_ref_id=null)
This method was introduced as a band-aid fix for #22764.
Interface GlobalHttpState.
static get(string $a_var)
__construct($wsp_access_handler=null, ?int $wsp_node_id=null)
Class ilBuddySystemGUI.
$context
Definition: webdav.php:29
static getInstanceByGlobalUser()
static convertUpperCamelCaseToUnderscoreCase(string $value)
Convert a value given in camel case conversion to underscore case conversion (e.g.
Class ChatMainBarProvider .
static _getAllReferences(int $id)
get all reference ids for object ID
static getInstanceByObjId(int $a_obj_id)
Get instance by obj type.
Class ilRoleMailboxAddress.
static _hasUntrashedReference(int $obj_id)
checks whether an object has at least one reference that is not in trash
static _lookupPref(int $a_usr_id, string $a_keyword)
doesExposeMembers(ilObject $object)
setRefId(int $ref_id)
$path
Definition: ltiservices.php:32
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
$ref_id
Definition: ltiauth.php:67
static http()
Fetches the global http state from ILIAS.
string $key
Consumer key/client ID value.
Definition: System.php:193
static _sortIds(array $a_ids, string $a_table, string $a_field, string $a_id_name)
Function that sorts ids by a given table field using WHERE IN E.g: __sort(array(6,7),&#39;usr_data&#39;,&#39;lastname&#39;,&#39;usr_id&#39;) => sorts by lastname.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
Error Handling & global info handling uses PEAR error class.
static _getMembershipByType(int $a_usr_id, array $a_type, bool $a_only_member_role=false)
get membership by type Get course or group membership
static set(string $a_var, $a_val)
Set a value.
static _lookupActive(int $a_usr_id)
getRequestValue(string $key, \ILIAS\Refinery\Transformation $trafo, $default=null)
static _lookupLogin(int $a_user_id)