0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | program/steps/addressbook/copy.inc |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2007-2013, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Copy a contact record from one direcotry to another |
|
|
16 | |
|
|
17 +-----------------------------------------------------------------------+
|
|
18 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 // only process ajax requests
|
|
23 if (!$OUTPUT->ajax_call)
|
|
24 return;
|
|
25
|
|
26
|
|
27 $cids = rcmail_get_cids();
|
|
28 $target = rcube_utils::get_input_value('_to', rcube_utils::INPUT_POST);
|
|
29 $target_group = rcube_utils::get_input_value('_togid', rcube_utils::INPUT_POST);
|
|
30
|
|
31 $success = 0;
|
|
32 $errormsg = 'copyerror';
|
|
33 $maxnum = $RCMAIL->config->get('max_group_members', 0);
|
|
34
|
|
35 foreach ($cids as $source => $cid) {
|
|
36 // Something wrong, target not specified
|
|
37 if (!strlen($target)) {
|
|
38 break;
|
|
39 }
|
|
40
|
|
41 // It maight happen when copying records from search result
|
|
42 // Do nothing, go to next source
|
|
43 if ((string)$target == (string)$source) {
|
|
44 continue;
|
|
45 }
|
|
46
|
|
47 $CONTACTS = $RCMAIL->get_address_book($source);
|
|
48 $TARGET = $RCMAIL->get_address_book($target);
|
|
49
|
|
50 if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
|
|
51 break;
|
|
52 }
|
|
53
|
|
54 $ids = array();
|
|
55
|
|
56 foreach ($cid as $cid) {
|
|
57 $a_record = $CONTACTS->get_record($cid, true);
|
|
58
|
|
59 // avoid copying groups
|
|
60 if ($a_record['_type'] == 'group')
|
|
61 continue;
|
|
62
|
|
63 // Check if contact exists, if so, we'll need it's ID
|
|
64 // Note: Some addressbooks allows empty email address field
|
|
65 // @TODO: should we check all email addresses?
|
|
66 $email = $CONTACTS->get_col_values('email', $a_record, true);
|
|
67 if (!empty($email))
|
|
68 $result = $TARGET->search('email', $email[0], 1, true, true);
|
|
69 else if (!empty($a_record['name']))
|
|
70 $result = $TARGET->search('name', $a_record['name'], 1, true, true);
|
|
71 else
|
|
72 $result = new rcube_result_set();
|
|
73
|
|
74 // insert contact record
|
|
75 if (!$result->count) {
|
|
76 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
|
|
77 'record' => $a_record, 'source' => $target, 'group' => $target_group));
|
|
78
|
|
79 if (!$plugin['abort']) {
|
|
80 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
|
|
81 $ids[] = $insert_id;
|
|
82 $success++;
|
|
83 }
|
|
84 }
|
|
85 else if ($plugin['result']) {
|
|
86 $ids = array_merge($ids, $plugin['result']);
|
|
87 $success++;
|
|
88 }
|
|
89 }
|
|
90 else {
|
|
91 $record = $result->first();
|
|
92 $ids[] = $record['ID'];
|
|
93 $errormsg = empty($email) ? 'contactnameexists' : 'contactexists';
|
|
94 }
|
|
95 }
|
|
96
|
|
97 // assign to group
|
|
98 if ($target_group && $TARGET->groups && !empty($ids)) {
|
|
99 $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
|
|
100 'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
|
|
101
|
|
102 if (!$plugin['abort']) {
|
|
103 $TARGET->reset();
|
|
104 $TARGET->set_group($target_group);
|
|
105
|
|
106 if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
|
|
107 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
|
|
108 $OUTPUT->send();
|
|
109 }
|
|
110
|
|
111 if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
|
|
112 $success = $cnt;
|
|
113 }
|
|
114 else if ($plugin['result']) {
|
|
115 $success = $plugin['result'];
|
|
116 }
|
|
117
|
|
118 $errormsg = $plugin['message'] ?: 'copyerror';
|
|
119 }
|
|
120 }
|
|
121
|
|
122 if (!$success)
|
|
123 $OUTPUT->show_message($errormsg, 'error');
|
|
124 else
|
|
125 $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
|
|
126
|
|
127 // send response
|
|
128 $OUTPUT->send();
|