ILIAS  release_4-3 Revision
 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  }
134  function addEntry($a_login,$a_firstname,$a_lastname,$a_email)
135  {
136  global $ilDB;
137 
138  $nextId = $ilDB->nextId($this->table_addr); //addr_id,
139  $statement = $ilDB->manipulateF("
140  INSERT INTO ".$this->table_addr."
141  (
142  addr_id,
143  user_id,
144  login,
145  firstname,
146  lastname,
147  email
148  )
149  VALUES (%s, %s, %s, %s, %s, %s)",
150  array('integer', 'integer', 'text', 'text', 'text', 'text'),
151  array($nextId, $this->user_id, $a_login, $a_firstname, $a_lastname, $a_email));
152 
153  return true;
154  }
155 
166  function updateEntry($a_addr_id,$a_login,$a_firstname,$a_lastname,$a_email)
167  {
168  global $ilDB;
169  $statement = $ilDB->manipulateF(
170  "UPDATE ".$this->table_addr ."
171  SET login = %s,
172  firstname = %s,
173  lastname = %s,
174  email = %s
175  WHERE user_id = %s
176  AND addr_id = %s",
177  array('text', 'text', 'text', 'text', 'integer', 'integer'),
178  array($a_login, $a_firstname, $a_lastname, $a_email, $this->user_id, $a_addr_id));
179 
180  return true;
181  }
182 
188  function getEntries()
189  {
190  global $ilDB;
191 
192  $data_types = array();
193  $data = array();
194  $query = ("SELECT * FROM ".$this->table_addr." WHERE user_id = ".$ilDB->quote($this->user_id, 'integer')). " ";
195 
196  if (trim($this->getSearchQuery()) != '')
197  {
198  $sub_query .= " AND ( %s
199  OR %s
200  OR %s
201  OR %s) ";
202 
203  $query .= sprintf($sub_query, $ilDB->like('login','text','%'.trim($this->getSearchQuery()).'%'),
204  $ilDB->like('firstname','text','%'.trim($this->getSearchQuery()).'%'),
205  $ilDB->like('lastname','text','%'.trim($this->getSearchQuery()).'%'),
206  $ilDB->like('email','text','%'.trim($this->getSearchQuery()).'%')
207  );
208 
209 
210  }
211 
212  $query .= " ORDER BY login, lastname";
213 
214  $res = $ilDB->query($query, $data_types, $data);
215 
216  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
217  {
218  $entries[] = array(
219  "addr_id" => $row->addr_id,
220  "login" => ($row->login),
221  "firstname" => ($row->firstname),
222  "lastname" => ($row->lastname),
223  "email" => ($row->email));
224  }
225  return $entries ? $entries : array();
226  }
233  function getEntry($a_addr_id)
234  {
235  global $ilDB;
236 
237  $res = $ilDB->queryf("
238  SELECT * FROM ".$this->table_addr."
239  WHERE user_id = %s
240  AND addr_id = %s",
241  array('integer', 'integer'),
242  array($this->user_id, $a_addr_id));
243 
244  $row = $res->fetchRow(DB_FETCHMODE_OBJECT);
245 
246  return array(
247  "addr_id" => $row->addr_id,
248  "login" => ($row->login),
249  "firstname" => ($row->firstname),
250  "lastname" => ($row->lastname),
251  "email" => ($row->email));
252  }
253 
260  function entryToString($a_addr_id)
261  {
262  $entry = $this->getEntry($a_addr_id);
263  if (!$entry)
264  return "???";
265  else
266  {
267  $out = "";
268  if ($entry['firstname'] && $entry['lastname'])
269  $out .= $entry['lastname'] . ', ' . $entry['firstname'] . ' ';
270  else if ($entry['firstname'])
271  $out .= $entry['firstname'] . ' ';
272  else if ($entry['lastname'])
273  $out .= $entry['lastname'] . ' ';
274 
275  if ($entry['login'])
276  $out .= '(' . $entry['login'] . ') ';
277 
278  if ($entry['email'])
279  $out .= '[' . $entry['email'] . ']';
280  return $out;
281  }
282  }
283 
290  function deleteEntries($a_entries)
291  {
292  if(is_array($a_entries))
293  {
294  foreach($a_entries as $entry)
295  {
296  $this->deleteEntry($entry);
297  }
298  }
299  return true;
300  }
307  function deleteEntry($a_addr_id)
308  {
309  global $ilDB;
310 
311  $statement = $ilDB->manipulateF('
312  DELETE FROM addressbook_mlist_ass
313  WHERE addr_id = %s',
314  array('integer'), array($a_addr_id));
315 
316  $statement = $ilDB->manipulateF("
317  DELETE FROM ".$this->table_addr."
318  WHERE user_id = %s
319  AND addr_id = %s",
320  array('integer', 'integer'),
321  array($this->user_id, $a_addr_id));
322 
323  return true;
324  }
325 
332  function checkEntry($a_login)
333  {
334  global $ilDB;
335 
336  if ($a_login != '')
337  {
338  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
339  array('integer', 'text'), array($this->user_id, $a_login));
340 
341  while($record = $ilDB->fetchAssoc($result))
342  {
343  return $record['addr_id'];
344  }
345  }
346 
347  return 0;
348  }
349 
350  /* Check whether an entry with a given login name already exists */
351  function checkEntryByLogin($a_login)
352  {
353  global $ilDB;
354 
355  if ($a_login != "")
356  {
357  $result = $ilDB->queryf("SELECT addr_id FROM ".$this->table_addr." WHERE user_id = %s AND login = %s",
358  array('integer', 'text'), array($this->user_id, $a_login));
359 
360  while($record = $ilDB->fetchAssoc($result))
361  {
362  return $record['addr_id'];
363  }
364  }
365 
366  return 0;
367  }
368 
369  public function setSearchQuery($search_query = '')
370  {
371  $this->search_query = $search_query;
372  }
373  public function getSearchQuery()
374  {
375  return $this->search_query;
376  }
377 }
378 ?>