ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailAutoCompleteUserProvider.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
26 public function current(): array
27 {
28 return [
29 'login' => $this->data['login'],
30 'firstname' => $this->data['firstname'],
31 'lastname' => $this->data['lastname'],
32 ];
33 }
34
35 public function key(): string
36 {
37 return $this->data['login'];
38 }
39
40 public function rewind(): void
41 {
42 if ($this->res !== null) {
43 $this->db->free($this->res);
44 $this->res = null;
45 }
46
47 $select_part = $this->getSelectPart();
48 $where_part = $this->getWherePart($this->quoted_term);
49 $order_by_part = $this->getOrderByPart();
50 $query = implode(' ', [
51 'SELECT ' . $select_part,
52 'FROM ' . $this->getFromPart(),
53 $where_part !== '' ? 'WHERE ' . $where_part : '',
54 $order_by_part !== '' ? 'ORDER BY ' . $order_by_part : '',
55 ]);
56
57 $this->res = $this->db->query($query);
58 }
59
60 protected function getSelectPart(): string
61 {
62 $fields = [
63 'login',
64 sprintf(
65 '(CASE WHEN (firstname IS NOT NULL AND (profpref.value = %s OR profpref.value = %s)) THEN firstname ELSE \'\' END) firstname',
66 $this->db->quote('y', 'text'),
67 $this->db->quote('g', 'text')
68 ),
69 sprintf(
70 '(CASE WHEN (lastname IS NOT NULL AND (profpref.value = %s OR profpref.value = %s)) THEN lastname ELSE \'\' END) lastname',
71 $this->db->quote('y', 'text'),
72 $this->db->quote('g', 'text')
73 ),
74 sprintf(
75 '(CASE WHEN (email IS NOT NULL AND (profpref.value = %s OR profpref.value = %s) ' .
76 "AND pubemail.value = %s) THEN email ELSE '' END) email",
77 $this->db->quote('y', 'text'),
78 $this->db->quote('g', 'text'),
79 $this->db->quote('y', 'text')
80 ),
81 ];
82
83 $fields[] = 'profpref.value profile_value';
84 $fields[] = 'pubemail.value email_value';
85
86 return implode(', ', $fields);
87 }
88
89 protected function getFromPart(): string
90 {
91 $joins = [];
92
93 $joins[] = '
94 LEFT JOIN usr_pref profpref
95 ON profpref.usr_id = usr_data.usr_id
96 AND profpref.keyword = ' . $this->db->quote('public_profile', 'text');
97
98 $joins[] = '
99 LEFT JOIN usr_pref pubemail
100 ON pubemail.usr_id = usr_data.usr_id
101 AND pubemail.keyword = ' . $this->db->quote('public_email', 'text');
102
103 return 'usr_data ' . implode(' ', $joins);
104 }
105
106 protected function getWherePart(string $search_query): string
107 {
108 $outer_conditions = [];
109 $outer_conditions[] = 'usr_data.usr_id != ' . $this->db->quote(ANONYMOUS_USER_ID, 'integer');
110 $outer_conditions[] = 'usr_data.active != ' . $this->db->quote(0, 'integer');
111
112 $field_conditions = [];
113 foreach ($this->getFields() as $field) {
114 $field_condition = $this->getQueryConditionByFieldAndValue($field, $search_query);
115
116 if ('email' === $field) {
117 // If privacy should be respected,
118 // the profile setting of every user concerning the email address has to be
119 // respected (in every user context, no matter if the user is 'logged in' or 'anonymous').
120 $email_query = [];
121 $email_query[] = $field_condition;
122 $email_query[] = 'pubemail.value = ' . $this->db->quote('y', 'text');
123 $field_conditions[] = '(' . implode(' AND ', $email_query) . ')';
124 } else {
125 $field_conditions[] = $field_condition;
126 }
127 }
128
129 // If the current user context ist 'logged in' and privacy should be respected,
130 // all fields >>>except the login<<<
131 // should only be searchable if the users' profile is published (y oder g)
132 // In 'anonymous' context we do not need this additional conditions,
133 // because we checked the privacy setting in the condition above: profile = 'g'
134 if ($field_conditions !== []) {
135 $fields = '(' . implode(' OR ', $field_conditions) . ')';
136
137 $field_conditions = ['(' . implode(' AND ', [
138 $fields,
139 $this->db->in('profpref.value', ['y', 'g'], false, 'text'),
140 ]) . ')'];
141 }
142
143 // The login field must be searchable regardless (for 'logged in' users) of any privacy settings.
144 // We handled the general condition for 'anonymous' context above: profile = 'g'
145 $field_conditions[] = $this->getQueryConditionByFieldAndValue('login', $search_query);
146
147 if (ilUserAccountSettings::getInstance()->isUserAccessRestricted()) {
148 $outer_conditions[] = $this->db->in(
149 'time_limit_owner',
150 ilUserFilter::getInstance()->getFolderIds(),
151 false,
152 'integer'
153 );
154 }
155
156 if ($field_conditions !== []) {
157 $outer_conditions[] = '(' . implode(' OR ', $field_conditions) . ')';
158 }
159
160 return implode(' AND ', $outer_conditions);
161 }
162
163 protected function getOrderByPart(): string
164 {
165 return 'login ASC';
166 }
167
168 protected function getQueryConditionByFieldAndValue(string $field, $a_str): string
169 {
170 return $this->db->like($field, 'text', $a_str . '%');
171 }
172
176 protected function getFields(): array
177 {
178 $available_fields = [];
179 foreach (['firstname', 'lastname'] as $field) {
181 $available_fields[] = $field;
182 }
183 }
184 return $available_fields;
185 }
186}
const ANONYMOUS_USER_ID
Definition: constants.php:27