materio_search_api_ajax.pages.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. function materio_search_api_ajax_search($keys, $page = 0){
  3. $debug = false;
  4. global $user;
  5. // TODO: set research path configurable
  6. $search_path = "explore";
  7. # content type filter s
  8. // $types = $_GET['types'];
  9. if(isset($_GET['types'])){
  10. # if user have access to filters;
  11. foreach($_GET['types'] as $type => $value)
  12. if($value == 'true')
  13. $active_types[] = $type;
  14. // if no filter checked we checked them all by default
  15. if(!isset($active_types))
  16. foreach($_GET['types'] as $type => $value)
  17. $active_types[] = $type;
  18. }else{
  19. # if user have no access to filters
  20. $index = search_api_index_load(variable_get('mainsearchindex', -1));
  21. $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
  22. foreach ($indexed_bundles as $bundle) {
  23. $active_types[] = $bundle;
  24. }
  25. }
  26. user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $active_types)));
  27. # execute search
  28. $_GET['page'] = $page;
  29. $path = $search_path . '/' . $keys ;//. ($page ? '?page='.$page : '');
  30. // dsm($menuhandler, 'menuhandler');
  31. // check if request is ajax, if not rediret to search_api_page page with right keys
  32. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  33. drupal_goto($path, array(), 301);
  34. exit ;
  35. }
  36. // get results
  37. menu_set_active_item($path);
  38. $return = menu_execute_active_handler($path, FALSE);
  39. //dsm($return, '$return');
  40. if (is_int($return)) {
  41. switch ($return) {
  42. case MENU_NOT_FOUND :
  43. drupal_add_http_header('Status', '404 Not Found');
  44. break;
  45. case MENU_ACCESS_DENIED :
  46. drupal_add_http_header('Status', '403 Forbidden');
  47. break;
  48. case MENU_SITE_OFFLINE :
  49. drupal_add_http_header('Status', '503 Service unavailable');
  50. break;
  51. }
  52. } elseif (isset($return)) {
  53. if (is_array($return)) {
  54. $count = $return['results']['#results']['result count'];
  55. $return = drupal_render($return);
  56. // $return = theme($return['#theme'], $return);
  57. }
  58. $rep = array(
  59. 'path'=>$path,
  60. 'keys'=>$keys,
  61. 'search_path'=>$search_path,
  62. 'return'=>$return,
  63. 'active_types'=>$active_types,
  64. 'title' => drupal_get_title(),
  65. );
  66. if(isset($count))
  67. $rep['count'] = $count;
  68. if ($debug) {
  69. dsm($rep, 'rep');
  70. return "debug display";
  71. }else{
  72. drupal_json_output($rep);
  73. }
  74. }
  75. }
  76. function materio_search_api_ajax_viewmode_change($vm){
  77. // dsm($vm);
  78. // global $user;
  79. // // dsm($user, 'user');
  80. // $entity_infos = entity_get_info();
  81. // // dsm($entity_infos, 'entity_infos');
  82. // if (in_array($vm, variable_get('availableviewmodes', array()))) {
  83. // user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  84. // $rep = array('statut'=>'saved');
  85. // }else{
  86. // $rep = array('statut'=>'viewmode not allowed');
  87. // }
  88. $_GET['page'] = 0;
  89. $rep = _materio_search_api_change_viewmode($vm);
  90. drupal_json_output($rep);
  91. }
  92. function materio_search_api_ajax_actuality($page = 0){
  93. $_GET['page'] = $page;
  94. $path = 'actuality';
  95. // check if request is ajax, if not rediret to search_api_page page with right keys
  96. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  97. drupal_goto($path, array(), 301);
  98. exit ;
  99. }
  100. // get results
  101. menu_set_active_item($path);
  102. $return = menu_execute_active_handler($path, FALSE);
  103. //dsm($return, '$return');
  104. if (is_int($return)) {
  105. switch ($return) {
  106. case MENU_NOT_FOUND :
  107. drupal_add_http_header('Status', '404 Not Found');
  108. break;
  109. case MENU_ACCESS_DENIED :
  110. drupal_add_http_header('Status', '403 Forbidden');
  111. break;
  112. case MENU_SITE_OFFLINE :
  113. drupal_add_http_header('Status', '503 Service unavailable');
  114. break;
  115. }
  116. } elseif (isset($return)) {
  117. if (is_array($return)) {
  118. $return = drupal_render($return);
  119. }
  120. $rep = array(
  121. 'path' => $path,
  122. 'return'=>$return,
  123. 'title' => drupal_get_title(),
  124. );
  125. if ($debug) {
  126. dsm($rep, 'rep');
  127. return "debug display";
  128. }else{
  129. drupal_json_output($rep);
  130. }
  131. }
  132. }