Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

autocomplete.inc 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/mail/autocomplete.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2008-2013, Roundcube Dev Team |
  8. | Copyright (C) 2011-2013, Kolab Systems AG |
  9. | |
  10. | Licensed under the GNU General Public License version 3 or |
  11. | any later version with exceptions for skins & plugins. |
  12. | See the README file for a full license statement. |
  13. | |
  14. | PURPOSE: |
  15. | Perform a search on configured address books for the address |
  16. | autocompletion of the message compose screen |
  17. +-----------------------------------------------------------------------+
  18. | Author: Thomas Bruederli <roundcube@gmail.com> |
  19. +-----------------------------------------------------------------------+
  20. */
  21. if ($RCMAIL->action == 'group-expand') {
  22. $abook = $RCMAIL->get_address_book(rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC));
  23. if ($gid = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GET)) {
  24. $abook->set_group($gid);
  25. $abook->set_pagesize(9999); // TODO: limit number of group members by config?
  26. $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
  27. $result = $abook->list_records($RCMAIL->config->get('contactlist_fields'));
  28. $members = array();
  29. while ($result && ($record = $result->iterate())) {
  30. $emails = (array) $abook->get_col_values('email', $record, true);
  31. if (!empty($emails) && ($email = array_shift($emails))) {
  32. $members[] = format_email_recipient($email, rcube_addressbook::compose_list_name($record));
  33. }
  34. }
  35. $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
  36. }
  37. $OUTPUT->send();
  38. }
  39. $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
  40. $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
  41. $single = (bool) $RCMAIL->config->get('autocomplete_single');
  42. $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
  43. $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  44. $reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
  45. if (strlen($source)) {
  46. $book_types = array($source);
  47. }
  48. else {
  49. $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
  50. }
  51. $contacts = array();
  52. if (!empty($book_types) && strlen($search)) {
  53. $sort_keys = array();
  54. $books_num = count($book_types);
  55. $search_lc = mb_strtolower($search);
  56. $mode |= rcube_addressbook::SEARCH_GROUPS;
  57. foreach ($book_types as $abook_id) {
  58. $abook = $RCMAIL->get_address_book($abook_id);
  59. $abook->set_pagesize($MAXNUM);
  60. if ($result = $abook->search($RCMAIL->config->get('contactlist_fields'), $search, $mode, true, true, 'email')) {
  61. while ($record = $result->iterate()) {
  62. // Contact can have more than one e-mail address
  63. $email_arr = (array)$abook->get_col_values('email', $record, true);
  64. $email_cnt = count($email_arr);
  65. $idx = 0;
  66. foreach ($email_arr as $email) {
  67. if (empty($email)) {
  68. continue;
  69. }
  70. $name = rcube_addressbook::compose_list_name($record);
  71. $contact = format_email_recipient($email, $name);
  72. // skip entries that don't match
  73. if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
  74. continue;
  75. }
  76. $index = $contact;
  77. // skip duplicates
  78. if (empty($contacts[$index])) {
  79. $contact = array(
  80. 'name' => $contact,
  81. 'type' => $record['_type'],
  82. 'id' => $record['ID'],
  83. 'source' => $abook_id,
  84. );
  85. if (($display = rcube_addressbook::compose_search_name($record, $email, $name)) && $display != $contact['name']) {
  86. $contact['display'] = $display;
  87. }
  88. // groups with defined email address will not be expanded to its members' addresses
  89. if ($record['_type'] == 'group') {
  90. $contact['email'] = $email;
  91. }
  92. $contacts[$index] = $contact;
  93. $sort_keys[$index] = sprintf('%s %03d', $contact['display'] ?: $name, $idx++);
  94. if (count($contacts) >= $MAXNUM) {
  95. break 2;
  96. }
  97. }
  98. // skip redundant entries (show only first email address)
  99. if ($single) {
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. // also list matching contact groups
  106. if ($abook->groups && count($contacts) < $MAXNUM) {
  107. foreach ($abook->list_groups($search, $mode) as $group) {
  108. $abook->reset();
  109. $abook->set_group($group['ID']);
  110. $group_prop = $abook->get_group($group['ID']);
  111. // group (distribution list) with email address(es)
  112. if ($group_prop['email']) {
  113. $idx = 0;
  114. foreach ((array)$group_prop['email'] as $email) {
  115. $index = format_email_recipient($email, $group['name']);
  116. if (empty($contacts[$index])) {
  117. $sort_keys[$index] = sprintf('%s %03d', $group['name'] , $idx++);
  118. $contacts[$index] = array(
  119. 'name' => $index,
  120. 'email' => $email,
  121. 'type' => 'group',
  122. 'id' => $group['ID'],
  123. 'source' => $abook_id,
  124. );
  125. if (count($contacts) >= $MAXNUM) {
  126. break 2;
  127. }
  128. }
  129. }
  130. }
  131. // show group with count
  132. else if (($result = $abook->count()) && $result->count) {
  133. if (empty($contacts[$group['name']])) {
  134. $sort_keys[$group['name']] = $group['name'];
  135. $contacts[$group['name']] = array(
  136. 'name' => $group['name'] . ' (' . intval($result->count) . ')',
  137. 'type' => 'group',
  138. 'id' => $group['ID'],
  139. 'source' => $abook_id,
  140. );
  141. if (count($contacts) >= $MAXNUM) {
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (count($contacts)) {
  150. // sort contacts index
  151. asort($sort_keys, SORT_LOCALE_STRING);
  152. // re-sort contacts according to index
  153. foreach ($sort_keys as $idx => $val) {
  154. $sort_keys[$idx] = $contacts[$idx];
  155. }
  156. $contacts = array_values($sort_keys);
  157. }
  158. }
  159. // Allow autocomplete result optimization via plugin
  160. $pluginResult = $RCMAIL->plugins->exec_hook('contacts_autocomplete_after', array(
  161. 'search' => $search,
  162. 'contacts' => $contacts, // Provide already-found contacts to plugin if they are required
  163. ));
  164. $contacts = $pluginResult['contacts'];
  165. $OUTPUT->command('ksearch_query_results', $contacts, $search, $reqid);
  166. $OUTPUT->send();