400 lines
13 KiB
PHP
Executable File
400 lines
13 KiB
PHP
Executable File
<?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
|
|
global $language;
|
|
$index_machine_name = variable_get('autocompletesearchindex_'.$language->language, -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);
|
|
$query->keys($tosearch);
|
|
|
|
$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(){
|
|
global $user;
|
|
|
|
//dsm("materio_search_api_results_search");
|
|
$args = func_get_args();
|
|
$typed = implode('/', $args);
|
|
// dsm($typed, 'typed');
|
|
|
|
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");
|
|
|
|
|
|
// $match = array('\\', '+', '-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '~', '*', '?', ':', '"', ';', ' ');
|
|
// $replace = array('\\\\', '\\+', '\\-', '\\&', '\\|', '\\!', '\\(', '\\)', '\\{', '\\}', '\\[', '\\]', '\\^', '\\~', '\\*', '\\?', '\\:', '\\"', '\\;', '\\ ');
|
|
foreach ($words[0] as $word) {
|
|
// dsm($word);
|
|
$word = trim($word);
|
|
// $word = str_replace($match, $replace, $word);
|
|
// dsm($word, 'word');
|
|
$keys[] = $word;
|
|
}
|
|
// dsm($keys, 'keys');
|
|
|
|
global $language;
|
|
|
|
|
|
// if(user_access('use materio search api')){
|
|
|
|
// }else if(user_access('use materio search api for breves')){
|
|
|
|
// }
|
|
|
|
if(user_access('use materio search api')){
|
|
// switch index depending on key words type full text or taxonomy term (autocomplete selection)
|
|
$searchmode = isset($user->data['materiosearchapi_searchmode']) ? $user->data['materiosearchapi_searchmode'] : "fulltext";
|
|
switch($searchmode){
|
|
case "fulltext":
|
|
$index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
|
|
break;
|
|
case "taxonomy":
|
|
$index_machine_name = variable_get('taxonomysearchindex_'.$language->language, -1);
|
|
break;
|
|
}
|
|
$index = search_api_index_load($index_machine_name);
|
|
}else if(user_access('use materio search api for breves')){
|
|
$index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
|
|
// dsm($index_machine_name, '$index_machine_name');
|
|
$index = search_api_index_load($index_machine_name);
|
|
|
|
$could_index_machine_name = variable_get('mainsearchindex_'.$language->language, -1);
|
|
$could_index = search_api_index_load($index_machine_name);
|
|
}
|
|
|
|
// $index_machine_name = variable_get('brevessearchindex_'.$language->language, -1);
|
|
// dsm($index_machine_name, '$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');
|
|
// dsm($viewmode, 'viewmode');
|
|
|
|
if ($keys) {
|
|
// TODO: cache the results with cache graceful : http://drupal.org/project/cache_graceful
|
|
try {
|
|
$limit = variable_get($viewmode.'_limite', '10');
|
|
$offset = pager_find_page() * $limit; //$page*$limit;//
|
|
|
|
$query = search_api_query($index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
|
|
->keys(implode(' ', $keys))
|
|
->range($offset, $limit);
|
|
|
|
$filter = $query->createFilter('OR');
|
|
foreach ($bundles_filter as $type) {
|
|
$filter->condition('type', $type, '=');
|
|
}
|
|
// dsm($filter, 'filter');
|
|
$query->filter($filter);
|
|
|
|
// $query->setOption('search_api_bypass_access', true);
|
|
$query->setOption('search_api_access_account', $user);
|
|
|
|
$results = $query->execute();
|
|
|
|
# in case of utilisateur search, run a real search to indicate how much items you could find
|
|
if(isset($could_index)){
|
|
$could_query = search_api_query($could_index_machine_name, array('conjunction'=>'OR', 'parse mode'=>'direct'))
|
|
->keys(implode(' ', $keys))
|
|
->range($offset, $limit);
|
|
// ->filter($filter);
|
|
|
|
$could_results = $could_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');
|
|
|
|
// $accessible_results = array();
|
|
// foreach ($results['results'] as $nid => $entity) {
|
|
// if(node_access('view', node_load($nid)))
|
|
// $accessible_results[$nid] = $entity;
|
|
// }
|
|
// $results['results'] = $accessible_results;
|
|
// $results['result count'] = count($accessible_results);
|
|
|
|
|
|
|
|
if(user_access('use materio search api')){
|
|
$items = $index->loadItems(array_keys($results['results']));
|
|
// dsm($items, 'items');
|
|
// $count = $results['result count'];
|
|
}else if(user_access('use materio search api for breves')){
|
|
$items = array();
|
|
$breves = $index->loadItems(array_keys($results['results']));
|
|
foreach ($breves as $nid => $breve) {
|
|
|
|
if(!node_access('view', $breve))
|
|
continue;
|
|
|
|
$items[] = $breve;
|
|
$materiaux = field_get_items('node',$breve,'field_materiau_ref');
|
|
// dsm($materiaux, 'materiaux');
|
|
if($materiaux){
|
|
foreach ($materiaux as $value) {
|
|
$materiau = node_load($value['target_id']);
|
|
if(node_access('view', $materiau))
|
|
$items[] = $materiau;
|
|
}
|
|
}
|
|
}
|
|
|
|
$results['breves count'] = $results['result count'];
|
|
$results['result count'] = count($items);
|
|
$results['could results'] = $could_results;
|
|
}
|
|
|
|
|
|
$ret['results']['#theme'] = 'materio_search_api_results';
|
|
$ret['results']['#index'] = $index;//search_api_index_load($index_machine_name);
|
|
$ret['results']['#results'] = $results;
|
|
$ret['results']['#items'] = $items;
|
|
$ret['results']['#view_mode'] = $viewmode;
|
|
$ret['results']['#keys'] = $keys;
|
|
|
|
drupal_set_title('<i class="icon-materio-search"></i>'.check_plain($typed), PASS_THROUGH);
|
|
|
|
if(isset($results)){
|
|
// Load pager.
|
|
// if ($results['result count'] > $page->options['per_page']) {
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* materio_search_api_actuality()
|
|
*
|
|
*/
|
|
function materio_search_api_actuality(){
|
|
global $user;
|
|
|
|
if(isset($user->roles[1])){
|
|
$date_limit = strtotime('-6 month');
|
|
// dsm(date('d m y', $date_limit));
|
|
}
|
|
|
|
$viewmode = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
|
|
$limit = 10;//variable_get($viewmode.'_limite', '10');
|
|
$offset = pager_find_page() * $limit;
|
|
// dsm($offset);
|
|
|
|
$query = new EntityFieldQuery;
|
|
$query
|
|
->entityCondition('entity_type', 'node')
|
|
->propertyCondition('status', 1)
|
|
->entityCondition('bundle', array('breve'))
|
|
->propertyOrderBy('created', 'DESC')
|
|
->range($offset,$limit);
|
|
|
|
if(isset($user->roles[1])){
|
|
$query->propertyCondition('created', $date_limit, '>');
|
|
}
|
|
|
|
$result = $query->execute();
|
|
// dsm($result, '$result');
|
|
|
|
$count_query = new EntityFieldQuery;
|
|
$count_query
|
|
->entityCondition('entity_type', 'node')
|
|
->propertyCondition('status', 1)
|
|
->entityCondition('bundle', array('breve'));
|
|
// dsm($count, 'count');
|
|
if(isset($user->roles[1])){
|
|
$count_query->propertyCondition('created', $date_limit, '>');
|
|
}
|
|
$count = $count_query->count()->execute();
|
|
|
|
pager_default_initialize($count, $limit);
|
|
|
|
$items = array();
|
|
if(isset($result['node'])){
|
|
foreach ($result['node'] as $nid => $n) {
|
|
$breve = node_load($nid);
|
|
|
|
if(!node_access('view', $breve))
|
|
continue;
|
|
|
|
$items[] = $breve;
|
|
$materiaux = field_get_items('node',$breve,'field_materiau_ref');
|
|
// dsm($materiaux, 'materiaux');
|
|
if($materiaux){
|
|
foreach ($materiaux as $value) {
|
|
$materiau = node_load($value['target_id']);
|
|
if(node_access('view', $materiau))
|
|
$items[] = $materiau;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// drupal_set_title(t('Actualities'));
|
|
drupal_set_title(t(''));
|
|
|
|
return theme('materio_search_api_actuality', array(
|
|
'items' => $items,
|
|
'view_mode' => $viewmode,
|
|
'count' => $count,
|
|
'pager' => theme('pager'),
|
|
));
|
|
}
|
|
|
|
|
|
function materio_search_api_viewmode_change($vm){
|
|
//dsm($vm, 'materio_search_api_viewmode_change');
|
|
$rep = _materio_search_api_change_viewmode($vm);
|
|
|
|
//return 'debug mode for materio_search_api_viewmode_change';
|
|
// drupal_json_output($rep);
|
|
|
|
drupal_goto();
|
|
}
|