materio-base-legacy/materiobasemod.pages.inc
bachy fb30fe966e materiobaseajax module
first ajax geed results
+ 2 different custom autocomplete func (not used for now)
	- deselect
	- searchapi

Signed-off-by: bachy <git@g-u-i.net>
2012-07-23 23:02:09 +02:00

100 lines
2.7 KiB
PHP

<?php
/**
* materiobase_search_autocomplete()
*
* inspired by taxonomy_autocomplete()
*
*/
function materiobase_search_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);
}
function materiobase_search_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);
/*
TODO riche serach engine + \\ etc gmail like
*/
if ($typed != '') {
$query = search_api_query('referencement');
$query_filter = $query->createFilter();
$query_filter->condition('name', $typed);
// $query_filter->condition('type', 'article');
$query->filter($query_filter);
$tags_return = $query->execute();
dsm($tags_return, '$tags_return');
$term_matches = array();
$index = search_api_index_load('referencement');
foreach ($index->loadItems(array_keys($tags_return['results'])) as $item) {
dsm($item, '$item');
}
//
// 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);
}