diff --git a/js/materio_search_api_ajax.js b/js/materio_search_api_ajax.js index ff23d1b7..66b9638c 100644 --- a/js/materio_search_api_ajax.js +++ b/js/materio_search_api_ajax.js @@ -2,8 +2,8 @@ (function($) { -MaterioBaseMod = function(){ - +MaterioSearchApiAjax = function(){ + var _History = window.History, _isloadingresults = false; _$content = $('#content'); @@ -39,13 +39,8 @@ MaterioBaseMod = function(){ // /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686 $("#edit-searchfield").bind('autocompleteSelect', function(event) { - setTimeout(function(){ - loadResults(getSearchKeys()); - },10); - - // loadResults($(this).val()); + $(this).parents('.form').trigger('submit'); }); - _$content .bind('jsp-initialised', function(event, isScrollable){ @@ -72,18 +67,36 @@ MaterioBaseMod = function(){ }; + + function getSearchKeys(){ + return $('#materio-search-api-search-form').find('input[name*="searchfield"]').val(); + }; function loadResults(keys){ // trace('keys', keys); if(keys !== undefined){ keys = keys.replace('/', ' '); + + var types = {}; + $('#edit-bundles-filter', '#materio-search-api-search-form').find('input[type*="checkbox"]').each(function(i){ + $this = $(this); + // if ( $this.attr('checked') ) { + + types[$this.val()] = $this.attr('checked'); + // } + }); + trace('types', types); + + if(!_isloadingresults){ _isloadingresults = true; $('#materio-search-api-search-form').addClass('loading'); // TODO: record ajax path in a variable from materio_search_api_ajax_init - $.getJSON('/materio_search_api_ajax/search/'+keys, function(json){ - // trace('json', json); - $(window).trigger('resultsloaded'); + $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/search/'+keys, + {'types':types}, + function(json){ + trace('json', json); + $.event.trigger('resultsloaded'); _isloadingresults = false; $('#materio-search-api-search-form').removeClass('loading'); updateContent(json); @@ -95,37 +108,28 @@ MaterioBaseMod = function(){ function updateContent(json){ if(json.return){ - // TODO: set jscrollpane to top $('.inner-content',_$content).html(json.return); - // TODO: this a DIRTY sheet of what could be refresh results transitions - // var $newContent = $(json.return); - // trace('newContent', $newContent); - // $('.node-materiau',$newContent).hide(); - // $('.node-materiau','#content').fadeOut(300); - // setTimeout(function(){ - // $('#content').html($newContent); - // $('.node-materiau','#content').fadeIn(500); - // },300); - + var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.search_path + '/' + json.keys; + + _History.pushState({content:json.return}, json.keys, path); + $('input[name=location]','#feedback-form').attr('value', path); - _History.pushState({content:json.return}, json.keys, '/'+json.search_path + '/' +json.keys); - // navigate({content:json.return}, '', '/'+json.page.path + '/' +json.keys); + for (language in Drupal.settings.materio_search_api_ajax.languages) { + var l = Drupal.settings.materio_search_api_ajax.languages[language]; + $('#block-locale-language li.'+language+' a').attr('href', Drupal.settings.basePath + l.prefix+'/' + json.search_path + '/' + json.keys) + }; - var event = jQuery.Event('resultsupdated'); + var event = jQuery.Event('resultschanged'); event.container = $('.search-results', _$content); - $(window).trigger(event); - + $.event.trigger(event); }else{ trace('no results'); } }; - function getSearchKeys(){ - return $('#materio-search-api-search-form').find('input[name*="searchfield"]').val(); - }; /** * infinit scroll @@ -200,8 +204,8 @@ MaterioBaseMod = function(){ }; function changeViewMode(vm, $btn){ - $.getJSON('/materiosearchapi/viewmode/change/'+vm, function(json){ - // trace('viewmode json', json); + $.getJSON(Drupal.settings.basePath+'materio_search_api_ajax/viewmode/change/'+vm, function(json){ + trace('viewmode json', json); if (json.statut == "saved"){ loadResults(getSearchKeys()); $('.viewmode-link').removeClass('active'); @@ -217,6 +221,6 @@ MaterioBaseMod = function(){ }; - var materiobasemod = new MaterioBaseMod(); + var materiosearchapiajax = new MaterioSearchApiAjax(); })(jQuery); \ No newline at end of file diff --git a/materio_search_api.admin.inc b/materio_search_api.admin.inc index 8f20e149..4def4f0a 100644 --- a/materio_search_api.admin.inc +++ b/materio_search_api.admin.inc @@ -33,7 +33,7 @@ function materio_search_api_settings(){ $form['availableviewmodes'] = array( '#type'=>'select', '#options'=>$viewmode_options, - '#default_value' => variable_get('availableviewmodes', -1), + '#default_value' => variable_get('availableviewmodes', array()), '#title' => t('Availble View modes'), '#multiple' => true, ); @@ -45,5 +45,16 @@ function materio_search_api_settings(){ '#title' => t('Defalut View mode'), ); + + foreach (variable_get('availableviewmodes', array()) as $viewmode => $value) { + $form[$viewmode.'_limite'] = array( + '#type'=>'textfield', + '#size'=>5, + '#default_value' => variable_get($viewmode.'_limite', '10'), + '#title' => t('@vm limite', array('@vm'=>$viewmode)), + ); + } + + return system_settings_form($form); } \ No newline at end of file diff --git a/materio_search_api.info b/materio_search_api.info index 0f32d8b4..15edb421 100644 --- a/materio_search_api.info +++ b/materio_search_api.info @@ -1,4 +1,4 @@ -name = materio_search_api +name = Materio SearchApi description = "Materio Search Api module" ; Core version (required) diff --git a/materio_search_api.module b/materio_search_api.module index 4855ddb8..bb395945 100644 --- a/materio_search_api.module +++ b/materio_search_api.module @@ -30,6 +30,10 @@ function materio_search_api_permission() { 'title' => t('administer Materio search api'), 'description' => t('Administer materio search api.'), ), + 'use materio search api filters' => array( + 'title' => t('use Materio search api filters'), + 'description' => t('Use materio search api filters.'), + ), ); } @@ -92,6 +96,11 @@ function materio_search_api_block_info() { 'info' => t('Materio search api view mode selection'), 'cache' => DRUPAL_NO_CACHE ); + + // $blocks['materio_search_api_filters'] = array( + // 'info' => t('Materio search api filters'), + // 'cache' => DRUPAL_NO_CACHE + // ); return $blocks; } @@ -117,6 +126,12 @@ function materio_search_api_block_view($delta = '') { $block['content'] = theme('materio_search_api_select_viewmode_block', array()); } break; + // case 'materio_search_api_filters': + // if (user_access('use materio search api filters')) { + // $block['subject'] = t('Filters'); + // $block['content'] = theme('materio_search_api_filters_block', array()); + // } + // break; } return $block; } @@ -146,31 +161,35 @@ function materio_search_api_entity_info_alter(&$entity_info) { ); } -/** -* Implements hook_preprocess_node(). -*/ -function materio_search_api_preprocess_node(&$vars) { - // $vars['theme_hook_suggestions'][] = 'node__'.$vars['view_mode']; - // $vars['theme_hook_suggestions'][] = 'node__' . $vars['type'] . '__' . $vars['view_mode']; - // dsm($vars, '$vars'); -} - -/** -* Implements hook_preprocess_field(). -*/ -function materio_search_api_preprocess_field(&$vars) { - //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#view_mode']; - //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_type'] . '__' . $vars['element']['#view_mode']; - //$vars['theme_hook_suggestions'][] = 'field__' . $vars['element']['#field_name'] . '__' . $vars['element']['#view_mode']; - // dsm($vars, '$vars'); -} - /** * materiobase_search_form() */ function materio_search_api_search_form($form, &$form_state){ + // dsm($form_state, 'form_state'); + // dsm($form, 'form'); + global $user; $form = array(); + if(user_access('use materio search api 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) { + $bundles_options[$bundle] = $bundle; + $default_bundles[] = $bundle; + } + + $user_bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles; + + $form['bundles_filter'] = array( + '#type'=>'checkboxes', + '#options' => $bundles_options, + '#default_value' => $user_bundles_filter, + '#attributes' => array('class'=>array('btn-group')), + ); + } + $args = arg(); $path = array_shift($args); $keys = implode('/', $args); @@ -203,9 +222,30 @@ function materio_search_api_search_form_validate($form, &$form_state){ } function materio_search_api_search_form_submit($form, &$form_state){ - $keys = $form_state['values']['searchfield']; - // drupal_go_to('base/explore/'.$keys); - $form_state['redirect'] = 'explore/'.$keys; + // dsm($form_state, 'form_state'); + global $user; + if(user_access('use materio search api filters')){ + foreach($form_state['values']['bundles_filter'] as $bundle => $value) + if($value) + $bundles[] = $bundle; + + # if no filter checked we checked them all by default + if(!isset($bundles)) + foreach($form_state['values']['bundles_filter'] as $bundle => $value) + $bundles[] = $bundle; + }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) { + $bundles[] = $bundle; + } + } + + user_save($user, array("data"=>array('materiosearchapi_bundlesfilter' => $bundles))); + + $form_state['redirect'] = 'explore/'.$form_state['values']['searchfield']; } @@ -246,6 +286,11 @@ function materio_search_api_theme($existing, $type, $theme, $path) { 'template' => 'materio-search-api-select-viewmode-block', 'path' => drupal_get_path('module', 'materio_search_api').'/templates', ), + // 'materio_search_api_filters_block' => array( + // 'arguments' => array(), + // 'template' => 'materio-search-api-filters-block', + // 'path' => drupal_get_path('module', 'materio_search_api').'/templates', + // ), 'materio_search_api_results' => array( 'arguments' => array(), 'template' => 'materio-search-api-results', @@ -293,6 +338,36 @@ function template_preprocess_materio_search_api_select_viewmode_block(&$vars){ $vars['content'] = $content; } +// function template_preprocess_materio_search_api_filters_block(&$vars){ + +// $index_machine_name = variable_get('mainsearchindex', -1); +// $index = search_api_index_load($index_machine_name); +// dsm($index, 'index'); + +// // $entity_infos = entity_get_info($index->item_type); +// // dsm($entity_infos, 'entity_infos'); +// $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles']; + +// dsm($indexed_bundles, 'indexed_bundles'); + + +// $vars['content'] = drupal_get_form('materio_search_api_filters_form', $indexed_bundles); +// } + +// function materio_search_api_filters_form($form, $form_state, $bundles){ +// $form = array(); +// dsm($bundles, 'bundles'); +// foreach ($bundles as $bundle) { +// $form[$bundle . '_filter'] = array( +// '#type'=>'checkbox', +// '#default_value' => -1, +// '#title' => $bundle, +// ); +// } + +// return $form; +// } + /** * Function for preprocessing the variables for the search_api_page_results * template. diff --git a/materio_search_api.pages.inc b/materio_search_api.pages.inc index 91ad708f..e9c3abb0 100644 --- a/materio_search_api.pages.inc +++ b/materio_search_api.pages.inc @@ -142,24 +142,45 @@ function materio_search_api_results_search(){//, $limit = 20, $page = 0 // $escaper = array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", '"', "~", "*", "?", ":", '\\'); foreach ($words[0] as $word) { - // $word = preg_replace('/\b-/', '\-', trim($word)); // dsm($word); - $keys[] = $word; } + $index_machine_name = variable_get('mainsearchindex', -1); + $index = search_api_index_load($index_machine_name); + + $indexed_bundles = $index->options['data_alter_callbacks']['search_api_alter_bundle_filter']['settings']['bundles']; + foreach ($indexed_bundles as $bundle) { + $default_bundles[] = $bundle; + } + $bundles_filter = isset($user->data['materiosearchapi_bundlesfilter']) ? $user->data['materiosearchapi_bundlesfilter'] : $default_bundles; + // dsm($bundles_filter, 'bundles_filter'); + + $viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full'); + if ($keys) { try { - // TODO: change limit function of viewmode small card -> many items, big cards -> few items - $limit = 15; + $limit = variable_get($viewmode.'_limite', '10'); $offset = pager_find_page() * $limit; //$page*$limit;// - $index_machine_name = variable_get('mainsearchindex', -1); $query = search_api_query($index_machine_name, array('parse mode'=>'direct')) ->keys(implode(' ', $keys)) + // TODO: add bundle filter + // ->condition('type', 'breve') ->range($offset, $limit); + $filter = $query->createFilter('OR'); + foreach ($bundles_filter as $type) { + $filter->condition('type', $type, '='); + } + // dsm($filter, 'filter'); + $query->filter($filter); + + // $bundle_query_filter = $query->createFilter(); + // $bundle_query_filter->condition('type', 'materiau');//$bundles_filter); + // $query->filter($bundle_query_filter); + $results = $query->execute(); } catch (SearchApiException $e) { @@ -192,19 +213,13 @@ function materio_search_api_results_search(){//, $limit = 20, $page = 0 */ $ret['results']['#index'] = search_api_index_load('materiaux_breves'); $ret['results']['#results'] = $results; - /* - TODO choose view mode from user data - */ - - $ret['results']['#view_mode'] = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full'); + + $ret['results']['#view_mode'] = $viewmode; $ret['results']['#keys'] = $keys; // Load pager. // if ($results['result count'] > $page->options['per_page']) { - /* - TODO set per page limit as module settings - */ pager_default_initialize($results['result count'], $limit); $ret['results']['#pager'] = theme('pager'); // } diff --git a/materio_search_api_ajax.module b/materio_search_api_ajax.module index f2fa57bb..d75a2c13 100644 --- a/materio_search_api_ajax.module +++ b/materio_search_api_ajax.module @@ -12,12 +12,17 @@ * Implements hook_init(). */ function materio_search_api_ajax_init() { + if (module_exists('locale')) { + $languages = language_list('enabled'); + } + drupal_add_js(array('materio_search_api_ajax' => array( 'module_path' => drupal_get_path('module', 'materio_search_api_ajax'), // 'strings'=>array( // 'sitetitle'=>, // 'siteslogan'=>, // ), + 'languages' => isset($languages) ? $languages[1] : array(), )), 'setting'); drupal_add_js(drupal_get_path('module', 'materio_search_api_ajax').'/js/libraries/jquery.history.js'); @@ -30,16 +35,23 @@ function materio_search_api_ajax_init() { function materio_search_api_ajax_menu() { $items = array(); - $items['materio_search_api_ajax/search/%'] = array( + $base = array( + 'type' => MENU_CALLBACK, + 'file' => 'materio_search_api_ajax.pages.inc', + 'access arguments' => array('use materio search api'), + ); + + $items['materio_search_api_ajax/search/%'] = $base+array( 'title' => 'Matrio base ajax', 'page callback' => 'materio_search_api_ajax_search', 'page arguments' => array(2,3), 'access callback' => TRUE, - 'access arguments' => array('use materio search api'), - 'file' => 'materio_search_api_ajax.pages.inc', - 'type' => MENU_CALLBACK, ); + $items['materio_search_api_ajax/viewmode/change'] = $base+array( + 'page callback' => 'materio_search_api_ajax_viewmode_change', + 'page argument' => array(3), + ); return $items; } diff --git a/materio_search_api_ajax.pages.inc b/materio_search_api_ajax.pages.inc index 6f131f20..ecc03daa 100644 --- a/materio_search_api_ajax.pages.inc +++ b/materio_search_api_ajax.pages.inc @@ -2,10 +2,36 @@ function materio_search_api_ajax_search($keys, $page = 0){ - // TODO: set research path configurable $debug = false; + global $user; + // TODO: set research path configurable $search_path = "explore"; - + + # 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))); + + # execute search $_GET['page'] = $page; $path = $search_path . '/' . $keys ;//. ($page ? '?page='.$page : ''); @@ -46,6 +72,7 @@ function materio_search_api_ajax_search($keys, $page = 0){ 'keys'=>$keys, 'search_path'=>$search_path, 'return'=>$return, + 'active_types'=>$active_types, ); if ($debug) { @@ -57,4 +84,27 @@ function materio_search_api_ajax_search($keys, $page = 0){ } +} + + +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'); + } + + //return 'debug mode for materio_search_api_viewmode_change'; + drupal_json_output($rep); +} + } \ No newline at end of file