-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_appliance_suggest.module
209 lines (179 loc) · 6.16 KB
/
google_appliance_suggest.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @file
* Google Appliance Suggest module enables Query Suggestions feature of Google
* Search Appliance hardware device.
*/
/**
* Default module settings.
*/
define('SGAS_DEFAULT_MAX', 5);
define('SGAS_DEFAULT_CACHE', 0);
define('SGAS_DEFAULT_HIDE_THROBBER', 1);
define('SGAS_DEFAULT_AUTOSUBMIT', 1);
/**
* Implements hook_form_alter().
*
* Adds autocomplete functionality to both search forms provided by the Google
* Appliance module.
*/
function google_appliance_suggest_form_alter(&$form, &$form_state, $form_id) {
$gsa_forms = array(
'google_appliance_block_form',
'google_appliance_search_form',
);
// Apply autocomplete to both block and standard search forms.
if (in_array($form_id, $gsa_forms)) {
global $base_url;
$settings = _google_appliance_suggest_get_settings();
// Only alter the form if the module is enabled.
if ($settings['max']) {
// Set up the path for the autocomplete callback.
$module = drupal_get_path('module', 'google_appliance_suggest');
$file = 'google_appliance_suggest_callback.php';
$autocomplete_path = $base_url . '/' . $module . '/' . $file . '?q=';
// Attach the autocomplete callback.
if ($form_id == 'google_appliance_search_form') {
$form['basic']['search_keys']['#autocomplete_path'] = $autocomplete_path;
}
else {
$form['search_keys']['#autocomplete_path'] = $autocomplete_path;
}
// If desired, hide standard Drupal autocomplete styles.
if ($settings['hide_throbber']) {
$form['#attached']['css'][] = $module . '/google_appliance_suggest.css';
}
// If desired, automatically submit the form when a query is suggested.
if ($settings['autosubmit']) {
$form['#attached']['js'][] = $module . '/google_appliance_suggest.js';
// Add the auto_submit class that the JS hooks onto.
if ($form_id == 'google_appliance_search_form') {
$form['basic']['search_keys']['#attributes']['class'][] = 'auto_submit';
}
else {
$form['search_keys']['#attributes']['class'][] = 'auto_submit';
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds Google Search Appliance Query Suggestion settings to the main appliance
* admin form.
*/
function google_appliance_suggest_form_google_appliance_admin_settings_alter(&$form, &$form_state, $form_id) {
$settings = _google_appliance_suggest_get_settings();
// Create new fieldset for query suggestion setup.
$form['suggest_settings'] = array(
'#title' => t('Query Suggestion Settings'),
'#type' => 'fieldset',
'#collapsible' => 1,
'#collapsed' => 0,
);
// Field for max number of query suggestions returned.
$form['suggest_settings']['max'] = array(
'#title' => t('Maximum suggestions returned'),
'#type' => 'select',
'#options' => array(
0 => 0,
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
7 => 7,
8 => 8,
9 => 9,
10 => 10,
),
'#default_value' => $settings['max'],
'#description' => t('Maximum number of query suggestions to display to the user. Setting this to 0 disables query suggestions.'),
);
// Switch whether or not to hide the default Drupal throbber.
$form['suggest_settings']['hide_throbber'] = array(
'#title' => t('Hide throbber'),
'#type' => 'checkbox',
'#description' => t('Hides the throbber image Drupal provides by default on auto-complete form elements.'),
'#default_value' => $settings['hide_throbber'],
);
// Switch whether or not to autosubmit the form when a suggestion is selected.
$form['suggest_settings']['autosubmit'] = array(
'#title' => t('Auto-submit query suggestion'),
'#type' => 'checkbox',
'#description' => t('Automatically submits the search form when a user selects a query suggestion.'),
'#default_value' => $settings['autosubmit'],
);
// Switch whether or not to enable query suggest caching.
$form['suggest_settings']['cache'] = array(
'#title' => t('Enable suggestion caching'),
'#type' => 'checkbox',
'#description' => t('Speeds up query suggestions by caching suggestion results.'),
'#default_value' => $settings['cache'],
);
// Button to clear query suggestion cache.
$form['suggest_settings']['clear_cache'] = array(
'#type' => 'submit',
'#value' => t('Clear suggestion cache'),
'#submit' => array('google_appliance_suggest_clear_cache_submit'),
'#disabled' => (boolean) !$settings['cache'],
);
// Add another submit handler to save query suggestion variables.
array_unshift($form['#submit'], 'google_appliance_suggest_admin_settings_submit');
}
/**
* Submit handler for query suggestion settings.
*/
function google_appliance_suggest_admin_settings_submit($form, &$form_state) {
$field_keys = array(
'max',
'cache',
'hide_throbber',
'autosubmit',
);
// Save settings.
foreach ($field_keys as $field) {
variable_set('google_appliance_suggest_' . $field, $form_state['values'][$field]);
}
// Refresh settings getter.
$settings = _google_appliance_suggest_get_settings(TRUE);
}
/**
* Submit handler for suggestion cache clearing.
*/
function google_appliance_suggest_clear_cache_submit($form, &$form_state) {
$deleted = db_delete('cache_page')
->condition('cid', '%google_appliance_suggest%', 'LIKE')
->execute();
drupal_set_message(t('@num suggestions cleared from cache.', array('@num' => $deleted)));
}
/**
* Helper function to get Google Appliance Suggest settings.
*
* @param $refresh boolean
* If this is set to TRUE, this function will freshly return variable settings
* for query suggestions.
*
* @return
* A keyed array of module settings keyed by $field_keys.
*/
function _google_appliance_suggest_get_settings($refresh = FALSE) {
static $settings;
$field_keys = array(
'max',
'cache',
'hide_throbber',
'autosubmit',
);
if ($refresh || empty($settings)) {
foreach ($field_keys as $field) {
$settings[$field] = trim(variable_get(
'google_appliance_suggest_' . $field,
constant('SGAS_DEFAULT_' . strtoupper($field))
));
}
}
return $settings;
}