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