/[drupal]/contributions/modules/sphinx/sphinx_suggestion.module
ViewVC logotype

Contents of /contributions/modules/sphinx/sphinx_suggestion.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Thu Sep 25 13:13:19 2008 UTC (14 months ago) by johsw
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.1: +71 -69 lines
File MIME type: text/x-php
Inclusion of facet functionality
1 <?php
2 // $Id$
3
4 function sphinx_suggestion_menu() {
5 if (!$may_cache) {
6 $items[] = array(
7 'path' => 'admin/settings/sphinx/suggestions',
8 'title' => t('Suggestions'),
9 'description' => t('Configure the Sphinx suggestions'),
10 'callback' => 'drupal_get_form',
11 'callback arguments' => array('sphinx_suggestion_administration'),
12 'access' => user_access('administer sphinx suggestions'),
13 'type' => MENU_LOCAL_TASK,
14 );
15 }
16 return $items;
17 }
18
19 function sphinx_suggestion_perm() {
20 return array('administer sphinx suggestions');
21 }
22
23 function sphinx_suggestion_form_alter($form_id, &$form) {
24 if ($form_id == 'sphinx_search_form') {
25 drupal_add_css(drupal_get_path('module', 'sphinx_suggestion') .'/sphinx_suggestion.css');
26 $terms = $form['sphinx_search']['searchstring']['#default_value'];
27 $weight = $form['sphinx_search']['searchstring']['#weight'];
28 if (!empty($terms)) {
29 $suggestion = _sphinx_suggestion_do_suggestion($terms);
30 if ($terms != $suggestion) {
31 $form['sphinx_search']['searchstring_suggestion'] = array(
32 '#value' => '<div class="sphinx-suggestion">'. t('Did you mean !suggest', array('!suggest' => '<span class="sphinx-suggestion-term">'. _sphinx_suggestion_make_suggestion_linke($suggestion, 0) .'</span>')) .'</div>',
33 '#weight' => $weight + 1,
34 );
35 }
36 }
37 $iid = $form['sphinx_search']['iid']['#value'];
38 $sql = 'SELECT fid, field_name, display_name FROM {sphinx_fields} WHERE iid=%d AND active=1 ORDER BY weight';
39 $result = db_query($sql, $iid);
40 while ($fields = db_fetch_object($result)) {
41 $terms = $form['sphinx_search']['advanced']['field'][$fields->field_name]['#default_value'];
42 $weight = $form['sphinx_search']['advanced']['field'][$fields->field_name]['#weight'];
43 if (!empty($terms)) {
44 $suggestion = _sphinx_suggestion_do_suggestion($terms);
45 if ($terms != $suggestion) {
46 $form['sphinx_search']['advanced']['field'][$fields->field_name .'_suggestion'] = array(
47 '#value' => '<div class="sphinx-suggestion">'. t('Did you mean !suggest', array('!suggest' => '<span class="sphinx-suggestion-term">'. _sphinx_suggestion_make_suggestion_linke($suggestion, $fields->fid) .'</span>')) .'</div>',
48 '#weight' => $weight + 1,
49 );
50 }
51 }
52 }
53 }
54 }
55
56 function _sphinx_suggestion_do_suggestion($terms) {
57 $search_array = explode(' ', $terms);
58 $lang = variable_get('sphinx_suggestion_default_language', 'en');
59 $spel = variable_get('sphinx_suggestion_default_spelling', '');
60 $pspell_link = pspell_new($lang, $spel, "", "", PSPELL_BAD_SPELLERS);
61 $edit = false;
62 for ($n = 0; $n < count($search_array); $n++) {
63 if (!pspell_check($pspell_link, $search_array[$n])) {
64 $suggestions = pspell_suggest($pspell_link, $search_array[$n]);
65 if (!empty($suggestions[0])) {
66 $search_array[$n] = $suggestions[0];
67 $edit = true;
68 }
69 }
70 }
71 return implode(' ', $search_array);
72 }
73
74 function _sphinx_suggestion_make_suggestion_linke($suggestion, $fid) {
75 $url = parse_url($_SERVER['REQUEST_URI']);
76 //print_r($url['query']);
77 if ($fid == 0) {
78 $path_elements = explode('/', $url['path']);
79 $path_elements[count($path_elements) - 1] = $suggestion;
80 $path = substr(implode('/', $path_elements), 1);
81 return l($suggestion, $path, null, $url['query']);
82 }
83 else {
84 $query_elements = explode('&', $url['query']);
85 for ($n = 0; $n < count($query_elements); $n++) {
86 $parts = explode('=', $query_elements[$n]);
87 if ($parts[0] == 'as_f'. $fid) {
88 $query_elements[$n] = $parts[0] .'='. $suggestion;
89 }
90 }
91 $query = implode('&', $query_elements);
92 return l($suggestion, substr($url['path'], 1), null, $query);
93 }
94 }
95
96 function sphinx_suggestion_administration() {
97 // Access log settings:
98
99
100 $form['sphinx_suggestion']['language'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Language'),
103 '#default_value' => variable_get('sphinx_suggestion_default_language', 'en'),
104 '#description' => t('The language parameter is the language code which consists of the two letter ISO 639 language code (ie "en" or "da")'),
105 );
106 $form['sphinx_suggestion']['spelling'] = array(
107 '#type' => 'textfield',
108 '#title' => t('Spelling'),
109 '#default_value' => variable_get('sphinx_suggestion_default_spelling', ''),
110 '#description' => t("The spelling parameter is the requested spelling for languages with more than one spelling such as English. Known values are 'american', 'british', and 'canadian'."),
111 );
112 return system_settings_form($form);
113 }
114
115 function sphinx_suggestion_administration_validate($form_id, $form_values) {}
116
117 function sphinx_suggestion_administration_submit($form_id, $form_values) {
118 variable_set('sphinx_suggestion_default_language', $form_values['language']);
119 variable_set('sphinx_suggestion_default_spelling', $form_values['spelling']);
120 drupal_set_message(t('Language settings updated!'));
121 }
122

  ViewVC Help
Powered by ViewVC 1.1.2