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');
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();
62 * PHP 4 object constructor
64 * @see rcube_contacts::__construct()
66 function rcube_contacts($dbconn, $user)
68 $this->__construct($dbconn, $user);
73 * Set internal list page
75 * @param number Page number to list
78 function set_page($page)
80 $this->list_page = (int)$page;
85 * Set internal page size
87 * @param number Number of messages to display on one page
90 function set_pagesize($size)
92 $this->page_size = (int)$size;
97 * Save a search string for future listings
99 * @param string SQL params to use in listing method
101 function set_search_set($filter)
103 $this->filter = $filter;
108 * Getter for saved search properties
110 * @return mixed Search properties used by this class
112 function get_search_set()
114 return $this->filter;
119 * Reset all saved results and search parameters
123 $this->result = null;
124 $this->filter = null;
125 $this->search_fields = null;
126 $this->search_string = null;
131 * Close connection to source
132 * Called on script shutdown
138 * List the current set of contact records
140 * @param array List of cols to show
141 * @param int Only return this number of records, use negative values for tail
142 * @return array Indexed list of contact records, each a hash array
144 function list_records($cols=null, $subset=0)
146 // count contacts for this user
147 $this->result = $this->count();
150 // get contacts from DB
151 if ($this->result->count)
153 $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first;
154 $length = $subset != 0 ? abs($subset) : $this->page_size;
156 $sql_result = $this->db->limitquery(
157 "SELECT * FROM ".$this->db_name."
160 ($this->filter ? " AND (".$this->filter.")" : "") .
167 while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result)))
169 $sql_arr['ID'] = $sql_arr[$this->primary_key];
170 // make sure we have a name to display
171 if (empty($sql_arr['name']))
172 $sql_arr['name'] = $sql_arr['email'];
173 $this->result->add($sql_arr);
176 return $this->result;
183 * @param array List of fields to search in
184 * @param string Search value
185 * @param boolean True if results are requested, False if count only
186 * @return Indexed list of contact records and 'count' value
188 function search($fields, $value, $strict=false, $select=true)
190 if (!is_array($fields))
191 $fields = array($fields);
193 $add_where = array();
194 foreach ($fields as $col)
196 if ($col == 'ID' || $col == $this->primary_key)
198 $ids = !is_array($value) ? split(',', $value) : $value;
199 $add_where[] = $this->primary_key." IN (".join(',', $ids).")";
202 $add_where[] = $this->db->quoteIdentifier($col)."=".$this->db->quote($value);
204 $add_where[] = $this->db->quoteIdentifier($col)." LIKE ".$this->db->quote(strlen($value)>2 ? "%$value%" : "$value%");
207 if (!empty($add_where))
209 $this->set_search_set(join(' OR ', $add_where));
211 $this->list_records();
213 $this->result = $this->count();
216 return $this->result;
221 * Count number of available contacts in database
223 * @return Result array with values for 'count' and 'first'
227 // count contacts for this user
228 $sql_result = $this->db->query(
229 "SELECT COUNT(contact_id) AS rows
230 FROM ".$this->db_name."
233 ($this->filter ? " AND (".$this->filter.")" : ""),
236 $sql_arr = $this->db->fetch_assoc($sql_result);
237 return new rcube_result_set($sql_arr['rows'], ($this->list_page-1) * $this->page_size);;
242 * Return the last result set
244 * @return Result array or NULL if nothing selected yet
246 function get_result($as_res=true)
248 return $this->result;
253 * Get a specific contact record
255 * @param mixed record identifier(s)
256 * @return Result object with all record fields or False if not found
258 function get_record($id, $assoc=false)
260 // return cached result
261 if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id)
262 return $assoc ? $first : $this->result;
265 "SELECT * FROM ".$this->db_name."
272 if ($sql_arr = $this->db->fetch_assoc())
274 $sql_arr['ID'] = $sql_arr[$this->primary_key];
275 $this->result = new rcube_result_set(1);
276 $this->result->add($sql_arr);
279 return $assoc && $sql_arr ? $sql_arr : $this->result;
284 * Create a new contact record
286 * @param array Assoziative array with save data
287 * @return The created record ID on success, False on error
289 function insert($save_data, $check=false)
291 if (is_object($save_data) && is_a($save_data, rcube_result_set))
292 return $this->insert_recset($save_data, $check);
294 $insert_id = $existing = false;
297 $existing = $this->search('email', $save_data['email'], true, false);
299 $a_insert_cols = $a_insert_values = array();
300 foreach ($this->table_cols as $col)
301 if (isset($save_data[$col]))
303 $a_insert_cols[] = $this->db->quoteIdentifier($col);
304 $a_insert_values[] = $this->db->quote($save_data[$col]);
307 if (!$existing->count && !empty($a_insert_cols))
310 "INSERT INTO ".$this->db_name."
311 (user_id, changed, del, ".join(', ', $a_insert_cols).")
312 VALUES (?, ".$this->db->now().", 0, ".join(', ', $a_insert_values).")",
315 $insert_id = $this->db->insert_id(get_sequence_name('contacts'));
323 * Insert new contacts for each row in set
325 function insert_recset($result, $check=false)
328 while ($row = $result->next())
330 if ($insert = $this->insert($row, $check))
338 * Update a specific contact record
340 * @param mixed Record identifier
341 * @param array Assoziative array with save data
342 * @return True on success, False on error
344 function update($id, $save_cols)
347 $write_sql = array();
348 foreach ($this->table_cols as $col)
349 if (isset($save_cols[$col]))
350 $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($save_cols[$col]));
352 if (!empty($write_sql))
355 "UPDATE ".$this->db_name."
356 SET changed=".$this->db->now().", ".join(', ', $write_sql)."
363 $updated = $this->db->affected_rows();
371 * Mark one or more contact records as deleted
373 * @param array Record identifiers
375 function delete($ids)
378 $ids = join(',', $ids);
381 "UPDATE ".$this->db_name."
384 AND contact_id IN (".$ids.")",
387 return $this->db->affected_rows();
392 * Remove all records from the database
394 function delete_all()
396 $this->db->query("DELETE FROM {$this->db_name} WHERE user_id=?", $this->user_id);
397 return $this->db->affected_rows();