]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/copy.inc
Imported Upstream version 0.7
[roundcube.git] / program / steps / addressbook / copy.inc
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, The Roundcube Dev Team                            |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Copy a contact record from one direcotry to another                 |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: copy.inc 471 2007-02-09 21:25:50Z thomasb $
19
20 */
21
22 // only process ajax requests
23 if (!$OUTPUT->ajax_call)
24   return;
25
26
27 $cids         = rcmail_get_cids();
28 $target       = get_input_value('_to', RCUBE_INPUT_POST);
29 $target_group = get_input_value('_togid', RCUBE_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 {
37     // Something wrong, target not specified
38     if (!strlen($target)) {
39         break;
40     }
41
42     // It maight happen when copying records from search result
43     // Do nothing, go to next source
44     if ((string)$target == (string)$source) {
45         continue;
46     }
47
48     $CONTACTS = $RCMAIL->get_address_book($source);
49     $TARGET   = $RCMAIL->get_address_book($target);
50
51     if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
52         break;
53     }
54
55     $ids = array();
56
57     foreach ($cid as $cid) {
58         $a_record = $CONTACTS->get_record($cid, true);
59
60         // Check if contact exists, if so, we'll need it's ID
61         // Note: Some addressbooks allows empty email address field
62         if (!empty($a_record['email']))
63             $result = $TARGET->search('email', $a_record['email'], 1, true, true);
64         else if (!empty($a_record['name']))
65             $result = $TARGET->search('name', $a_record['name'], 1, true, true);
66         else
67             $result = new rcube_result_set();
68
69         // insert contact record
70         if (!$result->count) {
71             $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
72                 'record' => $a_record, 'source' => $target, 'group' => $target_group));
73
74             if (!$plugin['abort']) {
75                 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
76                     $ids[] = $insert_id;
77                     $success++;
78                 }
79             }
80             else if ($plugin['result']) {
81                 $ids = array_merge($ids, $plugin['result']);
82                 $success++;
83             }
84         }
85         else {
86             $record = $result->first();
87             $ids[] = $record['ID'];
88             $errormsg = empty($a_record['email']) ? 'contactnameexists' : 'contactexists';
89         }
90     }
91
92     // assign to group
93     if ($target_group && $TARGET->groups && !empty($ids)) {
94         $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
95             'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
96
97         if (!$plugin['abort']) {
98             $TARGET->reset();
99             $TARGET->set_group($target_group);
100
101             if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
102                 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
103                 $OUTPUT->send();
104             }
105
106             if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
107                 $success = $cnt;
108         }
109         else if ($plugin['result']) {
110             $success = $plugin['result'];
111         }
112
113         $errormsg = $plugin['message'] ? $plugin['message'] : 'copyerror';
114     }
115 }
116
117 if ($success == 0)
118     $OUTPUT->show_message($errormsg, 'error');
119 else
120     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
121
122 // send response
123 $OUTPUT->send();