4 +-----------------------------------------------------------------------+
5 | program/include/rcube_addressbook.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2006-2011, The Roundcube Dev Team |
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_addressbook.php 4965 2011-07-25 11:48:50Z alec $
24 * Abstract skeleton of an address book/repository
26 * @package Addressbook
28 abstract class rcube_addressbook
30 /** constants for error reporting **/
31 const ERROR_READ_ONLY = 1;
32 const ERROR_NO_CONNECTION = 2;
33 const ERROR_INCOMPLETE = 3;
34 const ERROR_SAVING = 4;
35 const ERROR_SEARCH = 5;
37 /** public properties (mandatory) */
39 public $groups = false;
40 public $readonly = true;
41 public $undelete = false;
42 public $ready = false;
43 public $group_id = null;
44 public $list_page = 1;
45 public $page_size = 10;
46 public $coltypes = array('name' => array('limit'=>1), 'firstname' => array('limit'=>1), 'surname' => array('limit'=>1), 'email' => array('limit'=>1));
51 * Returns addressbook name (e.g. for addressbooks listing)
53 abstract function get_name();
56 * Save a search string for future listings
58 * @param mixed Search params to use in listing method, obtained by get_search_set()
60 abstract function set_search_set($filter);
63 * Getter for saved search properties
65 * @return mixed Search properties used by this class
67 abstract function get_search_set();
70 * Reset saved results and search parameters
72 abstract function reset();
75 * Refresh saved search set after data has changed
77 * @return mixed New search set
79 function refresh_search()
81 return $this->get_search_set();
85 * List the current set of contact records
87 * @param array List of cols to show
88 * @param int Only return this number of records, use negative values for tail
89 * @return array Indexed list of contact records, each a hash array
91 abstract function list_records($cols=null, $subset=0);
96 * @param array List of fields to search in
97 * @param string Search value
98 * @param boolean True if results are requested, False if count only
99 * @param boolean True to skip the count query (select only)
100 * @param array List of fields that cannot be empty
101 * @return object rcube_result_set List of contact records and 'count' value
103 abstract function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array());
106 * Count number of available contacts in database
108 * @return rcube_result_set Result set with values for 'count' and 'first'
110 abstract function count();
113 * Return the last result set
115 * @return rcube_result_set Current result set or NULL if nothing selected yet
117 abstract function get_result();
120 * Get a specific contact record
122 * @param mixed record identifier(s)
123 * @param boolean True to return record as associative array, otherwise a result set is returned
125 * @return mixed Result object with all record fields or False if not found
127 abstract function get_record($id, $assoc=false);
130 * Returns the last error occured (e.g. when updating/inserting failed)
132 * @return array Hash array with the following fields: type, message
140 * Setter for errors for internal use
142 * @param int Error type (one of this class' error constants)
143 * @param string Error message (name of a text label)
145 protected function set_error($type, $message)
147 $this->error = array('type' => $type, 'message' => $message);
151 * Close connection to source
152 * Called on script shutdown
157 * Set internal list page
159 * @param number Page number to list
162 function set_page($page)
164 $this->list_page = (int)$page;
168 * Set internal page size
170 * @param number Number of messages to display on one page
173 function set_pagesize($size)
175 $this->page_size = (int)$size;
180 * Check the given data before saving.
181 * If input isn't valid, the message to display can be fetched using get_error()
183 * @param array Assoziative array with data to save
184 * @return boolean True if input is valid, False if not.
186 public function validate($save_data)
188 // check validity of email addresses
189 foreach ($this->get_col_values('email', $save_data, true) as $email) {
190 if (strlen($email)) {
191 if (!check_email(rcube_idn_to_ascii($email))) {
192 $this->set_error('warning', rcube_label(array('name' => 'emailformaterror', 'vars' => array('email' => $email))));
203 * Create a new contact record
205 * @param array Assoziative array with save data
206 * Keys: Field name with optional section in the form FIELD:SECTION
207 * Values: Field value. Can be either a string or an array of strings for multiple values
208 * @param boolean True to check for duplicates first
209 * @return mixed The created record ID on success, False on error
211 function insert($save_data, $check=false)
213 /* empty for read-only address books */
217 * Create new contact records for every item in the record set
219 * @param object rcube_result_set Recordset to insert
220 * @param boolean True to check for duplicates first
221 * @return array List of created record IDs
223 function insertMultiple($recset, $check=false)
226 if (is_object($recset) && is_a($recset, rcube_result_set)) {
227 while ($row = $recset->next()) {
228 if ($insert = $this->insert($row, $check))
236 * Update a specific contact record
238 * @param mixed Record identifier
239 * @param array Assoziative array with save data
240 * Keys: Field name with optional section in the form FIELD:SECTION
241 * Values: Field value. Can be either a string or an array of strings for multiple values
242 * @return boolean True on success, False on error
244 function update($id, $save_cols)
246 /* empty for read-only address books */
250 * Mark one or more contact records as deleted
252 * @param array Record identifiers
253 * @param bool Remove records irreversible (see self::undelete)
255 function delete($ids, $force=true)
257 /* empty for read-only address books */
261 * Unmark delete flag on contact record(s)
263 * @param array Record identifiers
265 function undelete($ids)
267 /* empty for read-only address books */
271 * Mark all records in database as deleted
273 function delete_all()
275 /* empty for read-only address books */
279 * Setter for the current group
280 * (empty, has to be re-implemented by extending class)
282 function set_group($gid) { }
285 * List all active contact groups of this source
287 * @param string Optional search string to match group name
288 * @return array Indexed list of contact groups, each a hash array
290 function list_groups($search = null)
292 /* empty for address books don't supporting groups */
297 * Create a contact group with the given name
299 * @param string The group name
300 * @return mixed False on error, array with record props in success
302 function create_group($name)
304 /* empty for address books don't supporting groups */
309 * Delete the given group and all linked group members
311 * @param string Group identifier
312 * @return boolean True on success, false if no data was changed
314 function delete_group($gid)
316 /* empty for address books don't supporting groups */
321 * Rename a specific contact group
323 * @param string Group identifier
324 * @param string New name to set for this group
325 * @param string New group identifier (if changed, otherwise don't set)
326 * @return boolean New name on success, false if no data was changed
328 function rename_group($gid, $newname, &$newid)
330 /* empty for address books don't supporting groups */
335 * Add the given contact records the a certain group
337 * @param string Group identifier
338 * @param array List of contact identifiers to be added
339 * @return int Number of contacts added
341 function add_to_group($group_id, $ids)
343 /* empty for address books don't supporting groups */
348 * Remove the given contact records from a certain group
350 * @param string Group identifier
351 * @param array List of contact identifiers to be removed
352 * @return int Number of deleted group members
354 function remove_from_group($group_id, $ids)
356 /* empty for address books don't supporting groups */
361 * Get group assignments of a specific contact record
363 * @param mixed Record identifier
365 * @return array List of assigned groups as ID=>Name pairs
368 function get_record_groups($id)
370 /* empty for address books don't supporting groups */
376 * Utility function to return all values of a certain data column
377 * either as flat list or grouped by subtype
379 * @param string Col name
380 * @param array Record data array as used for saving
381 * @param boolean True to return one array with all values, False for hash array with values grouped by type
382 * @return array List of column values
384 function get_col_values($col, $data, $flat = false)
387 foreach ($data as $c => $values) {
388 if (strpos($c, $col) === 0) {
390 $out = array_merge($out, (array)$values);
393 list($f, $type) = explode(':', $c);
394 $out[$type] = array_merge((array)$out[$type], (array)$values);
404 * Normalize the given string for fulltext search.
405 * Currently only optimized for Latin-1 characters; to be extended
407 * @param string Input string (UTF-8)
408 * @return string Normalized string
410 protected static function normalize_string($str)
413 $arr = explode(" ", preg_replace(
414 array('/[\s;\+\-\/]+/i', '/(\d)[-.\s]+(\d)/', '/\s\w{1,3}\s/'),
415 array(' ', '\\1\\2', ' '),
418 foreach ($arr as $i => $part) {
419 if (utf8_encode(utf8_decode($part)) == $part) { // is latin-1 ?
420 $arr[$i] = utf8_encode(strtr(strtolower(strtr(utf8_decode($part),
421 'ÇçäâàåéêëèïîìÅÉöôòüûùÿøØáíóúñÑÁÂÀãÃÊËÈÍÎÏÓÔõÕÚÛÙýÝ',
422 'ccaaaaeeeeiiiaeooouuuyooaiounnaaaaaeeeiiioooouuuyy')),
423 array('ß' => 'ss', 'ae' => 'a', 'oe' => 'o', 'ue' => 'u')));
426 $arr[$i] = mb_strtolower($part);
429 return join(" ", $arr);
434 * Compose a valid display name from the given structured contact data
436 * @param array Hash array with contact data as key-value pairs
437 * @param bool The name will be used on the list
439 * @return string Display name
441 public static function compose_display_name($contact, $list_mode = false)
443 $contact = rcmail::get_instance()->plugins->exec_hook('contact_displayname', $contact);
444 $fn = $contact['name'];
447 $fn = join(' ', array_filter(array($contact['prefix'], $contact['firstname'], $contact['middlename'], $contact['surname'], $contact['suffix'])));
449 // use email address part for name
450 $email = is_array($contact['email']) ? $contact['email'][0] : $contact['email'];
452 if ($email && (empty($fn) || $fn == $email)) {
453 // Use full email address on contacts list
457 list($emailname) = explode('@', $email);
458 if (preg_match('/(.*)[\.\-\_](.*)/', $emailname, $match))
459 $fn = trim(ucfirst($match[1]).' '.ucfirst($match[2]));
461 $fn = ucfirst($emailname);