Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

search.inc 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. // get sources list
  118. $sources = $RCMAIL->get_address_sources();
  119. $search_set = array();
  120. $records = array();
  121. $sort_col = $RCMAIL->config->get('addressbook_sort_col', 'name');
  122. $afields = $RCMAIL->config->get('contactlist_fields');
  123. foreach ($sources as $s) {
  124. $source = $RCMAIL->get_address_book($s['id']);
  125. // check if search fields are supported....
  126. if (is_array($fields)) {
  127. $cols = $source->coltypes[0] ? array_flip($source->coltypes) : $source->coltypes;
  128. $supported = 0;
  129. foreach ($fields as $f) {
  130. if (array_key_exists($f, $cols)) {
  131. $supported ++;
  132. }
  133. }
  134. // in advanced search we require all fields (AND operator)
  135. // in quick search we require at least one field (OR operator)
  136. if (($adv && $supported < count($fields)) || (!$adv && !$supported)) {
  137. continue;
  138. }
  139. }
  140. // reset page
  141. $source->set_page(1);
  142. $source->set_pagesize(9999);
  143. // get contacts count
  144. $result = $source->search($fields, $search, $mode, false);
  145. if (!$result->count) {
  146. continue;
  147. }
  148. // get records
  149. $result = $source->list_records($afields);
  150. while ($row = $result->next()) {
  151. $row['sourceid'] = $s['id'];
  152. $key = rcube_addressbook::compose_contact_key($row, $sort_col);
  153. $records[$key] = $row;
  154. }
  155. unset($result);
  156. $search_set[$s['id']] = $source->get_search_set();
  157. }
  158. // sort the records
  159. ksort($records, SORT_LOCALE_STRING);
  160. // create resultset object
  161. $count = count($records);
  162. $result = new rcube_result_set($count);
  163. // cut first-page records
  164. if ($PAGE_SIZE < $count) {
  165. $records = array_slice($records, 0, $PAGE_SIZE);
  166. }
  167. $result->records = array_values($records);
  168. // search request ID
  169. $search_request = md5('addr'
  170. .(is_array($fields) ? implode($fields, ',') : $fields)
  171. .(is_array($search) ? implode($search, ',') : $search));
  172. // save search settings in session
  173. $_SESSION['search'][$search_request] = $search_set;
  174. $_SESSION['search_params'] = array('id' => $search_request, 'data' => array($fields, $search));
  175. $_SESSION['page'] = 1;
  176. if ($adv)
  177. $OUTPUT->command('list_contacts_clear');
  178. if ($result->count > 0) {
  179. // create javascript list
  180. rcmail_js_contacts_list($result);
  181. $OUTPUT->show_message('contactsearchsuccessful', 'confirmation', array('nr' => $result->count));
  182. }
  183. else {
  184. $OUTPUT->show_message('nocontactsfound', 'notice');
  185. }
  186. // update message count display
  187. $OUTPUT->command('set_env', 'search_request', $search_request);
  188. $OUTPUT->command('set_env', 'pagecount', ceil($result->count / $PAGE_SIZE));
  189. $OUTPUT->command('set_rowcount', rcmail_get_rowcount_text($result));
  190. // Re-set current source
  191. $OUTPUT->command('set_env', 'search_id', $sid);
  192. $OUTPUT->command('set_env', 'source', '');
  193. $OUTPUT->command('set_env', 'group', '');
  194. if (!$sid) {
  195. // unselect currently selected directory/group
  196. $OUTPUT->command('unselect_directory');
  197. // enable "Save search" command
  198. $OUTPUT->command('enable_command', 'search-create', true);
  199. }
  200. $OUTPUT->command('update_group_commands');
  201. // send response
  202. $OUTPUT->send($adv ? 'iframe' : null);
  203. }
  204. function rcmail_contact_search_form($attrib)
  205. {
  206. global $RCMAIL, $CONTACT_COLTYPES;
  207. $i_size = $attrib['size'] ?: 30;
  208. $form = array(
  209. 'main' => array(
  210. 'name' => $RCMAIL->gettext('properties'),
  211. 'content' => array(
  212. ),
  213. ),
  214. 'personal' => array(
  215. 'name' => $RCMAIL->gettext('personalinfo'),
  216. 'content' => array(
  217. ),
  218. ),
  219. 'other' => array(
  220. 'name' => $RCMAIL->gettext('other'),
  221. 'content' => array(
  222. ),
  223. ),
  224. );
  225. // get supported coltypes from all address sources
  226. $sources = $RCMAIL->get_address_sources();
  227. $coltypes = array();
  228. foreach ($sources as $s) {
  229. $CONTACTS = $RCMAIL->get_address_book($s['id']);
  230. if (is_array($CONTACTS->coltypes)) {
  231. $contact_cols = $CONTACTS->coltypes[0] ? array_flip($CONTACTS->coltypes) : $CONTACTS->coltypes;
  232. $coltypes = array_merge($coltypes, $contact_cols);
  233. }
  234. }
  235. // merge supported coltypes with $CONTACT_COLTYPES
  236. foreach ($coltypes as $col => $colprop) {
  237. $coltypes[$col] = $CONTACT_COLTYPES[$col] ? array_merge($CONTACT_COLTYPES[$col], (array)$colprop) : (array)$colprop;
  238. }
  239. // build form fields list
  240. foreach ($coltypes as $col => $colprop)
  241. {
  242. if ($colprop['type'] != 'image' && !$colprop['nosearch'])
  243. {
  244. $ftype = $colprop['type'] == 'select' ? 'select' : 'text';
  245. $label = isset($colprop['label']) ? $colprop['label'] : $RCMAIL->gettext($col);
  246. $category = $colprop['category'] ?: 'other';
  247. // load jquery UI datepicker for date fields
  248. if ($colprop['type'] == 'date')
  249. $colprop['class'] .= ($colprop['class'] ? ' ' : '') . 'datepicker';
  250. else if ($ftype == 'text')
  251. $colprop['size'] = $i_size;
  252. $content = html::div('row', html::div('contactfieldlabel label', rcube::Q($label))
  253. . html::div('contactfieldcontent', rcube_output::get_edit_field('search_'.$col, '', $colprop, $ftype)));
  254. $form[$category]['content'][] = $content;
  255. }
  256. }
  257. $hiddenfields = new html_hiddenfield();
  258. $hiddenfields->add(array('name' => '_adv', 'value' => 1));
  259. $out = $RCMAIL->output->request_form(array(
  260. 'name' => 'form', 'method' => 'post',
  261. 'task' => $RCMAIL->task, 'action' => 'search',
  262. 'noclose' => true) + $attrib, $hiddenfields->show());
  263. $RCMAIL->output->add_gui_object('editform', $attrib['id']);
  264. unset($attrib['name']);
  265. unset($attrib['id']);
  266. foreach ($form as $f) {
  267. if (!empty($f['content'])) {
  268. $content = html::div('contactfieldgroup', join("\n", $f['content']));
  269. $out .= html::tag('fieldset', $attrib,
  270. html::tag('legend', null, rcube::Q($f['name']))
  271. . $content) . "\n";
  272. }
  273. }
  274. return $out . '</form>';
  275. }