change view mode ++ type filters

Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
bachy 2012-10-30 18:03:56 +01:00
parent 3faebabce1
commit 65ea77dea2
7 changed files with 243 additions and 76 deletions

View File

@ -2,8 +2,8 @@
(function($) { (function($) {
MaterioBaseMod = function(){ MaterioSearchApiAjax = function(){
var _History = window.History, var _History = window.History,
_isloadingresults = false; _isloadingresults = false;
_$content = $('#content'); _$content = $('#content');
@ -39,13 +39,8 @@ MaterioBaseMod = function(){
// /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686 // /!\ AUTOCOMPLETE SELECT EVENT need a patch http://drupal.org/node/365241#comment-5374686
$("#edit-searchfield").bind('autocompleteSelect', function(event) { $("#edit-searchfield").bind('autocompleteSelect', function(event) {
setTimeout(function(){ $(this).parents('.form').trigger('submit');
loadResults(getSearchKeys());
},10);
// loadResults($(this).val());
}); });
_$content _$content
.bind('jsp-initialised', function(event, isScrollable){ .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){ function loadResults(keys){
// trace('keys', keys); // trace('keys', keys);
if(keys !== undefined){ if(keys !== undefined){
keys = keys.replace('/', ' '); 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){ if(!_isloadingresults){
_isloadingresults = true; _isloadingresults = true;
$('#materio-search-api-search-form').addClass('loading'); $('#materio-search-api-search-form').addClass('loading');
// TODO: record ajax path in a variable from materio_search_api_ajax_init // TODO: record ajax path in a variable from materio_search_api_ajax_init
$.getJSON('/materio_search_api_ajax/search/'+keys, function(json){ $.getJSON(Drupal.settings.basePath+Drupal.settings.pathPrefix+'materio_search_api_ajax/search/'+keys,
// trace('json', json); {'types':types},
$(window).trigger('resultsloaded'); function(json){
trace('json', json);
$.event.trigger('resultsloaded');
_isloadingresults = false; _isloadingresults = false;
$('#materio-search-api-search-form').removeClass('loading'); $('#materio-search-api-search-form').removeClass('loading');
updateContent(json); updateContent(json);
@ -95,37 +108,28 @@ MaterioBaseMod = function(){
function updateContent(json){ function updateContent(json){
if(json.return){ if(json.return){
// TODO: set jscrollpane to top
$('.inner-content',_$content).html(json.return); $('.inner-content',_$content).html(json.return);
// TODO: this a DIRTY sheet of what could be refresh results transitions var path = Drupal.settings.basePath + Drupal.settings.pathPrefix + json.search_path + '/' + json.keys;
// var $newContent = $(json.return);
// trace('newContent', $newContent); _History.pushState({content:json.return}, json.keys, path);
// $('.node-materiau',$newContent).hide(); $('input[name=location]','#feedback-form').attr('value', path);
// $('.node-materiau','#content').fadeOut(300);
// setTimeout(function(){
// $('#content').html($newContent);
// $('.node-materiau','#content').fadeIn(500);
// },300);
_History.pushState({content:json.return}, json.keys, '/'+json.search_path + '/' +json.keys); for (language in Drupal.settings.materio_search_api_ajax.languages) {
// navigate({content:json.return}, '', '/'+json.page.path + '/' +json.keys); 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); event.container = $('.search-results', _$content);
$(window).trigger(event); $.event.trigger(event);
}else{ }else{
trace('no results'); trace('no results');
} }
}; };
function getSearchKeys(){
return $('#materio-search-api-search-form').find('input[name*="searchfield"]').val();
};
/** /**
* infinit scroll * infinit scroll
@ -200,8 +204,8 @@ MaterioBaseMod = function(){
}; };
function changeViewMode(vm, $btn){ function changeViewMode(vm, $btn){
$.getJSON('/materiosearchapi/viewmode/change/'+vm, function(json){ $.getJSON(Drupal.settings.basePath+'materio_search_api_ajax/viewmode/change/'+vm, function(json){
// trace('viewmode json', json); trace('viewmode json', json);
if (json.statut == "saved"){ if (json.statut == "saved"){
loadResults(getSearchKeys()); loadResults(getSearchKeys());
$('.viewmode-link').removeClass('active'); $('.viewmode-link').removeClass('active');
@ -217,6 +221,6 @@ MaterioBaseMod = function(){
}; };
var materiobasemod = new MaterioBaseMod(); var materiosearchapiajax = new MaterioSearchApiAjax();
})(jQuery); })(jQuery);

View File

@ -33,7 +33,7 @@ function materio_search_api_settings(){
$form['availableviewmodes'] = array( $form['availableviewmodes'] = array(
'#type'=>'select', '#type'=>'select',
'#options'=>$viewmode_options, '#options'=>$viewmode_options,
'#default_value' => variable_get('availableviewmodes', -1), '#default_value' => variable_get('availableviewmodes', array()),
'#title' => t('Availble View modes'), '#title' => t('Availble View modes'),
'#multiple' => true, '#multiple' => true,
); );
@ -45,5 +45,16 @@ function materio_search_api_settings(){
'#title' => t('Defalut View mode'), '#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); return system_settings_form($form);
} }

View File

@ -1,4 +1,4 @@
name = materio_search_api name = Materio SearchApi
description = "Materio Search Api module" description = "Materio Search Api module"
; Core version (required) ; Core version (required)

View File

@ -30,6 +30,10 @@ function materio_search_api_permission() {
'title' => t('administer Materio search api'), 'title' => t('administer Materio search api'),
'description' => 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'), 'info' => t('Materio search api view mode selection'),
'cache' => DRUPAL_NO_CACHE 'cache' => DRUPAL_NO_CACHE
); );
// $blocks['materio_search_api_filters'] = array(
// 'info' => t('Materio search api filters'),
// 'cache' => DRUPAL_NO_CACHE
// );
return $blocks; return $blocks;
} }
@ -117,6 +126,12 @@ function materio_search_api_block_view($delta = '') {
$block['content'] = theme('materio_search_api_select_viewmode_block', array()); $block['content'] = theme('materio_search_api_select_viewmode_block', array());
} }
break; 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; 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() * materiobase_search_form()
*/ */
function materio_search_api_search_form($form, &$form_state){ function materio_search_api_search_form($form, &$form_state){
// dsm($form_state, 'form_state');
// dsm($form, 'form');
global $user;
$form = array(); $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(); $args = arg();
$path = array_shift($args); $path = array_shift($args);
$keys = implode('/', $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){ function materio_search_api_search_form_submit($form, &$form_state){
$keys = $form_state['values']['searchfield']; // dsm($form_state, 'form_state');
// drupal_go_to('base/explore/'.$keys); global $user;
$form_state['redirect'] = 'explore/'.$keys; 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', 'template' => 'materio-search-api-select-viewmode-block',
'path' => drupal_get_path('module', 'materio_search_api').'/templates', '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( 'materio_search_api_results' => array(
'arguments' => array(), 'arguments' => array(),
'template' => 'materio-search-api-results', 'template' => 'materio-search-api-results',
@ -293,6 +338,36 @@ function template_preprocess_materio_search_api_select_viewmode_block(&$vars){
$vars['content'] = $content; $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 * Function for preprocessing the variables for the search_api_page_results
* template. * template.

View File

@ -142,24 +142,45 @@ function materio_search_api_results_search(){//, $limit = 20, $page = 0
// $escaper = array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", '"', "~", "*", "?", ":", '\\'); // $escaper = array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", '"', "~", "*", "?", ":", '\\');
foreach ($words[0] as $word) { foreach ($words[0] as $word) {
// $word = preg_replace('/\b-/', '\-', trim($word)); // $word = preg_replace('/\b-/', '\-', trim($word));
// dsm($word); // dsm($word);
$keys[] = $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) { if ($keys) {
try { try {
// TODO: change limit function of viewmode small card -> many items, big cards -> few items $limit = variable_get($viewmode.'_limite', '10');
$limit = 15;
$offset = pager_find_page() * $limit; //$page*$limit;// $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')) $query = search_api_query($index_machine_name, array('parse mode'=>'direct'))
->keys(implode(' ', $keys)) ->keys(implode(' ', $keys))
// TODO: add bundle filter
// ->condition('type', 'breve')
->range($offset, $limit); ->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(); $results = $query->execute();
} }
catch (SearchApiException $e) { 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']['#index'] = search_api_index_load('materiaux_breves');
$ret['results']['#results'] = $results; $ret['results']['#results'] = $results;
/*
TODO choose view mode from user data $ret['results']['#view_mode'] = $viewmode;
*/
$ret['results']['#view_mode'] = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
$ret['results']['#keys'] = $keys; $ret['results']['#keys'] = $keys;
// Load pager. // Load pager.
// if ($results['result count'] > $page->options['per_page']) { // if ($results['result count'] > $page->options['per_page']) {
/*
TODO set per page limit as module settings
*/
pager_default_initialize($results['result count'], $limit); pager_default_initialize($results['result count'], $limit);
$ret['results']['#pager'] = theme('pager'); $ret['results']['#pager'] = theme('pager');
// } // }

View File

@ -12,12 +12,17 @@
* Implements hook_init(). * Implements hook_init().
*/ */
function materio_search_api_ajax_init() { function materio_search_api_ajax_init() {
if (module_exists('locale')) {
$languages = language_list('enabled');
}
drupal_add_js(array('materio_search_api_ajax' => array( drupal_add_js(array('materio_search_api_ajax' => array(
'module_path' => drupal_get_path('module', 'materio_search_api_ajax'), 'module_path' => drupal_get_path('module', 'materio_search_api_ajax'),
// 'strings'=>array( // 'strings'=>array(
// 'sitetitle'=>, // 'sitetitle'=>,
// 'siteslogan'=>, // 'siteslogan'=>,
// ), // ),
'languages' => isset($languages) ? $languages[1] : array(),
)), 'setting'); )), 'setting');
drupal_add_js(drupal_get_path('module', 'materio_search_api_ajax').'/js/libraries/jquery.history.js'); 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() { function materio_search_api_ajax_menu() {
$items = array(); $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', 'title' => 'Matrio base ajax',
'page callback' => 'materio_search_api_ajax_search', 'page callback' => 'materio_search_api_ajax_search',
'page arguments' => array(2,3), 'page arguments' => array(2,3),
'access callback' => TRUE, '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; return $items;
} }

View File

@ -2,10 +2,36 @@
function materio_search_api_ajax_search($keys, $page = 0){ function materio_search_api_ajax_search($keys, $page = 0){
// TODO: set research path configurable
$debug = false; $debug = false;
global $user;
// TODO: set research path configurable
$search_path = "explore"; $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; $_GET['page'] = $page;
$path = $search_path . '/' . $keys ;//. ($page ? '?page='.$page : ''); $path = $search_path . '/' . $keys ;//. ($page ? '?page='.$page : '');
@ -46,6 +72,7 @@ function materio_search_api_ajax_search($keys, $page = 0){
'keys'=>$keys, 'keys'=>$keys,
'search_path'=>$search_path, 'search_path'=>$search_path,
'return'=>$return, 'return'=>$return,
'active_types'=>$active_types,
); );
if ($debug) { 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);
}
} }