materio_search_api_ajax.pages.inc 6.9 KB

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