commit | author | age
|
f11541
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
47124c
|
5 |
| program/include/rcube_contacts.php | |
f11541
|
6 |
| | |
e019f2
|
7 |
| This file is part of the Roundcube Webmail client | |
3e2637
|
8 |
| Copyright (C) 2006-2011, The Roundcube Dev Team | |
f11541
|
9 |
| Licensed under the GNU GPL | |
T |
10 |
| | |
|
11 |
| PURPOSE: | |
|
12 |
| Interface to the local address book database | |
|
13 |
| | |
|
14 |
+-----------------------------------------------------------------------+ |
|
15 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
16 |
+-----------------------------------------------------------------------+ |
|
17 |
|
638fb8
|
18 |
$Id$ |
f11541
|
19 |
|
T |
20 |
*/ |
|
21 |
|
6d969b
|
22 |
|
T |
23 |
/** |
|
24 |
* Model class for the local address book database |
|
25 |
* |
|
26 |
* @package Addressbook |
|
27 |
*/ |
cc97ea
|
28 |
class rcube_contacts extends rcube_addressbook |
f11541
|
29 |
{ |
495c0e
|
30 |
// protected for backward compat. with some plugins |
982e0b
|
31 |
protected $db_name = 'contacts'; |
A |
32 |
protected $db_groups = 'contactgroups'; |
|
33 |
protected $db_groupmembers = 'contactgroupmembers'; |
|
34 |
|
5c461b
|
35 |
/** |
A |
36 |
* Store database connection. |
|
37 |
* |
|
38 |
* @var rcube_mdb2 |
|
39 |
*/ |
3d6c04
|
40 |
private $db = null; |
A |
41 |
private $user_id = 0; |
|
42 |
private $filter = null; |
|
43 |
private $result = null; |
566b14
|
44 |
private $cache; |
3e2637
|
45 |
private $table_cols = array('name', 'email', 'firstname', 'surname'); |
T |
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'); |
25fdec
|
49 |
|
982e0b
|
50 |
// public properties |
0501b6
|
51 |
public $primary_key = 'contact_id'; |
T |
52 |
public $readonly = false; |
|
53 |
public $groups = true; |
|
54 |
public $list_page = 1; |
|
55 |
public $page_size = 10; |
|
56 |
public $group_id = 0; |
|
57 |
public $ready = false; |
|
58 |
public $coltypes = array('name', 'firstname', 'surname', 'middlename', 'prefix', 'suffix', 'nickname', |
|
59 |
'jobtitle', 'organization', 'department', 'assistant', 'manager', |
|
60 |
'gender', 'maidenname', 'spouse', 'email', 'phone', 'address', |
|
61 |
'birthday', 'anniversary', 'website', 'im', 'notes', 'photo'); |
f11541
|
62 |
|
25fdec
|
63 |
|
3d6c04
|
64 |
/** |
A |
65 |
* Object constructor |
|
66 |
* |
|
67 |
* @param object Instance of the rcube_db class |
|
68 |
* @param integer User-ID |
|
69 |
*/ |
|
70 |
function __construct($dbconn, $user) |
f11541
|
71 |
{ |
3d6c04
|
72 |
$this->db = $dbconn; |
A |
73 |
$this->user_id = $user; |
|
74 |
$this->ready = $this->db && !$this->db->is_error(); |
f11541
|
75 |
} |
T |
76 |
|
|
77 |
|
3d6c04
|
78 |
/** |
A |
79 |
* Save a search string for future listings |
|
80 |
* |
|
81 |
* @param string SQL params to use in listing method |
|
82 |
*/ |
|
83 |
function set_search_set($filter) |
f11541
|
84 |
{ |
3d6c04
|
85 |
$this->filter = $filter; |
566b14
|
86 |
$this->cache = null; |
3d6c04
|
87 |
} |
A |
88 |
|
25fdec
|
89 |
|
3d6c04
|
90 |
/** |
A |
91 |
* Getter for saved search properties |
|
92 |
* |
|
93 |
* @return mixed Search properties used by this class |
|
94 |
*/ |
|
95 |
function get_search_set() |
|
96 |
{ |
|
97 |
return $this->filter; |
|
98 |
} |
|
99 |
|
|
100 |
|
|
101 |
/** |
|
102 |
* Setter for the current group |
|
103 |
* (empty, has to be re-implemented by extending class) |
|
104 |
*/ |
|
105 |
function set_group($gid) |
|
106 |
{ |
|
107 |
$this->group_id = $gid; |
566b14
|
108 |
$this->cache = null; |
3d6c04
|
109 |
} |
A |
110 |
|
|
111 |
|
|
112 |
/** |
|
113 |
* Reset all saved results and search parameters |
|
114 |
*/ |
|
115 |
function reset() |
|
116 |
{ |
|
117 |
$this->result = null; |
|
118 |
$this->filter = null; |
566b14
|
119 |
$this->cache = null; |
3d6c04
|
120 |
} |
25fdec
|
121 |
|
3d6c04
|
122 |
|
A |
123 |
/** |
|
124 |
* List all active contact groups of this source |
|
125 |
* |
|
126 |
* @param string Search string to match group name |
|
127 |
* @return array Indexed list of contact groups, each a hash array |
|
128 |
*/ |
|
129 |
function list_groups($search = null) |
|
130 |
{ |
|
131 |
$results = array(); |
25fdec
|
132 |
|
3d6c04
|
133 |
if (!$this->groups) |
A |
134 |
return $results; |
d17a7f
|
135 |
|
631967
|
136 |
$sql_filter = $search ? " AND " . $this->db->ilike('name', '%'.$search.'%') : ''; |
3d6c04
|
137 |
|
A |
138 |
$sql_result = $this->db->query( |
982e0b
|
139 |
"SELECT * FROM ".get_table_name($this->db_groups). |
3d6c04
|
140 |
" WHERE del<>1". |
A |
141 |
" AND user_id=?". |
|
142 |
$sql_filter. |
|
143 |
" ORDER BY name", |
|
144 |
$this->user_id); |
|
145 |
|
|
146 |
while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|
147 |
$sql_arr['ID'] = $sql_arr['contactgroup_id']; |
|
148 |
$results[] = $sql_arr; |
|
149 |
} |
|
150 |
|
|
151 |
return $results; |
|
152 |
} |
|
153 |
|
|
154 |
|
|
155 |
/** |
|
156 |
* List the current set of contact records |
|
157 |
* |
0501b6
|
158 |
* @param array List of cols to show, Null means all |
3d6c04
|
159 |
* @param int Only return this number of records, use negative values for tail |
A |
160 |
* @param boolean True to skip the count query (select only) |
|
161 |
* @return array Indexed list of contact records, each a hash array |
|
162 |
*/ |
|
163 |
function list_records($cols=null, $subset=0, $nocount=false) |
|
164 |
{ |
|
165 |
if ($nocount || $this->list_page <= 1) { |
|
166 |
// create dummy result, we don't need a count now |
|
167 |
$this->result = new rcube_result_set(); |
|
168 |
} else { |
|
169 |
// count all records |
|
170 |
$this->result = $this->count(); |
|
171 |
} |
|
172 |
|
|
173 |
$start_row = $subset < 0 ? $this->result->first + $this->page_size + $subset : $this->result->first; |
|
174 |
$length = $subset != 0 ? abs($subset) : $this->page_size; |
|
175 |
|
|
176 |
if ($this->group_id) |
982e0b
|
177 |
$join = " LEFT JOIN ".get_table_name($this->db_groupmembers)." AS m". |
3d6c04
|
178 |
" ON (m.contact_id = c.".$this->primary_key.")"; |
A |
179 |
|
|
180 |
$sql_result = $this->db->limitquery( |
982e0b
|
181 |
"SELECT * FROM ".get_table_name($this->db_name)." AS c" . |
821a56
|
182 |
$join . |
3d6c04
|
183 |
" WHERE c.del<>1" . |
566b14
|
184 |
" AND c.user_id=?" . |
A |
185 |
($this->group_id ? " AND m.contactgroup_id=?" : ""). |
|
186 |
($this->filter ? " AND (".$this->filter.")" : "") . |
3d6c04
|
187 |
" ORDER BY c.name", |
A |
188 |
$start_row, |
|
189 |
$length, |
|
190 |
$this->user_id, |
|
191 |
$this->group_id); |
|
192 |
|
0501b6
|
193 |
// determine whether we have to parse the vcard or if only db cols are requested |
T |
194 |
$read_vcard = !$cols || count(array_intersect($cols, $this->table_cols)) < count($cols); |
|
195 |
|
3d6c04
|
196 |
while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
A |
197 |
$sql_arr['ID'] = $sql_arr[$this->primary_key]; |
0501b6
|
198 |
|
T |
199 |
if ($read_vcard) |
|
200 |
$sql_arr = $this->convert_db_data($sql_arr); |
|
201 |
else |
|
202 |
$sql_arr['email'] = preg_split('/,\s*/', $sql_arr['email']); |
|
203 |
|
3d6c04
|
204 |
// make sure we have a name to display |
A |
205 |
if (empty($sql_arr['name'])) |
0501b6
|
206 |
$sql_arr['name'] = $sql_arr['email'][0]; |
T |
207 |
|
3d6c04
|
208 |
$this->result->add($sql_arr); |
A |
209 |
} |
25fdec
|
210 |
|
3d6c04
|
211 |
$cnt = count($this->result->records); |
A |
212 |
|
|
213 |
// update counter |
|
214 |
if ($nocount) |
|
215 |
$this->result->count = $cnt; |
|
216 |
else if ($this->list_page <= 1) { |
|
217 |
if ($cnt < $this->page_size && $subset == 0) |
|
218 |
$this->result->count = $cnt; |
821a56
|
219 |
else if (isset($this->cache['count'])) |
A |
220 |
$this->result->count = $this->cache['count']; |
3d6c04
|
221 |
else |
A |
222 |
$this->result->count = $this->_count(); |
|
223 |
} |
25fdec
|
224 |
|
3d6c04
|
225 |
return $this->result; |
A |
226 |
} |
|
227 |
|
|
228 |
|
|
229 |
/** |
|
230 |
* Search contacts |
|
231 |
* |
|
232 |
* @param array List of fields to search in |
|
233 |
* @param string Search value |
|
234 |
* @param boolean True for strict (=), False for partial (LIKE) matching |
|
235 |
* @param boolean True if results are requested, False if count only |
|
236 |
* @param boolean True to skip the count query (select only) |
25fdec
|
237 |
* @param array List of fields that cannot be empty |
0501b6
|
238 |
* @return object rcube_result_set Contact records and 'count' value |
3d6c04
|
239 |
*/ |
25fdec
|
240 |
function search($fields, $value, $strict=false, $select=true, $nocount=false, $required=array()) |
3d6c04
|
241 |
{ |
A |
242 |
if (!is_array($fields)) |
|
243 |
$fields = array($fields); |
25fdec
|
244 |
if (!is_array($required) && !empty($required)) |
A |
245 |
$required = array($required); |
566b14
|
246 |
|
25fdec
|
247 |
$where = $and_where = array(); |
A |
248 |
|
3d6c04
|
249 |
foreach ($fields as $col) { |
A |
250 |
if ($col == 'ID' || $col == $this->primary_key) { |
25fdec
|
251 |
$ids = !is_array($value) ? explode(',', $value) : $value; |
A |
252 |
$ids = $this->db->array2list($ids, 'integer'); |
|
253 |
$where[] = 'c.' . $this->primary_key.' IN ('.$ids.')'; |
3d6c04
|
254 |
} |
3e2637
|
255 |
else if ($strict) { |
25fdec
|
256 |
$where[] = $this->db->quoteIdentifier($col).' = '.$this->db->quote($value); |
3e2637
|
257 |
} |
T |
258 |
else if ($col == '*') { |
|
259 |
$words = array(); |
|
260 |
foreach(explode(" ", self::normalize_string($value)) as $word) |
|
261 |
$words[] = $this->db->ilike('words', '%'.$word.'%'); |
|
262 |
$where[] = '(' . join(' AND ', $words) . ')'; |
|
263 |
} |
3d6c04
|
264 |
else |
25fdec
|
265 |
$where[] = $this->db->ilike($col, '%'.$value.'%'); |
3d6c04
|
266 |
} |
25fdec
|
267 |
|
A |
268 |
foreach ($required as $col) { |
|
269 |
$and_where[] = $this->db->quoteIdentifier($col).' <> '.$this->db->quote(''); |
|
270 |
} |
|
271 |
|
|
272 |
if (!empty($where)) |
|
273 |
$where = join(' OR ', $where); |
|
274 |
|
|
275 |
if (!empty($and_where)) |
|
276 |
$where = ($where ? "($where) AND " : '') . join(' AND ', $and_where); |
|
277 |
|
|
278 |
if (!empty($where)) { |
|
279 |
$this->set_search_set($where); |
3d6c04
|
280 |
if ($select) |
A |
281 |
$this->list_records(null, 0, $nocount); |
|
282 |
else |
|
283 |
$this->result = $this->count(); |
|
284 |
} |
|
285 |
|
|
286 |
return $this->result; |
|
287 |
} |
|
288 |
|
|
289 |
|
|
290 |
/** |
|
291 |
* Count number of available contacts in database |
|
292 |
* |
|
293 |
* @return rcube_result_set Result object |
|
294 |
*/ |
|
295 |
function count() |
|
296 |
{ |
566b14
|
297 |
$count = isset($this->cache['count']) ? $this->cache['count'] : $this->_count(); |
25fdec
|
298 |
|
566b14
|
299 |
return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); |
3d6c04
|
300 |
} |
A |
301 |
|
|
302 |
|
|
303 |
/** |
|
304 |
* Count number of available contacts in database |
|
305 |
* |
|
306 |
* @return int Contacts count |
|
307 |
*/ |
|
308 |
private function _count() |
|
309 |
{ |
|
310 |
if ($this->group_id) |
982e0b
|
311 |
$join = " LEFT JOIN ".get_table_name($this->db_groupmembers)." AS m". |
3d6c04
|
312 |
" ON (m.contact_id=c.".$this->primary_key.")"; |
25fdec
|
313 |
|
3d6c04
|
314 |
// count contacts for this user |
A |
315 |
$sql_result = $this->db->query( |
|
316 |
"SELECT COUNT(c.contact_id) AS rows". |
982e0b
|
317 |
" FROM ".get_table_name($this->db_name)." AS c". |
821a56
|
318 |
$join. |
A |
319 |
" WHERE c.del<>1". |
3d6c04
|
320 |
" AND c.user_id=?". |
A |
321 |
($this->group_id ? " AND m.contactgroup_id=?" : ""). |
|
322 |
($this->filter ? " AND (".$this->filter.")" : ""), |
|
323 |
$this->user_id, |
|
324 |
$this->group_id |
4307cc
|
325 |
); |
3d6c04
|
326 |
|
A |
327 |
$sql_arr = $this->db->fetch_assoc($sql_result); |
566b14
|
328 |
|
A |
329 |
$this->cache['count'] = (int) $sql_arr['rows']; |
|
330 |
|
|
331 |
return $this->cache['count']; |
f11541
|
332 |
} |
T |
333 |
|
|
334 |
|
3d6c04
|
335 |
/** |
A |
336 |
* Return the last result set |
|
337 |
* |
5c461b
|
338 |
* @return mixed Result array or NULL if nothing selected yet |
3d6c04
|
339 |
*/ |
A |
340 |
function get_result() |
f11541
|
341 |
{ |
3d6c04
|
342 |
return $this->result; |
f11541
|
343 |
} |
25fdec
|
344 |
|
A |
345 |
|
3d6c04
|
346 |
/** |
A |
347 |
* Get a specific contact record |
|
348 |
* |
|
349 |
* @param mixed record identifier(s) |
5c461b
|
350 |
* @return mixed Result object with all record fields or False if not found |
3d6c04
|
351 |
*/ |
A |
352 |
function get_record($id, $assoc=false) |
f11541
|
353 |
{ |
3d6c04
|
354 |
// return cached result |
A |
355 |
if ($this->result && ($first = $this->result->first()) && $first[$this->primary_key] == $id) |
|
356 |
return $assoc ? $first : $this->result; |
25fdec
|
357 |
|
a61bbb
|
358 |
$this->db->query( |
982e0b
|
359 |
"SELECT * FROM ".get_table_name($this->db_name). |
3d6c04
|
360 |
" WHERE contact_id=?". |
A |
361 |
" AND user_id=?". |
|
362 |
" AND del<>1", |
|
363 |
$id, |
|
364 |
$this->user_id |
a61bbb
|
365 |
); |
3d6c04
|
366 |
|
A |
367 |
if ($sql_arr = $this->db->fetch_assoc()) { |
0501b6
|
368 |
$record = $this->convert_db_data($sql_arr); |
3d6c04
|
369 |
$this->result = new rcube_result_set(1); |
0501b6
|
370 |
$this->result->add($record); |
3d6c04
|
371 |
} |
A |
372 |
|
0501b6
|
373 |
return $assoc && $record ? $record : $this->result; |
a61bbb
|
374 |
} |
3d6c04
|
375 |
|
A |
376 |
|
|
377 |
/** |
b393e5
|
378 |
* Get group assignments of a specific contact record |
cb7d32
|
379 |
* |
T |
380 |
* @param mixed Record identifier |
b393e5
|
381 |
* @return array List of assigned groups as ID=>Name pairs |
cb7d32
|
382 |
*/ |
T |
383 |
function get_record_groups($id) |
|
384 |
{ |
|
385 |
$results = array(); |
|
386 |
|
|
387 |
if (!$this->groups) |
|
388 |
return $results; |
|
389 |
|
|
390 |
$sql_result = $this->db->query( |
|
391 |
"SELECT cgm.contactgroup_id, cg.name FROM " . get_table_name($this->db_groupmembers) . " AS cgm" . |
|
392 |
" LEFT JOIN " . get_table_name($this->db_groups) . " AS cg ON (cgm.contactgroup_id = cg.contactgroup_id AND cg.del<>1)" . |
|
393 |
" WHERE cgm.contact_id=?", |
|
394 |
$id |
|
395 |
); |
|
396 |
while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|
397 |
$results[$sql_arr['contactgroup_id']] = $sql_arr['name']; |
|
398 |
} |
|
399 |
|
|
400 |
return $results; |
|
401 |
} |
|
402 |
|
|
403 |
|
|
404 |
/** |
07b95d
|
405 |
* Check the given data before saving. |
T |
406 |
* If input not valid, the message to display can be fetched using get_error() |
|
407 |
* |
|
408 |
* @param array Assoziative array with data to save |
|
409 |
* @return boolean True if input is valid, False if not. |
|
410 |
*/ |
|
411 |
public function validate($save_data) |
|
412 |
{ |
|
413 |
// check for name input |
|
414 |
$valid = parent::validate($save_data); |
|
415 |
|
|
416 |
// require at least one e-mail address (syntax check is done later in save.inc) |
|
417 |
if ($valid && !array_filter($this->get_col_values('email', $save_data, true))) { |
|
418 |
$this->set_error('warning', 'noemailwarning'); |
|
419 |
$valid = false; |
|
420 |
} |
|
421 |
|
|
422 |
return $valid; |
|
423 |
} |
|
424 |
|
|
425 |
|
|
426 |
/** |
3d6c04
|
427 |
* Create a new contact record |
A |
428 |
* |
b393e5
|
429 |
* @param array Associative array with save data |
5c461b
|
430 |
* @return integer|boolean The created record ID on success, False on error |
3d6c04
|
431 |
*/ |
A |
432 |
function insert($save_data, $check=false) |
|
433 |
{ |
0501b6
|
434 |
if (!is_array($save_data)) |
T |
435 |
return false; |
3d6c04
|
436 |
|
A |
437 |
$insert_id = $existing = false; |
|
438 |
|
0501b6
|
439 |
if ($check) { |
T |
440 |
foreach ($save_data as $col => $values) { |
|
441 |
if (strpos($col, 'email') === 0) { |
|
442 |
foreach ((array)$values as $email) { |
715c79
|
443 |
if ($existing = $this->search('email', $email, false, false)) |
0501b6
|
444 |
break 2; |
T |
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
} |
3d6c04
|
449 |
|
0501b6
|
450 |
$save_data = $this->convert_save_data($save_data); |
3d6c04
|
451 |
$a_insert_cols = $a_insert_values = array(); |
A |
452 |
|
0501b6
|
453 |
foreach ($save_data as $col => $value) { |
T |
454 |
$a_insert_cols[] = $this->db->quoteIdentifier($col); |
|
455 |
$a_insert_values[] = $this->db->quote($value); |
|
456 |
} |
3d6c04
|
457 |
|
A |
458 |
if (!$existing->count && !empty($a_insert_cols)) { |
|
459 |
$this->db->query( |
982e0b
|
460 |
"INSERT INTO ".get_table_name($this->db_name). |
3d6c04
|
461 |
" (user_id, changed, del, ".join(', ', $a_insert_cols).")". |
A |
462 |
" VALUES (".intval($this->user_id).", ".$this->db->now().", 0, ".join(', ', $a_insert_values).")" |
|
463 |
); |
|
464 |
|
982e0b
|
465 |
$insert_id = $this->db->insert_id($this->db_name); |
3d6c04
|
466 |
} |
A |
467 |
|
|
468 |
// also add the newly created contact to the active group |
|
469 |
if ($insert_id && $this->group_id) |
|
470 |
$this->add_to_group($this->group_id, $insert_id); |
|
471 |
|
566b14
|
472 |
$this->cache = null; |
A |
473 |
|
3d6c04
|
474 |
return $insert_id; |
A |
475 |
} |
|
476 |
|
|
477 |
|
|
478 |
/** |
|
479 |
* Update a specific contact record |
|
480 |
* |
|
481 |
* @param mixed Record identifier |
|
482 |
* @param array Assoziative array with save data |
5c461b
|
483 |
* @return boolean True on success, False on error |
3d6c04
|
484 |
*/ |
A |
485 |
function update($id, $save_cols) |
|
486 |
{ |
|
487 |
$updated = false; |
|
488 |
$write_sql = array(); |
0501b6
|
489 |
$record = $this->get_record($id, true); |
T |
490 |
$save_cols = $this->convert_save_data($save_cols, $record); |
3d6c04
|
491 |
|
0501b6
|
492 |
foreach ($save_cols as $col => $value) { |
T |
493 |
$write_sql[] = sprintf("%s=%s", $this->db->quoteIdentifier($col), $this->db->quote($value)); |
|
494 |
} |
3d6c04
|
495 |
|
A |
496 |
if (!empty($write_sql)) { |
|
497 |
$this->db->query( |
982e0b
|
498 |
"UPDATE ".get_table_name($this->db_name). |
3d6c04
|
499 |
" SET changed=".$this->db->now().", ".join(', ', $write_sql). |
A |
500 |
" WHERE contact_id=?". |
|
501 |
" AND user_id=?". |
|
502 |
" AND del<>1", |
|
503 |
$id, |
|
504 |
$this->user_id |
|
505 |
); |
|
506 |
|
|
507 |
$updated = $this->db->affected_rows(); |
0501b6
|
508 |
$this->result = null; // clear current result (from get_record()) |
3d6c04
|
509 |
} |
25fdec
|
510 |
|
3d6c04
|
511 |
return $updated; |
A |
512 |
} |
0501b6
|
513 |
|
T |
514 |
|
|
515 |
private function convert_db_data($sql_arr) |
|
516 |
{ |
|
517 |
$record = array(); |
|
518 |
$record['ID'] = $sql_arr[$this->primary_key]; |
|
519 |
|
|
520 |
if ($sql_arr['vcard']) { |
|
521 |
unset($sql_arr['email']); |
|
522 |
$vcard = new rcube_vcard($sql_arr['vcard']); |
|
523 |
$record += $vcard->get_assoc() + $sql_arr; |
|
524 |
} |
|
525 |
else { |
|
526 |
$record += $sql_arr; |
|
527 |
$record['email'] = preg_split('/,\s*/', $record['email']); |
|
528 |
} |
|
529 |
|
|
530 |
return $record; |
|
531 |
} |
|
532 |
|
|
533 |
|
|
534 |
private function convert_save_data($save_data, $record = array()) |
|
535 |
{ |
|
536 |
$out = array(); |
3e2637
|
537 |
$words = ''; |
0501b6
|
538 |
|
T |
539 |
// copy values into vcard object |
|
540 |
$vcard = new rcube_vcard($record['vcard'] ? $record['vcard'] : $save_data['vcard']); |
|
541 |
$vcard->reset(); |
|
542 |
foreach ($save_data as $key => $values) { |
|
543 |
list($field, $section) = explode(':', $key); |
3e2637
|
544 |
$fulltext = in_array($field, $this->fulltext_cols); |
0501b6
|
545 |
foreach ((array)$values as $value) { |
T |
546 |
if (isset($value)) |
|
547 |
$vcard->set($field, $value, $section); |
3e2637
|
548 |
if ($fulltext && is_array($value)) |
T |
549 |
$words .= ' ' . self::normalize_string(join(" ", $value)); |
|
550 |
else if ($fulltext && strlen($value) >= 3) |
|
551 |
$words .= ' ' . self::normalize_string($value); |
0501b6
|
552 |
} |
T |
553 |
} |
569f83
|
554 |
$out['vcard'] = $vcard->export(false); |
0501b6
|
555 |
|
T |
556 |
foreach ($this->table_cols as $col) { |
|
557 |
$key = $col; |
|
558 |
if (!isset($save_data[$key])) |
|
559 |
$key .= ':home'; |
|
560 |
if (isset($save_data[$key])) |
|
561 |
$out[$col] = is_array($save_data[$key]) ? join(',', $save_data[$key]) : $save_data[$key]; |
|
562 |
} |
|
563 |
|
|
564 |
// save all e-mails in database column |
|
565 |
$out['email'] = join(", ", $vcard->email); |
|
566 |
|
3e2637
|
567 |
// join words for fulltext search |
T |
568 |
$out['words'] = join(" ", array_unique(explode(" ", $words))); |
|
569 |
|
0501b6
|
570 |
return $out; |
T |
571 |
} |
3d6c04
|
572 |
|
A |
573 |
|
|
574 |
/** |
|
575 |
* Mark one or more contact records as deleted |
|
576 |
* |
|
577 |
* @param array Record identifiers |
|
578 |
*/ |
|
579 |
function delete($ids) |
|
580 |
{ |
566b14
|
581 |
if (!is_array($ids)) |
A |
582 |
$ids = explode(',', $ids); |
3d6c04
|
583 |
|
a004bb
|
584 |
$ids = $this->db->array2list($ids, 'integer'); |
3d6c04
|
585 |
|
A |
586 |
// flag record as deleted |
|
587 |
$this->db->query( |
982e0b
|
588 |
"UPDATE ".get_table_name($this->db_name). |
3d6c04
|
589 |
" SET del=1, changed=".$this->db->now(). |
A |
590 |
" WHERE user_id=?". |
|
591 |
" AND contact_id IN ($ids)", |
|
592 |
$this->user_id |
|
593 |
); |
|
594 |
|
566b14
|
595 |
$this->cache = null; |
A |
596 |
|
3d6c04
|
597 |
return $this->db->affected_rows(); |
A |
598 |
} |
|
599 |
|
|
600 |
|
|
601 |
/** |
|
602 |
* Remove all records from the database |
|
603 |
*/ |
|
604 |
function delete_all() |
|
605 |
{ |
982e0b
|
606 |
$this->db->query("DELETE FROM ".get_table_name($this->db_name)." WHERE user_id = ?", $this->user_id); |
566b14
|
607 |
$this->cache = null; |
3d6c04
|
608 |
return $this->db->affected_rows(); |
A |
609 |
} |
|
610 |
|
|
611 |
|
|
612 |
/** |
|
613 |
* Create a contact group with the given name |
|
614 |
* |
|
615 |
* @param string The group name |
5c461b
|
616 |
* @return mixed False on error, array with record props in success |
3d6c04
|
617 |
*/ |
A |
618 |
function create_group($name) |
|
619 |
{ |
|
620 |
$result = false; |
|
621 |
|
|
622 |
// make sure we have a unique name |
|
623 |
$name = $this->unique_groupname($name); |
25fdec
|
624 |
|
3d6c04
|
625 |
$this->db->query( |
982e0b
|
626 |
"INSERT INTO ".get_table_name($this->db_groups). |
3d6c04
|
627 |
" (user_id, changed, name)". |
A |
628 |
" VALUES (".intval($this->user_id).", ".$this->db->now().", ".$this->db->quote($name).")" |
|
629 |
); |
25fdec
|
630 |
|
982e0b
|
631 |
if ($insert_id = $this->db->insert_id($this->db_groups)) |
3d6c04
|
632 |
$result = array('id' => $insert_id, 'name' => $name); |
25fdec
|
633 |
|
3d6c04
|
634 |
return $result; |
A |
635 |
} |
|
636 |
|
|
637 |
|
|
638 |
/** |
|
639 |
* Delete the given group (and all linked group members) |
|
640 |
* |
|
641 |
* @param string Group identifier |
|
642 |
* @return boolean True on success, false if no data was changed |
|
643 |
*/ |
|
644 |
function delete_group($gid) |
|
645 |
{ |
|
646 |
// flag group record as deleted |
|
647 |
$sql_result = $this->db->query( |
982e0b
|
648 |
"UPDATE ".get_table_name($this->db_groups). |
3d6c04
|
649 |
" SET del=1, changed=".$this->db->now(). |
A |
650 |
" WHERE contactgroup_id=?", |
|
651 |
$gid |
|
652 |
); |
|
653 |
|
566b14
|
654 |
$this->cache = null; |
A |
655 |
|
3d6c04
|
656 |
return $this->db->affected_rows(); |
A |
657 |
} |
|
658 |
|
|
659 |
|
|
660 |
/** |
|
661 |
* Rename a specific contact group |
|
662 |
* |
|
663 |
* @param string Group identifier |
|
664 |
* @param string New name to set for this group |
|
665 |
* @return boolean New name on success, false if no data was changed |
|
666 |
*/ |
|
667 |
function rename_group($gid, $newname) |
|
668 |
{ |
|
669 |
// make sure we have a unique name |
|
670 |
$name = $this->unique_groupname($newname); |
25fdec
|
671 |
|
3d6c04
|
672 |
$sql_result = $this->db->query( |
982e0b
|
673 |
"UPDATE ".get_table_name($this->db_groups). |
3d6c04
|
674 |
" SET name=?, changed=".$this->db->now(). |
A |
675 |
" WHERE contactgroup_id=?", |
|
676 |
$name, $gid |
|
677 |
); |
|
678 |
|
|
679 |
return $this->db->affected_rows() ? $name : false; |
|
680 |
} |
|
681 |
|
|
682 |
|
|
683 |
/** |
|
684 |
* Add the given contact records the a certain group |
|
685 |
* |
|
686 |
* @param string Group identifier |
|
687 |
* @param array List of contact identifiers to be added |
|
688 |
* @return int Number of contacts added |
|
689 |
*/ |
|
690 |
function add_to_group($group_id, $ids) |
|
691 |
{ |
|
692 |
if (!is_array($ids)) |
|
693 |
$ids = explode(',', $ids); |
25fdec
|
694 |
|
3d6c04
|
695 |
$added = 0; |
1126fc
|
696 |
$exists = array(); |
A |
697 |
|
|
698 |
// get existing assignments ... |
|
699 |
$sql_result = $this->db->query( |
|
700 |
"SELECT contact_id FROM ".get_table_name($this->db_groupmembers). |
|
701 |
" WHERE contactgroup_id=?". |
|
702 |
" AND contact_id IN (".$this->db->array2list($ids, 'integer').")", |
|
703 |
$group_id |
|
704 |
); |
|
705 |
while ($sql_result && ($sql_arr = $this->db->fetch_assoc($sql_result))) { |
|
706 |
$exists[] = $sql_arr['contact_id']; |
|
707 |
} |
|
708 |
// ... and remove them from the list |
|
709 |
$ids = array_diff($ids, $exists); |
25fdec
|
710 |
|
3d6c04
|
711 |
foreach ($ids as $contact_id) { |
1126fc
|
712 |
$this->db->query( |
A |
713 |
"INSERT INTO ".get_table_name($this->db_groupmembers). |
|
714 |
" (contactgroup_id, contact_id, created)". |
|
715 |
" VALUES (?, ?, ".$this->db->now().")", |
3d6c04
|
716 |
$group_id, |
A |
717 |
$contact_id |
|
718 |
); |
|
719 |
|
1126fc
|
720 |
if (!$this->db->db_error) |
A |
721 |
$added++; |
3d6c04
|
722 |
} |
A |
723 |
|
|
724 |
return $added; |
|
725 |
} |
|
726 |
|
|
727 |
|
|
728 |
/** |
|
729 |
* Remove the given contact records from a certain group |
|
730 |
* |
|
731 |
* @param string Group identifier |
|
732 |
* @param array List of contact identifiers to be removed |
|
733 |
* @return int Number of deleted group members |
|
734 |
*/ |
|
735 |
function remove_from_group($group_id, $ids) |
|
736 |
{ |
|
737 |
if (!is_array($ids)) |
|
738 |
$ids = explode(',', $ids); |
|
739 |
|
a004bb
|
740 |
$ids = $this->db->array2list($ids, 'integer'); |
A |
741 |
|
3d6c04
|
742 |
$sql_result = $this->db->query( |
982e0b
|
743 |
"DELETE FROM ".get_table_name($this->db_groupmembers). |
3d6c04
|
744 |
" WHERE contactgroup_id=?". |
A |
745 |
" AND contact_id IN ($ids)", |
|
746 |
$group_id |
|
747 |
); |
|
748 |
|
|
749 |
return $this->db->affected_rows(); |
|
750 |
} |
|
751 |
|
|
752 |
|
|
753 |
/** |
|
754 |
* Check for existing groups with the same name |
|
755 |
* |
|
756 |
* @param string Name to check |
|
757 |
* @return string A group name which is unique for the current use |
|
758 |
*/ |
|
759 |
private function unique_groupname($name) |
|
760 |
{ |
|
761 |
$checkname = $name; |
|
762 |
$num = 2; $hit = false; |
25fdec
|
763 |
|
3d6c04
|
764 |
do { |
A |
765 |
$sql_result = $this->db->query( |
982e0b
|
766 |
"SELECT 1 FROM ".get_table_name($this->db_groups). |
3d6c04
|
767 |
" WHERE del<>1". |
A |
768 |
" AND user_id=?". |
3e696d
|
769 |
" AND name=?", |
3d6c04
|
770 |
$this->user_id, |
A |
771 |
$checkname); |
25fdec
|
772 |
|
3d6c04
|
773 |
// append number to make name unique |
A |
774 |
if ($hit = $this->db->num_rows($sql_result)) |
|
775 |
$checkname = $name . ' ' . $num++; |
|
776 |
} while ($hit > 0); |
25fdec
|
777 |
|
3d6c04
|
778 |
return $checkname; |
3b67e3
|
779 |
} |
T |
780 |
|
f11541
|
781 |
} |