materio_search_api.pages.inc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * materiobase_search_autocomplete_dbselect()
  4. *
  5. * inspired by taxonomy_autocomplete()
  6. *
  7. * OBSOLETE : this fonction use a direct dbselect request to provide results forautocomplete
  8. *
  9. */
  10. function materio_search_api_autocomplete_dbselect($typed = ''){
  11. // If the request has a '/' in the search text, then the menu system will have
  12. // split it into multiple arguments, recover the intended $tags_typed.
  13. $args = func_get_args();
  14. $typed = implode('/', $args);
  15. /*
  16. TODO riche serach engine + \\ etc gmail like
  17. */
  18. if ($typed != '') {
  19. // Part of the criteria for the query come from the field's own settings.
  20. $vids = array();
  21. $vocabularies = taxonomy_vocabulary_get_names();
  22. foreach ($vocabularies as $voc) {
  23. $vids[] = $voc->vid;
  24. }
  25. $query = db_select('taxonomy_term_data', 't');
  26. $query->addTag('translatable');
  27. $query->addTag('term_access');
  28. // Select rows that match by term name.
  29. $tags_return = $query
  30. ->fields('t', array('tid', 'name'))
  31. ->condition('t.vid', $vids)
  32. ->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
  33. ->range(0, 10)
  34. ->execute()
  35. ->fetchAllKeyed();
  36. $term_matches = array();
  37. foreach ($tags_return as $tid => $name) {
  38. $n = $name;
  39. // Term names containing commas or quotes must be wrapped in quotes.
  40. // if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  41. // $n = '"' . str_replace('"', '""', $name) . '"';
  42. // }
  43. $term_matches[$n] = check_plain($name);
  44. }
  45. }
  46. drupal_json_output($term_matches);
  47. }
  48. /**
  49. * materio_search_api_autocomplete_searchapi($typed = '')
  50. *
  51. * GOOD one using searchapi (SOLR)
  52. */
  53. function materio_search_api_autocomplete_searchapi($typed = ''){
  54. // If the request has a '/' in the search text, then the menu system will have
  55. // split it into multiple arguments, recover the intended $tags_typed.
  56. $args = func_get_args();
  57. $typed = implode('/', $args);
  58. // dsm($typed, 'typed');
  59. if ($typed != '') {
  60. // search for patterns like key -another key +lastkey
  61. // and provide auto completion for the last key
  62. preg_match_all('/\s?[\+|-]?[^\s]+/', $typed, $adv_search_q);
  63. preg_match('/^(\+|-)?(.*)$/', trim(array_pop($adv_search_q[0])), $last);
  64. $tosearch = isset($last[2]) ? $last[2] : $typed;
  65. // build the query
  66. $index_machine_name = variable_get('autocompletesearchindex', -1);
  67. $query = search_api_query($index_machine_name);
  68. $query_filter = $query->createFilter();
  69. $query_filter->condition('name', $tosearch);
  70. // $query_filter->condition('type', 'article');
  71. $query->filter($query_filter);
  72. $tags_return = $query->execute();
  73. // dsm($tags_return, '$tags_return');
  74. if($tags_return['result count']){
  75. $term_matches = array();
  76. $index = search_api_index_load($index_machine_name);
  77. $delta = 0;
  78. foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
  79. // dsm($item, '$item');
  80. //$term_matches[$item->tid] = check_plain($item->name);
  81. // $term_matches[check_plain($item->name)] = check_plain($item->name);
  82. // TODO: leave tags with nodes
  83. $term_matches[ trim(implode(' ', $adv_search_q[0]).' '.$last[1].$item->name)] = check_plain($item->name);
  84. $delta++;
  85. if($delta > 7)
  86. break;
  87. }
  88. drupal_json_output($term_matches);
  89. }else{
  90. drupal_json_output(array());
  91. }
  92. }else{
  93. return;
  94. }
  95. // dsm($term_matches, 'term_matches');
  96. // return 'debug mode of materio_search_api_autocomplete_searchapi';
  97. }
  98. /**
  99. * materio_search_api_results_search()
  100. *
  101. *
  102. */
  103. function materio_search_api_results_search(){//, $limit = 20, $page = 0
  104. global $user;
  105. //dsm("materio_search_api_results_search");
  106. $args = func_get_args();
  107. $typed = implode('/', $args);
  108. preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
  109. //dsm($pages, '$pages');
  110. if($pages[0]){
  111. $typed = str_replace($pages[0][0], '', $typed);
  112. // $page = $pages[1][0];
  113. }
  114. // else{
  115. // $page = 0;
  116. // }
  117. preg_match_all('/\s?[^\s]+\s?/', $typed, $words);
  118. // dsm($words, "words");
  119. // $escaper = array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", '"', "~", "*", "?", ":", '\\');
  120. foreach ($words[0] as $word) {
  121. // $word = preg_replace('/\b-/', '\-', trim($word));
  122. // dsm($word);
  123. $keys[] = $word;
  124. }
  125. if ($keys) {
  126. try {
  127. $limit = 15;
  128. $offset = pager_find_page() * $limit; //$page*$limit;//
  129. $index_machine_name = variable_get('mainsearchindex', -1);
  130. $query = search_api_query($index_machine_name, array('parse mode'=>'direct'))
  131. ->keys(implode(' ', $keys))
  132. ->range($offset, $limit);
  133. $results = $query->execute();
  134. }
  135. catch (SearchApiException $e) {
  136. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  137. watchdog('materiobasemod', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  138. }
  139. // dsm($results, 'results');
  140. # Load spellcheck.
  141. // if (isset($results['search_api_spellcheck'])) {
  142. // $ret['results']['#spellcheck'] = array(
  143. // '#theme' => 'search_api_spellcheck',
  144. // '#spellcheck' => $results['search_api_spellcheck'],
  145. // // Let the theme function know where the key is stored by passing its arg
  146. // // number. We can work this out from the number of args in the page path.
  147. // '#options' => array(
  148. // 'arg' => array(count(arg(NULL, $page->path))),
  149. // ),
  150. // '#prefix' => '<p class="search-api-spellcheck suggestion">',
  151. // '#suffix' => '</p>',
  152. // );
  153. // }
  154. $ret['results']['#theme'] = 'materio_search_api_results';
  155. /*
  156. TODO choose index in module settings
  157. */
  158. $ret['results']['#index'] = search_api_index_load('materiaux_breves');
  159. $ret['results']['#results'] = $results;
  160. /*
  161. TODO choose view mode from user data
  162. */
  163. $ret['results']['#view_mode'] = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  164. $ret['results']['#keys'] = $keys;
  165. // Load pager.
  166. // if ($results['result count'] > $page->options['per_page']) {
  167. /*
  168. TODO set per page limit as module settings
  169. */
  170. pager_default_initialize($results['result count'], $limit);
  171. $ret['results']['#pager'] = theme('pager');
  172. // }
  173. if (!empty($results['ignored'])) {
  174. drupal_set_message(
  175. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  176. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  177. 'warning'
  178. );
  179. }
  180. if (!empty($results['warnings'])) {
  181. foreach ($results['warnings'] as $warning) {
  182. drupal_set_message($warning, 'warning');
  183. }
  184. }
  185. }
  186. return $ret;
  187. }
  188. function materio_search_api_viewmode_change($vm){
  189. // dsm($vm);
  190. global $user;
  191. // dsm($user, 'user');
  192. $entity_infos = entity_get_info();
  193. // dsm($entity_infos, 'entity_infos');
  194. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  195. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  196. $rep = array('statut'=>'saved');
  197. }else{
  198. $rep = array('statut'=>'viewmode not allowed');
  199. }
  200. //return 'debug mode for materio_search_api_viewmode_change';
  201. drupal_json_output($rep);
  202. }