4 +-----------------------------------------------------------------------+
5 | program/include/rcube_contacts.inc |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2006-2007, 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 $
31 var $table_cols = array('name', 'email', 'firstname', 'surname');
33 /** public properties */
34 var $primary_key = 'contact_id';
35 var $readonly = false;
44 * @param object Instance of the rcube_db class
45 * @param integer User-ID
47 function __construct($dbconn, $user)
50 $this->db_name = get_table_name('contacts');
51 $this->user_id = $user;
52 $this->ready = $this->db && !$this->db->is_error();
56 * PHP 4 object constructor
58 * @see rcube_contacts::__construct
60 function rcube_contacts($dbconn, $user)
62 $this->__construct($dbconn, $user);
67 * Set internal list page
69 * @param number Page number to list
72 function set_page($page)
74 $this->list_page = (int)$page;
79 * Set internal page size
81 * @param number Number of messages to display on one page
84 function set_pagesize($size)
86 $this->page_size = (int)$size;
91 * Save a search string for future listings
93 * @param string SQL params to use in listing method
95 function set_search_set($filter)
97 $this->filter = $filter;
102 * Getter for saved search properties
104 * @return mixed Search properties used by this class
106 function get_search_set()
108 return $this->filter;
113 * Reset all saved results and search parameters
117 $this->result = null;
118 $this->filter = null;
119 $this->search_fields = null;
120 $this->search_string = null;
125 * List the current set of contact records
127 * @param array List of cols to show
128 * @return array Indexed list of contact records, each a hash array
130 function list_records($cols=null, $subset=0)
132 // count contacts for this user
133 $this->result = $this->count();
136 // get contacts from DB
137 if ($this->result->count)
139 $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first;
140 $length = $subset != 0 ? abs($subset) : $this->page_size;
142 $sql_result = $this->db->limitquery(
143 "SELECT * FROM ".$this->db_name."
146 ($this->filter ? " AND (".$this->filter.")" : "") .
153 while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result)))
155 $sql_arr['ID'] = $sql_arr[$this->primary_key];
156 // make sure we have a name to display
157 if (empty($sql_arr['name']))
158 $sql_arr['name'] = $sql_arr['email'];
159 $this->result->add($sql_arr);
162 return $this->result;
169 * @param array List of fields to search in
170 * @param string Search value
171 * @param boolean True if results are requested, False if count only
172 * @return Indexed list of contact records and 'count' value
174 function search($fields, $value, $select=true)
176 if (!is_array($fields))
177 $fields = array($fields);
179 $add_where = array();
180 foreach ($fields as $col)
182 if ($col == 'ID' || $col == $this->primary_key)
184 $ids = !is_array($value) ? split(',', $value) : $value;
185 $add_where[] = $this->primary_key." IN (".join(',', $ids).")";
188 $add_where[] = $this->db->quoteIdentifier($col)." LIKE ".$this->db->quote(strlen($value)>2 ? "%$value%" : "$value%");
191 if (!empty($add_where))
193 $this->set_search_set(join(' OR ', $add_where));
195 $this->list_records();
197 $this->result = $this->count();
200 return $this->result;
205 * Count number of available contacts in database
207 * @return Result array with values for 'count' and 'first'
211 // count contacts for this user
212 $sql_result = $this->db->query(
213 "SELECT COUNT(contact_id) AS rows
214 FROM ".$this->db_name."
217 ($this->filter ? " AND (".$this->filter.")" : ""),
220 $sql_arr = $this->db->fetch_assoc($sql_result);
221 return new rcube_result_set($sql_arr['rows'], ($this->list_page-1) * $this->page_size);;
226 * Return the last result set
228 * @return Result array or NULL if nothing selected yet
230 function get_result($as_res=true)
232 return $this->result;
237 * Get a specific contact record
239 * @param mixed record identifier(s)
240 * @return Result object with all record fields or False if not found
242 function get_record($id, $assoc=false)
244 // return cached result
245 if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id)
246 return $assoc ? $first : $this->result;
249 "SELECT * FROM ".$this->db_name."
256 if ($sql_arr = $this->db->fetch_assoc())
258 $sql_arr['ID'] = $sql_arr[$this->primary_key];
259 $this->result = new rcube_result_set(1);
260 $this->result->add($sql_arr);
263 return $assoc && $sql_arr ? $sql_arr : $this->result;
268 * Create a new contact record
270 * @param array Assoziative array with save data
271 * @return The created record ID on success, False on error
273 function insert($save_data, $check=false)
275 if (is_object($save_data) && is_a($save_data, rcube_result_set))
276 return $this->insert_recset($save_data, $check);
278 $insert_id = $existing = false;
281 $existing = $this->search('email', $save_data['email'], false);
283 $a_insert_cols = $a_insert_values = array();
284 foreach ($this->table_cols as $col)
285 if (isset($save_data[$col]))
287 $a_insert_cols[] = $this->db->quoteIdentifier($col);
288 $a_insert_values[] = $this->db->quote($save_data[$col]);
291 if (!$existing->count && !empty($a_insert_cols))
294 "INSERT INTO ".$this->db_name."
295 (user_id, changed, del, ".join(', ', $a_insert_cols).")
296 VALUES (?, ".$this->db->now().", 0, ".join(', ', $a_insert_values).")",
299 $insert_id = $this->db->insert_id(get_sequence_name('contacts'));
307 * Insert new contacts for each row in set
309 function insert_recset($result, $check=false)
312 while ($row = $result->next())
314 if ($insert = $this->insert($row, $check))
322 * Update a specific contact record
324 * @param mixed Record identifier
325 * @param array Assoziative array with save data
326 * @return True on success, False on error
328 function update($id, $save_cols)
331 $write_sql = array();
332 foreach ($this->table_cols as $col)
333 if (isset($save_cols[$col]))
334 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($save_cols[$col]));
336 if (!empty($write_sql))
339 "UPDATE ".$this->db_name."
340 SET changed=".$this->db->now().", ".join(', ', $write_sql)."
347 $updated = $this->db->affected_rows();
355 * Mark one or more contact records as deleted
357 * @param array Record identifiers
359 function delete($ids)
362 $ids = join(',', $ids);
365 "UPDATE ".$this->db_name."
368 AND contact_id IN (".$ids.")",
371 return $this->db->affected_rows();
378 * RoundCube result set class.
379 * Representing an address directory result set.
381 class rcube_result_set
386 var $records = array();
388 function __construct($c=0, $f=0)
390 $this->count = (int)$c;
391 $this->first = (int)$f;
394 function rcube_result_set($c=0, $f=0)
396 $this->__construct($c, $f);
401 $this->records[] = $rec;
406 return $this->records[$this->current++];
412 return $this->records[$this->current++];
418 return $this->iterate();