4 +-----------------------------------------------------------------------+
5 | program/include/rcube_contacts.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2006-2008, RoundCube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Interface to the local address book database |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_contacts.inc 328 2006-08-30 17:41:21Z thomasb $
24 * Model class for the local address book database
26 * @package Addressbook
37 var $table_cols = array('name', 'email', 'firstname', 'surname', 'vcard');
39 /** public properties */
40 var $primary_key = 'contact_id';
41 var $readonly = false;
50 * @param object Instance of the rcube_db class
51 * @param integer User-ID
53 function __construct($dbconn, $user)
56 $this->db_name = get_table_name('contacts');
57 $this->user_id = $user;
58 $this->ready = $this->db && !$this->db->is_error();
63 * Set internal list page
65 * @param number Page number to list
68 function set_page($page)
70 $this->list_page = (int)$page;
75 * Set internal page size
77 * @param number Number of messages to display on one page
80 function set_pagesize($size)
82 $this->page_size = (int)$size;
87 * Save a search string for future listings
89 * @param string SQL params to use in listing method
91 function set_search_set($filter)
93 $this->filter = $filter;
98 * Getter for saved search properties
100 * @return mixed Search properties used by this class
102 function get_search_set()
104 return $this->filter;
109 * Reset all saved results and search parameters
113 $this->result = null;
114 $this->filter = null;
115 $this->search_fields = null;
116 $this->search_string = null;
121 * Close connection to source
122 * Called on script shutdown
128 * List the current set of contact records
130 * @param array List of cols to show
131 * @param int Only return this number of records, use negative values for tail
132 * @return array Indexed list of contact records, each a hash array
134 function list_records($cols=null, $subset=0)
136 // count contacts for this user
137 $this->result = $this->count();
140 // get contacts from DB
141 if ($this->result->count)
143 $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first;
144 $length = $subset != 0 ? abs($subset) : $this->page_size;
146 $sql_result = $this->db->limitquery(
147 "SELECT * FROM ".$this->db_name."
150 ($this->filter ? " AND (".$this->filter.")" : "") .
157 while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result)))
159 $sql_arr['ID'] = $sql_arr[$this->primary_key];
160 // make sure we have a name to display
161 if (empty($sql_arr['name']))
162 $sql_arr['name'] = $sql_arr['email'];
163 $this->result->add($sql_arr);
166 return $this->result;
173 * @param array List of fields to search in
174 * @param string Search value
175 * @param boolean True if results are requested, False if count only
176 * @return Indexed list of contact records and 'count' value
178 function search($fields, $value, $strict=false, $select=true)
180 if (!is_array($fields))
181 $fields = array($fields);
183 $add_where = array();
184 foreach ($fields as $col)
186 if ($col == 'ID' || $col == $this->primary_key)
188 $ids = !is_array($value) ? split(',', $value) : $value;
189 $add_where[] = $this->primary_key.' IN ('.join(',', $ids).')';
192 $add_where[] = $this->db->quoteIdentifier($col).'='.$this->db->quote($value);
194 $add_where[] = $this->db->ilike($col, '%'.$value.'%');
197 if (!empty($add_where))
199 $this->set_search_set(join(' OR ', $add_where));
201 $this->list_records();
203 $this->result = $this->count();
206 return $this->result;
211 * Count number of available contacts in database
213 * @return Result array with values for 'count' and 'first'
217 // count contacts for this user
218 $sql_result = $this->db->query(
219 "SELECT COUNT(contact_id) AS rows
220 FROM ".$this->db_name."
223 ($this->filter ? " AND (".$this->filter.")" : ""),
226 $sql_arr = $this->db->fetch_assoc($sql_result);
227 return new rcube_result_set($sql_arr['rows'], ($this->list_page-1) * $this->page_size);;
232 * Return the last result set
234 * @return Result array or NULL if nothing selected yet
236 function get_result($as_res=true)
238 return $this->result;
243 * Get a specific contact record
245 * @param mixed record identifier(s)
246 * @return Result object with all record fields or False if not found
248 function get_record($id, $assoc=false)
250 // return cached result
251 if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id)
252 return $assoc ? $first : $this->result;
255 "SELECT * FROM ".$this->db_name."
262 if ($sql_arr = $this->db->fetch_assoc())
264 $sql_arr['ID'] = $sql_arr[$this->primary_key];
265 $this->result = new rcube_result_set(1);
266 $this->result->add($sql_arr);
269 return $assoc && $sql_arr ? $sql_arr : $this->result;
274 * Create a new contact record
276 * @param array Assoziative array with save data
277 * @return The created record ID on success, False on error
279 function insert($save_data, $check=false)
281 if (is_object($save_data) && is_a($save_data, rcube_result_set))
282 return $this->insert_recset($save_data, $check);
284 $insert_id = $existing = false;
287 $existing = $this->search('email', $save_data['email'], true, false);
289 $a_insert_cols = $a_insert_values = array();
290 foreach ($this->table_cols as $col)
291 if (isset($save_data[$col]))
293 $a_insert_cols[] = $this->db->quoteIdentifier($col);
294 $a_insert_values[] = $this->db->quote($save_data[$col]);
297 if (!$existing->count && !empty($a_insert_cols))
300 "INSERT INTO ".$this->db_name."
301 (user_id, changed, del, ".join(', ', $a_insert_cols).")
302 VALUES (?, ".$this->db->now().", 0, ".join(', ', $a_insert_values).")",
305 $insert_id = $this->db->insert_id(get_sequence_name('contacts'));
313 * Insert new contacts for each row in set
315 function insert_recset($result, $check=false)
318 while ($row = $result->next())
320 if ($insert = $this->insert($row, $check))
328 * Update a specific contact record
330 * @param mixed Record identifier
331 * @param array Assoziative array with save data
332 * @return True on success, False on error
334 function update($id, $save_cols)
337 $write_sql = array();
338 foreach ($this->table_cols as $col)
339 if (isset($save_cols[$col]))
340 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($save_cols[$col]));
342 if (!empty($write_sql))
345 "UPDATE ".$this->db_name."
346 SET changed=".$this->db->now().", ".join(', ', $write_sql)."
353 $updated = $this->db->affected_rows();
361 * Mark one or more contact records as deleted
363 * @param array Record identifiers
365 function delete($ids)
368 $ids = join(',', $ids);
371 "UPDATE ".$this->db_name."
374 AND contact_id IN (".$ids.")",
377 return $this->db->affected_rows();
382 * Remove all records from the database
384 function delete_all()
386 $this->db->query("DELETE FROM {$this->db_name} WHERE user_id=?", $this->user_id);
387 return $this->db->affected_rows();