materio_search_api.pages.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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(){
  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[] = trim($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. ->range($offset, $limit);
  141. $filter = $query->createFilter('OR');
  142. foreach ($bundles_filter as $type) {
  143. $filter->condition('type', $type, '=');
  144. }
  145. // dsm($filter, 'filter');
  146. $query->filter($filter);
  147. // $bundle_query_filter = $query->createFilter();
  148. // $bundle_query_filter->condition('type', 'materiau');//$bundles_filter);
  149. // $query->filter($bundle_query_filter);
  150. $results = $query->execute();
  151. }
  152. catch (SearchApiException $e) {
  153. $ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
  154. watchdog('materiobasemod', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
  155. }
  156. // dsm($results, 'results');
  157. // $accessible_results = array();
  158. // foreach ($results['results'] as $nid => $entity) {
  159. // if(node_access('view', node_load($nid)))
  160. // $accessible_results[$nid] = $entity;
  161. // }
  162. // $results['results'] = $accessible_results;
  163. // $results['result count'] = count($accessible_results);
  164. # Load spellcheck.
  165. // if (isset($results['search_api_spellcheck'])) {
  166. // $ret['results']['#spellcheck'] = array(
  167. // '#theme' => 'search_api_spellcheck',
  168. // '#spellcheck' => $results['search_api_spellcheck'],
  169. // // Let the theme function know where the key is stored by passing its arg
  170. // // number. We can work this out from the number of args in the page path.
  171. // '#options' => array(
  172. // 'arg' => array(count(arg(NULL, $page->path))),
  173. // ),
  174. // '#prefix' => '<p class="search-api-spellcheck suggestion">',
  175. // '#suffix' => '</p>',
  176. // );
  177. // }
  178. $ret['results']['#theme'] = 'materio_search_api_results';
  179. /*
  180. TODO choose index in module settings
  181. */
  182. $ret['results']['#index'] = search_api_index_load('materiaux_breves');
  183. $ret['results']['#results'] = $results;
  184. $ret['results']['#view_mode'] = $viewmode;
  185. $ret['results']['#keys'] = $keys;
  186. drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($typed), PASS_THROUGH);
  187. // Load pager.
  188. // if ($results['result count'] > $page->options['per_page']) {
  189. pager_default_initialize($results['result count'], $limit);
  190. $ret['results']['#pager'] = theme('pager');
  191. // }
  192. if (!empty($results['ignored'])) {
  193. drupal_set_message(
  194. t('The following search keys are too short or too common and were therefore ignored: "@list".',
  195. array( '@list' => implode(t('", "'), $results['ignored']) ) ),
  196. 'warning'
  197. );
  198. }
  199. if (!empty($results['warnings'])) {
  200. foreach ($results['warnings'] as $warning) {
  201. drupal_set_message($warning, 'warning');
  202. }
  203. }
  204. }
  205. return $ret;
  206. }
  207. /**
  208. * materio_search_api_actuality()
  209. *
  210. */
  211. function materio_search_api_actuality(){
  212. global $user;
  213. if(!user_access('use materio search api')){
  214. $date = strtotime('-6 month');
  215. // dsm($date);
  216. }
  217. $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
  218. $limit = 10;//variable_get($viewmode.'_limite', '10');
  219. $offset = pager_find_page() * $limit;
  220. // dsm($offset);
  221. $query = new EntityFieldQuery;
  222. $query
  223. ->entityCondition('entity_type', 'node')
  224. ->propertyCondition('status', 1)
  225. ->entityCondition('bundle', array('breve'))
  226. ->propertyOrderBy('created', 'DESC')
  227. ->range($offset,$limit);
  228. if(!user_access('use materio search api')){
  229. $query->propertyCondition('created', $date, '>');
  230. }
  231. $result = $query->execute();
  232. // dsm($result, '$result');
  233. $count_query = new EntityFieldQuery;
  234. $count_query
  235. ->entityCondition('entity_type', 'node')
  236. ->propertyCondition('status', 1)
  237. ->entityCondition('bundle', array('breve'));
  238. // dsm($count, 'count');
  239. if(!user_access('use materio search api')){
  240. $count_query->propertyCondition('created', $date, '>');
  241. }
  242. $count = $count_query->count()->execute();
  243. pager_default_initialize($count, $limit);
  244. foreach ($result['node'] as $nid => $n) {
  245. $breve = node_load($nid);
  246. if(!node_access('view', $breve))
  247. continue;
  248. $items[] = $breve;
  249. $materiaux = field_get_items('node',$breve,'field_materiau_ref');
  250. // dsm($materiaux, 'materiaux');
  251. if($materiaux){
  252. foreach ($materiaux as $value) {
  253. $materiau = node_load($value['target_id']);
  254. if(node_access('view', $materiau))
  255. $items[] = $materiau;
  256. }
  257. }
  258. }
  259. drupal_set_title(t('Actualities'));
  260. return theme('materio_search_api_actuality', array(
  261. 'items' => $items,
  262. 'view_mode' => $viewmode,
  263. 'count' => $count,
  264. 'pager' => theme('pager'),
  265. ));
  266. }
  267. function materio_search_api_viewmode_change($vm){
  268. dsm($vm, 'materio_search_api_viewmode_change');
  269. $rep = _materio_search_api_change_viewmode($vm);
  270. //return 'debug mode for materio_search_api_viewmode_change';
  271. // drupal_json_output($rep);
  272. drupal_goto();
  273. }