materio_search_api_ajax.pages.inc 5.8 KB

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