123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- <?php
- function materio_search_api_autocomplete_dbselect($typed = ''){
-
-
- $args = func_get_args();
- $typed = implode('/', $args);
-
-
-
- if ($typed != '') {
-
- $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');
-
- $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_matches[$n] = check_plain($name);
- }
- }
- drupal_json_output($term_matches);
-
- }
- function materio_search_api_autocomplete_searchapi($typed = ''){
-
-
- $args = func_get_args();
- $typed = implode('/', $args);
-
-
- if ($typed != '') {
-
-
- 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;
-
- $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($query_filter);
- $tags_return = $query->execute();
-
- 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) {
-
-
-
-
- $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;
- }
-
-
-
-
- }
- function materio_search_api_results_search(){
- global $user;
-
- $args = func_get_args();
- $typed = implode('/', $args);
-
- preg_match_all('/\?page=([0-9]+)/', $typed, $pages);
-
- if($pages[0]){
- $typed = str_replace($pages[0][0], '', $typed);
-
- }
-
-
-
-
- preg_match_all('/\s?[^\s]+\s?/', $typed, $words);
-
-
- foreach ($words[0] as $word) {
-
-
- $keys[] = $word;
- }
- if ($keys) {
- try {
-
- $limit = 15;
- $offset = pager_find_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']));
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $ret['results']['#theme'] = 'materio_search_api_results';
-
-
- $ret['results']['#index'] = search_api_index_load('materiaux_breves');
- $ret['results']['#results'] = $results;
-
-
- $ret['results']['#view_mode'] = isset($user->data['materiosearchapi_viewmode']) ? $user->data['materiosearchapi_viewmode'] : variable_get('defaultviewmode', 'full');
- $ret['results']['#keys'] = $keys;
-
-
-
- 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){
-
- global $user;
-
- $entity_infos = entity_get_info();
-
- 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');
- }
-
- drupal_json_output($rep);
- }
|