materio_search_api_ajax.pages.inc 7.0 KB

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