1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Whups_Ajax_Imple_ContactAutoCompleter extends Horde_Core_Ajax_Imple_AutoCompleter
14: {
15: 16: 17: 18: 19: 20: 21: 22:
23: protected function _attach($js_params)
24: {
25: $js_params['indicator'] = $this->_params['triggerId'] . '_loading_img';
26:
27: $ret = array(
28: 'params' => $js_params,
29: 'raw_params' => array(
30: 'onSelect' => 'function (v) { if (!v.endsWith(";")) { v += ","; } return v + " "; }',
31: 'onType' => 'function (e) { return e.include("<") ? "" : e; }'
32: )
33: );
34:
35: $ret['ajax'] = 'ContactAutoCompleter';
36:
37: return $ret;
38: }
39:
40: 41: 42: 43: 44: 45: 46:
47: public function handle($args, $post)
48: {
49:
50: if (empty($args['input']) ||
51: !($input = Horde_Util::getFormData($args['input']))) {
52: return array();
53: }
54:
55: return $this->_getAddressList($input);
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67:
68: protected function _getAddressList($addrString = '')
69: {
70: if (!preg_match('|[^\s]|', $addrString)) {
71: return array();
72: }
73:
74: $search = reset(array_filter(array_map('trim', Horde_Mime_Address::explode($addrString, ',;'))));
75: $searchpref = Whups::getAddressbookSearchParams();
76:
77: try {
78: $res = $GLOBALS['registry']->call('contacts/search', array($search, $searchpref['sources'], $searchpref['fields']));
79: } catch (Horde_Exception $e) {
80: Horde::logMessage($e, 'ERR');
81: return array();
82: }
83:
84: if (!count($res)) {
85: return array();
86: }
87:
88: 89:
90: $search = array();
91: foreach (reset($res) as $val) {
92: if (!empty($val['email'])) {
93: if (strpos($val['email'], ',') !== false) {
94: $search[] = Horde_Mime_Address::encode($val['name'], 'personal') . ': ' . $val['email'] . ';';
95: } else {
96: $mbox_host = explode('@', $val['email']);
97: if (isset($mbox_host[1])) {
98: $search[] = Horde_Mime_Address::writeAddress($mbox_host[0], $mbox_host[1], $val['name']);
99: }
100: }
101: }
102: }
103:
104: $sort_list = array();
105: foreach ($search as $val) {
106: $sort_list[$val] = levenshtein($addrString, $val);
107: }
108: asort($sort_list, SORT_NUMERIC);
109:
110: return array_keys($sort_list);
111: }
112: }
113: