/
autocomplete_widgets.common.inc
executable file
·424 lines (378 loc) · 14.9 KB
/
autocomplete_widgets.common.inc
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
<?php
/**
* @file
* Common functions for Autocomplete Widgets module.
*/
/**
* Fetch an array of options for the given widget.
*
* @param $instance
* A structured array describing the field instance.
* @param $string
* Optional string to filter values on (used by autocomplete).
* @param $match
* Operator to match filtered name against. Can be any of:
* 'contains', 'equals', 'starts_with'
* @param $keys
* Optional keys to lookup (the $string and $match arguments will be
* ignored).
* @param $limit
* If non-zero, limit the size of the result set.
*
* @return
* An array of valid values in the form:
* array(
* key => value,
* ...
* )
*/
function _autocomplete_widgets_get_options($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
static $results = array();
// Create unique id for static cache.
if (!isset($keys) || !is_array($keys)) {
$keys = array();
}
$cid = $instance['field_name'] .':'. $match .':'. ($string !== '' ? $string : implode('-', $keys)) . ':' . $limit;
if (!isset($results[$cid])) {
switch ($instance['widget']['type']) {
case 'autocomplete_widgets_allowvals':
$results[$cid] = _autocomplete_widgets_get_options_allowvals($instance, $string, $match, $keys, $limit);
break;
case 'autocomplete_widgets_flddata':
$results[$cid] = _autocomplete_widgets_get_options_flddata($instance, $string, $match, $keys, $limit);
break;
case 'autocomplete_widgets_suggested':
$results[$cid] = _autocomplete_widgets_get_options_suggested($instance, $string, $match, $keys, $limit);
break;
case 'autocomplete_widgets_node_reference':
$results[$cid] = _autocomplete_widgets_get_options_node_reference($instance, $string, $match, $keys, $limit);
break;
case 'autocomplete_widgets_node_title':
$results[$cid] = _autocomplete_widgets_get_options_node_title($instance, $string, $match, $keys, $limit);
break;
default:
$results[$cid] = array();
}
}
return $results[$cid];
}
/**
* Fetch an array of options for the given widget (allowed values).
*
* Options are retrieved from the allowed values defined for the field.
*/
function _autocomplete_widgets_get_options_allowvals($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
$field_name = $instance['field_name'];
$allowed_values = list_allowed_values(field_info_field($field_name));
$limit = (!isset($limit) || !is_numeric($limit)) ? count($allowed_values) : $limit;
$case_sensitive = $instance['widget']['settings']['autocomplete_case'];
$filter_xss = !empty($instance['widget']['settings']['autocomplete_xss']);
$options = array();
$count = 0; //@todo: cant the count var be replaced with a call to count()?
_autocomplete_widgets_sort_options($allowed_values, $instance);
foreach ($allowed_values as $key => $value) {
if ($filter_xss) {
// Filter all HTML in $value, then trim white spaces.
$value = trim(filter_xss($value, array()));
}
if ($string === '') {
if (isset($keys) && is_array($keys)) {
if (in_array($key, $keys)) {
$options[$key] = $value;
$count++;
}
}
else {
$options[$key] = $value;
$count++;
}
}
else if ($match == 'equals') {
if ($value == $string) {
$options[$key] = $value;
$count++;
}
}
else {
$pos = $case_sensitive ? strpos($value, $string) : strpos(backdrop_strtolower($value), backdrop_strtolower($string));
if (($match == 'starts_with' && $pos === 0) || ($match == 'contains' && $pos !== FALSE)) {
$options[$key] = $value;
$count++;
}
}
if ($count >= $limit) {
break;
}
}
return $options;
}
/**
* Fetch an array of options for the given widget (field data).
*
* Options are retrieved from existing values for the field.
*/
function _autocomplete_widgets_get_options_flddata($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
$entity_info = entity_get_info($instance['entity_type']);
$table = 'field_data_' . $instance['field_name'];
$column = $instance['field_name'] . '_value';
$order = isset($instance['widget']['settings']['order']) ? $instance['widget']['settings']['order'] : '';
$case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
$select = db_select($entity_info['base table'], 'b');
if ($entity_info['base table'] == 'node' && !empty($instance['widget']['settings']['obey_access_controls'])) {
// Add entity_field_access so that node permission are respected.
$select->addTag('node_access');
if (!user_access('bypass node access')) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
if (user_access('view own unpublished content') && $own_unpublished = db_query('SELECT nid FROM {node} WHERE uid = :uid AND status = :status', array(':uid' => $GLOBALS['user']->uid, ':status' => NODE_NOT_PUBLISHED))->fetchCol()) {
$select->condition(db_or()
->condition('n.status', NODE_PUBLISHED)
->condition('n.nid', $own_unpublished, 'IN')
);
}
else {
// If not, restrict the query to published nodes.
$select->condition('n.status', NODE_PUBLISHED);
}
}
}
$id = 'b.' . $entity_info['entity keys']['id'];
$select->join($table, 'fd', 'revision_id = ' . $id);
$select->addField('fd', $column);
if ($string !== '') {
switch ($match) {
case 'equals':
$select->condition($column, $string);
break;
case 'starts_with':
$select->condition($column, $string . '%', 'LIKE');
break;
case 'contains':
default:
$select->condition($column, '%' . $string . '%', 'LIKE');
break;
}
}
elseif (isset($keys) && is_array($keys)) {
$select->condition($column, $keys, 'IN');
}
if (!empty($limit)) {
$select->range(0, $limit);
}
if (!empty($order)) {
$select->orderBy($column, $order);
}
$rows = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
$options = array();
foreach ($rows as $row) {
// MySQL does not do case sensitive text comparisons with Backdrop's default
// colation (utf8_general_ci) so we deal with it here after the fact.
if (!$case_sensitive || ($case_sensitive && strpos($row[$column], $string) !== FALSE)) {
$options[$row[$column]] = $row[$column];
}
}
return $options;
}
/**
* Fetch an array of options for the given widget (suggested).
*
* Options are retrieved from the suggested values defined for the field.
*/
function _autocomplete_widgets_get_options_suggested($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
$case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
$options = explode("\n", $instance['widget']['settings']['suggested_values']);
$options = array_map('trim', $options);
$options = array_filter($options, 'strlen');
_autocomplete_widgets_sort_options($options, $instance);
switch ($match) {
case 'contains':
case 'starts_with':
$matched_options = array();
$string = !$case_sensitive ? strtolower($string) : $string;
foreach ($options as $key => $option) {
$option = !$case_sensitive ? strtolower($option) : $option;
if ($match == 'contains' && strpos($option, $string) !== FALSE) {
$matched_options[] = $options[$key];
}
elseif ($match == 'starts_with' && strpos($option, $string) === 0) {
$matched_options[] = $options[$key];
}
}
$options = $matched_options;
break;
case 'equals':
if (in_array($string, $options, TRUE)) {
$options = array($string);
}
break;
}
return $options;
}
/**
* Fetch an array of options for the given widget (node_reference).
*
* Options are retrieved from the titles of the allowed node types.
*/
function _autocomplete_widgets_get_options_node_reference($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
$field_name = $instance['field_name'];
$table = 'field_data_' . $field_name;
$column = $field_name . '_value';
$options = array();
$case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
$order = isset($instance['widget']['settings']['order']) ? $instance['widget']['settings']['order'] : '';
$field_query = db_select($table, 'fd')
->fields('fd', array($column));
$node_title_query = db_select('node', 'n')
->fields('n', array('title'))
->condition('n.type', $instance['widget']['settings']['allowed_node_types'], 'IN')
->addTag('node_access');
$field_query = db_select('node', 'n');
if (!empty($instance['widget']['settings']['obey_access_controls'])) {
// Add entity_field_access so that node permission are respected.
$field_query->addTag('node_access');
if (!user_access('bypass node access')) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
if (user_access('view own unpublished content') && $own_unpublished = db_query('SELECT nid FROM {node} WHERE uid = :uid AND status = :status', array(':uid' => $GLOBALS['user']->uid, ':status' => NODE_NOT_PUBLISHED))->fetchCol()) {
$field_query->condition(db_or()
->condition('n.status', NODE_PUBLISHED)
->condition('n.nid', $own_unpublished, 'IN')
);
$node_title_query->condition(db_or()
->condition('n.status', NODE_PUBLISHED)
->condition('n.nid', $own_unpublished, 'IN')
);
}
else {
// If not, restrict the query to published nodes.
$field_query->condition('n.status', NODE_PUBLISHED);
$node_title_query->condition('n.status', NODE_PUBLISHED);
}
}
}
$field_query->join($table, 'fd', 'revision_id = n.vid');
$field_query->addField('fd', $column);
if (!empty($order)) {
$field_query->orderBy($column, $order);
$node_title_query->orderBy('title', $order);
}
if ($string !== '') {
switch ($match) {
case 'starts_with':
$field_query->condition($column, $string . '%', 'LIKE');
$node_title_query->condition('n.title', $string . '%', 'LIKE');
break;
case 'contains':
default:
$field_query->condition($column, '%' . $string . '%', 'LIKE');
$node_title_query->condition('n.title', '%' . $string . '%', 'LIKE');
break;
}
// @todo: can these fetch all's be replaced with fetchAssoc?
// MySQL does not do case sensitive text comparisons with Backdrop's default
// colation (utf8_general_ci) so we deal with it here after the fact.
$rows = $node_title_query->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
if (!$case_sensitive || ($case_sensitive && strpos($row['title'], $string) !== FALSE)) {
$options[$row['title']] = $row['title'];
}
}
$rows = $field_query->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
if (!$case_sensitive || ($case_sensitive && strpos($row[$column], $string) !== FALSE)) {
$options[$row[$column]] = $row[$column];
}
}
}
// @todo: limit should be accounted for here...
return $options;
}
/**
* Fetch an array of options for the given widget (node_title).
*
* Options are retrieved from the titles of the allowed node types.
*/
function _autocomplete_widgets_get_options_node_title($instance, $string = '', $match = 'contains', $keys = NULL, $limit = NULL) {
$options = array();
$case_sensitive = !empty($instance['widget']['settings']['autocomplete_case']);
$order = isset($instance['widget']['settings']['order']) ? $instance['widget']['settings']['order'] : '';
$node_title_query = db_select('node', 'n')
->fields('n', array('title'))
->condition('n.type', $instance['widget']['settings']['allowed_node_types'], 'IN')
->addTag('node_access');
if (!empty($instance['widget']['settings']['obey_access_controls'])) {
if (!user_access('bypass node access')) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
if (user_access('view own unpublished content') && $own_unpublished = db_query('SELECT nid FROM {node} WHERE uid = :uid AND status = :status', array(':uid' => $GLOBALS['user']->uid, ':status' => NODE_NOT_PUBLISHED))->fetchCol()) {
$node_title_query->condition(db_or()
->condition('n.status', NODE_PUBLISHED)
->condition('n.nid', $own_unpublished, 'IN')
);
}
else {
// If not, restrict the query to published nodes.
$node_title_query->condition('n.status', NODE_PUBLISHED);
}
}
}
if (!empty($order)) {
$node_title_query->orderBy('title', $order);
}
if ($string !== '') {
switch ($match) {
case 'starts_with':
$node_title_query->condition('n.title', $string . '%', 'LIKE');
break;
case 'contains':
default:
$node_title_query->condition('n.title', '%' . $string . '%', 'LIKE');
break;
}
// @todo: can these fetch all's be replaced with fetchAssoc?
// MySQL does not do case sensitive text comparisons with Backdrop's default
// colation (utf8_general_ci) so we deal with it here after the fact.
$rows = $node_title_query->execute()->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row) {
if (!$case_sensitive || ($case_sensitive && strpos($row['title'], $string) !== FALSE)) {
$options[$row['title']] = $row['title'];
}
}
}
// @todo: limit should be accounted for here...
return $options;
}
/**
* Validate a list autocomplete element.
*/
function _autocomplete_widgets_validate_allowvals($element, &$form_state) {
$instance = field_widget_instance($element, $form_state);
if ($instance['widget']['type'] == 'autocomplete_widgets_allowvals') {
$label = $element['#value'];
if ($label !== '') {
module_load_include('inc', 'autocomplete_widgets', 'autocomplete_widgets.common');
$options = _autocomplete_widgets_get_options($instance, $label, 'equals', NULL, 1);
if (empty($options)) {
form_error($element, t('%name: %label is not a valid option for this field.', array('%name' => $instance['label'], '%label' => $label)));
}
}
}
}
/**
* Sort an array of options fo the given field instance.
*/
function _autocomplete_widgets_sort_options(&$options, $instance) {
if (isset($instance['widget']['settings']['order'])) {
switch ($instance['widget']['settings']['order']) {
case 'ASC':
asort($options);
break;
case 'DESC':
arsort($options);
break;
}
}
}