Thomas Bruederli
2014-03-21 d2215764898919f1ea3b461fb08ac430db4340a4
commit | author | age
f11541 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/copy.inc                                    |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5d2ee 8  | Copyright (C) 2007-2013, The Roundcube Dev Team                       |
7fe381 9  |                                                                       |
T 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.                     |
f11541 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Copy a contact record from one direcotry to another                 |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
881217 22 // only process ajax requests
T 23 if (!$OUTPUT->ajax_call)
24   return;
25
ecf295 26
A 27 $cids         = rcmail_get_cids();
6b2b2e 28 $target       = rcube_utils::get_input_value('_to', rcube_utils::INPUT_POST);
AM 29 $target_group = rcube_utils::get_input_value('_togid', rcube_utils::INPUT_POST);
757d2b 30
04310e 31 $success  = 0;
T 32 $errormsg = 'copyerror';
33 $maxnum   = $RCMAIL->config->get('max_group_members', 0);
ade8e1 34
f5d2ee 35 foreach ($cids as $source => $cid) {
ecf295 36     // Something wrong, target not specified
A 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
87a2f6 43     if ((string)$target == (string)$source) {
ecf295 44         continue;
A 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
7d43f8 54     $ids = array();
A 55
ecf295 56     foreach ($cid as $cid) {
A 57         $a_record = $CONTACTS->get_record($cid, true);
6fd71e 58
6ff6be 59         // avoid copying groups
TB 60         if ($a_record['_type'] == 'group')
61             continue;
62
2d761b 63         // Check if contact exists, if so, we'll need it's ID
A 64         // Note: Some addressbooks allows empty email address field
a3f745 65         // @TODO: should we check all email addresses?
AM 66         $email = $CONTACTS->get_col_values('email', $a_record, true);
67         if (!empty($email))
68             $result = $TARGET->search('email', $email[0], 1, true, true);
2d761b 69         else if (!empty($a_record['name']))
f21a04 70             $result = $TARGET->search('name', $a_record['name'], 1, true, true);
2d761b 71         else
A 72             $result = new rcube_result_set();
7d43f8 73
ecf295 74         // insert contact record
A 75         if (!$result->count) {
76             $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
77                 'record' => $a_record, 'source' => $target, 'group' => $target_group));
ce92ba 78
ecf295 79             if (!$plugin['abort']) {
A 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             }
7d43f8 89         }
ecf295 90         else {
A 91             $record = $result->first();
92             $ids[] = $record['ID'];
2d761b 93             $errormsg = empty($a_record['email']) ? 'contactnameexists' : 'contactexists';
7d43f8 94         }
A 95     }
96
97     // assign to group
98     if ($target_group && $TARGET->groups && !empty($ids)) {
ecf295 99         $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
A 100             'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
7d43f8 101
ecf295 102         if (!$plugin['abort']) {
A 103             $TARGET->reset();
104             $TARGET->set_group($target_group);
7d43f8 105
ecf295 106             if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
A 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;
7d43f8 113         }
ecf295 114         else if ($plugin['result']) {
A 115             $success = $plugin['result'];
116         }
2d761b 117
04310e 118         $errormsg = $plugin['message'] ? $plugin['message'] : 'copyerror';
757d2b 119     }
f11541 120 }
ecf295 121
a45f9b 122 if (!$success)
04310e 123     $OUTPUT->show_message($errormsg, 'error');
ecf295 124 else
A 125     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
6b603d 126
f11541 127 // send response
T 128 $OUTPUT->send();