4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/copy.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2007, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Copy a contact record from one direcotry to another |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: copy.inc 471 2007-02-09 21:25:50Z thomasb $
22 // only process ajax requests
23 if (!$OUTPUT->ajax_call)
27 $cids = rcmail_get_cids();
28 $target = get_input_value('_to', RCUBE_INPUT_POST);
29 $target_group = get_input_value('_togid', RCUBE_INPUT_POST);
32 $errormsg = 'copyerror';
33 $maxnum = $RCMAIL->config->get('max_group_members', 0);
35 foreach ($cids as $source => $cid)
37 // Something wrong, target not specified
38 if (!strlen($target)) {
42 // It maight happen when copying records from search result
43 // Do nothing, go to next source
44 if ((string)$target == (string)$source) {
48 $CONTACTS = $RCMAIL->get_address_book($source);
49 $TARGET = $RCMAIL->get_address_book($target);
51 if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
57 foreach ($cid as $cid) {
58 $a_record = $CONTACTS->get_record($cid, true);
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'], true, true, true);
64 else if (!empty($a_record['name']))
65 $result = $TARGET->search('name', $a_record['name'], true, true, true);
67 $result = new rcube_result_set();
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));
74 if (!$plugin['abort']) {
75 if ($insert_id = $TARGET->insert($plugin['record'], false)) {
80 else if ($plugin['result']) {
81 $ids = array_merge($ids, $plugin['result']);
86 $record = $result->first();
87 $ids[] = $record['ID'];
88 $errormsg = empty($a_record['email']) ? 'contactnameexists' : 'contactexists';
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));
97 if (!$plugin['abort']) {
99 $TARGET->set_group($target_group);
101 if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
102 $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
106 if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
109 else if ($plugin['result']) {
110 $success = $plugin['result'];
113 $errormsg = $plugin['message'] ? $plugin['message'] : 'copyerror';
118 $OUTPUT->show_message($errormsg, 'error');
120 $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));