123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- function materio_search_api_ajax_search($page = 0){
- $debug = false;
- global $user, $base_url;
- // TODO: set research path configurable
- $search_path = "explore";
- # execute search
- $_GET['page'] = $page;
- $keys = rawurldecode($_GET['keys']);
- // $keys = $_GET['keys'];
- // dsm($keys, 'keys');
- // foreach(explode(' ', $keys) as $word){
- // $words[] = rawurlencode($word);
- // }
- // $keys_encoded = implode(' ', $words);
- // dsm($words, 'words');
- $keys_encoded = rawurlencode($keys);
- $path = $search_path . '/' . $keys;//. ($page ? '?page='.$page : '');
- // check if request is ajax, if not rediret to search_api_page page with right keys
- if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
- drupal_goto($path, array(), 301);
- exit ;
- }
- // TODO: if no permission, redirect to home
- # content type filter s
- // $types = $_GET['types'];
- if(isset($_GET['types'])){
- # if user have access to filters;
- foreach($_GET['types'] as $type => $value)
- if($value == 'true')
- $active_types[] = $type;
- // if no filter checked we checked them all by default
- if(!isset($active_types))
- foreach($_GET['types'] as $type => $value)
- $active_types[] = $type;
- }else{
- # if user have no access to filters
- $index = search_api_index_load(variable_get('mainsearchindex', -1));
- $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles'];
- foreach ($indexed_bundles as $bundle) {
- $active_types[] = $bundle;
- }
- }
- user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $active_types)));
- # if we are not already on the search page then don't respond with search results but redirect to search page
- if(isset($_GET['current_path'])){
- // url() generates the prefix using hook_url_outbound_alter(). Instead of
- // running the hook_url_outbound_alter() again here, extract the prefix
- // from url().
- url('', array('prefix' => &$prefix));
- $cur_path = str_replace($base_url.base_path().$prefix, '', $_GET['current_path']);
- // dsm($cur_path, 'cur_path');
- $cur_is_search_path = strpos($cur_path, $search_path);
- // dsm($matches, '$matches');
- if($cur_is_search_path === false){
- $rep = array(
- "redirect" => $base_url.base_path().$prefix.$path,
- );
- drupal_json_output($rep);
- exit;
- }
- }
- // define mode (between full text or only term selected on autocompletion)
- // TODO implement the new search mode (only term)
- if(isset($_GET['searchmode'])){
- $searchmode = $_GET['searchmode'];
- user_save($user, array("data"=>array('materiosearchapi_searchmode' => $searchmode)));
- }
- // dsm($path, 'path');
- // execute the searcj path and retrive results
- menu_set_active_item($path);
- $return = menu_execute_active_handler($path, FALSE);
- //dsm($return, '$return');
- if (is_int($return)) {
- switch ($return) {
- case MENU_NOT_FOUND :
- drupal_add_http_header('Status', '404 Not Found');
- break;
- case MENU_ACCESS_DENIED :
- drupal_add_http_header('Status', '403 Forbidden');
- break;
- case MENU_SITE_OFFLINE :
- drupal_add_http_header('Status', '503 Service unavailable');
- break;
- }
- } elseif (isset($return)) {
- if (is_array($return)) {
- $count = $return['results']['#results']['result count'];
- $return = drupal_render($return);
- // $return = theme($return['#theme'], $return);
- }
- $rep = array(
- 'path'=>$search_path.'/'.$keys_encoded,
- 'keys'=>$keys,
- 'keys_encoded'=>$keys_encoded,
- 'search_path'=>$search_path,
- 'returned'=>$return,
- 'active_types'=>$active_types,
- 'title' => drupal_get_title(),
- );
- if(isset($count))
- $rep['count'] = $count;
- if ($debug) {
- //dsm($rep, 'rep');
- return "debug display";
- }else{
- drupal_json_output($rep);
- }
- }
- }
- function materio_search_api_ajax_viewmode_change($vm){
- // dsm($vm);
- // global $user;
- // // dsm($user, 'user');
- // $entity_infos = entity_get_info();
- // // dsm($entity_infos, 'entity_infos');
- // if (in_array($vm, variable_get('availableviewmodes', array()))) {
- // user_save($user, array("data"=>array('materiosearchapi_viewmode' => $vm)));
- // $rep = array('statut'=>'saved');
- // }else{
- // $rep = array('statut'=>'viewmode not allowed');
- // }
- $_GET['page'] = 0;
- $rep = _materio_search_api_change_viewmode($vm);
- drupal_json_output($rep);
- }
- function materio_search_api_ajax_actuality($page = 0){
- $debug = false;
- $_GET['page'] = $page;
- $path = 'actuality';
- // check if request is ajax, if not rediret to search_api_page page with right keys
- if (!$debug && (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest')) {
- drupal_goto($path, array(), 301);
- exit ;
- }
- // if(isset($_GET['current_path'])){
- // // dsm($_GET['current_path'], '$_GET[current_path]');
- // // url() generates the prefix using hook_url_outbound_alter(). Instead of
- // // running the hook_url_outbound_alter() again here, extract the prefix
- // // from url().
- // url('', array('prefix' => &$prefix));
- // $cur_path = str_replace($base_url.base_path().$prefix, '', $_GET['current_path']);
- // // dsm($cur_path, 'cur_path');
- // $cur_is_search_path = strpos($cur_path, $search_path);
- // // dsm($matches, '$matches');
- // if($cur_is_search_path === false){
- // $rep = array(
- // "redirect" => $base_url.base_path().$prefix.$path,
- // );
- // drupal_json_output($rep);
- // exit;
- // }
- // }
- // get results
- menu_set_active_item($path);
- $return = menu_execute_active_handler($path, FALSE);
- //dsm($return, '$return');
- if (is_int($return)) {
- switch ($return) {
- case MENU_NOT_FOUND :
- drupal_add_http_header('Status', '404 Not Found');
- break;
- case MENU_ACCESS_DENIED :
- drupal_add_http_header('Status', '403 Forbidden');
- break;
- case MENU_SITE_OFFLINE :
- drupal_add_http_header('Status', '503 Service unavailable');
- break;
- }
- } elseif (isset($return)) {
- if (is_array($return)) {
- $return = drupal_render($return);
- }
- $rep = array(
- 'path' => $path,
- 'returned'=>$return,
- 'title' => drupal_get_title(),
- );
- if ($debug) {
- //dsm($rep, 'rep');
- return "debug display";
- }else{
- drupal_json_output($rep);
- }
- }
- }
- function materio_search_api_ajax_node($nid){
- global $user;
- $viewmode = isset($_GET['viewmode']) ? $_GET['viewmode'] : (isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full'));
- $rep = array(
- "get" => $_GET,
- "viewmode" => $viewmode
- );
- if($nodeview = node_view(node_load($nid),$viewmode)){
- $rep = $rep+array(
- 'nid' => $nid,
- 'node' => drupal_render($nodeview),
- );
- drupal_json_output($rep);
- }
- }
|