Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/addressbook/search.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2005-2011, The Roundcube Dev Team |
  8. | Copyright (C) 2011, 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. | Search action (and form) for address book contacts |
  16. | |
  17. +-----------------------------------------------------------------------+
  18. | Author: Thomas Bruederli <roundcube@gmail.com> |
  19. | Author: Aleksander Machniak <machniak@kolabsys.com> |
  20. +-----------------------------------------------------------------------+
  21. */
  22. if ($RCMAIL->action == 'search-create') {
  23. $id = rcube_utils::get_input_value('_search', rcube_utils::INPUT_POST);
  24. $name = rcube_utils::get_input_value('_name', rcube_utils::INPUT_POST, true);
  25. if (($params = $_SESSION['search_params']) && $params['id'] == $id) {
  26. $data = array(
  27. 'type' => rcube_user::SEARCH_ADDRESSBOOK,
  28. 'name' => $name,
  29. 'data' => array(
  30. 'fields' => $params['data'][0],
  31. 'search' => $params['data'][1],
  32. ),
  33. );
  34. $plugin = $RCMAIL->plugins->exec_hook('saved_search_create', array('data' => $data));
  35. if (!$plugin['abort'])
  36. $result = $RCMAIL->user->insert_search($plugin['data']);
  37. else
  38. $result = $plugin['result'];
  39. }
  40. if ($result) {
  41. $OUTPUT->show_message('savedsearchcreated', 'confirmation');
  42. $OUTPUT->command('insert_saved_search', rcube::Q($name), rcube::Q($result));
  43. }
  44. else
  45. $OUTPUT->show_message($plugin['message'] ?: 'savedsearchcreateerror', 'error');
  46. $OUTPUT->send();
  47. }
  48. if ($RCMAIL->action == 'search-delete') {
  49. $id = rcube_utils::get_input_value('_sid', rcube_utils::INPUT_POST);
  50. $plugin = $RCMAIL->plugins->exec_hook('saved_search_delete', array('id' => $id));
  51. if (!$plugin['abort'])
  52. $result = $RCMAIL->user->delete_search($id);
  53. else
  54. $result = $plugin['result'];
  55. if ($result) {
  56. $OUTPUT->show_message('savedsearchdeleted', 'confirmation');
  57. $OUTPUT->command('remove_search_item', rcube::Q($id));
  58. // contact list will be cleared, clear also page counter
  59. $OUTPUT->command('set_rowcount', $RCMAIL->gettext('nocontactsfound'));
  60. $OUTPUT->set_env('pagecount', 0);
  61. }
  62. else
  63. $OUTPUT->show_message($plugin['message'] ?: 'savedsearchdeleteerror', 'error');
  64. $OUTPUT->send();
  65. }
  66. if (!isset($_GET['_form'])) {
  67. rcmail_contact_search();
  68. }
  69. $OUTPUT->add_handler('searchform', 'rcmail_contact_search_form');
  70. $OUTPUT->send('contactsearch');
  71. function rcmail_contact_search()
  72. {
  73. global $RCMAIL, $OUTPUT, $SEARCH_MODS_DEFAULT, $PAGE_SIZE;
  74. $adv = isset($_POST['_adv']);
  75. $sid = rcube_utils::get_input_value('_sid', rcube_utils::INPUT_GET);
  76. // get search criteria from saved search
  77. if ($sid && ($search = $RCMAIL->user->get_search($sid))) {
  78. $fields = $search['data']['fields'];
  79. $search = $search['data']['search'];
  80. }
  81. // get fields/values from advanced search form
  82. else if ($adv) {
  83. foreach (array_keys($_POST) as $key) {
  84. $s = trim(rcube_utils::get_input_value($key, rcube_utils::INPUT_POST, true));
  85. if (strlen($s) && preg_match('/^_search_([a-zA-Z0-9_-]+)$/', $key, $m)) {
  86. $search[] = $s;
  87. $fields[] = $m[1];
  88. }
  89. }
  90. if (empty($fields)) {
  91. // do nothing, show the form again
  92. return;
  93. }
  94. }
  95. // quick-search
  96. else {
  97. $search = trim(rcube_utils::get_input_value('_q', rcube_utils::INPUT_GET, true));
  98. $fields = explode(',', rcube_utils::get_input_value('_headers', rcube_utils::INPUT_GET));
  99. if (empty($fields)) {
  100. $fields = array_keys($SEARCH_MODS_DEFAULT);
  101. }
  102. else {
  103. $fields = array_filter($fields);
  104. }
  105. // update search_mods setting
  106. $old_mods = $RCMAIL->config->get('addressbook_search_mods');
  107. $search_mods = array_fill_keys($fields, 1);
  108. if ($old_mods != $search_mods) {
  109. $RCMAIL->user->save_prefs(array('addressbook_search_mods' => $search_mods));
  110. }
  111. if (in_array('*', $fields)) {
  112. $fields = '*';
  113. }
  114. }
  115. // Values matching mode
  116. $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
  117. $mode |= rcube_addressbook::SEARCH_GROUPS;
  118. // get sources list
  119. $sources = $RCMAIL->get_address_sources();
  120. $search_set = array();
  121. $records = array();
  122. $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  123. $afields = $RCMAIL->config->get('contactlist_fields');
  124. foreach ($sources as $s) {
  125. $source = $RCMAIL->get_address_book($s['id']);
  126. // check if search fields are supported....
  127. if (is_array($fields)) {
  128. $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
  129. $supported = 0;
  130. foreach ($fields as $f) {
  131. if (array_key_exists($f, $cols)) {
  132. $supported ++;
  133. }
  134. }
  135. // in advanced search we require all fields (AND operator)
  136. // in quick search we require at least one field (OR operator)
  137. if (($adv && $supported < count($fields)) || (!$adv && !$supported)) {
  138. continue;
  139. }
  140. }
  141. // reset page
  142. $source->set_page(1);
  143. $source->set_pagesize(9999);
  144. // get contacts count
  145. $result = $source->search($fields, $search, $mode, false);
  146. if (!$result->count) {
  147. continue;
  148. }
  149. // get records
  150. $result = $source->list_records($afields);
  151. while ($row = $result->next()) {
  152. $row['sourceid'] = $s['id'];
  153. $key = rcube_addressbook::compose_contact_key($row, $sort_col);
  154. $records[$key] = $row;
  155. }
  156. unset($result);
  157. $search_set[$s['id']] = $source->get_search_set();
  158. }
  159. // sort the records
  160. ksort($records, SORT_LOCALE_STRING);
  161. // create resultset object
  162. $count = count($records);
  163. $result = new rcube_result_set($count);
  164. // cut first-page records
  165. if ($PAGE_SIZE < $count) {
  166. $records = array_slice($records, 0, $PAGE_SIZE);
  167. }
  168. $result->records = array_values($records);
  169. // search request ID
  170. $search_request = md5('addr'
  171. .(is_array($fields) ? implode($fields, ',') : $fields)
  172. .(is_array($search) ? implode($search, ',') : $search));
  173. // save search settings in session
  174. $_SESSION['search'][$search_request] = $search_set;
  175. $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($fields, $search));
  176. $_SESSION['page'] = 1;
  177. if ($adv)
  178. $OUTPUT->command('list_contacts_clear');
  179. if ($result->count > 0) {
  180. // create javascript list
  181. rcmail_js_contacts_list($result);
  182. $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
  183. }
  184. else {
  185. $OUTPUT->show_message('nocontactsfound', 'notice');
  186. }
  187. // update message count display
  188. $OUTPUT->set_env('search_request', $search_request);
  189. $OUTPUT->set_env('pagecount', ceil($result->count / $PAGE_SIZE));
  190. $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
  191. // Re-set current source
  192. $OUTPUT->set_env('search_id', $sid);
  193. $OUTPUT->set_env('source', '');
  194. $OUTPUT->set_env('group', '');
  195. // Re-set list header
  196. $OUTPUT->command('set_group_prop', null);
  197. if ($adv) {
  198. $OUTPUT->command('parent.set_env', array(
  199. 'search_request' => $search_request,
  200. 'pagecount' => ceil($result->count / $PAGE_SIZE),
  201. 'search_id' => $sid,
  202. 'source' => '',
  203. 'group' => '',
  204. ));
  205. }
  206. if (!$sid) {
  207. // unselect currently selected directory/group
  208. $OUTPUT->command('unselect_directory');
  209. // enable "Save search" command
  210. $OUTPUT->command('enable_command', 'search-create', true);
  211. }
  212. $OUTPUT->command('update_group_commands');
  213. // send response
  214. $OUTPUT->send($adv ? 'iframe' : null);
  215. }
  216. function rcmail_contact_search_form($attrib)
  217. {
  218. global $RCMAIL, $CONTACT_COLTYPES;
  219. $i_size = $attrib['size'] ?: 30;
  220. $form = array(
  221. 'main' => array(
  222. 'name' => $RCMAIL->gettext('properties'),
  223. 'content' => array(
  224. ),
  225. ),
  226. 'personal' => array(
  227. 'name' => $RCMAIL->gettext('personalinfo'),
  228. 'content' => array(
  229. ),
  230. ),
  231. 'other' => array(
  232. 'name' => $RCMAIL->gettext('other'),
  233. 'content' => array(
  234. ),
  235. ),
  236. );
  237. // get supported coltypes from all address sources
  238. $sources = $RCMAIL->get_address_sources();
  239. $coltypes = array();
  240. foreach ($sources as $s) {
  241. $CONTACTS = $RCMAIL->get_address_book($s['id']);
  242. if (is_array($CONTACTS->coltypes)) {
  243. $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
  244. $coltypes = array_merge($coltypes, $contact_cols);
  245. }
  246. }
  247. // merge supported coltypes with $CONTACT_COLTYPES
  248. foreach ($coltypes as $col => $colprop) {
  249. $coltypes[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], (array)$colprop) : (array)$colprop;
  250. }
  251. // build form fields list
  252. foreach ($coltypes as $col => $colprop)
  253. {
  254. if ($colprop['type'] != 'image' && !$colprop['nosearch'])
  255. {
  256. $ftype = $colprop['type'] == 'select' ? 'select' : 'text';
  257. $label = isset($colprop['label']) ? $colprop['label'] : $RCMAIL->gettext($col);
  258. $category = $colprop['category'] ?: 'other';
  259. // load jquery UI datepicker for date fields
  260. if ($colprop['type'] == 'date')
  261. $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
  262. else if ($ftype == 'text')
  263. $colprop['size'] = $i_size;
  264. $content = html::div('row', html::div('contactfieldlabel label', rcube::Q($label))
  265. . html::div('contactfieldcontent', rcube_output::get_edit_field('search_'.$col, '', $colprop, $ftype)));
  266. $form[$category]['content'][] = $content;
  267. }
  268. }
  269. $hiddenfields = new html_hiddenfield();
  270. $hiddenfields->add(array('name' => '_adv', 'value' => 1));
  271. $out = $RCMAIL->output->request_form(array(
  272. 'name' => 'form', 'method' => 'post',
  273. 'task' => $RCMAIL->task, 'action' => 'search',
  274. 'noclose' => true) + $attrib, $hiddenfields->show());
  275. $RCMAIL->output->add_gui_object('editform', $attrib['id']);
  276. unset($attrib['name']);
  277. unset($attrib['id']);
  278. foreach ($form as $f) {
  279. if (!empty($f['content'])) {
  280. $content = html::div('contactfieldgroup', join("\n", $f['content']));
  281. $out .= html::tag('fieldset', $attrib,
  282. html::tag('legend', null, rcube::Q($f['name']))
  283. . $content) . "\n";
  284. }
  285. }
  286. return $out . '</form>';
  287. }