ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailAutoCompleteUserProvider.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/User/classes/class.ilUserAutoComplete.php';
5 require_once 'Services/Mail/classes/class.ilMailAutoCompleteRecipientProvider.php';
6 
11 {
16  public function __construct($quoted_term, $term)
17  {
19  }
20 
25  public function valid()
26  {
27  $this->data = $this->db->fetchAssoc($this->res);
28 
29  return is_array($this->data);
30  }
31 
36  public function current()
37  {
38  return array(
39  'login' => $this->data['login'],
40  'firstname' => $this->data['firstname'],
41  'lastname' => $this->data['lastname']
42  );
43  }
44 
49  public function key()
50  {
51  return $this->data['login'];
52  }
53 
57  public function rewind()
58  {
59  if($this->res)
60  {
61  $this->db->free($this->res);
62  $this->res = null;
63  }
64  $select_part = $this->getSelectPart();
65  $where_part = $this->getWherePart($this->quoted_term);
66  $order_by_part = $this->getOrderByPart();
67  $query = implode(" ", array(
68  'SELECT ' . $select_part,
69  'FROM ' . $this->getFromPart(),
70  $where_part ? 'WHERE ' . $where_part : '',
71  $order_by_part ? 'ORDER BY ' . $order_by_part : ''
72  ));
73 
74  $this->res = $this->db->query($query);
75  }
76 
80  protected function getSelectPart()
81  {
82  $fields = array(
83  'login',
84  'firstname',
85  'lastname',
86  'email'
87  );
88 
89  $fields[] = 'profpref.value profile_value';
90  $fields[] = 'pubemail.value email_value';
91 
92  return implode(', ', $fields);
93  }
94 
98  protected function getFromPart()
99  {
103  global $ilDB;
104 
105  $joins = array();
106 
107  $joins[] = '
108  LEFT JOIN usr_pref profpref
109  ON profpref.usr_id = usr_data.usr_id
110  AND profpref.keyword = ' . $ilDB->quote('public_profile', 'text');
111 
112  $joins[] = '
113  LEFT JOIN usr_pref pubemail
114  ON pubemail.usr_id = usr_data.usr_id
115  AND pubemail.keyword = ' . $ilDB->quote('public_email', 'text');
116 
117  if($joins)
118  {
119  return 'usr_data ' . implode(' ', $joins);
120  }
121  else
122  {
123  return 'usr_data ';
124  }
125  }
126 
131  protected function getWherePart($search_query)
132  {
136  global $ilDB;
137 
138  $outer_conditions = array();
139  $outer_conditions[] = 'usr_data.usr_id != ' . $ilDB->quote(ANONYMOUS_USER_ID, 'integer');
140 
141  $field_conditions = array();
142  foreach($this->getFields() as $field)
143  {
144  $field_condition = $this->getQueryConditionByFieldAndValue($field, $search_query);
145 
146  if('email' == $field)
147  {
148  // If privacy should be respected, the profile setting of every user concerning the email address has to be
149  // respected (in every user context, no matter if the user is 'logged in' or 'anonymous').
150  $email_query = array();
151  $email_query[] = $field_condition;
152  $email_query[] = 'pubemail.value = ' . $ilDB->quote('y', 'text');
153  $field_conditions[] = '(' . implode(' AND ', $email_query) . ')';
154  }
155  else
156  {
157  $field_conditions[] = $field_condition;
158  }
159  }
160 
161  // If the current user context ist 'logged in' and privacy should be respected, all fields >>>except the login<<<
162  // should only be searchable if the users' profile is published (y oder g)
163  // In 'anonymous' context we do not need this additional conditions,
164  // because we checked the privacy setting in the condition above: profile = 'g'
165  if($field_conditions)
166  {
167  $fields = implode(' OR ', $field_conditions);
168 
169  $field_conditions[] = '(' . implode(' AND ', array(
170  $fields,
171  $ilDB->in('profpref.value', array('y', 'g'), false, 'text')
172  )) . ')';
173  }
174 
175  // The login field must be searchable regardless (for 'logged in' users) of any privacy settings.
176  // We handled the general condition for 'anonymous' context above: profile = 'g'
177  $field_conditions[] = $this->getQueryConditionByFieldAndValue('login', $search_query);
178 
179  include_once 'Services/User/classes/class.ilUserAccountSettings.php';
180  if(ilUserAccountSettings::getInstance()->isUserAccessRestricted())
181  {
182  include_once './Services/User/classes/class.ilUserFilter.php';
183  $outer_conditions[] = $ilDB->in('time_limit_owner', ilUserFilter::getInstance()->getFolderIds(), false, 'integer');
184  }
185 
186  if($field_conditions)
187  {
188  $outer_conditions[] = '(' . implode(' OR ', $field_conditions) . ')';
189  }
190 
191  return implode(' AND ', $outer_conditions);
192  }
193 
197  protected function getOrderByPart()
198  {
199  return 'login ASC';
200  }
201 
207  protected function getQueryConditionByFieldAndValue($field, $a_str)
208  {
212  global $ilDB;
213 
214  return $ilDB->like($field, 'text', $a_str . '%');
215  }
216 
221  protected function getFields()
222  {
223  $available_fields = array();
224  foreach(array('login', 'firstname', 'lastname') as $field)
225  {
226  include_once 'Services/Search/classes/class.ilUserSearchOptions.php';
228  {
229  $available_fields[] = $field;
230  }
231  }
232  return $available_fields;
233  }
234 }