materio_search_api_ajax.pages.inc 7.1 KB

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