aboutsummaryrefslogtreecommitdiff
path: root/views/default/input/autocomplete.php
blob: 052219a1ba2f4875ecf782c680d54527069ad45d (plain)
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
<?php

	/**
	 * Displays an autocomplete text input.
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @author Curverider Ltd
	 * @link http://elgg.org/
	 * 
	 * @TODO: This currently only works for ONE AUTOCOMPLETE TEXT FIELD on a page.
	 * 
	 * @uses $vars['value'] The current value, if any
	 * @uses $vars['js'] Any Javascript to enter into the input tag
	 * @uses $vars['internalname'] The name of the input field
	 * @uses $vars['match_on'] Array | str What to match on. all|array(group|user|friend|subtype)
	 * @uses $vars['match_owner'] Bool.  Match only entities that are owned by logged in user.
	 * 
	 */

	global $autocomplete_js_loaded;
	
	$internalname = $vars['internalname'];
	$value = $vars['value'];
	
	if(!$value)
		$value= '';
	
	if($vars['internal_id'])
		$id_autocomplete = $vars['internal_id'];
		
	$ac_url_params = http_build_query(array(
		'match_on' => $vars['match_on'],
		'match_owner' => $vars['match_owner'],
	));
	$ac_url = $vars['url'] . 'pg/autocomplete?' . $ac_url_params;
			
	if (!isset($autocomplete_js_loaded)) $autocomplete_js_loaded = false;
	
?>

<!-- show the input -->
	<input type="text" class='autocomplete' name ='<?php echo $internalname; ?>_autocomplete' value='<?php echo $value?>' />
	<input type="hidden" name="<?php echo $internalname; ?>" value='<?php echo $value; ?>' />
	
<?php
	if (!$autocomplete_js_loaded) {	
?>

<!-- include autocomplete -->
<script language="javascript" type="text/javascript" src="<?php echo $vars['url']; ?>vendors/jquery/jquery.autocomplete.min.js"></script>
<script type="text/javascript">
function bindAutocomplete() {
	$('input[type=text].autocomplete').autocomplete("<?php echo $ac_url; ?>", {
		minChars: 1,
		matchContains: true,
		autoFill: false,
		formatItem: function(row, i, max, term) {
			eval("var info = " + row + ";");
			var r = '';
			
			switch (info.type) {
				case 'user':
				case 'group':
					r = info.icon + info.name + ' - ' + info.desc;
					break;
					
				default:
					r = info.name + ' - ' + info.desc;
					break;
			}
			return r.replace(new RegExp("(" + term + ")", "gi"), "<b>$1</b>");
		}
	});

	$('input[type=text].autocomplete').result(function(event, data, formatted) {
		eval("var info = " + data + ";");
		$(this).val(info.name);

		var hidden = $(this).next();
		hidden.val(info.guid);
	});
}

$(document).ready(function() {
	bindAutocomplete();
});

</script>

<?php

		$autocomplete_js_loaded = true;
	} else {
?>
<!-- rebind autocomplete -->
<script type="text/javascript">bindAutocomplete();</script>
<?php
	}