materio_search_api_ajax.pages.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $return = drupal_render($return);
  55. }
  56. $rep = array(
  57. // 'id'=>$id,
  58. 'keys'=>$keys,
  59. 'search_path'=>$search_path,
  60. 'return'=>$return,
  61. 'active_types'=>$active_types,
  62. );
  63. if ($debug) {
  64. dsm($rep, 'rep');
  65. return "debug display";
  66. }else{
  67. drupal_json_output($rep);
  68. }
  69. }
  70. }
  71. function materio_search_api_ajax_viewmode_change($vm){
  72. // dsm($vm);
  73. global $user;
  74. // dsm($user, 'user');
  75. $entity_infos = entity_get_info();
  76. // dsm($entity_infos, 'entity_infos');
  77. if (in_array($vm, variable_get('availableviewmodes', array()))) {
  78. user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  79. $rep = array('statut'=>'saved');
  80. }else{
  81. $rep = array('statut'=>'viewmode not allowed');
  82. }
  83. //return 'debug mode for materio_search_api_viewmode_change';
  84. drupal_json_output($rep);
  85. }
  86. }