thomascube
2012-03-14 a621a9d7ecf334c4894ef8f5168eb6208e5ae0e4
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                     |
f5e7b3 8  | Copyright (C) 2007, 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  $Id: copy.inc 471 2007-02-09 21:25:50Z thomasb $
22
23 */
24
881217 25 // only process ajax requests
T 26 if (!$OUTPUT->ajax_call)
27   return;
28
ecf295 29
A 30 $cids         = rcmail_get_cids();
31 $target       = get_input_value('_to', RCUBE_INPUT_POST);
ca38db 32 $target_group = get_input_value('_togid', RCUBE_INPUT_POST);
757d2b 33
04310e 34 $success  = 0;
T 35 $errormsg = 'copyerror';
36 $maxnum   = $RCMAIL->config->get('max_group_members', 0);
ade8e1 37
ecf295 38 foreach ($cids as $source => $cid)
A 39 {
40     // Something wrong, target not specified
41     if (!strlen($target)) {
42         break;
43     }
44
45     // It maight happen when copying records from search result
46     // Do nothing, go to next source
87a2f6 47     if ((string)$target == (string)$source) {
ecf295 48         continue;
A 49     }
50
51     $CONTACTS = $RCMAIL->get_address_book($source);
52     $TARGET   = $RCMAIL->get_address_book($target);
53
54     if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
55         break;
56     }
57
7d43f8 58     $ids = array();
A 59
ecf295 60     foreach ($cid as $cid) {
A 61         $a_record = $CONTACTS->get_record($cid, true);
6fd71e 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
65         if (!empty($a_record['email']))
f21a04 66             $result = $TARGET->search('email', $a_record['email'], 1, true, true);
2d761b 67         else if (!empty($a_record['name']))
f21a04 68             $result = $TARGET->search('name', $a_record['name'], 1, true, true);
2d761b 69         else
A 70             $result = new rcube_result_set();
7d43f8 71
ecf295 72         // insert contact record
A 73         if (!$result->count) {
74             $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
75                 'record' => $a_record, 'source' => $target, 'group' => $target_group));
ce92ba 76
ecf295 77             if (!$plugin['abort']) {
A 78                 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
79                     $ids[] = $insert_id;
80                     $success++;
81                 }
82             }
83             else if ($plugin['result']) {
84                 $ids = array_merge($ids, $plugin['result']);
85                 $success++;
86             }
7d43f8 87         }
ecf295 88         else {
A 89             $record = $result->first();
90             $ids[] = $record['ID'];
2d761b 91             $errormsg = empty($a_record['email']) ? 'contactnameexists' : 'contactexists';
7d43f8 92         }
A 93     }
94
95     // assign to group
96     if ($target_group && $TARGET->groups && !empty($ids)) {
ecf295 97         $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
A 98             'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
7d43f8 99
ecf295 100         if (!$plugin['abort']) {
A 101             $TARGET->reset();
102             $TARGET->set_group($target_group);
7d43f8 103
ecf295 104             if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
A 105                 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
106                 $OUTPUT->send();
107             }
108
109             if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
110                 $success = $cnt;
7d43f8 111         }
ecf295 112         else if ($plugin['result']) {
A 113             $success = $plugin['result'];
114         }
2d761b 115
04310e 116         $errormsg = $plugin['message'] ? $plugin['message'] : 'copyerror';
757d2b 117     }
f11541 118 }
ecf295 119
A 120 if ($success == 0)
04310e 121     $OUTPUT->show_message($errormsg, 'error');
ecf295 122 else
A 123     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
6b603d 124
f11541 125 // send response
T 126 $OUTPUT->send();