Newer
Older
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/**
* @file
* Search Autocomplete
* Helper functions to retrive suggestions from database
*
* @authors
* Miroslav Talenberg (Dominique CLAUSE) <http://www.axiomcafe.fr/contact>
*
* Sponsored by:
* www.axiomcafe.fr
*/
/**
* MENU CALLBACK:
* Define the form to configure the suggestions.
* @return A rendered form
*/
function search_autocomplete_form_configure($form, &$form_state) {
$base = "admin/config/search/search_autocomplete"; // base URL for this module configurations
// get data from database
$fid = arg(4);
$result = db_select('search_autocomplete_forms', 'f')
->fields('f')
->condition('fid', $fid)
->execute()
->fetchAllAssoc('fid');
foreach ($result as $item) {
$form['fid'] = array(
'#type' => 'hidden',
'#value' => $fid,
);
// ------------------------------------------------------------------
// HOW - How to display Search Autocomplete suggestions
$form['search_autocomplete_how'] = array(
'#type' => 'fieldset',
'#title' => t('HOW - How to display Search Autocomplete suggestions?'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
// Number of characters before suggestions
$trigger = array();
for ($i=1; $i<20; $i++) {
$trigger[$i] = $i . ' ' . t('characters');
}
$form['search_autocomplete_how']['min_char'] = array(
'#type' => 'select',
'#title' => t('Minimum keyword size that uncouple autocomplete search'),
'#default_value' => $item->min_char,
'#options' => $trigger,
'#multiple' => FALSE,
'#required' => TRUE,
);
// Number of suggestions to display
$limit = array();
for ($i=1;$i<50;$i++) {
$limit[$i] = $i . ' ' . t('results');
}
$form['search_autocomplete_how']['max_sug'] = array(
'#type' => 'select',
'#title' => t('Limit of the autocomplete search result'),
'#default_value' => $item->max_sug,
'#options' => $limit,
'#multiple' => FALSE,
'#required' => TRUE
);
// ------------------------------------------------------------------
// WHAT - What to display in Search Autocomplete suggestions
$form['search_autocomplete_what'] = array(
'#type' => 'fieldset',
'#title' => t('WHAT - What to display in Search Autocomplete suggestions?'),
'#description' => t('Choose which data should be added to autocompletion suggestions.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#theme' => 'search_autocomplete_form_configuration_fieldset'
);
$form['search_autocomplete_what']['#tree'] = TRUE;
// Built possible suggestions
$result = db_select('search_autocomplete_suggestions', 's')
->fields('s')
->condition('sug_fid', $fid)
->orderBy('sug_weight')
->execute()
->fetchAllAssoc('sid');
foreach ($result as $suggestion) {
// if the module has no dependencies, or if the dependencies are enabled: activate the suggestion
if (drupal_strlen($suggestion->sug_dependencies) == 0 || module_exists($suggestion->sug_dependencies)) {
$activate = TRUE;
$title = $suggestion->sug_title;
} // else, do not activate the suggestion and has the dependencie reason
else {
$activate = FALSE;
$title = $suggestion->sug_title . ' ' . t('(require @module module)', array('@module' => $suggestion->sug_dependencies));
}
$sid = $suggestion->sid;
$form['search_autocomplete_what'][$sid]['sid'] = array(
'#type' => 'hidden',
'#value' => $sid,
'#disabled' => !$activate
);
$form['search_autocomplete_what'][$sid]['sug_title'] = array(
'#type' => 'item',
'#title' => $title,
'#disabled' => !$activate
);
$form['search_autocomplete_what'][$sid]['sug_enabled'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#default_value' => $suggestion->sug_enabled,
'#disabled' => !$activate
);
$form['search_autocomplete_what'][$sid]['sug_prefix'] = array( // -> sug_prefix
'#type' => 'textfield',
'#default_value' => $suggestion->sug_prefix,
'#maxlength' => 255,
'#size' => 35,
'#disabled' => !$activate
);
$form['search_autocomplete_what'][$sid]['sug_weight'] = array( // -> weight of the item in hierarchy
'#type' => 'weight',
'#default_value' => $suggestion->sug_weight,
'#disabled' => !$activate
);
$form['search_autocomplete_what'][$sid]['sug_edit'] = array( // -> weight of the item in hierarchy
'#type' => 'item',
'#title' => l(t('edit'), "$base/suggestion/" . $suggestion->sid . "/edit")
);
}
// ------------------------------------------------------------------
// ADVANCED - Advanced options
$form['search_autocomplete_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('ADVANCED - Advanced options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE
);
$form['search_autocomplete_advanced']['selector'] = array(
'#type' => 'textfield',
'#title' => t('ID selector for this form'),
'#description' => t('Please change only if you know what you do, read <a href="http://projects.axiomcafe.fr/search-autocomplete">documentation</a> first.'),
'#default_value' => $item->selector,
'#maxlength' => 255,
'#size' => 35
);
// Add button validation
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration')
);
}
return $form;
}
// -------------------------------------------------------------------------------------
/**
* Implementation of hook_submit().
* Save the changes in the database
*/
function search_autocomplete_form_configure_submit($form, &$form_state) {
$ok_query = TRUE; // so far so good!
//Update the database with the new values
$what = '';
$sids = '';
$weights = '';
// ###
// UPDATE THE FORM
// -> update form
$values = $form_state['values'];
db_update('search_autocomplete_forms')
->fields(array(
'min_char' => $values['min_char'],
'max_sug' => $values['max_sug'],
'selector' => $values['selector']
))
->condition('fid', $values['fid'])
->execute();
// -> update each suggestions
foreach ($form_state['input']['search_autocomplete_what'] as $key => $item) {
drupal_write_record('search_autocomplete_suggestions', $values['search_autocomplete_what'][$key], 'sid');
}
// ###
// UPDATE CHILD LIST BUT NOT THE ADVANCED OPTIONS
$fids = _search_autocomplete_get_all_children($values['fid']);
// update the settings for this form + every children form
foreach ($fids as $fid) {
// -> update form
db_update('search_autocomplete_forms')
->fields(array(
'min_char' => $values['min_char'],
'max_sug' => $values['max_sug']
))
->condition('fid', $fid)
->execute();
// -> update each suggestions
foreach ($form_state['input']['search_autocomplete_what'] as $key => $item) {
drupal_write_record('search_autocomplete_suggestions', $values['search_autocomplete_what'][$key], 'sid');
}
}
// ###
$form_state['redirect'] = 'admin/config/search/search_autocomplete';
$ok_query ? drupal_set_message(t("Configuration success !")) : drupal_set_message(t("An error has occured while saving the settings. Please, double check your settings!"), 'error');
}
// -------------------------------------------------------------------------------------
/**
* CALLBACK:
* Theme function for this treelist form
*/
function theme_search_autocomplete_form_configuration_fieldset($variables) {
$form = $variables['form'];
$header = array('Title', 'Enabled', 'Prefix of the item in suggestions', 'Weight', 'Operation');
$rows = array();
// for each elements to anchor in the form
foreach (element_children($form) as $key) {
$element = &$form[$key];
$element['sug_weight']['#attributes']['class'] = array('weight-group');
$rows[] = array(
'data' => array(
drupal_render($element['sug_title']),
drupal_render($element['sug_enabled']),
drupal_render($element['sug_prefix']),
drupal_render($element['sug_weight']),
drupal_render($element['sug_edit'])
),
'class' => array('draggable'),
);
}
// Themize the table and render the form
$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'draggable-table')));
$output .= drupal_render($form['submit']);
$output .= drupal_render_children($form);
drupal_add_tabledrag('draggable-table', 'order', 'sibling', 'weight-group');
return $output;
} // function theme_search_autocomplete_form_configuration()
/////////////////////////////////////////////////////////////////////////////////////////
//// HELPER FUNCTIONS ////
// -------------------------------------------------------------------------------------
/**
* Helper function: get the array of fids every of his children of the caller but not
* caller fid.
*/
function _search_autocomplete_get_all_children($fid, &$items = array(), $depth = 0) {
if ($depth)
$items[] = $fid;
//$result = db_query('SELECT * FROM {search_autocomplete_forms} WHERE parent_fid=:parent_fid', array(':parent_fid' => $fid));
$result = db_select('search_autocomplete_forms', 'f')
->fields('f')
->condition('parent_fid', $fid)
->execute()
->fetchAllAssoc('fid');
foreach ($result as $item) {
$depth++;
_search_autocomplete_get_all_children($item->fid, $items, $depth);
}
return $items;
}