0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | program/steps/addressbook/move.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 | Move a contact record from one direcotry to another |
|
|
16 +-----------------------------------------------------------------------+
|
|
17 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
18 | Author: Aleksander Machniak <alec@alec.pl> |
|
|
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 $all = 0;
|
|
32 $deleted = 0;
|
|
33 $success = 0;
|
|
34 $errormsg = 'moveerror';
|
|
35 $maxnum = $RCMAIL->config->get('max_group_members', 0);
|
|
36 $page = $_SESSION['page'] ?: 1;
|
|
37
|
|
38 foreach ($cids as $source => $source_cids) {
|
|
39 // Something wrong, target not specified
|
|
40 if (!strlen($target)) {
|
|
41 break;
|
|
42 }
|
|
43
|
|
44 // It maight happen when moving records from search result
|
|
45 // Do nothing, go to next source
|
|
46 if ((string)$target === (string)$source) {
|
|
47 continue;
|
|
48 }
|
|
49
|
|
50 $CONTACTS = $RCMAIL->get_address_book($source);
|
|
51 $TARGET = $RCMAIL->get_address_book($target);
|
|
52
|
|
53 if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
|
|
54 break;
|
|
55 }
|
|
56
|
|
57 if (!$CONTACTS || !$CONTACTS->ready || $CONTACTS->readonly) {
|
|
58 continue;
|
|
59 }
|
|
60
|
|
61 $ids = array();
|
|
62
|
|
63 foreach ($source_cids as $idx => $cid) {
|
|
64 $a_record = $CONTACTS->get_record($cid, true);
|
|
65
|
|
66 // avoid moving groups
|
|
67 if ($a_record['_type'] == 'group') {
|
|
68 unset($source_cids[$idx]);
|
|
69 continue;
|
|
70 }
|
|
71
|
|
72 // Check if contact exists, if so, we'll need it's ID
|
|
73 // Note: Some addressbooks allows empty email address field
|
|
74 // @TODO: should we check all email addresses?
|
|
75 $email = $CONTACTS->get_col_values('email', $a_record, true);
|
|
76 if (!empty($email))
|
|
77 $result = $TARGET->search('email', $email[0], 1, true, true);
|
|
78 else if (!empty($a_record['name']))
|
|
79 $result = $TARGET->search('name', $a_record['name'], 1, true, true);
|
|
80 else
|
|
81 $result = new rcube_result_set();
|
|
82
|
|
83 // insert contact record
|
|
84 if (!$result->count) {
|
|
85 $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
|
|
86 'record' => $a_record, 'source' => $target, 'group' => $target_group));
|
|
87
|
|
88 if (!$plugin['abort']) {
|
|
89 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
|
|
90 $ids[] = $insert_id;
|
|
91 $success++;
|
|
92 }
|
|
93 }
|
|
94 else if ($plugin['result']) {
|
|
95 $ids = array_merge($ids, $plugin['result']);
|
|
96 $success++;
|
|
97 }
|
|
98 }
|
|
99 else {
|
|
100 $record = $result->first();
|
|
101 $ids[] = $record['ID'];
|
|
102 $errormsg = empty($email) ? 'contactnameexists' : 'contactexists';
|
|
103 }
|
|
104 }
|
|
105
|
|
106 // remove source contacts
|
|
107 if ($success && !empty($source_cids)) {
|
|
108 $all += count($source_cids);
|
|
109 $plugin = $RCMAIL->plugins->exec_hook('contact_delete', array(
|
|
110 'id' => $source_cids, 'source' => $source));
|
|
111
|
|
112 $del_status = !$plugin['abort'] ? $CONTACTS->delete($source_cids) : $plugin['result'];
|
|
113
|
|
114 if ($del_status) {
|
|
115 $deleted += $del_status;
|
|
116 }
|
|
117 }
|
|
118
|
|
119 // assign to group
|
|
120 if ($target_group && $TARGET->groups && !empty($ids)) {
|
|
121 $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
|
|
122 'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
|
|
123
|
|
124 if (!$plugin['abort']) {
|
|
125 $TARGET->reset();
|
|
126 $TARGET->set_group($target_group);
|
|
127
|
|
128 if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
|
|
129 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
|
|
130 $OUTPUT->send();
|
|
131 }
|
|
132
|
|
133 if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
|
|
134 $success = $cnt;
|
|
135 }
|
|
136 else if ($plugin['result']) {
|
|
137 $success = $plugin['result'];
|
|
138 }
|
|
139
|
|
140 $errormsg = $plugin['message'] ?: 'moveerror';
|
|
141 }
|
|
142 }
|
|
143
|
|
144 if (!$deleted || $deleted != $all) {
|
|
145 $OUTPUT->command('list_contacts');
|
|
146 }
|
|
147 else {
|
|
148 // update saved search after data changed
|
|
149 if (($records = rcmail_search_update(true)) !== false) {
|
|
150 // create resultset object
|
|
151 $count = count($records);
|
|
152 $first = ($page-1) * $PAGE_SIZE;
|
|
153 $result = new rcube_result_set($count, $first);
|
|
154 $pages = ceil((count($records) + $delcnt) / $PAGE_SIZE);
|
|
155
|
|
156 // last page and it's empty, display previous one
|
|
157 if ($result->count && $result->count <= ($PAGE_SIZE * ($page - 1))) {
|
|
158 $OUTPUT->command('list_page', 'prev');
|
|
159 $rowcount = $RCMAIL->gettext('loading');
|
|
160 }
|
|
161 // get records from the next page to add to the list
|
|
162 else if ($pages > 1 && $page < $pages) {
|
|
163 // sort the records
|
|
164 ksort($records, SORT_LOCALE_STRING);
|
|
165
|
|
166 $first += $PAGE_SIZE;
|
|
167 // create resultset object
|
|
168 $res = new rcube_result_set($count, $first - $deleted);
|
|
169
|
|
170 if ($PAGE_SIZE < $count) {
|
|
171 $records = array_slice($records, $first - $deleted, $deleted);
|
|
172 }
|
|
173
|
|
174 $res->records = array_values($records);
|
|
175 $records = $res;
|
|
176 }
|
|
177 else {
|
|
178 unset($records);
|
|
179 }
|
|
180 }
|
|
181 else {
|
|
182 // count contacts for this user
|
|
183 $result = $CONTACTS->count();
|
|
184 $pages = ceil(($result->count + $deleted) / $PAGE_SIZE);
|
|
185
|
|
186 // last page and it's empty, display previous one
|
|
187 if ($result->count && $result->count <= ($PAGE_SIZE * ($page - 1))) {
|
|
188 $OUTPUT->command('list_page', 'prev');
|
|
189 $rowcount = $RCMAIL->gettext('loading');
|
|
190 }
|
|
191 // get records from the next page to add to the list
|
|
192 else if ($pages > 1 && $page < $pages) {
|
|
193 $CONTACTS->set_page($page);
|
|
194 $records = $CONTACTS->list_records(null, -$deleted);
|
|
195 }
|
|
196 }
|
|
197
|
|
198 // update message count display
|
|
199 $OUTPUT->set_env('pagecount', ceil($result->count / $PAGE_SIZE));
|
|
200 $OUTPUT->command('set_rowcount', $rowcount ?: rcmail_get_rowcount_text($result));
|
|
201
|
|
202 // add new rows from next page (if any)
|
|
203 if (!empty($records)) {
|
|
204 rcmail_js_contacts_list($records);
|
|
205 }
|
|
206 }
|
|
207
|
|
208 if (!$success)
|
|
209 $OUTPUT->show_message($errormsg, 'error');
|
|
210 else
|
|
211 $OUTPUT->show_message('movesuccess', 'notice', array('nr' => $success));
|
|
212
|
|
213 // send response
|
|
214 $OUTPUT->send();
|