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