ILIAS  Release_4_1_x_branch Revision 61804
 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 
93  $query = "SELECT * FROM ".$this->table_addr."
94  WHERE ( " .$ilDB->like('login', 'text', '%'.$a_query_str.'%'). "
95  OR " .$ilDB->like('firstname', 'text', '%'.$a_query_str.'%'). "
96  OR " .$ilDB->like('lastname', 'text', '%'.$a_query_str.'%'). "
97  OR " .$ilDB->like('email', 'text', '%'.$a_query_str.'%'). ")
98  AND user_id = ".$ilDB->quote($this->user_id, 'integer'). " " ;
99 
100 
101  $res = $ilDB->query($query);
102  }
103  else
104  {
105  $res = $ilDB->queryf("
106  SELECT * FROM ".$this->table_addr." WHERE user_id = %s",
107  array('text', 'integer'),
108  array($this->table_addr, $this->user_id));
109  }
110 
111  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
112  {
113  $entries[] = array(
114  "login" => ($row->login),
115  "firstname" => ($row->firstname),
116  "lastname" => ($row->lastname),
117  "email" => ($row->email));
118  }
119  return $entries ? $entries : array();
120  }
130  function addEntry($a_login,$a_firstname,$a_lastname,$a_email)
131  {
132  global $ilDB;
133 
134  $nextId = $ilDB->nextId($this->table_addr); //addr_id,
135  $statement = $ilDB->manipulateF("
136  INSERT INTO ".$this->table_addr."
137  (
138  addr_id,
139  user_id,
140  login,
141  firstname,
142  lastname,
143  email
144  )
145  VALUES (%s, %s, %s, %s, %s, %s)",
146  array('integer', 'integer', 'text', 'text', 'text', 'text'),
147  array($nextId, $this->user_id, $a_login, $a_firstname, $a_lastname, $a_email));
148 
149  return true;
150  }
151 
162  function updateEntry($a_addr_id,$a_login,$a_firstname,$a_lastname,$a_email)
163  {
164  global $ilDB;
165  $statement = $ilDB->manipulateF(
166  "UPDATE ".$this->table_addr ."
167  SET login = %s,
168  firstname = %s,
169  lastname = %s,
170  email = %s
171  WHERE user_id = %s
172  AND addr_id = %s",
173  array('text', 'text', 'text', 'text', 'integer', 'integer'),
174  array($a_login, $a_firstname, $a_lastname, $a_email, $this->user_id, $a_addr_id));
175 
176  return true;
177  }
178 
184  function getEntries()
185  {
186  global $ilDB;
187 
188  $data_types = array();
189  $data = array();
190  $query = ("SELECT * FROM ".$this->table_addr." WHERE user_id = ".$ilDB->quote($this->user_id, 'integer')). " ";
191 
192  if (trim($this->getSearchQuery()) != '')
193  {
194  $sub_query .= " AND ( %s
195  OR %s
196  OR %s
197  OR %s) ";
198 
199  $query .= sprintf($sub_query, $ilDB->like('login','text','%'.trim($this->getSearchQuery()).'%'),
200  $ilDB->like('firstname','text','%'.trim($this->getSearchQuery()).'%'),
201  $ilDB->like('lastname','text','%'.trim($this->getSearchQuery()).'%'),
202  $ilDB->like('email','text','%'.trim($this->getSearchQuery()).'%')
203  );
204 
205 
206  }
207 
208  $query .= " ORDER BY login, lastname";
209 
210  $res = $ilDB->query($query, $data_types, $data);
211 
212  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
213  {
214  $entries[] = array(
215  "addr_id" => $row->addr_id,
216  "login" => ($row->login),
217  "firstname" => ($row->firstname),
218  "lastname" => ($row->lastname),
219  "email" => ($row->email));
220  }
221  return $entries ? $entries : array();
222  }
229  function getEntry($a_addr_id)
230  {
231  global $ilDB;
232 
233  $res = $ilDB->queryf("
234  SELECT * FROM ".$this->table_addr."
235  WHERE user_id = %s
236  AND addr_id = %s",
237  array('integer', 'integer'),
238  array($this->user_id, $a_addr_id));
239 
240  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
241 
242  return array(
243  "addr_id" => $row->addr_id,
244  "login" => ($row->login),
245  "firstname" => ($row->firstname),
246  "lastname" => ($row->lastname),
247  "email" => ($row->email));
248  }
249 
256  function entryToString($a_addr_id)
257  {
258  $entry = $this->getEntry($a_addr_id);
259  if (!$entry)
260  return "???";
261  else
262  {
263  $out = "";
264  if ($entry['firstname'] && $entry['lastname'])
265  $out .= $entry['lastname'] . ', ' . $entry['firstname'] . ' ';
266  else if ($entry['firstname'])
267  $out .= $entry['firstname'] . ' ';
268  else if ($entry['lastname'])
269  $out .= $entry['lastname'] . ' ';
270 
271  if ($entry['login'])
272  $out .= '(' . $entry['login'] . ') ';
273 
274  if ($entry['email'])
275  $out .= '[' . $entry['email'] . ']';
276  return $out;
277  }
278  }
279 
286  function deleteEntries($a_entries)
287  {
288  if(is_array($a_entries))
289  {
290  foreach($a_entries as $entry)
291  {
292  $this->deleteEntry($entry);
293  }
294  }
295  return true;
296  }
303  function deleteEntry($a_addr_id)
304  {
305  global $ilDB;
306 
307  $statement = $ilDB->manipulateF('
308  DELETE FROM addressbook_mlist_ass
309  WHERE addr_id = %s',
310  array('integer'), array($a_addr_id));
311 
312  $statement = $ilDB->manipulateF("
313  DELETE FROM ".$this->table_addr."
314  WHERE user_id = %s
315  AND addr_id = %s",
316  array('integer', 'integer'),
317  array($this->user_id, $a_addr_id));
318 
319  return true;
320  }
321 
328  function checkEntry($a_login)
329  {
330  global $ilDB;
331 
332  if ($a_login != '')
333  {
334  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
335  array('integer', 'text'), array($this->user_id, $a_login));
336 
337  while($record = $ilDB->fetchAssoc($result))
338  {
339  return $record['addr_id'];
340  }
341  }
342 
343  return 0;
344  }
345 
346  /* Check whether an entry with a given login name already exists */
347  function checkEntryByLogin($a_login)
348  {
349  global $ilDB;
350 
351  if ($a_login != "")
352  {
353  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
354  array('integer', 'text'), array($this->user_id, $a_login));
355 
356  while($record = $ilDB->fetchAssoc($result))
357  {
358  return $record['addr_id'];
359  }
360  }
361 
362  return 0;
363  }
364 
365  public function setSearchQuery($search_query = '')
366  {
367  $this->search_query = $search_query;
368  }
369  public function getSearchQuery()
370  {
371  return $this->search_query;
372  }
373 }
374 ?>