| [f115416] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| [47124c22] | 5 | | program/include/rcube_contacts.php | |
|---|
| [f115416] | 6 | | | |
|---|
| [e019f2d] | 7 | | This file is part of the Roundcube Webmail client | |
|---|
| [3e26373] | 8 | | Copyright (C) 2006-2011, The Roundcube Dev Team | |
|---|
| [f115416] | 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Interface to the local address book database | |
|---|
| 13 | | | |
|---|
| 14 | +-----------------------------------------------------------------------+ |
|---|
| 15 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 16 | +-----------------------------------------------------------------------+ |
|---|
| 17 | |
|---|
| [638fb8a] | 18 | $Id$ |
|---|
| [f115416] | 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| [6d969b4] | 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Model class for the local address book database |
|---|
| 25 | * |
|---|
| 26 | * @package Addressbook |
|---|
| 27 | */ |
|---|
| [cc97ea0] | 28 | class rcube_contacts extends rcube_addressbook |
|---|
| [f115416] | 29 | { |
|---|
| [495c0e5] | 30 | // protected for backward compat. with some plugins |
|---|
| [982e0b0] | 31 | protected $db_name = 'contacts'; |
|---|
| 32 | protected $db_groups = 'contactgroups'; |
|---|
| 33 | protected $db_groupmembers = 'contactgroupmembers'; |
|---|
| 34 | |
|---|
| [5c461ba] | 35 | /** |
|---|
| 36 | * Store database connection. |
|---|
| 37 | * |
|---|
| 38 | * @var rcube_mdb2 |
|---|
| 39 | */ |
|---|
| [3d6c04d] | 40 | private $db = null; |
|---|
| 41 | private $user_id = 0; |
|---|
| 42 | private $filter = null; |
|---|
| 43 | private $result = null; |
|---|
| [566b142] | 44 | private $cache; |
|---|
| [3e26373] | 45 | private $table_cols = array('name', 'email', 'firstname', 'surname'); |
|---|
| 46 | private $fulltext_cols = array('name', 'firstname', 'surname', 'middlename', 'nickname', |
|---|
| 47 | 'jobtitle', 'organization', 'department', 'maidenname', 'email', 'phone', |
|---|
| 48 | 'address', 'street', 'locality', 'zipcode', 'region', 'country', 'website', 'im', 'notes'); |
|---|
| [25fdec5] | 49 | |
|---|
| [982e0b0] | 50 | // public properties |
|---|
| [0501b63] | 51 | public $primary_key = 'contact_id'; |
|---|
| [5e90652] | 52 | public $name; |
|---|
| [0501b63] | 53 | public $readonly = false; |
|---|
| 54 | public $groups = true; |
|---|
| [7f5a849] | 55 | public $undelete = true; |
|---|
| [0501b63] | 56 | public $list_page = 1; |
|---|
| 57 | public $page_size = 10; |
|---|
| 58 | public $group_id = 0; |
|---|
| 59 | public $ready = false; |
|---|
| 60 | public $coltypes = array('name', 'firstname', 'surname', 'middlename', 'prefix', 'suffix', 'nickname', |
|---|
| 61 | 'jobtitle', 'organization', 'department', 'assistant', 'manager', |
|---|
| 62 | 'gender', 'maidenname', 'spouse', 'email', 'phone', 'address', |
|---|
| 63 | 'birthday', 'anniversary', 'website', 'im', 'notes', 'photo'); |
|---|
| [f115416] | 64 | |
|---|
| [25fdec5] | 65 | |
|---|
| [3d6c04d] | 66 | /** |
|---|
| 67 | * Object constructor |
|---|
| 68 | * |
|---|
| 69 | * @param object Instance of the rcube_db class |
|---|
| 70 | * @param integer User-ID |
|---|
| 71 | */ |
|---|
| 72 | function __construct($dbconn, $user) |
|---|
| 73 | { |
|---|
| 74 | $this->db = $dbconn; |
|---|
| 75 | $this->user_id = $user; |
|---|
| 76 | $this->ready = $this->db && !$this->db->is_error(); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /** |
|---|
| [cc90ed1] | 81 | * Returns addressbook name |
|---|
| 82 | */ |
|---|
| 83 | function get_name() |
|---|
| 84 | { |
|---|
| 85 | return $this->name; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| [3d6c04d] | 90 | * Save a search string for future listings |
|---|
| 91 | * |
|---|
| [cc90ed1] | 92 | * @param string SQL params to use in listing method |
|---|
| [3d6c04d] | 93 | */ |
|---|
| 94 | function set_search_set($filter) |
|---|
| 95 | { |
|---|
| 96 | $this->filter = $filter; |
|---|
| [566b142] | 97 | $this->cache = null; |
|---|
| [3d6c04d] | 98 | } |
|---|
| 99 | |
|---|
| [25fdec5] | 100 | |
|---|
| [3d6c04d] | 101 | /** |
|---|
| 102 | * Getter for saved search properties |
|---|
| 103 | * |
|---|
| 104 | * @return mixed Search properties used by this class |
|---|
| 105 | */ |
|---|
| 106 | function get_search_set() |
|---|
| 107 | { |
|---|
| 108 | return $this->filter; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** |
|---|
| 113 | * Setter for the current group |
|---|
| 114 | * (empty, has to be re-implemented by extending class) |
|---|
| 115 | */ |
|---|
| 116 | function set_group($gid) |
|---|
| 117 | { |
|---|
| 118 | $this->group_id = $gid; |
|---|
| [566b142] | 119 | $this->cache = null; |
|---|
| [3d6c04d] | 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Reset all saved results and search parameters |
|---|
| 125 | */ |
|---|
| 126 | function reset() |
|---|
| 127 | { |
|---|
| 128 | $this->result = null; |
|---|
| 129 | $this->filter = null; |
|---|
| [566b142] | 130 | $this->cache = null; |
|---|
| [3d6c04d] | 131 | } |
|---|
| [25fdec5] | 132 | |
|---|
| [3d6c04d] | 133 | |
|---|
| 134 | /** |
|---|
| 135 | * List all active contact groups of this source |
|---|
| 136 | * |
|---|
| 137 | * @param string Search string to match group name |
|---|
| 138 | * @return array Indexed list of contact groups, each a hash array |
|---|
| 139 | */ |
|---|
| 140 | function list_groups($search = null) |
|---|
| 141 | { |
|---|
| 142 | $results = array(); |
|---|
| [25fdec5] | 143 | |
|---|
| [3d6c04d] | 144 | if (!$this->groups) |
|---|
| 145 | return $results; |
|---|
| [d17a7fea] | 146 | |
|---|
| [6319670] | 147 | $sql_filter = $search ? " AND " . $this->db->ilike('name', '%'.$search.'%') : ''; |
|---|
| [3d6c04d] | 148 | |
|---|
| 149 | $sql_result = $this->db->query( |
|---|
| [982e0b0] | 150 | "SELECT * FROM ".get_table_name($this->db_groups). |
|---|
| [3d6c04d] | 151 | " WHERE del<>1". |
|---|
| 152 | " AND user_id=?". |
|---|
| 153 | $sql_filter. |
|---|
| 154 | " ORDER BY name", |
|---|
| 155 | $this->user_id); |
|---|
| 156 | |
|---|
| 157 | while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|---|
| 158 | $sql_arr['ID'] = $sql_arr['contactgroup_id']; |
|---|
| 159 | $results[] = $sql_arr; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | return $results; |
|---|
| [a61bbb2] | 163 | } |
|---|
| [3d6c04d] | 164 | |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| [dc6c4f4] | 167 | * Get group properties such as name and email address(es) |
|---|
| 168 | * |
|---|
| 169 | * @param string Group identifier |
|---|
| 170 | * @return array Group properties as hash array |
|---|
| 171 | */ |
|---|
| 172 | function get_group($group_id) |
|---|
| 173 | { |
|---|
| 174 | $sql_result = $this->db->query( |
|---|
| 175 | "SELECT * FROM ".get_table_name($this->db_groups). |
|---|
| 176 | " WHERE del<>1". |
|---|
| 177 | " AND contactgroup_id=?". |
|---|
| 178 | " AND user_id=?", |
|---|
| 179 | $group_id, $this->user_id); |
|---|
| [f21a04c] | 180 | |
|---|
| [dc6c4f4] | 181 | if ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|---|
| 182 | $sql_arr['ID'] = $sql_arr['contactgroup_id']; |
|---|
| 183 | return $sql_arr; |
|---|
| 184 | } |
|---|
| [f21a04c] | 185 | |
|---|
| [dc6c4f4] | 186 | return null; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| [3d6c04d] | 190 | * List the current set of contact records |
|---|
| 191 | * |
|---|
| [0501b63] | 192 | * @param array List of cols to show, Null means all |
|---|
| [3d6c04d] | 193 | * @param int Only return this number of records, use negative values for tail |
|---|
| 194 | * @param boolean True to skip the count query (select only) |
|---|
| 195 | * @return array Indexed list of contact records, each a hash array |
|---|
| 196 | */ |
|---|
| 197 | function list_records($cols=null, $subset=0, $nocount=false) |
|---|
| 198 | { |
|---|
| 199 | if ($nocount || $this->list_page <= 1) { |
|---|
| 200 | // create dummy result, we don't need a count now |
|---|
| 201 | $this->result = new rcube_result_set(); |
|---|
| 202 | } else { |
|---|
| 203 | // count all records |
|---|
| 204 | $this->result = $this->count(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | $start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; |
|---|
| 208 | $length = $subset != 0 ? abs($subset) : $this->page_size; |
|---|
| 209 | |
|---|
| 210 | if ($this->group_id) |
|---|
| [982e0b0] | 211 | $join = " LEFT JOIN ".get_table_name($this->db_groupmembers)." AS m". |
|---|
| [3d6c04d] | 212 | " ON (m.contact_id = c.".$this->primary_key.")"; |
|---|
| 213 | |
|---|
| 214 | $sql_result = $this->db->limitquery( |
|---|
| [982e0b0] | 215 | "SELECT * FROM ".get_table_name($this->db_name)." AS c" . |
|---|
| [821a560] | 216 | $join . |
|---|
| [3d6c04d] | 217 | " WHERE c.del<>1" . |
|---|
| [566b142] | 218 | " AND c.user_id=?" . |
|---|
| 219 | ($this->group_id ? " AND m.contactgroup_id=?" : ""). |
|---|
| 220 | ($this->filter ? " AND (".$this->filter.")" : "") . |
|---|
| [0ec7fe4] | 221 | " ORDER BY ". $this->db->concat('c.name', 'c.email'), |
|---|
| [3d6c04d] | 222 | $start_row, |
|---|
| 223 | $length, |
|---|
| 224 | $this->user_id, |
|---|
| 225 | $this->group_id); |
|---|
| 226 | |
|---|
| [0501b63] | 227 | // determine whether we have to parse the vcard or if only db cols are requested |
|---|
| 228 | $read_vcard = !$cols || count(array_intersect($cols, $this->table_cols)) < count($cols); |
|---|
| [3cacf94] | 229 | |
|---|
| [3d6c04d] | 230 | while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|---|
| 231 | $sql_arr['ID'] = $sql_arr[$this->primary_key]; |
|---|
| [0501b63] | 232 | |
|---|
| 233 | if ($read_vcard) |
|---|
| 234 | $sql_arr = $this->convert_db_data($sql_arr); |
|---|
| 235 | else |
|---|
| 236 | $sql_arr['email'] = preg_split('/,\s*/', $sql_arr['email']); |
|---|
| [445a4ca] | 237 | |
|---|
| [3d6c04d] | 238 | // make sure we have a name to display |
|---|
| [445a4ca] | 239 | if (empty($sql_arr['name'])) { |
|---|
| 240 | if (empty($sql_arr['email'])) |
|---|
| 241 | $sql_arr['email'] = $this->get_col_values('email', $sql_arr, true); |
|---|
| [0501b63] | 242 | $sql_arr['name'] = $sql_arr['email'][0]; |
|---|
| [445a4ca] | 243 | } |
|---|
| [0501b63] | 244 | |
|---|
| [3d6c04d] | 245 | $this->result->add($sql_arr); |
|---|
| 246 | } |
|---|
| [25fdec5] | 247 | |
|---|
| [3d6c04d] | 248 | $cnt = count($this->result->records); |
|---|
| 249 | |
|---|
| 250 | // update counter |
|---|
| 251 | if ($nocount) |
|---|
| 252 | $this->result->count = $cnt; |
|---|
| 253 | else if ($this->list_page <= 1) { |
|---|
| 254 | if ($cnt < $this->page_size && $subset == 0) |
|---|
| 255 | $this->result->count = $cnt; |
|---|
| [821a560] | 256 | else if (isset($this->cache['count'])) |
|---|
| 257 | $this->result->count = $this->cache['count']; |
|---|
| [3d6c04d] | 258 | else |
|---|
| 259 | $this->result->count = $this->_count(); |
|---|
| 260 | } |
|---|
| [25fdec5] | 261 | |
|---|
| [3d6c04d] | 262 | return $this->result; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | /** |
|---|
| 267 | * Search contacts |
|---|
| 268 | * |
|---|
| [e9a9f2f6] | 269 | * @param mixed $fields The field name of array of field names to search in |
|---|
| 270 | * @param mixed $value Search value (or array of values when $fields is array) |
|---|
| [f21a04c] | 271 | * @param int $mode Matching mode: |
|---|
| 272 | * 0 - partial (*abc*), |
|---|
| 273 | * 1 - strict (=), |
|---|
| 274 | * 2 - prefix (abc*) |
|---|
| [e9a9f2f6] | 275 | * @param boolean $select True if results are requested, False if count only |
|---|
| 276 | * @param boolean $nocount True to skip the count query (select only) |
|---|
| 277 | * @param array $required List of fields that cannot be empty |
|---|
| 278 | * |
|---|
| [0501b63] | 279 | * @return object rcube_result_set Contact records and 'count' value |
|---|
| [3d6c04d] | 280 | */ |
|---|
| [f21a04c] | 281 | function search($fields, $value, $mode=0, $select=true, $nocount=false, $required=array()) |
|---|
| [f115416] | 282 | { |
|---|
| [3d6c04d] | 283 | if (!is_array($fields)) |
|---|
| 284 | $fields = array($fields); |
|---|
| [25fdec5] | 285 | if (!is_array($required) && !empty($required)) |
|---|
| 286 | $required = array($required); |
|---|
| 287 | |
|---|
| 288 | $where = $and_where = array(); |
|---|
| [f21a04c] | 289 | $mode = intval($mode); |
|---|
| [566b142] | 290 | |
|---|
| [e9a9f2f6] | 291 | foreach ($fields as $idx => $col) { |
|---|
| 292 | // direct ID search |
|---|
| [3d6c04d] | 293 | if ($col == 'ID' || $col == $this->primary_key) { |
|---|
| [25fdec5] | 294 | $ids = !is_array($value) ? explode(',', $value) : $value; |
|---|
| 295 | $ids = $this->db->array2list($ids, 'integer'); |
|---|
| 296 | $where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; |
|---|
| [e9a9f2f6] | 297 | continue; |
|---|
| [3d6c04d] | 298 | } |
|---|
| [e9a9f2f6] | 299 | // fulltext search in all fields |
|---|
| [3e26373] | 300 | else if ($col == '*') { |
|---|
| 301 | $words = array(); |
|---|
| [f21a04c] | 302 | foreach (explode(" ", self::normalize_string($value)) as $word) { |
|---|
| 303 | switch ($mode) { |
|---|
| 304 | case 1: // strict |
|---|
| 305 | $words[] = '(' . $this->db->ilike('words', $word.' %') |
|---|
| 306 | . ' OR ' . $this->db->ilike('words', '% '.$word.' %') |
|---|
| 307 | . ' OR ' . $this->db->ilike('words', '% '.$word) . ')'; |
|---|
| 308 | break; |
|---|
| 309 | case 2: // prefix |
|---|
| 310 | $words[] = '(' . $this->db->ilike('words', $word.'%') |
|---|
| 311 | . ' OR ' . $this->db->ilike('words', '% '.$word.'%') . ')'; |
|---|
| 312 | break; |
|---|
| 313 | default: // partial |
|---|
| 314 | $words[] = $this->db->ilike('words', '%'.$word.'%'); |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| [3e26373] | 317 | $where[] = '(' . join(' AND ', $words) . ')'; |
|---|
| [3cacf94] | 318 | } |
|---|
| [e9a9f2f6] | 319 | else { |
|---|
| 320 | $val = is_array($value) ? $value[$idx] : $value; |
|---|
| 321 | // table column |
|---|
| 322 | if (in_array($col, $this->table_cols)) { |
|---|
| [f21a04c] | 323 | switch ($mode) { |
|---|
| 324 | case 1: // strict |
|---|
| [e9a9f2f6] | 325 | $where[] = $this->db->quoteIdentifier($col).' = '.$this->db->quote($val); |
|---|
| [f21a04c] | 326 | break; |
|---|
| 327 | case 2: // prefix |
|---|
| 328 | $where[] = $this->db->ilike($col, $val.'%'); |
|---|
| 329 | break; |
|---|
| 330 | default: // partial |
|---|
| [e9a9f2f6] | 331 | $where[] = $this->db->ilike($col, '%'.$val.'%'); |
|---|
| 332 | } |
|---|
| 333 | } |
|---|
| 334 | // vCard field |
|---|
| 335 | else { |
|---|
| 336 | if (in_array($col, $this->fulltext_cols)) { |
|---|
| [f21a04c] | 337 | foreach (explode(" ", self::normalize_string($val)) as $word) { |
|---|
| 338 | switch ($mode) { |
|---|
| 339 | case 1: // strict |
|---|
| 340 | $words[] = '(' . $this->db->ilike('words', $word.' %') |
|---|
| 341 | . ' OR ' . $this->db->ilike('words', '% '.$word.' %') |
|---|
| 342 | . ' OR ' . $this->db->ilike('words', '% '.$word) . ')'; |
|---|
| 343 | break; |
|---|
| 344 | case 2: // prefix |
|---|
| 345 | $words[] = '(' . $this->db->ilike('words', $word.'%') |
|---|
| 346 | . ' OR ' . $this->db->ilike('words', ' '.$word.'%') . ')'; |
|---|
| 347 | break; |
|---|
| 348 | default: // partial |
|---|
| 349 | $words[] = $this->db->ilike('words', '%'.$word.'%'); |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| [e9a9f2f6] | 352 | $where[] = '(' . join(' AND ', $words) . ')'; |
|---|
| 353 | } |
|---|
| 354 | if (is_array($value)) |
|---|
| [a5be870] | 355 | $post_search[$col] = mb_strtolower($val); |
|---|
| [e9a9f2f6] | 356 | } |
|---|
| [3cacf94] | 357 | } |
|---|
| [25fdec5] | 358 | } |
|---|
| 359 | |
|---|
| [03eb13f] | 360 | foreach (array_intersect($required, $this->table_cols) as $col) { |
|---|
| [25fdec5] | 361 | $and_where[] = $this->db->quoteIdentifier($col).' <> '.$this->db->quote(''); |
|---|
| [3d6c04d] | 362 | } |
|---|
| [25fdec5] | 363 | |
|---|
| [e9a9f2f6] | 364 | if (!empty($where)) { |
|---|
| 365 | // use AND operator for advanced searches |
|---|
| 366 | $where = join(is_array($value) ? ' AND ' : ' OR ', $where); |
|---|
| 367 | } |
|---|
| [25fdec5] | 368 | |
|---|
| 369 | if (!empty($and_where)) |
|---|
| 370 | $where = ($where ? "($where) AND " : '') . join(' AND ', $and_where); |
|---|
| 371 | |
|---|
| [e9a9f2f6] | 372 | // Post-searching in vCard data fields |
|---|
| 373 | // we will search in all records and then build a where clause for their IDs |
|---|
| 374 | if (!empty($post_search)) { |
|---|
| 375 | $ids = array(0); |
|---|
| 376 | // build key name regexp |
|---|
| [62e2254] | 377 | $regexp = '/^(' . implode(array_keys($post_search), '|') . ')(?:.*)$/'; |
|---|
| [e9a9f2f6] | 378 | // use initial WHERE clause, to limit records number if possible |
|---|
| 379 | if (!empty($where)) |
|---|
| 380 | $this->set_search_set($where); |
|---|
| 381 | |
|---|
| 382 | // count result pages |
|---|
| 383 | $cnt = $this->count(); |
|---|
| 384 | $pages = ceil($cnt / $this->page_size); |
|---|
| 385 | $scnt = count($post_search); |
|---|
| 386 | |
|---|
| 387 | // get (paged) result |
|---|
| 388 | for ($i=0; $i<$pages; $i++) { |
|---|
| 389 | $this->list_records(null, $i, true); |
|---|
| 390 | while ($row = $this->result->next()) { |
|---|
| 391 | $id = $row[$this->primary_key]; |
|---|
| [5148d38] | 392 | $found = array(); |
|---|
| [e9a9f2f6] | 393 | foreach (preg_grep($regexp, array_keys($row)) as $col) { |
|---|
| 394 | $pos = strpos($col, ':'); |
|---|
| 395 | $colname = $pos ? substr($col, 0, $pos) : $col; |
|---|
| 396 | $search = $post_search[$colname]; |
|---|
| 397 | foreach ((array)$row[$col] as $value) { |
|---|
| 398 | // composite field, e.g. address |
|---|
| [f21a04c] | 399 | foreach ((array)$value as $val) { |
|---|
| 400 | $val = mb_strtolower($val); |
|---|
| 401 | switch ($mode) { |
|---|
| 402 | case 1: |
|---|
| 403 | $got = ($val == $search); |
|---|
| 404 | break; |
|---|
| 405 | case 2: |
|---|
| 406 | $got = ($search == substr($val, 0, strlen($search))); |
|---|
| 407 | break; |
|---|
| 408 | default: |
|---|
| 409 | $got = (strpos($val, $search) !== false); |
|---|
| 410 | break; |
|---|
| 411 | } |
|---|
| 412 | |
|---|
| 413 | if ($got) { |
|---|
| 414 | $found[$colname] = true; |
|---|
| 415 | break 2; |
|---|
| 416 | } |
|---|
| [e9a9f2f6] | 417 | } |
|---|
| 418 | } |
|---|
| 419 | } |
|---|
| 420 | // all fields match |
|---|
| [5148d38] | 421 | if (count($found) >= $scnt) { |
|---|
| [e9a9f2f6] | 422 | $ids[] = $id; |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | // build WHERE clause |
|---|
| 428 | $ids = $this->db->array2list($ids, 'integer'); |
|---|
| 429 | $where = 'c.' . $this->primary_key.' IN ('.$ids.')'; |
|---|
| [a5be870] | 430 | // reset counter |
|---|
| [e9a9f2f6] | 431 | unset($this->cache['count']); |
|---|
| [a5be870] | 432 | |
|---|
| 433 | // when we know we have an empty result |
|---|
| 434 | if ($ids == '0') { |
|---|
| 435 | $this->set_search_set($where); |
|---|
| 436 | return ($this->result = new rcube_result_set(0, 0)); |
|---|
| 437 | } |
|---|
| [e9a9f2f6] | 438 | } |
|---|
| 439 | |
|---|
| [25fdec5] | 440 | if (!empty($where)) { |
|---|
| 441 | $this->set_search_set($where); |
|---|
| [3d6c04d] | 442 | if ($select) |
|---|
| 443 | $this->list_records(null, 0, $nocount); |
|---|
| 444 | else |
|---|
| 445 | $this->result = $this->count(); |
|---|
| 446 | } |
|---|
| 447 | |
|---|
| [e9a9f2f6] | 448 | return $this->result; |
|---|
| [3d6c04d] | 449 | } |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | /** |
|---|
| 453 | * Count number of available contacts in database |
|---|
| 454 | * |
|---|
| 455 | * @return rcube_result_set Result object |
|---|
| 456 | */ |
|---|
| 457 | function count() |
|---|
| [f115416] | 458 | { |
|---|
| [566b142] | 459 | $count = isset($this->cache['count']) ? $this->cache['count'] : $this->_count(); |
|---|
| [25fdec5] | 460 | |
|---|
| [566b142] | 461 | return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); |
|---|
| [f115416] | 462 | } |
|---|
| [3d6c04d] | 463 | |
|---|
| 464 | |
|---|
| 465 | /** |
|---|
| 466 | * Count number of available contacts in database |
|---|
| 467 | * |
|---|
| 468 | * @return int Contacts count |
|---|
| 469 | */ |
|---|
| 470 | private function _count() |
|---|
| [f115416] | 471 | { |
|---|
| [3d6c04d] | 472 | if ($this->group_id) |
|---|
| [982e0b0] | 473 | $join = " LEFT JOIN ".get_table_name($this->db_groupmembers)." AS m". |
|---|
| [3d6c04d] | 474 | " ON (m.contact_id=c.".$this->primary_key.")"; |
|---|
| [25fdec5] | 475 | |
|---|
| [3d6c04d] | 476 | // count contacts for this user |
|---|
| 477 | $sql_result = $this->db->query( |
|---|
| 478 | "SELECT COUNT(c.contact_id) AS rows". |
|---|
| [982e0b0] | 479 | " FROM ".get_table_name($this->db_name)." AS c". |
|---|
| [821a560] | 480 | $join. |
|---|
| 481 | " WHERE c.del<>1". |
|---|
| [3d6c04d] | 482 | " AND c.user_id=?". |
|---|
| 483 | ($this->group_id ? " AND m.contactgroup_id=?" : ""). |
|---|
| 484 | ($this->filter ? " AND (".$this->filter.")" : ""), |
|---|
| 485 | $this->user_id, |
|---|
| 486 | $this->group_id |
|---|
| 487 | ); |
|---|
| 488 | |
|---|
| 489 | $sql_arr = $this->db->fetch_assoc($sql_result); |
|---|
| [566b142] | 490 | |
|---|
| 491 | $this->cache['count'] = (int) $sql_arr['rows']; |
|---|
| 492 | |
|---|
| 493 | return $this->cache['count']; |
|---|
| [f115416] | 494 | } |
|---|
| [3d6c04d] | 495 | |
|---|
| 496 | |
|---|
| 497 | /** |
|---|
| 498 | * Return the last result set |
|---|
| 499 | * |
|---|
| [5c461ba] | 500 | * @return mixed Result array or NULL if nothing selected yet |
|---|
| [3d6c04d] | 501 | */ |
|---|
| 502 | function get_result() |
|---|
| [f115416] | 503 | { |
|---|
| [3d6c04d] | 504 | return $this->result; |
|---|
| [f115416] | 505 | } |
|---|
| [25fdec5] | 506 | |
|---|
| 507 | |
|---|
| [3d6c04d] | 508 | /** |
|---|
| 509 | * Get a specific contact record |
|---|
| 510 | * |
|---|
| 511 | * @param mixed record identifier(s) |
|---|
| [5c461ba] | 512 | * @return mixed Result object with all record fields or False if not found |
|---|
| [3d6c04d] | 513 | */ |
|---|
| 514 | function get_record($id, $assoc=false) |
|---|
| 515 | { |
|---|
| 516 | // return cached result |
|---|
| 517 | if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id) |
|---|
| 518 | return $assoc ? $first : $this->result; |
|---|
| [25fdec5] | 519 | |
|---|
| [3d6c04d] | 520 | $this->db->query( |
|---|
| [982e0b0] | 521 | "SELECT * FROM ".get_table_name($this->db_name). |
|---|
| [3d6c04d] | 522 | " WHERE contact_id=?". |
|---|
| 523 | " AND user_id=?". |
|---|
| 524 | " AND del<>1", |
|---|
| 525 | $id, |
|---|
| 526 | $this->user_id |
|---|
| 527 | ); |
|---|
| 528 | |
|---|
| 529 | if ($sql_arr = $this->db->fetch_assoc()) { |
|---|
| [0501b63] | 530 | $record = $this->convert_db_data($sql_arr); |
|---|
| [3d6c04d] | 531 | $this->result = new rcube_result_set(1); |
|---|
| [0501b63] | 532 | $this->result->add($record); |
|---|
| [3d6c04d] | 533 | } |
|---|
| 534 | |
|---|
| [0501b63] | 535 | return $assoc && $record ? $record : $this->result; |
|---|
| [3d6c04d] | 536 | } |
|---|
| 537 | |
|---|
| 538 | |
|---|
| 539 | /** |
|---|
| [b393e54] | 540 | * Get group assignments of a specific contact record |
|---|
| [cb7d32e] | 541 | * |
|---|
| 542 | * @param mixed Record identifier |
|---|
| [b393e54] | 543 | * @return array List of assigned groups as ID=>Name pairs |
|---|
| [cb7d32e] | 544 | */ |
|---|
| 545 | function get_record_groups($id) |
|---|
| 546 | { |
|---|
| 547 | $results = array(); |
|---|
| 548 | |
|---|
| 549 | if (!$this->groups) |
|---|
| 550 | return $results; |
|---|
| 551 | |
|---|
| 552 | $sql_result = $this->db->query( |
|---|
| 553 | "SELECT cgm.contactgroup_id, cg.name FROM " . get_table_name($this->db_groupmembers) . " AS cgm" . |
|---|
| 554 | " LEFT JOIN " . get_table_name($this->db_groups) . " AS cg ON (cgm.contactgroup_id = cg.contactgroup_id AND cg.del<>1)" . |
|---|
| 555 | " WHERE cgm.contact_id=?", |
|---|
| 556 | $id |
|---|
| 557 | ); |
|---|
| 558 | while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|---|
| 559 | $results[$sql_arr['contactgroup_id']] = $sql_arr['name']; |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | return $results; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | /** |
|---|
| [07b95dc] | 567 | * Check the given data before saving. |
|---|
| 568 | * If input not valid, the message to display can be fetched using get_error() |
|---|
| 569 | * |
|---|
| 570 | * @param array Assoziative array with data to save |
|---|
| [39cafac] | 571 | * @param boolean Try to fix/complete record automatically |
|---|
| [07b95dc] | 572 | * @return boolean True if input is valid, False if not. |
|---|
| 573 | */ |
|---|
| [39cafac] | 574 | public function validate(&$save_data, $autofix = false) |
|---|
| [07b95dc] | 575 | { |
|---|
| [e848180] | 576 | // validate e-mail addresses |
|---|
| [39cafac] | 577 | $valid = parent::validate($save_data, $autofix); |
|---|
| [07b95dc] | 578 | |
|---|
| [e848180] | 579 | // require at least one e-mail address (syntax check is already done) |
|---|
| [07b95dc] | 580 | if ($valid && !array_filter($this->get_col_values('email', $save_data, true))) { |
|---|
| [39cafac] | 581 | $this->set_error(self::ERROR_VALIDATE, 'noemailwarning'); |
|---|
| [07b95dc] | 582 | $valid = false; |
|---|
| 583 | } |
|---|
| 584 | |
|---|
| 585 | return $valid; |
|---|
| 586 | } |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | /** |
|---|
| [3d6c04d] | 590 | * Create a new contact record |
|---|
| 591 | * |
|---|
| [b393e54] | 592 | * @param array Associative array with save data |
|---|
| [5c461ba] | 593 | * @return integer|boolean The created record ID on success, False on error |
|---|
| [3d6c04d] | 594 | */ |
|---|
| 595 | function insert($save_data, $check=false) |
|---|
| [f115416] | 596 | { |
|---|
| [0501b63] | 597 | if (!is_array($save_data)) |
|---|
| 598 | return false; |
|---|
| [3d6c04d] | 599 | |
|---|
| 600 | $insert_id = $existing = false; |
|---|
| 601 | |
|---|
| [0501b63] | 602 | if ($check) { |
|---|
| 603 | foreach ($save_data as $col => $values) { |
|---|
| 604 | if (strpos($col, 'email') === 0) { |
|---|
| 605 | foreach ((array)$values as $email) { |
|---|
| [715c796] | 606 | if ($existing = $this->search('email', $email, false, false)) |
|---|
| [0501b63] | 607 | break 2; |
|---|
| 608 | } |
|---|
| 609 | } |
|---|
| 610 | } |
|---|
| 611 | } |
|---|
| [3d6c04d] | 612 | |
|---|
| [0501b63] | 613 | $save_data = $this->convert_save_data($save_data); |
|---|
| [3d6c04d] | 614 | $a_insert_cols = $a_insert_values = array(); |
|---|
| 615 | |
|---|
| [0501b63] | 616 | foreach ($save_data as $col => $value) { |
|---|
| 617 | $a_insert_cols[] = $this->db->quoteIdentifier($col); |
|---|
| 618 | $a_insert_values[] = $this->db->quote($value); |
|---|
| 619 | } |
|---|
| [3d6c04d] | 620 | |
|---|
| 621 | if (!$existing->count && !empty($a_insert_cols)) { |
|---|
| 622 | $this->db->query( |
|---|
| [982e0b0] | 623 | "INSERT INTO ".get_table_name($this->db_name). |
|---|
| [3d6c04d] | 624 | " (user_id, changed, del, ".join(', ', $a_insert_cols).")". |
|---|
| 625 | " VALUES (".intval($this->user_id).", ".$this->db->now().", 0, ".join(', ', $a_insert_values).")" |
|---|
| 626 | ); |
|---|
| 627 | |
|---|
| [982e0b0] | 628 | $insert_id = $this->db->insert_id($this->db_name); |
|---|
| [3d6c04d] | 629 | } |
|---|
| 630 | |
|---|
| 631 | // also add the newly created contact to the active group |
|---|
| 632 | if ($insert_id && $this->group_id) |
|---|
| 633 | $this->add_to_group($this->group_id, $insert_id); |
|---|
| 634 | |
|---|
| [566b142] | 635 | $this->cache = null; |
|---|
| 636 | |
|---|
| [3d6c04d] | 637 | return $insert_id; |
|---|
| [f115416] | 638 | } |
|---|
| 639 | |
|---|
| [3d6c04d] | 640 | |
|---|
| 641 | /** |
|---|
| 642 | * Update a specific contact record |
|---|
| 643 | * |
|---|
| 644 | * @param mixed Record identifier |
|---|
| 645 | * @param array Assoziative array with save data |
|---|
| [5c461ba] | 646 | * @return boolean True on success, False on error |
|---|
| [3d6c04d] | 647 | */ |
|---|
| 648 | function update($id, $save_cols) |
|---|
| 649 | { |
|---|
| 650 | $updated = false; |
|---|
| 651 | $write_sql = array(); |
|---|
| [0501b63] | 652 | $record = $this->get_record($id, true); |
|---|
| 653 | $save_cols = $this->convert_save_data($save_cols, $record); |
|---|
| [feaf7b5] | 654 | |
|---|
| [0501b63] | 655 | foreach ($save_cols as $col => $value) { |
|---|
| 656 | $write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($value)); |
|---|
| 657 | } |
|---|
| [3d6c04d] | 658 | |
|---|
| 659 | if (!empty($write_sql)) { |
|---|
| 660 | $this->db->query( |
|---|
| [982e0b0] | 661 | "UPDATE ".get_table_name($this->db_name). |
|---|
| [3d6c04d] | 662 | " SET changed=".$this->db->now().", ".join(', ', $write_sql). |
|---|
| 663 | " WHERE contact_id=?". |
|---|
| 664 | " AND user_id=?". |
|---|
| 665 | " AND del<>1", |
|---|
| 666 | $id, |
|---|
| 667 | $this->user_id |
|---|
| 668 | ); |
|---|
| 669 | |
|---|
| 670 | $updated = $this->db->affected_rows(); |
|---|
| [0501b63] | 671 | $this->result = null; // clear current result (from get_record()) |
|---|
| [3d6c04d] | 672 | } |
|---|
| [25fdec5] | 673 | |
|---|
| [3d6c04d] | 674 | return $updated; |
|---|
| 675 | } |
|---|
| [63fda8a] | 676 | |
|---|
| 677 | |
|---|
| [0501b63] | 678 | private function convert_db_data($sql_arr) |
|---|
| 679 | { |
|---|
| 680 | $record = array(); |
|---|
| 681 | $record['ID'] = $sql_arr[$this->primary_key]; |
|---|
| [63fda8a] | 682 | |
|---|
| [0501b63] | 683 | if ($sql_arr['vcard']) { |
|---|
| 684 | unset($sql_arr['email']); |
|---|
| 685 | $vcard = new rcube_vcard($sql_arr['vcard']); |
|---|
| 686 | $record += $vcard->get_assoc() + $sql_arr; |
|---|
| 687 | } |
|---|
| 688 | else { |
|---|
| 689 | $record += $sql_arr; |
|---|
| 690 | $record['email'] = preg_split('/,\s*/', $record['email']); |
|---|
| 691 | } |
|---|
| [63fda8a] | 692 | |
|---|
| [0501b63] | 693 | return $record; |
|---|
| 694 | } |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | private function convert_save_data($save_data, $record = array()) |
|---|
| 698 | { |
|---|
| 699 | $out = array(); |
|---|
| [3e26373] | 700 | $words = ''; |
|---|
| [0501b63] | 701 | |
|---|
| 702 | // copy values into vcard object |
|---|
| 703 | $vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard']); |
|---|
| 704 | $vcard->reset(); |
|---|
| 705 | foreach ($save_data as $key => $values) { |
|---|
| 706 | list($field, $section) = explode(':', $key); |
|---|
| [3e26373] | 707 | $fulltext = in_array($field, $this->fulltext_cols); |
|---|
| [0501b63] | 708 | foreach ((array)$values as $value) { |
|---|
| 709 | if (isset($value)) |
|---|
| 710 | $vcard->set($field, $value, $section); |
|---|
| [3e26373] | 711 | if ($fulltext && is_array($value)) |
|---|
| 712 | $words .= ' ' . self::normalize_string(join(" ", $value)); |
|---|
| 713 | else if ($fulltext && strlen($value) >= 3) |
|---|
| 714 | $words .= ' ' . self::normalize_string($value); |
|---|
| [0501b63] | 715 | } |
|---|
| 716 | } |
|---|
| [569f830] | 717 | $out['vcard'] = $vcard->export(false); |
|---|
| [0501b63] | 718 | |
|---|
| 719 | foreach ($this->table_cols as $col) { |
|---|
| 720 | $key = $col; |
|---|
| 721 | if (!isset($save_data[$key])) |
|---|
| 722 | $key .= ':home'; |
|---|
| 723 | if (isset($save_data[$key])) |
|---|
| 724 | $out[$col] = is_array($save_data[$key]) ? join(',', $save_data[$key]) : $save_data[$key]; |
|---|
| 725 | } |
|---|
| 726 | |
|---|
| 727 | // save all e-mails in database column |
|---|
| 728 | $out['email'] = join(", ", $vcard->email); |
|---|
| 729 | |
|---|
| [3e26373] | 730 | // join words for fulltext search |
|---|
| 731 | $out['words'] = join(" ", array_unique(explode(" ", $words))); |
|---|
| 732 | |
|---|
| [0501b63] | 733 | return $out; |
|---|
| 734 | } |
|---|
| [f115416] | 735 | |
|---|
| 736 | |
|---|
| [3d6c04d] | 737 | /** |
|---|
| 738 | * Mark one or more contact records as deleted |
|---|
| 739 | * |
|---|
| [63fda8a] | 740 | * @param array Record identifiers |
|---|
| 741 | * @param boolean Remove record(s) irreversible (unsupported) |
|---|
| [3d6c04d] | 742 | */ |
|---|
| [63fda8a] | 743 | function delete($ids, $force=true) |
|---|
| [f115416] | 744 | { |
|---|
| [566b142] | 745 | if (!is_array($ids)) |
|---|
| 746 | $ids = explode(',', $ids); |
|---|
| [3d6c04d] | 747 | |
|---|
| [a004bb8] | 748 | $ids = $this->db->array2list($ids, 'integer'); |
|---|
| [3d6c04d] | 749 | |
|---|
| [63fda8a] | 750 | // flag record as deleted (always) |
|---|
| [3d6c04d] | 751 | $this->db->query( |
|---|
| [982e0b0] | 752 | "UPDATE ".get_table_name($this->db_name). |
|---|
| [3d6c04d] | 753 | " SET del=1, changed=".$this->db->now(). |
|---|
| 754 | " WHERE user_id=?". |
|---|
| 755 | " AND contact_id IN ($ids)", |
|---|
| 756 | $this->user_id |
|---|
| 757 | ); |
|---|
| 758 | |
|---|
| [566b142] | 759 | $this->cache = null; |
|---|
| 760 | |
|---|
| [3d6c04d] | 761 | return $this->db->affected_rows(); |
|---|
| [f115416] | 762 | } |
|---|
| [3d6c04d] | 763 | |
|---|
| 764 | |
|---|
| 765 | /** |
|---|
| [7f5a849] | 766 | * Undelete one or more contact records |
|---|
| 767 | * |
|---|
| 768 | * @param array Record identifiers |
|---|
| 769 | */ |
|---|
| 770 | function undelete($ids) |
|---|
| 771 | { |
|---|
| 772 | if (!is_array($ids)) |
|---|
| 773 | $ids = explode(',', $ids); |
|---|
| 774 | |
|---|
| 775 | $ids = $this->db->array2list($ids, 'integer'); |
|---|
| 776 | |
|---|
| [63fda8a] | 777 | // clear deleted flag |
|---|
| [7f5a849] | 778 | $this->db->query( |
|---|
| 779 | "UPDATE ".get_table_name($this->db_name). |
|---|
| 780 | " SET del=0, changed=".$this->db->now(). |
|---|
| 781 | " WHERE user_id=?". |
|---|
| 782 | " AND contact_id IN ($ids)", |
|---|
| 783 | $this->user_id |
|---|
| 784 | ); |
|---|
| 785 | |
|---|
| 786 | $this->cache = null; |
|---|
| 787 | |
|---|
| 788 | return $this->db->affected_rows(); |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | |
|---|
| 792 | /** |
|---|
| [3d6c04d] | 793 | * Remove all records from the database |
|---|
| 794 | */ |
|---|
| 795 | function delete_all() |
|---|
| [f115416] | 796 | { |
|---|
| [566b142] | 797 | $this->cache = null; |
|---|
| [7f5a849] | 798 | |
|---|
| 799 | $this->db->query("UPDATE ".get_table_name($this->db_name). |
|---|
| 800 | " SET del=1, changed=".$this->db->now(). |
|---|
| 801 | " WHERE user_id = ?", $this->user_id); |
|---|
| 802 | |
|---|
| [3d6c04d] | 803 | return $this->db->affected_rows(); |
|---|
| [f115416] | 804 | } |
|---|
| [3d6c04d] | 805 | |
|---|
| 806 | |
|---|
| 807 | /** |
|---|
| 808 | * Create a contact group with the given name |
|---|
| 809 | * |
|---|
| 810 | * @param string The group name |
|---|
| [5c461ba] | 811 | * @return mixed False on error, array with record props in success |
|---|
| [3d6c04d] | 812 | */ |
|---|
| 813 | function create_group($name) |
|---|
| 814 | { |
|---|
| 815 | $result = false; |
|---|
| 816 | |
|---|
| 817 | // make sure we have a unique name |
|---|
| 818 | $name = $this->unique_groupname($name); |
|---|
| [25fdec5] | 819 | |
|---|
| [3d6c04d] | 820 | $this->db->query( |
|---|
| [982e0b0] | 821 | "INSERT INTO ".get_table_name($this->db_groups). |
|---|
| [3d6c04d] | 822 | " (user_id, changed, name)". |
|---|
| 823 | " VALUES (".intval($this->user_id).", ".$this->db->now().", ".$this->db->quote($name).")" |
|---|
| 824 | ); |
|---|
| [25fdec5] | 825 | |
|---|
| [982e0b0] | 826 | if ($insert_id = $this->db->insert_id($this->db_groups)) |
|---|
| [3d6c04d] | 827 | $result = array('id' => $insert_id, 'name' => $name); |
|---|
| [25fdec5] | 828 | |
|---|
| [3d6c04d] | 829 | return $result; |
|---|
| 830 | } |
|---|
| 831 | |
|---|
| 832 | |
|---|
| 833 | /** |
|---|
| 834 | * Delete the given group (and all linked group members) |
|---|
| 835 | * |
|---|
| 836 | * @param string Group identifier |
|---|
| 837 | * @return boolean True on success, false if no data was changed |
|---|
| 838 | */ |
|---|
| 839 | function delete_group($gid) |
|---|
| 840 | { |
|---|
| 841 | // flag group record as deleted |
|---|
| 842 | $sql_result = $this->db->query( |
|---|
| [982e0b0] | 843 | "UPDATE ".get_table_name($this->db_groups). |
|---|
| [3d6c04d] | 844 | " SET del=1, changed=".$this->db->now(). |
|---|
| [dc6c4f4] | 845 | " WHERE contactgroup_id=?". |
|---|
| 846 | " AND user_id=?", |
|---|
| 847 | $gid, $this->user_id |
|---|
| [3d6c04d] | 848 | ); |
|---|
| 849 | |
|---|
| [566b142] | 850 | $this->cache = null; |
|---|
| 851 | |
|---|
| [3d6c04d] | 852 | return $this->db->affected_rows(); |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | |
|---|
| 856 | /** |
|---|
| 857 | * Rename a specific contact group |
|---|
| 858 | * |
|---|
| 859 | * @param string Group identifier |
|---|
| 860 | * @param string New name to set for this group |
|---|
| 861 | * @return boolean New name on success, false if no data was changed |
|---|
| 862 | */ |
|---|
| 863 | function rename_group($gid, $newname) |
|---|
| 864 | { |
|---|
| 865 | // make sure we have a unique name |
|---|
| 866 | $name = $this->unique_groupname($newname); |
|---|
| [25fdec5] | 867 | |
|---|
| [3d6c04d] | 868 | $sql_result = $this->db->query( |
|---|
| [982e0b0] | 869 | "UPDATE ".get_table_name($this->db_groups). |
|---|
| [3d6c04d] | 870 | " SET name=?, changed=".$this->db->now(). |
|---|
| [dc6c4f4] | 871 | " WHERE contactgroup_id=?". |
|---|
| 872 | " AND user_id=?", |
|---|
| 873 | $name, $gid, $this->user_id |
|---|
| [a61bbb2] | 874 | ); |
|---|
| [3d6c04d] | 875 | |
|---|
| 876 | return $this->db->affected_rows() ? $name : false; |
|---|
| [a61bbb2] | 877 | } |
|---|
| [3d6c04d] | 878 | |
|---|
| 879 | |
|---|
| 880 | /** |
|---|
| 881 | * Add the given contact records the a certain group |
|---|
| 882 | * |
|---|
| 883 | * @param string Group identifier |
|---|
| 884 | * @param array List of contact identifiers to be added |
|---|
| 885 | * @return int Number of contacts added |
|---|
| 886 | */ |
|---|
| 887 | function add_to_group($group_id, $ids) |
|---|
| 888 | { |
|---|
| 889 | if (!is_array($ids)) |
|---|
| 890 | $ids = explode(',', $ids); |
|---|
| [25fdec5] | 891 | |
|---|
| [3d6c04d] | 892 | $added = 0; |
|---|
| [1126fc6] | 893 | $exists = array(); |
|---|
| 894 | |
|---|
| 895 | // get existing assignments ... |
|---|
| 896 | $sql_result = $this->db->query( |
|---|
| 897 | "SELECT contact_id FROM ".get_table_name($this->db_groupmembers). |
|---|
| 898 | " WHERE contactgroup_id=?". |
|---|
| 899 | " AND contact_id IN (".$this->db->array2list($ids, 'integer').")", |
|---|
| 900 | $group_id |
|---|
| 901 | ); |
|---|
| 902 | while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|---|
| 903 | $exists[] = $sql_arr['contact_id']; |
|---|
| 904 | } |
|---|
| 905 | // ... and remove them from the list |
|---|
| 906 | $ids = array_diff($ids, $exists); |
|---|
| [25fdec5] | 907 | |
|---|
| [3d6c04d] | 908 | foreach ($ids as $contact_id) { |
|---|
| [1126fc6] | 909 | $this->db->query( |
|---|
| 910 | "INSERT INTO ".get_table_name($this->db_groupmembers). |
|---|
| 911 | " (contactgroup_id, contact_id, created)". |
|---|
| 912 | " VALUES (?, ?, ".$this->db->now().")", |
|---|
| [3d6c04d] | 913 | $group_id, |
|---|
| 914 | $contact_id |
|---|
| 915 | ); |
|---|
| 916 | |
|---|
| [1126fc6] | 917 | if (!$this->db->db_error) |
|---|
| 918 | $added++; |
|---|
| [3d6c04d] | 919 | } |
|---|
| 920 | |
|---|
| 921 | return $added; |
|---|
| 922 | } |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | /** |
|---|
| 926 | * Remove the given contact records from a certain group |
|---|
| 927 | * |
|---|
| 928 | * @param string Group identifier |
|---|
| 929 | * @param array List of contact identifiers to be removed |
|---|
| 930 | * @return int Number of deleted group members |
|---|
| 931 | */ |
|---|
| 932 | function remove_from_group($group_id, $ids) |
|---|
| 933 | { |
|---|
| 934 | if (!is_array($ids)) |
|---|
| 935 | $ids = explode(',', $ids); |
|---|
| 936 | |
|---|
| [a004bb8] | 937 | $ids = $this->db->array2list($ids, 'integer'); |
|---|
| 938 | |
|---|
| [3d6c04d] | 939 | $sql_result = $this->db->query( |
|---|
| [982e0b0] | 940 | "DELETE FROM ".get_table_name($this->db_groupmembers). |
|---|
| [3d6c04d] | 941 | " WHERE contactgroup_id=?". |
|---|
| 942 | " AND contact_id IN ($ids)", |
|---|
| 943 | $group_id |
|---|
| 944 | ); |
|---|
| 945 | |
|---|
| 946 | return $this->db->affected_rows(); |
|---|
| 947 | } |
|---|
| 948 | |
|---|
| 949 | |
|---|
| 950 | /** |
|---|
| 951 | * Check for existing groups with the same name |
|---|
| 952 | * |
|---|
| 953 | * @param string Name to check |
|---|
| 954 | * @return string A group name which is unique for the current use |
|---|
| 955 | */ |
|---|
| 956 | private function unique_groupname($name) |
|---|
| 957 | { |
|---|
| 958 | $checkname = $name; |
|---|
| 959 | $num = 2; $hit = false; |
|---|
| [25fdec5] | 960 | |
|---|
| [3d6c04d] | 961 | do { |
|---|
| 962 | $sql_result = $this->db->query( |
|---|
| [982e0b0] | 963 | "SELECT 1 FROM ".get_table_name($this->db_groups). |
|---|
| [3d6c04d] | 964 | " WHERE del<>1". |
|---|
| 965 | " AND user_id=?". |
|---|
| [3e696da] | 966 | " AND name=?", |
|---|
| [3d6c04d] | 967 | $this->user_id, |
|---|
| 968 | $checkname); |
|---|
| [25fdec5] | 969 | |
|---|
| [3d6c04d] | 970 | // append number to make name unique |
|---|
| 971 | if ($hit = $this->db->num_rows($sql_result)) |
|---|
| 972 | $checkname = $name . ' ' . $num++; |
|---|
| 973 | } while ($hit > 0); |
|---|
| [25fdec5] | 974 | |
|---|
| [3d6c04d] | 975 | return $checkname; |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| [f115416] | 978 | } |
|---|