Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

search_contacts.inc 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 address 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. $search_mode |= rcube_addressbook::SEARCH_GROUPS;
  30. foreach ($sources as $s) {
  31. $source = $RCMAIL->get_address_book($s['id']);
  32. $source->set_page(1);
  33. $source->set_pagesize(9999);
  34. // list matching groups of this source
  35. if ($source->groups) {
  36. $jsresult += rcmail_compose_contact_groups($source, $s['id'], $search, $search_mode);
  37. }
  38. // get contacts count
  39. $result = $source->search($afields, $search, $search_mode, true, true, 'email');
  40. if (!$result->count) {
  41. continue;
  42. }
  43. while ($row = $result->next()) {
  44. $row['sourceid'] = $s['id'];
  45. $key = rcube_addressbook::compose_contact_key($row, $addr_sort_col);
  46. $records[$key] = $row;
  47. }
  48. $search_set[$s['id']] = $source->get_search_set();
  49. unset($result);
  50. }
  51. $group_count = count($jsresult);
  52. // sort the records
  53. ksort($records, SORT_LOCALE_STRING);
  54. // create resultset object
  55. $count = count($records);
  56. $result = new rcube_result_set($count);
  57. // select the requested page
  58. if ($page_size < $count) {
  59. $records = array_slice($records, $result->first, $page_size);
  60. }
  61. $result->records = array_values($records);
  62. if (!empty($result) && $result->count > 0) {
  63. // create javascript list
  64. while ($row = $result->next()) {
  65. $name = rcube_addressbook::compose_list_name($row);
  66. $classname = $row['_type'] == 'group' ? 'group' : 'person';
  67. $keyname = $row['_type'] == 'group' ? 'contactgroup' : 'contact';
  68. // add record for every email address of the contact
  69. // (same as in list_contacts.inc)
  70. $emails = $source->get_col_values('email', $row, true);
  71. foreach ($emails as $i => $email) {
  72. $row_id = $row['ID'].'-'.$i;
  73. $jsresult[$row_id] = format_email_recipient($email, $name);
  74. $title = rcube_addressbook::compose_search_name($row, $email, $name);
  75. $OUTPUT->command('add_contact_row', $row_id, array(
  76. $keyname => html::a(array('title' => $title), rcube::Q($name ?: $email) .
  77. ($name && count($emails) > 1 ? '&nbsp;' . html::span('email', rcube::Q($email)) : '')
  78. )), $classname);
  79. }
  80. }
  81. // search request ID
  82. $search_request = md5('composeaddr' . $search);
  83. // save search settings in session
  84. $_SESSION['search'][$search_request] = $search_set;
  85. $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($afields, $search));
  86. $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
  87. $OUTPUT->set_env('search_request', $search_request);
  88. $OUTPUT->set_env('source', '');
  89. $OUTPUT->command('unselect_directory');
  90. }
  91. else if (!$group_count) {
  92. $OUTPUT->show_message('nocontactsfound', 'notice');
  93. }
  94. // update env
  95. $OUTPUT->set_env('contactdata', $jsresult);
  96. $OUTPUT->set_env('pagecount', ceil($result->count / $page_size));
  97. $OUTPUT->command('set_page_buttons');
  98. // send response
  99. $OUTPUT->send();