alecpl
2011-07-05 9d195d6e82c3be4e543a47ef8ff1e9fe54bd0939
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                            |
f11541 9  | Licensed under the GNU GPL                                            |
T 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
881217 22 // only process ajax requests
T 23 if (!$OUTPUT->ajax_call)
24   return;
25
ecf295 26
A 27 $cids         = rcmail_get_cids();
28 $target       = get_input_value('_to', RCUBE_INPUT_POST);
ca38db 29 $target_group = get_input_value('_togid', RCUBE_INPUT_POST);
757d2b 30
ecf295 31 $success = 0;
A 32 $maxnum  = $RCMAIL->config->get('max_group_members', 0);
ade8e1 33
ecf295 34 foreach ($cids as $source => $cid)
A 35 {
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
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
ecf295 59         // check if contact exists, if so, we'll need it's ID
A 60         $result = $TARGET->search('email', $a_record['email'], true, true);
7d43f8 61
ecf295 62         // insert contact record
A 63         if (!$result->count) {
64             $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
65                 'record' => $a_record, 'source' => $target, 'group' => $target_group));
ce92ba 66
ecf295 67             if (!$plugin['abort']) {
A 68                 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
69                     $ids[] = $insert_id;
70                     $success++;
71                 }
72             }
73             else if ($plugin['result']) {
74                 $ids = array_merge($ids, $plugin['result']);
75                 $success++;
76             }
7d43f8 77         }
ecf295 78         else {
A 79             $record = $result->first();
80             $ids[] = $record['ID'];
7d43f8 81         }
A 82     }
83
84     // assign to group
85     if ($target_group && $TARGET->groups && !empty($ids)) {
ecf295 86         $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
A 87             'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
7d43f8 88
ecf295 89         if (!$plugin['abort']) {
A 90             $TARGET->reset();
91             $TARGET->set_group($target_group);
7d43f8 92
ecf295 93             if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
A 94                 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
95                 $OUTPUT->send();
96             }
97
98             if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
99                 $success = $cnt;
7d43f8 100         }
ecf295 101         else if ($plugin['result']) {
A 102             $success = $plugin['result'];
103         }
757d2b 104     }
f11541 105 }
ecf295 106
A 107 if ($success == 0)
108     $OUTPUT->show_message('copyerror', 'error');
109 else
110     $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
6b603d 111
f11541 112 // send response
T 113 $OUTPUT->send();