You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

copy.inc 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/addressbook/copy.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2007-2013, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Copy a contact record from one direcotry to another |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. // only process ajax requests
  21. if (!$OUTPUT->ajax_call)
  22. return;
  23. $cids = rcmail_get_cids();
  24. $target = rcube_utils::get_input_value('_to', rcube_utils::INPUT_POST);
  25. $target_group = rcube_utils::get_input_value('_togid', rcube_utils::INPUT_POST);
  26. $success = 0;
  27. $errormsg = 'copyerror';
  28. $maxnum = $RCMAIL->config->get('max_group_members', 0);
  29. foreach ($cids as $source => $cid) {
  30. // Something wrong, target not specified
  31. if (!strlen($target)) {
  32. break;
  33. }
  34. // It maight happen when copying records from search result
  35. // Do nothing, go to next source
  36. if ((string)$target == (string)$source) {
  37. continue;
  38. }
  39. $CONTACTS = $RCMAIL->get_address_book($source);
  40. $TARGET = $RCMAIL->get_address_book($target);
  41. if (!$TARGET || !$TARGET->ready || $TARGET->readonly) {
  42. break;
  43. }
  44. $ids = array();
  45. foreach ($cid as $cid) {
  46. $a_record = $CONTACTS->get_record($cid, true);
  47. // avoid copying groups
  48. if ($a_record['_type'] == 'group')
  49. continue;
  50. // Check if contact exists, if so, we'll need it's ID
  51. // Note: Some addressbooks allows empty email address field
  52. // @TODO: should we check all email addresses?
  53. $email = $CONTACTS->get_col_values('email', $a_record, true);
  54. if (!empty($email))
  55. $result = $TARGET->search('email', $email[0], 1, true, true);
  56. else if (!empty($a_record['name']))
  57. $result = $TARGET->search('name', $a_record['name'], 1, true, true);
  58. else
  59. $result = new rcube_result_set();
  60. // insert contact record
  61. if (!$result->count) {
  62. $plugin = $RCMAIL->plugins->exec_hook('contact_create', array(
  63. 'record' => $a_record, 'source' => $target, 'group' => $target_group));
  64. if (!$plugin['abort']) {
  65. if ($insert_id = $TARGET->insert($plugin['record'], false)) {
  66. $ids[] = $insert_id;
  67. $success++;
  68. }
  69. }
  70. else if ($plugin['result']) {
  71. $ids = array_merge($ids, $plugin['result']);
  72. $success++;
  73. }
  74. }
  75. else {
  76. $record = $result->first();
  77. $ids[] = $record['ID'];
  78. $errormsg = empty($email) ? 'contactnameexists' : 'contactexists';
  79. }
  80. }
  81. // assign to group
  82. if ($target_group && $TARGET->groups && !empty($ids)) {
  83. $plugin = $RCMAIL->plugins->exec_hook('group_addmembers', array(
  84. 'group_id' => $target_group, 'ids' => $ids, 'source' => $target));
  85. if (!$plugin['abort']) {
  86. $TARGET->reset();
  87. $TARGET->set_group($target_group);
  88. if ($maxnum && ($TARGET->count()->count + count($plugin['ids']) > $maxnum)) {
  89. $OUTPUT->show_message('maxgroupmembersreached', 'warning', array('max' => $maxnum));
  90. $OUTPUT->send();
  91. }
  92. if (($cnt = $TARGET->add_to_group($target_group, $plugin['ids'])) && $cnt > $success)
  93. $success = $cnt;
  94. }
  95. else if ($plugin['result']) {
  96. $success = $plugin['result'];
  97. }
  98. $errormsg = $plugin['message'] ?: 'copyerror';
  99. }
  100. }
  101. if (!$success)
  102. $OUTPUT->show_message($errormsg, 'error');
  103. else
  104. $OUTPUT->show_message('copysuccess', 'notice', array('nr' => $success));
  105. // send response
  106. $OUTPUT->send();