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.

search_contacts.inc 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/mail/search_contacts.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2013-2014, 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. | Search contacts from the adress book widget |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. $search = rcube_utils::get_input_value('_q', rcube_utils::INPUT_GPC, true);
  21. $sources = $RCMAIL->get_address_sources();
  22. $search_mode = (int) $RCMAIL->config->get('addressbook_search_mode');
  23. $addr_sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  24. $afields = $RCMAIL->config->get('contactlist_fields');
  25. $page_size = $RCMAIL->config->get('addressbook_pagesize', $RCMAIL->config->get('pagesize', 50));
  26. $records = array();
  27. $search_set = array();
  28. $jsresult = array();
  29. foreach ($sources as $s) {
  30. $source = $RCMAIL->get_address_book($s['id']);
  31. $source->set_page(1);
  32. $source->set_pagesize(9999);
  33. // list matching groups of this source
  34. if ($source->groups) {
  35. $jsresult += rcmail_compose_contact_groups($source, $s['id'], $search, $search_mode);
  36. }
  37. // get contacts count
  38. $result = $source->search($afields, $search, $search_mode, true, true, 'email');
  39. if (!$result->count) {
  40. continue;
  41. }
  42. while ($row = $result->next()) {
  43. $row['sourceid'] = $s['id'];
  44. $key = rcube_addressbook::compose_contact_key($row, $addr_sort_col);
  45. $records[$key] = $row;
  46. }
  47. $search_set[$s['id']] = $source->get_search_set();
  48. unset($result);
  49. }
  50. $group_count = count($jsresult);
  51. // sort the records
  52. ksort($records, SORT_LOCALE_STRING);
  53. // create resultset object
  54. $count = count($records);
  55. $result = new rcube_result_set($count);
  56. // select the requested page
  57. if ($page_size < $count) {
  58. $records = array_slice($records, $result->first, $page_size);
  59. }
  60. $result->records = array_values($records);
  61. if (!empty($result) && $result->count > 0) {
  62. // create javascript list
  63. while ($row = $result->next()) {
  64. $name = rcube_addressbook::compose_list_name($row);
  65. // add record for every email address of the contact
  66. // (same as in list_contacts.inc)
  67. $emails = $source->get_col_values('email', $row, true);
  68. foreach ($emails as $i => $email) {
  69. $row_id = $row['ID'].'-'.$i;
  70. $jsresult[$row_id] = format_email_recipient($email, $name);
  71. $title = rcube_addressbook::compose_search_name($row, $email, $name);
  72. $OUTPUT->command('add_contact_row', $row_id, array(
  73. 'contact' => html::a(array('title' => $title), rcube::Q($name ?: $email) .
  74. ($name && count($emails) > 1 ? '&nbsp;' . html::span('email', rcube::Q($email)) : '')
  75. )), 'person');
  76. }
  77. }
  78. // search request ID
  79. $search_request = md5('composeaddr' . $search);
  80. // save search settings in session
  81. $_SESSION['search'][$search_request] = $search_set;
  82. $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($afields, $search));
  83. $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
  84. $OUTPUT->command('set_env', 'search_request', $search_request);
  85. $OUTPUT->command('set_env', 'source', '');
  86. $OUTPUT->command('unselect_directory');
  87. }
  88. else if (!$group_count) {
  89. $OUTPUT->show_message('nocontactsfound', 'notice');
  90. }
  91. // update env
  92. $OUTPUT->set_env('contactdata', $jsresult);
  93. $OUTPUT->set_env('pagecount', ceil($result->count / $page_size));
  94. $OUTPUT->command('set_page_buttons');
  95. // send response
  96. $OUTPUT->send();