ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAddressbook.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 
35 {
41  var $ilias;
42 
48  var $lng;
49 
55  var $user_id;
56 
63 
69  function ilAddressbook($a_user_id = 0)
70  {
71  global $ilias,$lng;
72 
73  $this->ilias = &$ilias;
74  $this->lng = &$lng;
75  $this->user_id = $a_user_id;
76 
77  $this->table_addr = 'addressbook';
78  }
79 
86  function searchUsers($a_query_str)
87  {
88  global $ilDB;
89 
90  if($a_query_str)
91  {
92  // #14768
93  $a_query_str = str_replace('%', '\%', $a_query_str);
94  $a_query_str = str_replace('_', '\_', $a_query_str);
95 
96  $query = "SELECT * FROM ".$this->table_addr."
97  WHERE ( " .$ilDB->like('login', 'text', '%'.$a_query_str.'%'). "
98  OR " .$ilDB->like('firstname', 'text', '%'.$a_query_str.'%'). "
99  OR " .$ilDB->like('lastname', 'text', '%'.$a_query_str.'%'). "
100  OR " .$ilDB->like('email', 'text', '%'.$a_query_str.'%'). ")
101  AND user_id = ".$ilDB->quote($this->user_id, 'integer'). " " ;
102 
103 
104  $res = $ilDB->query($query);
105  }
106  else
107  {
108  $res = $ilDB->queryf("
109  SELECT * FROM ".$this->table_addr." WHERE user_id = %s",
110  array('integer'),
111  array($this->user_id)
112  );
113  }
114 
115  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
116  {
117  $entries[] = array(
118  "login" => ($row->login),
119  "firstname" => ($row->firstname),
120  "lastname" => ($row->lastname),
121  "email" => ($row->email));
122  }
123  return $entries ? $entries : array();
124  }
135  public function addEntry($a_login,$a_firstname,$a_lastname,$a_email, $a_auto_update = 0)
136  {
137  global $ilDB;
138 
139  $nextId = $ilDB->nextId($this->table_addr); //addr_id,
140  $ilDB->manipulateF("
141  INSERT INTO ".$this->table_addr."
142  (
143  addr_id,
144  user_id,
145  login,
146  firstname,
147  lastname,
148  email,
149  auto_update
150  )
151  VALUES (%s, %s, %s, %s, %s, %s, %s)",
152  array('integer', 'integer', 'text', 'text', 'text', 'text', 'integer'),
153  array($nextId, $this->user_id, $a_login, $a_firstname, $a_lastname, $a_email, $a_auto_update));
154 
155  return true;
156  }
157 
169  public function updateEntry($a_addr_id,$a_login,$a_firstname,$a_lastname,$a_email, $a_auto_update = 0)
170  {
171  global $ilDB;
172  $statement = $ilDB->manipulateF(
173  "UPDATE ".$this->table_addr ."
174  SET login = %s,
175  firstname = %s,
176  lastname = %s,
177  email = %s,
178  auto_update = %s
179  WHERE user_id = %s
180  AND addr_id = %s",
181  array('text', 'text', 'text', 'text', 'integer', 'integer', 'integer'),
182  array($a_login, $a_firstname, $a_lastname, $a_email, $a_auto_update, $this->user_id, $a_addr_id));
183 
184  return true;
185  }
186 
192  public function getEntries()
193  {
194  global $ilDB;
195 
196  $data_types = array();
197  $data = array();
198  $query = ("SELECT * FROM ".$this->table_addr." WHERE user_id = ".$ilDB->quote($this->user_id, 'integer')). " ";
199 
200  if (trim($this->getSearchQuery()) != '')
201  {
202  $sub_query .= " AND ( %s
203  OR %s
204  OR %s
205  OR %s) ";
206 
207  $query .= sprintf($sub_query, $ilDB->like('login','text','%'.trim($this->getSearchQuery()).'%'),
208  $ilDB->like('firstname','text','%'.trim($this->getSearchQuery()).'%'),
209  $ilDB->like('lastname','text','%'.trim($this->getSearchQuery()).'%'),
210  $ilDB->like('email','text','%'.trim($this->getSearchQuery()).'%')
211  );
212 
213 
214  }
215 
216  $query .= " ORDER BY login, lastname";
217 
218  $res = $ilDB->query($query, $data_types, $data);
219 
220  while($row = $ilDB->fetchObject($res))
221  {
222  $entries[] = array(
223  "addr_id" => $row->addr_id,
224  "login" => ($row->login),
225  "firstname" => ($row->firstname),
226  "lastname" => ($row->lastname),
227  "email" => ($row->email),
228  "auto_update"=> $row->auto_update
229  );
230  }
231  return $entries ? $entries : array();
232  }
239  public function getEntry($a_addr_id)
240  {
241  global $ilDB;
242 
243  $res = $ilDB->queryf("
244  SELECT * FROM ".$this->table_addr."
245  WHERE user_id = %s
246  AND addr_id = %s",
247  array('integer', 'integer'),
248  array($this->user_id, $a_addr_id));
249 
250  $row = $ilDB->fetchObject($res);
251 
252  return array(
253  "addr_id" => $row->addr_id,
254  "login" => ($row->login),
255  "firstname" => ($row->firstname),
256  "lastname" => ($row->lastname),
257  "email" => ($row->email),
258  "auto_update"=> $row->auto_update
259  );
260  }
261 
268  function entryToString($a_addr_id)
269  {
270  $entry = $this->getEntry($a_addr_id);
271  if (!$entry)
272  return "???";
273  else
274  {
275  $out = "";
276  if ($entry['firstname'] && $entry['lastname'])
277  $out .= $entry['lastname'] . ', ' . $entry['firstname'] . ' ';
278  else if ($entry['firstname'])
279  $out .= $entry['firstname'] . ' ';
280  else if ($entry['lastname'])
281  $out .= $entry['lastname'] . ' ';
282 
283  if ($entry['login'])
284  $out .= '(' . $entry['login'] . ') ';
285 
286  if ($entry['email'])
287  $out .= '[' . $entry['email'] . ']';
288  return $out;
289  }
290  }
291 
298  function deleteEntries($a_entries)
299  {
300  if(is_array($a_entries))
301  {
302  foreach($a_entries as $entry)
303  {
304  $this->deleteEntry($entry);
305  }
306  }
307  return true;
308  }
315  function deleteEntry($a_addr_id)
316  {
317  global $ilDB;
318 
319  $statement = $ilDB->manipulateF('
320  DELETE FROM addressbook_mlist_ass
321  WHERE addr_id = %s',
322  array('integer'), array($a_addr_id));
323 
324  $statement = $ilDB->manipulateF("
325  DELETE FROM ".$this->table_addr."
326  WHERE user_id = %s
327  AND addr_id = %s",
328  array('integer', 'integer'),
329  array($this->user_id, $a_addr_id));
330 
331  return true;
332  }
333 
340  function checkEntry($a_login)
341  {
342  global $ilDB;
343 
344  if ($a_login != '')
345  {
346  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
347  array('integer', 'text'), array($this->user_id, $a_login));
348 
349  while($record = $ilDB->fetchAssoc($result))
350  {
351  return $record['addr_id'];
352  }
353  }
354 
355  return 0;
356  }
357 
358  /* Check whether an entry with a given login name already exists */
359  function checkEntryByLogin($a_login)
360  {
361  global $ilDB;
362 
363  if ($a_login != "")
364  {
365  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
366  array('integer', 'text'), array($this->user_id, $a_login));
367 
368  while($record = $ilDB->fetchAssoc($result))
369  {
370  return $record['addr_id'];
371  }
372  }
373 
374  return 0;
375  }
376 
377  public function setSearchQuery($search_query = '')
378  {
379  $this->search_query = $search_query;
380  }
381  public function getSearchQuery()
382  {
383  return $this->search_query;
384  }
385 
389  public static function onUserDeletion(ilObjUser $usr)
390  {
394  global $ilDB;
395 
396  $ilDB->manipulateF(
397  'UPDATE addressbook SET login = NULL, auto_update = %s WHERE login = %s AND email IS NOT NULL',
398  array('integer', 'text'),
399  array(0, $usr->getLogin())
400  );
401 
402  $ilDB->manipulateF(
403  'DELETE FROM addressbook_mlist_ass WHERE addr_id IN(
404  SELECT addr_id FROM addressbook WHERE login = %s AND email IS NULL
405  )',
406  array('text'),
407  array($usr->getLogin())
408  );
409 
410  $ilDB->manipulateF(
411  'DELETE FROM addressbook WHERE login = %s AND email IS NULL',
412  array('text'),
413  array($usr->getLogin())
414  );
415  }
416 
421  public static function onLoginNameChange($from, $to)
422  {
426  global $ilDB;
427 
428  $ilDB->manipulateF(
429  'UPDATE addressbook SET login = %s WHERE login = %s',
430  array('text', 'text'),
431  array($to, $from)
432  );
433  }
434 }
435 ?>