ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilMailAutoCompleteRecipientResult.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  final public const int MODE_STOP_ON_MAX_ENTRIES = 1;
24  final public const int MODE_FETCH_ALL = 2;
25  final public const int MAX_RESULT_ENTRIES = 1000;
26 
27  protected bool $allow_smtp;
28  protected int $user_id;
30  protected array $handled_recipients = [];
31  protected int $mode = self::MODE_STOP_ON_MAX_ENTRIES;
32  protected int $max_entries;
34  public array $result = [
35  'items' => [],
36  'hasMoreResults' => false
37  ];
38 
39  public function __construct(int $mode)
40  {
41  global $DIC;
42 
43  $this->allow_smtp = $DIC->rbac()->system()->checkAccess('smtp_mail', MAIL_SETTINGS_ID);
44  $this->user_id = $DIC->user()->getId();
45  $this->max_entries = ilSearchSettings::getInstance()->getAutoCompleteLength();
46 
47  $this->initMode($mode);
48  }
49 
53  protected function initMode(int $mode): void
54  {
55  if (!in_array($mode, [self::MODE_FETCH_ALL, self::MODE_STOP_ON_MAX_ENTRIES], true)) {
56  throw new InvalidArgumentException("Wrong mode passed!");
57  }
58  $this->mode = $mode;
59  }
60 
61  public function isResultAddable(): bool
62  {
63  if ($this->mode === self::MODE_STOP_ON_MAX_ENTRIES &&
64  $this->max_entries >= 0 && count($this->result['items']) >= $this->max_entries) {
65  return false;
66  }
67 
68  if ($this->mode === self::MODE_FETCH_ALL &&
69  count($this->result['items']) >= self::MAX_RESULT_ENTRIES) {
70  return false;
71  }
72 
73  return true;
74  }
75 
76  public function addResult(string $login, string $firstname, string $lastname): void
77  {
78  if ($login !== '' && !isset($this->handled_recipients[$login])) {
79  $recipient = [];
80  $recipient['value'] = $login;
81 
82  $label = $login;
83  if ($firstname && $lastname) {
84  $label .= " [" . $firstname . ", " . $lastname . "]";
85  }
86  $recipient['label'] = $label;
87 
88  $this->result['items'][] = $recipient;
89  $this->handled_recipients[$login] = 1;
90  }
91  }
92 
96  public function getItems(): array
97  {
98  return $this->result;
99  }
100 
101  public function numItems(): int
102  {
103  return count($this->result['items']);
104  }
105 }
addResult(string $login, string $firstname, string $lastname)
global $DIC
Definition: shib_login.php:26
const MAIL_SETTINGS_ID
Definition: constants.php:36