materio_search_api.pages.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. $index_machine_name = variable_get('mainsearchindex', -1);
  126. $index = search_api_index_load($index_machine_name);
  127. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  128. foreach ($indexed_bundles as $bundle) {
  129. $default_bundles[] = $bundle;
  130. }
  131. $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles;
  132. // dsm($bundles_filter, 'bundles_filter');
  133. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  134. if ($keys) {
  135. try {
  136. $limit = variable_get($viewmode.'_limite', '10');
  137. $offset = pager_find_page() * $limit; //$page*$limit;//
  138. $query = search_api_query($index_machine_name, array('parse mode'=>'direct'))
  139. ->keys(implode(' ', $keys))
  140. // TODO: add bundle filter
  141. // ->condition('type', 'breve')
  142. ->range($offset, $limit);
  143. $filter = $query->createFilter('OR');
  144. foreach ($bundles_filter as $type) {
  145. $filter->condition('type', $type, '=');
  146. }
  147. // dsm($filter, 'filter');
  148. $query->filter($filter);
  149. // $bundle_query_filter = $query->createFilter();
  150. // $bundle_query_filter->condition('type', 'materiau');//$bundles_filter);
  151. // $query->filter($bundle_query_filter);
  152. $results = $query->execute();
  153. }
  154. catch (SearchApiException $e) {
  155. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  156. watchdog('materiobasemod', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  157. }
  158. // dsm($results, 'results');
  159. # Load spellcheck.
  160. // if (isset($results['search_api_spellcheck'])) {
  161. // $ret['results']['#spellcheck'] = array(
  162. // '#theme' => 'search_api_spellcheck',
  163. // '#spellcheck' => $results['search_api_spellcheck'],
  164. // // Let the theme function know where the key is stored by passing its arg
  165. // // number. We can work this out from the number of args in the page path.
  166. // '#options' => array(
  167. // 'arg' => array(count(arg(NULL, $page->path))),
  168. // ),
  169. // '#prefix' => '<p class="search-api-spellcheck suggestion">',
  170. // '#suffix' => '</p>',
  171. // );
  172. // }
  173. $ret['results']['#theme'] = 'materio_search_api_results';
  174. /*
  175. TODO choose index in module settings
  176. */
  177. $ret['results']['#index'] = search_api_index_load('materiaux_breves');
  178. $ret['results']['#results'] = $results;
  179. $ret['results']['#view_mode'] = $viewmode;
  180. $ret['results']['#keys'] = $keys;
  181. // Load pager.
  182. // if ($results['result count'] > $page->options['per_page']) {
  183. pager_default_initialize($results['result count'], $limit);
  184. $ret['results']['#pager'] = theme('pager');
  185. // }
  186. if (!empty($results['ignored'])) {
  187. drupal_set_message(
  188. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  189. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  190. 'warning'
  191. );
  192. }
  193. if (!empty($results['warnings'])) {
  194. foreach ($results['warnings'] as $warning) {
  195. drupal_set_message($warning, 'warning');
  196. }
  197. }
  198. }
  199. return $ret;
  200. }
  201. /**
  202. * materio_search_api_actuality()
  203. *
  204. */
  205. function materio_search_api_actuality(){
  206. $date = strtotime('-6 month');
  207. $limit = 10;//variable_get($viewmode.'_limite', '10');
  208. $offset = pager_find_page() * $limit;
  209. $query = new EntityFieldQuery;
  210. $query
  211. ->entityCondition('entity_type', 'node')
  212. ->propertyCondition('status', 1)
  213. ->entityCondition('bundle', array('breve'))
  214. ->propertyCondition('created', $date, '>')
  215. ->propertyOrderBy('created', 'DESC')
  216. ->range($offset,$limit);
  217. $result = $query->execute();
  218. // dsm($result, '$result');
  219. $count_query = new EntityFieldQuery;
  220. $count = $count_query
  221. ->entityCondition('entity_type', 'node')
  222. ->propertyCondition('status', 1)
  223. ->entityCondition('bundle', array('breve'))
  224. ->propertyCondition('created', $date, '>')
  225. ->count()->execute();
  226. // dsm($count, 'count');
  227. pager_default_initialize($count, $limit);
  228. foreach ($result['node'] as $nid => $n) {
  229. $breve = node_load($nid);
  230. if(!node_access('view', $breve))
  231. continue;
  232. $items[] = $breve;
  233. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  234. // dsm($materiaux, 'materiaux');
  235. if($materiaux){
  236. foreach ($materiaux as $value) {
  237. $materiau = node_load($value['target_id']);
  238. if(node_access('view', $materiau))
  239. $items[] = $materiau;
  240. }
  241. }
  242. }
  243. return theme('materio_search_api_actuality', array(
  244. 'items' => $items,
  245. 'view_mode' => 'cardmedium',
  246. 'count' => $count,
  247. 'pager' => theme('pager'),
  248. ));
  249. }