252
materio_search_api.pages.inc
Normal file
252
materio_search_api.pages.inc
Normal file
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* materiobase_search_autocomplete_dbselect()
|
||||
*
|
||||
* inspired by taxonomy_autocomplete()
|
||||
*
|
||||
* OBSOLETE : this fonction use a direct dbselect request to provide results forautocomplete
|
||||
*
|
||||
*/
|
||||
function materio_search_api_autocomplete_dbselect($typed = ''){
|
||||
// If the request has a '/' in the search text, then the menu system will have
|
||||
// split it into multiple arguments, recover the intended $tags_typed.
|
||||
$args = func_get_args();
|
||||
$typed = implode('/', $args);
|
||||
|
||||
/*
|
||||
TODO riche serach engine + \\ etc gmail like
|
||||
*/
|
||||
|
||||
if ($typed != '') {
|
||||
|
||||
// Part of the criteria for the query come from the field's own settings.
|
||||
$vids = array();
|
||||
$vocabularies = taxonomy_vocabulary_get_names();
|
||||
foreach ($vocabularies as $voc) {
|
||||
$vids[] = $voc->vid;
|
||||
}
|
||||
|
||||
$query = db_select('taxonomy_term_data', 't');
|
||||
$query->addTag('translatable');
|
||||
$query->addTag('term_access');
|
||||
|
||||
// Select rows that match by term name.
|
||||
$tags_return = $query
|
||||
->fields('t', array('tid', 'name'))
|
||||
->condition('t.vid', $vids)
|
||||
->condition('t.name', '%' . db_like($typed) . '%', 'LIKE')
|
||||
->range(0, 10)
|
||||
->execute()
|
||||
->fetchAllKeyed();
|
||||
|
||||
$term_matches = array();
|
||||
foreach ($tags_return as $tid => $name) {
|
||||
$n = $name;
|
||||
// Term names containing commas or quotes must be wrapped in quotes.
|
||||
// if (strpos($name, ',') !== FALSE || strpos($name, '"') !== FALSE) {
|
||||
// $n = '"' . str_replace('"', '""', $name) . '"';
|
||||
// }
|
||||
$term_matches[$n] = check_plain($name);
|
||||
}
|
||||
}
|
||||
|
||||
drupal_json_output($term_matches);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* materio_search_api_autocomplete_searchapi($typed = '')
|
||||
*
|
||||
* GOOD one using searchapi (SOLR)
|
||||
*/
|
||||
function materio_search_api_autocomplete_searchapi($typed = ''){
|
||||
// If the request has a '/' in the search text, then the menu system will have
|
||||
// split it into multiple arguments, recover the intended $tags_typed.
|
||||
$args = func_get_args();
|
||||
$typed = implode('/', $args);
|
||||
|
||||
// dsm($typed, 'typed');
|
||||
if ($typed != '') {
|
||||
|
||||
// search for patterns like key -another key +lastkey
|
||||
// and provide auto completion for the last key
|
||||
preg_match_all('/\s?[\+|-]?[^\s]+/', $typed, $adv_search_q);
|
||||
preg_match('/^(\+|-)?(.*)$/', trim(array_pop($adv_search_q[0])), $last);
|
||||
$tosearch = isset($last[2]) ? $last[2] : $typed;
|
||||
|
||||
// build the query
|
||||
$index_machine_name = variable_get('autocompletesearchindex', -1);
|
||||
$query = search_api_query($index_machine_name);
|
||||
$query_filter = $query->createFilter();
|
||||
$query_filter->condition('name', $tosearch);
|
||||
// $query_filter->condition('type', 'article');
|
||||
$query->filter($query_filter);
|
||||
$tags_return = $query->execute();
|
||||
// dsm($tags_return, '$tags_return');
|
||||
|
||||
if($tags_return['result count']){
|
||||
$term_matches = array();
|
||||
$index = search_api_index_load($index_machine_name);
|
||||
$delta = 0;
|
||||
foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
|
||||
// dsm($item, '$item');
|
||||
//$term_matches[$item->tid] = check_plain($item->name);
|
||||
// $term_matches[check_plain($item->name)] = check_plain($item->name);
|
||||
// TODO: leave tags with nodes
|
||||
$term_matches[ trim(implode(' ', $adv_search_q[0]).' '.$last[1].$item->name)] = check_plain($item->name);
|
||||
|
||||
$delta++;
|
||||
if($delta > 7)
|
||||
break;
|
||||
}
|
||||
drupal_json_output($term_matches);
|
||||
}else{
|
||||
drupal_json_output(array());
|
||||
}
|
||||
}else{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// dsm($term_matches, 'term_matches');
|
||||
// return 'debug mode of materio_search_api_autocomplete_searchapi';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* materio_search_api_results_search()
|
||||
*
|
||||
*
|
||||
*/
|
||||
function materio_search_api_results_search(){//, $limit = 20, $page = 0
|
||||
global $user;
|
||||
|
||||
//dsm("materio_search_api_results_search");
|
||||
$args = func_get_args();
|
||||
$typed = implode('/', $args);
|
||||
|
||||
preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
|
||||
//dsm($pages, '$pages');
|
||||
if($pages[0]){
|
||||
$typed = str_replace($pages[0][0], '', $typed);
|
||||
// $page = $pages[1][0];
|
||||
}
|
||||
// else{
|
||||
// $page = 0;
|
||||
// }
|
||||
|
||||
|
||||
preg_match_all('/\s?[^\s]+\s?/', $typed, $words);
|
||||
// dsm($words, "words");
|
||||
|
||||
// $escaper = array("+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", '"', "~", "*", "?", ":", '\\');
|
||||
foreach ($words[0] as $word) {
|
||||
|
||||
// $word = preg_replace('/\b-/', '\-', trim($word));
|
||||
// dsm($word);
|
||||
|
||||
$keys[] = $word;
|
||||
}
|
||||
|
||||
if ($keys) {
|
||||
try {
|
||||
$limit = 15;
|
||||
$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))
|
||||
->range($offset, $limit);
|
||||
|
||||
$results = $query->execute();
|
||||
}
|
||||
catch (SearchApiException $e) {
|
||||
$ret['message'] = t('An error occurred while executing the search. Please try again or contact the site administrator if the problem persists.');
|
||||
watchdog('materiobasemod', 'An error occurred while executing a search: !msg.', array('!msg' => $e->getMessage()), WATCHDOG_ERROR, l(t('search page'), $_GET['q']));
|
||||
}
|
||||
|
||||
// dsm($results, 'results');
|
||||
|
||||
# Load spellcheck.
|
||||
// if (isset($results['search_api_spellcheck'])) {
|
||||
// $ret['results']['#spellcheck'] = array(
|
||||
// '#theme' => 'search_api_spellcheck',
|
||||
// '#spellcheck' => $results['search_api_spellcheck'],
|
||||
// // Let the theme function know where the key is stored by passing its arg
|
||||
// // number. We can work this out from the number of args in the page path.
|
||||
// '#options' => array(
|
||||
// 'arg' => array(count(arg(NULL, $page->path))),
|
||||
// ),
|
||||
// '#prefix' => '<p class="search-api-spellcheck suggestion">',
|
||||
// '#suffix' => '</p>',
|
||||
// );
|
||||
|
||||
// }
|
||||
|
||||
|
||||
$ret['results']['#theme'] = 'materio_search_api_results';
|
||||
/*
|
||||
TODO choose index in module settings
|
||||
*/
|
||||
$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']['#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');
|
||||
// }
|
||||
|
||||
if (!empty($results['ignored'])) {
|
||||
drupal_set_message(
|
||||
t('The following search keys are too short or too common and were therefore ignored: "@list".',
|
||||
array( '@list' => implode(t('", "'), $results['ignored']) ) ),
|
||||
'warning'
|
||||
);
|
||||
}
|
||||
if (!empty($results['warnings'])) {
|
||||
foreach ($results['warnings'] as $warning) {
|
||||
drupal_set_message($warning, 'warning');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function materio_search_api_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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user