1: <?php
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: class IMP_Contacts implements Serializable
30: {
31: 32: 33: 34: 35:
36: private $_changed = false;
37:
38: 39: 40: 41: 42:
43: private $_fields;
44:
45: 46: 47: 48: 49:
50: private $_sources;
51:
52: 53:
54: public function __get($name)
55: {
56: global $registry;
57:
58: switch ($name) {
59: case 'changed':
60: return $this->_changed;
61:
62: case 'fields':
63: case 'sources':
64: if (!isset($this->_fields)) {
65: $this->_init();
66: }
67: return $this->{'_' . $name};
68:
69: case 'source_list':
70: if ($registry->hasMethod('contacts/sources')) {
71: try {
72: return $registry->call('contacts/sources');
73: } catch (Horde_Exception $e) {}
74: }
75: return array();
76: }
77: }
78:
79: 80: 81:
82: public function clearCache()
83: {
84: unset($this->_fields, $this->_sources);
85: $this->_changed = true;
86: }
87:
88: 89: 90: 91: 92: 93: 94: 95: 96:
97: public function addAddress($addr, $name)
98: {
99: global $registry, $prefs;
100:
101: if (empty($name)) {
102: $name = $addr;
103: }
104:
105: $result = $registry->call('contacts/import', array(array(
106: 'email' => $addr,
107: 'name' => $name
108: ), 'array', $prefs->getValue('add_source')));
109:
110: $escapeName = @htmlspecialchars($name, ENT_COMPAT, 'UTF-8');
111:
112: try {
113: if ($contact_link = $registry->link('contacts/show', array('uid' => $result, 'source' => $prefs->getValue('add_source')))) {
114: return Horde::link(Horde::url($contact_link), sprintf(_("Go to address book entry of \"%s\""), $name)) . $escapeName . '</a>';
115: }
116: } catch (Horde_Exception $e) {}
117:
118: return $escapeName;
119: }
120:
121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131:
132: public function searchEmail($str, array $opts = array())
133: {
134: global $registry;
135:
136: if (!$registry->hasMethod('contacts/search')) {
137: return new Horde_Mail_Rfc822_List();
138: }
139:
140: $sources = empty($opts['sources'])
141: ? $this->sources
142: : $opts['sources'];
143:
144: if (empty($opts['email_exact'])) {
145: $customStrict = array();
146: $fields = $this->fields;
147: $returnFields = array('email', 'name');
148: } else {
149: $customStrict = $returnFields = array('email');
150: $fields = array_fill_keys($sources, array('email'));
151: }
152:
153: try {
154: $search = $registry->call('contacts/search', array($str, array(
155: 'customStrict' => $customStrict,
156: 'fields' => $fields,
157: 'returnFields' => $returnFields,
158: 'rfc822Return' => true,
159: 'sources' => $sources
160: )));
161: } catch (Horde_Exception $e) {
162: Horde::log($e, 'ERR');
163: return new Horde_Mail_Rfc822_List();
164: }
165:
166: if (empty($opts['levenshtein'])) {
167: return $search;
168: }
169:
170: $sort_list = array();
171: foreach ($search->base_addresses as $val) {
172: $sort_list[strval($val)] = @levenshtein($str, $val);
173: }
174: asort($sort_list, SORT_NUMERIC);
175:
176: return new Horde_Mail_Rfc822_List(array_keys($sort_list));
177: }
178:
179: 180: 181:
182: private function _init()
183: {
184: global $prefs;
185:
186: $fields = json_decode($prefs->getValue('search_fields'), true);
187: $src = json_decode($prefs->getValue('search_sources'));
188:
189: $this->_fields = empty($fields) ? array() : $fields;
190: $this->_sources = empty($src) ? array() : $src;
191:
192: $this->_changed = true;
193: }
194:
195:
196:
197: 198:
199: public function serialize()
200: {
201: return json_encode(array(
202: $this->_fields,
203: $this->_sources
204: ));
205: }
206:
207: 208:
209: public function unserialize($data)
210: {
211: list($this->_fields, $this->_sources) = json_decode($data, true);
212: }
213:
214: }
215: