materiobasemod.pages.inc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * materiobase_search_autocomplete()
  4. *
  5. * inspired by taxonomy_autocomplete()
  6. *
  7. */
  8. function materiobase_search_autocomplete_dbselect($typed = ''){
  9. // If the request has a '/' in the search text, then the menu system will have
  10. // split it into multiple arguments, recover the intended $tags_typed.
  11. $args = func_get_args();
  12. $typed = implode('/', $args);
  13. /*
  14. TODO riche serach engine + \\ etc gmail like
  15. */
  16. if ($typed != '') {
  17. // Part of the criteria for the query come from the field's own settings.
  18. $vids = array();
  19. $vocabularies = taxonomy_vocabulary_get_names();
  20. foreach ($vocabularies as $voc) {
  21. $vids[] = $voc->vid;
  22. }
  23. $query = db_select('taxonomy_term_data', 't');
  24. $query->addTag('translatable');
  25. $query->addTag('term_access');
  26. // Select rows that match by term name.
  27. $tags_return = $query
  28. ->fields('t', array('tid', 'name'))
  29. ->condition('t.vid', $vids)
  30. ->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
  31. ->range(0, 10)
  32. ->execute()
  33. ->fetchAllKeyed();
  34. $term_matches = array();
  35. foreach ($tags_return as $tid => $name) {
  36. $n = $name;
  37. // Term names containing commas or quotes must be wrapped in quotes.
  38. // if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  39. // $n = '"' . str_replace('"', '""', $name) . '"';
  40. // }
  41. $term_matches[$n] = check_plain($name);
  42. }
  43. }
  44. drupal_json_output($term_matches);
  45. }
  46. function materiobase_search_autocomplete_searchapi($typed = ''){
  47. // If the request has a '/' in the search text, then the menu system will have
  48. // split it into multiple arguments, recover the intended $tags_typed.
  49. $args = func_get_args();
  50. $typed = implode('/', $args);
  51. /*
  52. TODO riche serach engine + \\ etc gmail like
  53. */
  54. if ($typed != '') {
  55. $query = search_api_query('referencement');
  56. $query_filter = $query->createFilter();
  57. $query_filter->condition('name', $typed);
  58. // $query_filter->condition('type', 'article');
  59. $query->filter($query_filter);
  60. $tags_return = $query->execute();
  61. dsm($tags_return, '$tags_return');
  62. $term_matches = array();
  63. $index = search_api_index_load('referencement');
  64. foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
  65. dsm($item, '$item');
  66. }
  67. //
  68. // foreach ($tags_return as $tid => $name) {
  69. // $n = $name;
  70. // // Term names containing commas or quotes must be wrapped in quotes.
  71. // // if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
  72. // // $n = '"' . str_replace('"', '""', $name) . '"';
  73. // // }
  74. // $term_matches[$n] = check_plain($name);
  75. // }
  76. }
  77. drupal_json_output($term_matches);
  78. }