source: subversion/trunk/roundcubemail/program/steps/addressbook/copy.inc @ 3755

Last change on this file since 3755 was 3755, checked in by alec, 3 years ago
File size: 3.2 KB
Line 
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, RoundCube Dev. - Switzerland                      |
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
23if (!$OUTPUT->ajax_call)
24  return;
25
26$cid = get_input_value('_cid', RCUBE_INPUT_POST);
27$target = get_input_value('_to', RCUBE_INPUT_POST);
28$target_group = get_input_value('_togid', RCUBE_INPUT_POST);
29
30if ($cid && preg_match('/^[a-zA-Z0-9\+\/=_-]+(,[a-zA-Z0-9\+\/=_-]+)*$/', $cid) && strlen($target) && $target !== $source)
31{
32  $success = 0;
33  $TARGET = $RCMAIL->get_address_book($target);
34
35  if ($TARGET && $TARGET->ready && !$TARGET->readonly) {
36    $arr_cids = explode(',', $cid);
37    $ids = array();
38
39    foreach ($arr_cids as $cid) {
40      $plugin = $RCMAIL->plugins->exec_hook('create_contact', array(
41        'record' => $CONTACTS->get_record($cid, true),
42        'source' => $target,
43        'group' => $target_group,
44      ));
45      $a_record = $plugin['record'];
46
47      if (!$plugin['abort']) {
48        // check if contact exists, if so, we'll need it's ID
49        $result = $TARGET->search('email', $a_record['email'], true, true);
50
51        // insert contact record
52        if (!$result->count) {
53          if ($insert_id = $TARGET->insert($a_record, false)) {
54            $ids[] = $insert_id;
55            $success++;
56          }
57        }
58        else {
59          $record = $result->first();
60          $ids[] = $record['ID'];
61        }
62      }
63    }
64
65    // assign to group
66    if ($target_group && $TARGET->groups && !empty($ids)) {
67      $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
68        'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
69
70      if (!$plugin['abort']) {
71        $TARGET->reset();
72        $TARGET->set_group($target_group);
73
74        if (($maxnum = $RCMAIL->config->get('max_group_members', 0)) && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
75          $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
76          $OUTPUT->send();
77        }
78
79        if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
80          $success = $cnt;
81      }
82    }
83  }
84
85  if ($success == 0)
86    $OUTPUT->show_message('copyerror', 'error');
87  else
88    $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
89}
90
91// send response
92$OUTPUT->send();
93
94?>
Note: See TracBrowser for help on using the repository browser.