materio_search_api_ajax.pages.inc 7.0 KB

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