materio_search_api_ajax.pages.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // dsm($path, 'path');
  61. // execute the searcj path and retrive results
  62. menu_set_active_item($path);
  63. $return = menu_execute_active_handler($path, FALSE);
  64. //dsm($return, '$return');
  65. if (is_int($return)) {
  66. switch ($return) {
  67. case MENU_NOT_FOUND :
  68. drupal_add_http_header('Status', '404 Not Found');
  69. break;
  70. case MENU_ACCESS_DENIED :
  71. drupal_add_http_header('Status', '403 Forbidden');
  72. break;
  73. case MENU_SITE_OFFLINE :
  74. drupal_add_http_header('Status', '503 Service unavailable');
  75. break;
  76. }
  77. } elseif (isset($return)) {
  78. if (is_array($return)) {
  79. $count = $return['results']['#results']['result count'];
  80. $return = drupal_render($return);
  81. // $return = theme($return['#theme'], $return);
  82. }
  83. $rep = array(
  84. 'path'=>$search_path.'/'.$keys_encoded,
  85. 'keys'=>$keys,
  86. 'keys_encoded'=>$keys_encoded,
  87. 'search_path'=>$search_path,
  88. 'returned'=>$return,
  89. 'active_types'=>$active_types,
  90. 'title' => drupal_get_title(),
  91. );
  92. if(isset($count))
  93. $rep['count'] = $count;
  94. if ($debug) {
  95. //dsm($rep, 'rep');
  96. return "debug display";
  97. }else{
  98. drupal_json_output($rep);
  99. }
  100. }
  101. }
  102. function materio_search_api_ajax_viewmode_change($vm){
  103. // dsm($vm);
  104. // global $user;
  105. // // dsm($user, 'user');
  106. // $entity_infos = entity_get_info();
  107. // // dsm($entity_infos, 'entity_infos');
  108. // if (in_array($vm, variable_get('availableviewmodes', array()))) {
  109. // user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
  110. // $rep = array('statut'=>'saved');
  111. // }else{
  112. // $rep = array('statut'=>'viewmode not allowed');
  113. // }
  114. $_GET['page'] = 0;
  115. $rep = _materio_search_api_change_viewmode($vm);
  116. drupal_json_output($rep);
  117. }
  118. function materio_search_api_ajax_actuality($page = 0){
  119. $debug = false;
  120. $_GET['page'] = $page;
  121. $path = 'actuality';
  122. // check if request is ajax, if not rediret to search_api_page page with right keys
  123. if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
  124. drupal_goto($path, array(), 301);
  125. exit ;
  126. }
  127. // if(isset($_GET['current_path'])){
  128. // // dsm($_GET['current_path'], '$_GET[current_path]');
  129. // // url() generates the prefix using hook_url_outbound_alter(). Instead of
  130. // // running the hook_url_outbound_alter() again here, extract the prefix
  131. // // from url().
  132. // url('', array('prefix' => &$prefix));
  133. // $cur_path = str_replace($base_url.base_path().$prefix, '', $_GET['current_path']);
  134. // // dsm($cur_path, 'cur_path');
  135. // $cur_is_search_path = strpos($cur_path, $search_path);
  136. // // dsm($matches, '$matches');
  137. // if($cur_is_search_path === false){
  138. // $rep = array(
  139. // "redirect" => $base_url.base_path().$prefix.$path,
  140. // );
  141. // drupal_json_output($rep);
  142. // exit;
  143. // }
  144. // }
  145. // get results
  146. menu_set_active_item($path);
  147. $return = menu_execute_active_handler($path, FALSE);
  148. //dsm($return, '$return');
  149. if (is_int($return)) {
  150. switch ($return) {
  151. case MENU_NOT_FOUND :
  152. drupal_add_http_header('Status', '404 Not Found');
  153. break;
  154. case MENU_ACCESS_DENIED :
  155. drupal_add_http_header('Status', '403 Forbidden');
  156. break;
  157. case MENU_SITE_OFFLINE :
  158. drupal_add_http_header('Status', '503 Service unavailable');
  159. break;
  160. }
  161. } elseif (isset($return)) {
  162. if (is_array($return)) {
  163. $return = drupal_render($return);
  164. }
  165. $rep = array(
  166. 'path' => $path,
  167. 'returned'=>$return,
  168. 'title' => drupal_get_title(),
  169. );
  170. if ($debug) {
  171. //dsm($rep, 'rep');
  172. return "debug display";
  173. }else{
  174. drupal_json_output($rep);
  175. }
  176. }
  177. }
  178. function materio_search_api_ajax_node($nid){
  179. global $user;
  180. $viewmode = isset($_GET['viewmode']) ? $_GET['viewmode'] : (isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full'));
  181. $rep = array(
  182. "get" => $_GET,
  183. "viewmode" => $viewmode
  184. );
  185. if($nodeview = node_view(node_load($nid),$viewmode)){
  186. $rep = $rep+array(
  187. 'nid' => $nid,
  188. 'node' => drupal_render($nodeview),
  189. );
  190. drupal_json_output($rep);
  191. }
  192. }