Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * new version of civicrm apis. See blog post at | |
30 | * http://civicrm.org/node/131 | |
31 | * @todo Write sth | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_Contact | |
731a0992 | 35 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
36 | * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $ |
37 | * | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Create or update a contact (note you should always call this via civicrm_api() & never directly) | |
42 | * | |
cf470720 TO |
43 | * @param array $params |
44 | * Input parameters. | |
6a488035 TO |
45 | * |
46 | * Allowed @params array keys are: | |
47 | * {@getfields contact_create} | |
48 | * | |
49 | * | |
77b97be7 | 50 | * @throws API_Exception |
6a488035 TO |
51 | * @example ContactCreate.php Example of Create Call |
52 | * | |
a6c01b45 | 53 | * @return array |
72b3a70c | 54 | * API Result Array |
6a488035 | 55 | * |
6a488035 TO |
56 | */ |
57 | function civicrm_api3_contact_create($params) { | |
58 | ||
59 | $contactID = CRM_Utils_Array::value('contact_id', $params, CRM_Utils_Array::value('id', $params)); | |
60 | $dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE); | |
61 | $values = _civicrm_api3_contact_check_params($params, $dupeCheck); | |
62 | if ($values) { | |
63 | return $values; | |
64 | } | |
65 | ||
66 | if (!$contactID) { | |
67 | // If we get here, we're ready to create a new contact | |
68 | if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) { | |
69 | $defLocType = CRM_Core_BAO_LocationType::getDefault(); | |
70 | $params['email'] = array( | |
35671d00 TO |
71 | 1 => array( |
72 | 'email' => $email, | |
6a488035 TO |
73 | 'is_primary' => 1, |
74 | 'location_type_id' => ($defLocType->id) ? $defLocType->id : 1, | |
75 | ), | |
76 | ); | |
77 | } | |
78 | } | |
79 | ||
80 | if (!empty($params['home_url'])) { | |
cbf48754 | 81 | $websiteTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'); |
35671d00 TO |
82 | $params['website'] = array( |
83 | 1 => array( | |
84 | 'website_type_id' => key($websiteTypes), | |
6a488035 TO |
85 | 'url' => $params['home_url'], |
86 | ), | |
87 | ); | |
88 | } | |
89 | ||
6ecbca5b | 90 | _civicrm_api3_greeting_format_params($params); |
6a488035 TO |
91 | |
92 | $values = array(); | |
6a488035 | 93 | |
6ecbca5b | 94 | if (empty($params['contact_type']) && $contactID) { |
95 | $params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($contactID); | |
6a488035 TO |
96 | } |
97 | ||
6ecbca5b | 98 | if (!isset($params['contact_sub_type']) && $contactID) { |
99 | $params['contact_sub_type'] = CRM_Contact_BAO_Contact::getContactSubType($contactID); | |
6a488035 TO |
100 | } |
101 | ||
6ecbca5b | 102 | _civicrm_api3_custom_format_params($params, $values, $params['contact_type'], $contactID); |
6a488035 TO |
103 | |
104 | $params = array_merge($params, $values); | |
6ecbca5b | 105 | //@todo we should just call basic_create here - but need to make contact:create accept 'id' on the bao |
6a488035 TO |
106 | $contact = _civicrm_api3_contact_update($params, $contactID); |
107 | ||
108 | if (is_a($contact, 'CRM_Core_Error')) { | |
6ecbca5b | 109 | throw new API_Exception($contact->_errors[0]['message']); |
6a488035 TO |
110 | } |
111 | else { | |
112 | $values = array(); | |
113 | _civicrm_api3_object_to_array_unique_fields($contact, $values[$contact->id]); | |
114 | } | |
115 | ||
116 | return civicrm_api3_create_success($values, $params, 'Contact', 'create'); | |
117 | } | |
118 | ||
11e09c59 | 119 | /** |
6a488035 TO |
120 | * Adjust Metadata for Create action |
121 | * | |
cf470720 TO |
122 | * @param array $params |
123 | * Array or parameters determined by getfields. | |
6a488035 TO |
124 | */ |
125 | function _civicrm_api3_contact_create_spec(&$params) { | |
126 | $params['contact_type']['api.required'] = 1; | |
127 | $params['id']['api.aliases'] = array('contact_id'); | |
128 | $params['current_employer'] = array( | |
129 | 'title' => 'Current Employer', | |
130 | 'description' => 'Name of Current Employer', | |
9c5991b3 | 131 | 'type' => CRM_Utils_Type::T_STRING, |
6a488035 | 132 | ); |
0391dc25 | 133 | $params['dupe_check'] = array( |
134 | 'title' => 'Check for Duplicates', | |
135 | 'description' => 'Throw error if contact create matches dedupe rule', | |
136 | ); | |
6ecbca5b | 137 | $params['prefix_id']['api.aliases'] = array('individual_prefix', 'individual_prefix_id'); |
138 | $params['suffix_id']['api.aliases'] = array('individual_suffix', 'individual_suffix_id'); | |
6a488035 TO |
139 | } |
140 | ||
141 | /** | |
142 | * Retrieve one or more contacts, given a set of search params | |
143 | * | |
cf470720 | 144 | * @param array input parameters |
6a488035 | 145 | * |
a6c01b45 | 146 | * @return array |
72b3a70c | 147 | * API Result Array |
6a488035 TO |
148 | */ |
149 | function civicrm_api3_contact_get($params) { | |
150 | $options = array(); | |
151 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
152 | $contacts = _civicrm_api3_get_using_query_object('contact', $params, $options); | |
6ecbca5b | 153 | return civicrm_api3_create_success($contacts, $params, 'contact'); |
6a488035 TO |
154 | } |
155 | ||
aa1b1481 | 156 | /** |
c490a46a | 157 | * @param array $params |
aa1b1481 EM |
158 | * |
159 | * @return int | |
160 | */ | |
6a488035 TO |
161 | function civicrm_api3_contact_getcount($params) { |
162 | $options = array(); | |
163 | _civicrm_api3_contact_get_supportanomalies($params, $options); | |
35671d00 | 164 | $count = _civicrm_api3_get_using_query_object('contact', $params, $options, 1); |
972322c5 | 165 | return (int) $count; |
6a488035 | 166 | } |
11e09c59 TO |
167 | |
168 | /** | |
6a488035 TO |
169 | * Adjust Metadata for Get action |
170 | * | |
cf470720 TO |
171 | * @param array $params |
172 | * Array or parameters determined by getfields. | |
6a488035 TO |
173 | */ |
174 | function _civicrm_api3_contact_get_spec(&$params) { | |
175 | $params['contact_is_deleted']['api.default'] = 0; | |
176 | ||
177 | //we declare all these pseudofields as there are other undocumented fields accessible | |
178 | // via the api - but if check permissions is set we only allow declared fields | |
179 | $params['address_id']['title'] = 'Primary Address ID'; | |
180 | $params['street_address']['title'] = 'Primary Address Street Address'; | |
181 | $params['supplemental_address_1']['title'] = 'Primary Address Supplemental Address 1'; | |
182 | $params['supplemental_address_2']['title'] = 'Primary Address Supplemental Address 2'; | |
23c2fe57 | 183 | $params['current_employer']['title'] = 'Current Employer'; |
6a488035 TO |
184 | $params['city']['title'] = 'Primary Address City'; |
185 | $params['postal_code_suffix']['title'] = 'Primary Address Post Code Suffix'; | |
186 | $params['postal_code']['title'] = 'Primary Address Post Code'; | |
187 | $params['geo_code_1']['title'] = 'Primary Address Latitude'; | |
188 | $params['geo_code_2']['title'] = 'Primary Address Longitude'; | |
189 | $params['state_province_id']['title'] = 'Primary Address State Province ID'; | |
190 | $params['state_province_name']['title'] = 'Primary Address State Province Name'; | |
191 | $params['state_province']['title'] = 'Primary Address State Province'; | |
41847fb4 | 192 | $params['country_id']['title'] = 'Primary Address Country ID'; |
6a488035 TO |
193 | $params['country']['title'] = 'Primary Address country'; |
194 | $params['worldregion_id']['title'] = 'Primary Address World Region ID'; | |
195 | $params['worldregion']['title'] = 'Primary Address World Region'; | |
196 | $params['phone_id']['title'] = 'Primary Phone ID'; | |
7d4ceb59 | 197 | $params['phone']['title'] = 'Primary Phone'; |
6a488035 TO |
198 | $params['phone_type_id']['title'] = 'Primary Phone Type ID'; |
199 | $params['provider_id']['title'] = 'Primary Phone Provider ID'; | |
200 | $params['email_id']['title'] = 'Primary Email ID'; | |
201 | $params['email']['title'] = 'Primary Email'; | |
c2280651 | 202 | $params['gender_id']['title'] = 'Gender ID'; |
203 | $params['gender']['title'] = 'Gender'; | |
6a488035 | 204 | $params['on_hold']['title'] = 'Primary Email On Hold'; |
c206647d EM |
205 | $params['im']['title'] = 'Primary Instant Messenger'; |
206 | $params['im_id']['title'] = 'Primary Instant Messenger ID'; | |
6a488035 TO |
207 | $params['group_id']['title'] = 'Group Memberships (filter)'; |
208 | $params['group']['title'] = 'Group Memberships (filter, array)'; | |
209 | $params['tag']['title'] = 'Assigned tags (filter, array)'; | |
c206647d EM |
210 | $params['birth_date_low'] = array('name' => 'birth_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or greater than')); |
211 | $params['birth_date_high'] = array('name' => 'birth_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birth Date is equal to or less than')); | |
35671d00 | 212 | $params['deceased_date_low'] = array('name' => 'deceased_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or greater than')); |
9f60788a | 213 | $params['deceased_date_high'] = array('name' => 'deceased_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or less than')); |
6a488035 TO |
214 | } |
215 | ||
11e09c59 | 216 | /** |
6a488035 TO |
217 | * We are supporting 'showAll' = 'all', 'trash' or 'active' for contact get |
218 | * and for getcount | |
219 | * - hopefully some day we'll come up with a std syntax for the 3-way-boolean of | |
220 | * 0, 1 or not set | |
221 | * | |
222 | * We also support 'filter_group_id' & 'filter.group_id' | |
223 | * | |
cf470720 TO |
224 | * @param array $params |
225 | * As passed into api get or getcount function. | |
226 | * @param array $options | |
227 | * Array of options (so we can modify the filter). | |
6a488035 TO |
228 | */ |
229 | function _civicrm_api3_contact_get_supportanomalies(&$params, &$options) { | |
230 | if (isset($params['showAll'])) { | |
231 | if (strtolower($params['showAll']) == "active") { | |
232 | $params['contact_is_deleted'] = 0; | |
233 | } | |
234 | if (strtolower($params['showAll']) == "trash") { | |
235 | $params['contact_is_deleted'] = 1; | |
236 | } | |
237 | if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) { | |
238 | unset($params['contact_is_deleted']); | |
239 | } | |
240 | } | |
241 | // support for group filters | |
242 | if (array_key_exists('filter_group_id', $params)) { | |
243 | $params['filter.group_id'] = $params['filter_group_id']; | |
244 | unset($params['filter_group_id']); | |
245 | } | |
246 | // filter.group_id works both for 1,2,3 and array (1,2,3) | |
247 | if (array_key_exists('filter.group_id', $params)) { | |
248 | if (is_array($params['filter.group_id'])) { | |
249 | $groups = $params['filter.group_id']; | |
250 | } | |
4f99ca55 TO |
251 | else { |
252 | $groups = explode(',', $params['filter.group_id']); | |
35671d00 | 253 | } |
6a488035 TO |
254 | unset($params['filter.group_id']); |
255 | $groups = array_flip($groups); | |
256 | $groups[key($groups)] = 1; | |
257 | $options['input_params']['group'] = $groups; | |
258 | } | |
259 | } | |
260 | ||
261 | /** | |
262 | * Delete a contact with given contact id | |
263 | * | |
cf470720 | 264 | * @param array $params |
c23f45d3 | 265 | * input parameters per getfields |
6a488035 | 266 | * |
a6c01b45 | 267 | * @return array |
72b3a70c | 268 | * API Result Array |
6a488035 TO |
269 | */ |
270 | function civicrm_api3_contact_delete($params) { | |
271 | ||
272 | $contactID = CRM_Utils_Array::value('id', $params); | |
273 | ||
274 | $session = CRM_Core_Session::singleton(); | |
275 | if ($contactID == $session->get('userID')) { | |
276 | return civicrm_api3_create_error('This contact record is linked to the currently logged in user account - and cannot be deleted.'); | |
277 | } | |
0d8afee2 CW |
278 | $restore = !empty($params['restore']) ? $params['restore'] : FALSE; |
279 | $skipUndelete = !empty($params['skip_undelete']) ? $params['skip_undelete'] : FALSE; | |
f182074e PN |
280 | |
281 | // CRM-12929 | |
282 | // restrict permanent delete if a contact has financial trxn associated with it | |
283 | $error = NULL; | |
284 | if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($contactID), $error)) { | |
ad3f841d | 285 | return civicrm_api3_create_error($error['_qf_default']); |
f182074e | 286 | } |
6a488035 TO |
287 | if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) { |
288 | return civicrm_api3_create_success(); | |
289 | } | |
290 | else { | |
291 | return civicrm_api3_create_error('Could not delete contact'); | |
292 | } | |
293 | } | |
294 | ||
295 | ||
aa1b1481 | 296 | /** |
c490a46a | 297 | * @param array $params |
aa1b1481 | 298 | * @param bool $dupeCheck |
aa1b1481 EM |
299 | * |
300 | * @return null | |
301 | * @throws API_Exception | |
302 | * @throws CiviCRM_API3_Exception | |
303 | */ | |
c1d12cc0 | 304 | function _civicrm_api3_contact_check_params(&$params, $dupeCheck) { |
6a488035 TO |
305 | |
306 | switch (strtolower(CRM_Utils_Array::value('contact_type', $params))) { | |
307 | case 'household': | |
35671d00 | 308 | civicrm_api3_verify_mandatory($params, NULL, array('household_name')); |
6a488035 | 309 | break; |
35671d00 | 310 | |
6a488035 | 311 | case 'organization': |
35671d00 | 312 | civicrm_api3_verify_mandatory($params, NULL, array('organization_name')); |
6a488035 | 313 | break; |
35671d00 | 314 | |
6a488035 | 315 | case 'individual': |
35671d00 | 316 | civicrm_api3_verify_one_mandatory($params, NULL, array( |
6a488035 TO |
317 | 'first_name', |
318 | 'last_name', | |
319 | 'email', | |
320 | 'display_name', | |
321 | ) | |
35671d00 TO |
322 | ); |
323 | break; | |
6a488035 TO |
324 | } |
325 | ||
fe18a93c CW |
326 | // Fixme: This really needs to be handled at a lower level. @See CRM-13123 |
327 | if (isset($params['preferred_communication_method'])) { | |
328 | $params['preferred_communication_method'] = CRM_Utils_Array::implodePadded($params['preferred_communication_method']); | |
329 | } | |
330 | ||
8cc574cf | 331 | if (!empty($params['contact_sub_type']) && !empty($params['contact_type'])) { |
35671d00 TO |
332 | if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type']))) { |
333 | throw new API_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $params['contact_sub_type'])); | |
6a488035 | 334 | } |
35671d00 | 335 | } |
6a488035 TO |
336 | |
337 | if ($dupeCheck) { | |
338 | // check for record already existing | |
339 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']); | |
340 | ||
341 | // CRM-6431 | |
342 | // setting 'check_permission' here means that the dedupe checking will be carried out even if the | |
343 | // person does not have permission to carry out de-dupes | |
344 | // this is similar to the front end form | |
345 | if (isset($params['check_permission'])) { | |
346 | $dedupeParams['check_permission'] = $params['check_permission']; | |
347 | } | |
348 | ||
67cfa333 | 349 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array()); |
6a488035 | 350 | |
35671d00 | 351 | if (count($ids) > 0) { |
86bfa4f6 | 352 | throw new API_Exception("Found matching contacts: " . implode(',', $ids), "duplicate", array("ids" => $ids)); |
6a488035 TO |
353 | } |
354 | } | |
355 | ||
8d99ab37 | 356 | // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability |
6a488035 | 357 | if (!empty($params['current_employer'])) { |
8d99ab37 CW |
358 | $organizationParams = array( |
359 | 'organization_name' => $params['current_employer'], | |
360 | ); | |
6a488035 TO |
361 | |
362 | $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization'); | |
363 | ||
364 | $dedupParams['check_permission'] = FALSE; | |
365 | $dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Supervised'); | |
366 | ||
367 | // check for mismatch employer name and id | |
368 | if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)) { | |
369 | throw new API_Exception('Employer name and Employer id Mismatch'); | |
370 | } | |
371 | ||
372 | // show error if multiple organisation with same name exist | |
373 | if (empty($params['employer_id']) && (count($dupeIds) > 1)) { | |
374 | throw new API_Exception('Found more than one Organisation with same Name.'); | |
375 | } | |
8d99ab37 CW |
376 | |
377 | if ($dupeIds) { | |
378 | $params['employer_id'] = $dupeIds[0]; | |
379 | } | |
380 | else { | |
381 | $result = civicrm_api3('contact', 'create', array( | |
382 | 'organization_name' => $params['current_employer'], | |
21dfd5f5 | 383 | 'contact_type' => 'Organization', |
8d99ab37 CW |
384 | )); |
385 | $params['employer_id'] = $result['id']; | |
386 | } | |
6a488035 TO |
387 | } |
388 | ||
389 | return NULL; | |
390 | } | |
391 | ||
392 | /** | |
393 | * Takes an associative array and creates a contact object and all the associated | |
394 | * derived objects (i.e. individual, location, email, phone etc) | |
395 | * | |
cf470720 TO |
396 | * @param array $params |
397 | * (reference ) an assoc array of name/value pairs. | |
398 | * @param int $contactID | |
399 | * If present the contact with that ID is updated. | |
6a488035 | 400 | * |
54f1aa2a | 401 | * @return CRM_Contact_BAO_Contact|CRM_Core_Error |
6a488035 TO |
402 | */ |
403 | function _civicrm_api3_contact_update($params, $contactID = NULL) { | |
6ecbca5b | 404 | //@todo - doesn't contact create support 'id' which is already set- check & remove |
6a488035 TO |
405 | if ($contactID) { |
406 | $params['contact_id'] = $contactID; | |
407 | } | |
408 | ||
6ecbca5b | 409 | return CRM_Contact_BAO_Contact::create($params); |
6a488035 TO |
410 | } |
411 | ||
412 | /** | |
413 | * Validate the addressee or email or postal greetings | |
414 | * | |
cf470720 TO |
415 | * @param array $params |
416 | * Associative array of property name/value. | |
6a488035 TO |
417 | * pairs to insert in new contact. |
418 | * | |
77b97be7 | 419 | * @throws API_Exception |
6a488035 | 420 | * |
6a488035 TO |
421 | */ |
422 | function _civicrm_api3_greeting_format_params($params) { | |
423 | $greetingParams = array('', '_id', '_custom'); | |
424 | foreach (array('email', 'postal', 'addressee') as $key) { | |
425 | $greeting = '_greeting'; | |
426 | if ($key == 'addressee') { | |
427 | $greeting = ''; | |
428 | } | |
429 | ||
430 | $formatParams = FALSE; | |
431 | // unset display value from params. | |
432 | if (isset($params["{$key}{$greeting}_display"])) { | |
433 | unset($params["{$key}{$greeting}_display"]); | |
434 | } | |
435 | ||
436 | // check if greetings are present in present | |
437 | foreach ($greetingParams as $greetingValues) { | |
438 | if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) { | |
439 | $formatParams = TRUE; | |
440 | break; | |
441 | } | |
442 | } | |
443 | ||
444 | if (!$formatParams) { | |
445 | continue; | |
446 | } | |
447 | ||
448 | $nullValue = FALSE; | |
449 | $filter = array( | |
450 | 'contact_type' => $params['contact_type'], | |
451 | 'greeting_type' => "{$key}{$greeting}", | |
452 | ); | |
453 | ||
454 | $greetings = CRM_Core_PseudoConstant::greeting($filter); | |
455 | $greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params); | |
456 | $greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params); | |
457 | $customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params); | |
458 | ||
459 | if (!$greetingId && $greetingVal) { | |
460 | $params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings); | |
461 | } | |
462 | ||
463 | if ($customGreeting && $greetingId && | |
464 | ($greetingId != array_search('Customized', $greetings)) | |
465 | ) { | |
6ecbca5b | 466 | throw new API_Exception(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting', |
6a488035 TO |
467 | array(1 => $key) |
468 | )); | |
469 | } | |
470 | ||
471 | if ($greetingVal && $greetingId && | |
472 | ($greetingId != CRM_Utils_Array::key($greetingVal, $greetings)) | |
473 | ) { | |
6ecbca5b | 474 | throw new API_Exception(ts('Mismatch in %1 greeting id and %1 greeting', |
6a488035 TO |
475 | array(1 => $key) |
476 | )); | |
477 | } | |
478 | ||
479 | if ($greetingId) { | |
480 | ||
481 | if (!array_key_exists($greetingId, $greetings)) { | |
6ecbca5b | 482 | throw new API_Exception(ts('Invalid %1 greeting Id', array(1 => $key))); |
6a488035 TO |
483 | } |
484 | ||
485 | if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) { | |
6ecbca5b | 486 | throw new API_Exception(ts('Please provide a custom value for %1 greeting', |
6a488035 TO |
487 | array(1 => $key) |
488 | )); | |
489 | } | |
490 | } | |
491 | elseif ($greetingVal) { | |
492 | ||
493 | if (!in_array($greetingVal, $greetings)) { | |
6ecbca5b | 494 | throw new API_Exception(ts('Invalid %1 greeting', array(1 => $key))); |
6a488035 TO |
495 | } |
496 | ||
497 | $greetingId = CRM_Utils_Array::key($greetingVal, $greetings); | |
498 | } | |
499 | ||
500 | if ($customGreeting) { | |
501 | $greetingId = CRM_Utils_Array::key('Customized', $greetings); | |
502 | } | |
503 | ||
35671d00 | 504 | $customValue = isset($params['contact_id']) ? CRM_Core_DAO::getFieldValue( |
ad3f841d DL |
505 | 'CRM_Contact_DAO_Contact', |
506 | $params['contact_id'], | |
507 | "{$key}{$greeting}_custom" | |
35671d00 | 508 | ) : FALSE; |
6a488035 TO |
509 | |
510 | if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) { | |
511 | $nullValue = TRUE; | |
512 | } | |
513 | elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) { | |
514 | $nullValue = TRUE; | |
515 | } | |
516 | elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params) | |
517 | && empty($params["{$key}{$greeting}_custom"]) | |
518 | ) { | |
519 | $nullValue = TRUE; | |
520 | } | |
521 | ||
522 | $params["{$key}{$greeting}_id"] = $greetingId; | |
523 | ||
524 | if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) { | |
525 | unset($params["{$key}{$greeting}_custom"]); | |
526 | } | |
527 | ||
528 | if ($nullValue) { | |
529 | $params["{$key}{$greeting}_id"] = ''; | |
530 | $params["{$key}{$greeting}_custom"] = ''; | |
531 | } | |
532 | ||
533 | if (isset($params["{$key}{$greeting}"])) { | |
534 | unset($params["{$key}{$greeting}"]); | |
535 | } | |
536 | } | |
537 | } | |
538 | ||
539 | /** | |
03f32517 | 540 | * Old contact quick search api |
6a488035 | 541 | * |
03f32517 | 542 | * @deprecated |
6a488035 TO |
543 | * |
544 | * {@example ContactGetquick.php 0} | |
d0997921 | 545 | * @param array $params |
645ee340 EM |
546 | * @return array |
547 | * @throws \API_Exception | |
6a488035 | 548 | */ |
6a488035 TO |
549 | function civicrm_api3_contact_getquick($params) { |
550 | civicrm_api3_verify_mandatory($params, NULL, array('name')); | |
aca85468 | 551 | $name = CRM_Utils_Type::escape(CRM_Utils_Array::value('name', $params), 'String'); |
6a488035 TO |
552 | |
553 | // get the autocomplete options from settings | |
554 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
555 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
556 | 'contact_autocomplete_options' | |
557 | ) | |
558 | ); | |
559 | ||
560 | // get the option values for contact autocomplete | |
561 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
562 | ||
563 | $list = array(); | |
564 | foreach ($acpref as $value) { | |
8cc574cf | 565 | if ($value && !empty($acOptions[$value])) { |
6a488035 TO |
566 | $list[$value] = $acOptions[$value]; |
567 | } | |
568 | } | |
569 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
570 | if (!empty($params['field_name'])) { | |
1aba4d9a | 571 | $field_name = CRM_Utils_String::munge($params['field_name']); |
5d8ba7a7 | 572 | // Unique name contact_id = id |
1aba4d9a CW |
573 | if ($field_name == 'contact_id') { |
574 | $field_name = 'id'; | |
5d8ba7a7 | 575 | } |
6a488035 | 576 | // phone_numeric should be phone |
1aba4d9a | 577 | $searchField = str_replace('_numeric', '', $field_name); |
22e263ad | 578 | if (!in_array($searchField, $list)) { |
6a488035 TO |
579 | $list[] = $searchField; |
580 | } | |
581 | } | |
582 | ||
583 | $select = $actualSelectElements = array('sort_name'); | |
584 | $where = ''; | |
585 | $from = array(); | |
586 | foreach ($list as $value) { | |
587 | $suffix = substr($value, 0, 2) . substr($value, -1); | |
588 | switch ($value) { | |
589 | case 'street_address': | |
590 | case 'city': | |
591 | case 'postal_code': | |
592 | $selectText = $value; | |
593 | $value = "address"; | |
594 | $suffix = 'sts'; | |
595 | case 'phone': | |
596 | case 'email': | |
597 | $actualSelectElements[] = $select[] = ($value == 'address') ? $selectText : $value; | |
aebdef3c JL |
598 | if ($value == 'phone') { |
599 | $actualSelectElements[] = $select[] = 'phone_ext'; | |
600 | } | |
6a488035 TO |
601 | $from[$value] = "LEFT JOIN civicrm_{$value} {$suffix} ON ( cc.id = {$suffix}.contact_id AND {$suffix}.is_primary = 1 ) "; |
602 | break; | |
603 | ||
604 | case 'country': | |
605 | case 'state_province': | |
606 | $select[] = "{$suffix}.name as {$value}"; | |
607 | $actualSelectElements[] = "{$suffix}.name"; | |
608 | if (!in_array('address', $from)) { | |
609 | $from['address'] = 'LEFT JOIN civicrm_address sts ON ( cc.id = sts.contact_id AND sts.is_primary = 1) '; | |
610 | } | |
611 | $from[$value] = " LEFT JOIN civicrm_{$value} {$suffix} ON ( sts.{$value}_id = {$suffix}.id ) "; | |
612 | break; | |
613 | ||
614 | default: | |
615 | if ($value != 'id') { | |
616 | $suffix = 'cc'; | |
617 | if (!empty($params['field_name']) && $params['field_name'] == 'value') { | |
133da98d | 618 | $suffix = CRM_Utils_String::munge(CRM_Utils_Array::value('table_name', $params, 'cc')); |
6a488035 TO |
619 | } |
620 | $actualSelectElements[] = $select[] = $suffix . '.' . $value; | |
621 | } | |
622 | break; | |
623 | } | |
624 | } | |
625 | ||
626 | $config = CRM_Core_Config::singleton(); | |
627 | $as = $select; | |
628 | $select = implode(', ', $select); | |
629 | if (!empty($select)) { | |
630 | $select = ", $select"; | |
631 | } | |
632 | $actualSelectElements = implode(', ', $actualSelectElements); | |
633 | $selectAliases = $from; | |
634 | unset($selectAliases['address']); | |
635 | $selectAliases = implode(', ', array_keys($selectAliases)); | |
636 | if (!empty($selectAliases)) { | |
637 | $selectAliases = ", $selectAliases"; | |
638 | } | |
639 | $from = implode(' ', $from); | |
133da98d CW |
640 | $limit = (int) CRM_Utils_Array::value('limit', $params); |
641 | $limit = $limit > 0 ? $limit : 10; | |
6a488035 TO |
642 | |
643 | // add acl clause here | |
644 | list($aclFrom, $aclWhere) = CRM_Contact_BAO_Contact_Permission::cacheClause('cc'); | |
645 | ||
646 | if ($aclWhere) { | |
647 | $where .= " AND $aclWhere "; | |
648 | } | |
649 | ||
a7488080 | 650 | if (!empty($params['org'])) { |
6a488035 TO |
651 | $where .= " AND contact_type = \"Organization\""; |
652 | ||
653 | // CRM-7157, hack: get current employer details when | |
654 | // employee_id is present. | |
655 | $currEmpDetails = array(); | |
a7488080 | 656 | if (!empty($params['employee_id'])) { |
6a488035 | 657 | if ($currentEmployer = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
133da98d | 658 | (int) $params['employee_id'], |
6a488035 TO |
659 | 'employer_id' |
660 | )) { | |
661 | if ($config->includeWildCardInName) { | |
662 | $strSearch = "%$name%"; | |
663 | } | |
664 | else { | |
665 | $strSearch = "$name%"; | |
666 | } | |
667 | ||
668 | // get current employer details | |
669 | $dao = CRM_Core_DAO::executeQuery("SELECT cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data, sort_name | |
670 | FROM civicrm_contact cc {$from} WHERE cc.contact_type = \"Organization\" AND cc.id = {$currentEmployer} AND cc.sort_name LIKE '$strSearch'"); | |
671 | if ($dao->fetch()) { | |
672 | $currEmpDetails = array( | |
673 | 'id' => $dao->id, | |
674 | 'data' => $dao->data, | |
675 | ); | |
676 | } | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
a7488080 | 681 | if (!empty($params['contact_sub_type'])) { |
69164898 N |
682 | $contactSubType = CRM_Utils_Type::escape($params['contact_sub_type'], 'String'); |
683 | $where .= " AND cc.contact_sub_type = '{$contactSubType}'"; | |
684 | } | |
685 | ||
e1b717cb P |
686 | if (!empty($params['contact_type'])) { |
687 | $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String'); | |
688 | $where .= " AND cc.contact_type LIKE '{$contactType}'"; | |
689 | } | |
690 | ||
6a488035 | 691 | //set default for current_employer or return contact with particular id |
a7488080 | 692 | if (!empty($params['id'])) { |
1aba4d9a | 693 | $where .= " AND cc.id = " . (int) $params['id']; |
6a488035 TO |
694 | } |
695 | ||
a7488080 | 696 | if (!empty($params['cid'])) { |
1aba4d9a | 697 | $where .= " AND cc.id <> " . (int) $params['cid']; |
6a488035 TO |
698 | } |
699 | ||
700 | //contact's based of relationhip type | |
701 | $relType = NULL; | |
a7488080 | 702 | if (!empty($params['rel'])) { |
6a488035 TO |
703 | $relation = explode('_', CRM_Utils_Array::value('rel', $params)); |
704 | $relType = CRM_Utils_Type::escape($relation[0], 'Integer'); | |
705 | $rel = CRM_Utils_Type::escape($relation[2], 'String'); | |
706 | } | |
707 | ||
708 | if ($config->includeWildCardInName) { | |
709 | $strSearch = "%$name%"; | |
710 | } | |
711 | else { | |
712 | $strSearch = "$name%"; | |
713 | } | |
714 | $includeEmailFrom = $includeNickName = $exactIncludeNickName = ''; | |
715 | if ($config->includeNickNameInName) { | |
716 | $includeNickName = " OR nick_name LIKE '$strSearch'"; | |
717 | $exactIncludeNickName = " OR nick_name LIKE '$name'"; | |
718 | } | |
719 | ||
720 | //CRM-10687 | |
721 | if (!empty($params['field_name']) && !empty($params['table_name'])) { | |
1aba4d9a | 722 | $table_name = CRM_Utils_String::munge($params['table_name']); |
8d3b1aa6 PC |
723 | $whereClause = " WHERE ( $table_name.$field_name LIKE '$strSearch') {$where}"; |
724 | $exactWhereClause = " WHERE ( $table_name.$field_name = '$name') {$where}"; | |
6a488035 TO |
725 | // Search by id should be exact |
726 | if ($field_name == 'id' || $field_name == 'external_identifier') { | |
727 | $whereClause = $exactWhereClause; | |
728 | } | |
729 | } | |
730 | else { | |
731 | if ($config->includeEmailInName) { | |
732 | if (!in_array('email', $list)) { | |
733 | $includeEmailFrom = "LEFT JOIN civicrm_email eml ON ( cc.id = eml.contact_id AND eml.is_primary = 1 )"; | |
734 | } | |
735 | $whereClause = " WHERE ( email LIKE '$strSearch' OR sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
736 | $exactWhereClause = " WHERE ( email LIKE '$name' OR sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
737 | } | |
738 | else { | |
739 | $whereClause = " WHERE ( sort_name LIKE '$strSearch' $includeNickName ) {$where} "; | |
740 | $exactWhereClause = " WHERE ( sort_name LIKE '$name' $exactIncludeNickName ) {$where} "; | |
741 | } | |
742 | } | |
743 | ||
744 | $additionalFrom = ''; | |
745 | if ($relType) { | |
746 | $additionalFrom = " | |
747 | INNER JOIN civicrm_relationship_type r ON ( | |
748 | r.id = {$relType} | |
749 | AND ( cc.contact_type = r.contact_type_{$rel} OR r.contact_type_{$rel} IS NULL ) | |
750 | AND ( cc.contact_sub_type = r.contact_sub_type_{$rel} OR r.contact_sub_type_{$rel} IS NULL ) | |
751 | )"; | |
752 | } | |
753 | ||
754 | // check if only CMS users are requested | |
a7488080 | 755 | if (!empty($params['cmsuser'])) { |
6a488035 TO |
756 | $additionalFrom = " |
757 | INNER JOIN civicrm_uf_match um ON (um.contact_id=cc.id) | |
758 | "; | |
759 | } | |
760 | ||
51e61eae ARW |
761 | $orderByInner = ""; |
762 | $orderByOuter = "ORDER BY exactFirst"; | |
763 | if ($config->includeOrderByClause) { | |
764 | $orderByInner = "ORDER BY sort_name"; | |
765 | $orderByOuter .= ", sort_name"; | |
766 | } | |
767 | ||
6a488035 TO |
768 | //CRM-5954 |
769 | $query = " | |
770 | SELECT DISTINCT(id), data, sort_name {$selectAliases} | |
771 | FROM ( | |
772 | ( SELECT 0 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
773 | FROM civicrm_contact cc {$from} | |
774 | {$aclFrom} | |
775 | {$additionalFrom} {$includeEmailFrom} | |
776 | {$exactWhereClause} | |
777 | LIMIT 0, {$limit} ) | |
778 | UNION | |
779 | ( SELECT 1 as exactFirst, cc.id as id, CONCAT_WS( ' :: ', {$actualSelectElements} ) as data {$select} | |
780 | FROM civicrm_contact cc {$from} | |
781 | {$aclFrom} | |
782 | {$additionalFrom} {$includeEmailFrom} | |
783 | {$whereClause} | |
51e61eae | 784 | {$orderByInner} |
6a488035 TO |
785 | LIMIT 0, {$limit} ) |
786 | ) t | |
51e61eae | 787 | {$orderByOuter} |
6a488035 TO |
788 | LIMIT 0, {$limit} |
789 | "; | |
790 | // send query to hook to be modified if needed | |
791 | CRM_Utils_Hook::contactListQuery($query, | |
792 | $name, | |
133da98d CW |
793 | empty($params['context']) ? NULL : CRM_Utils_Type::escape($params['context'], 'String'), |
794 | empty($params['id']) ? NULL : $params['id'] | |
6a488035 TO |
795 | ); |
796 | ||
797 | $dao = CRM_Core_DAO::executeQuery($query); | |
798 | ||
799 | $contactList = array(); | |
800 | $listCurrentEmployer = TRUE; | |
801 | while ($dao->fetch()) { | |
802 | $t = array('id' => $dao->id); | |
803 | foreach ($as as $k) { | |
35671d00 | 804 | $t[$k] = isset($dao->$k) ? $dao->$k : ''; |
6a488035 TO |
805 | } |
806 | $t['data'] = $dao->data; | |
807 | $contactList[] = $t; | |
a7488080 | 808 | if (!empty($params['org']) && |
6a488035 TO |
809 | !empty($currEmpDetails) && |
810 | $dao->id == $currEmpDetails['id'] | |
811 | ) { | |
812 | $listCurrentEmployer = FALSE; | |
813 | } | |
814 | } | |
815 | ||
816 | //return organization name if doesn't exist in db | |
817 | if (empty($contactList)) { | |
a7488080 | 818 | if (!empty($params['org'])) { |
6a488035 TO |
819 | if ($listCurrentEmployer && !empty($currEmpDetails)) { |
820 | $contactList = array( | |
821 | array( | |
822 | 'data' => $currEmpDetails['data'], | |
21dfd5f5 TO |
823 | 'id' => $currEmpDetails['id'], |
824 | ), | |
6a488035 TO |
825 | ); |
826 | } | |
827 | else { | |
828 | $contactList = array( | |
829 | array( | |
830 | 'data' => $name, | |
21dfd5f5 TO |
831 | 'id' => $name, |
832 | ), | |
6a488035 TO |
833 | ); |
834 | } | |
835 | } | |
836 | } | |
837 | ||
a14e9d08 CW |
838 | return civicrm_api3_create_success($contactList, $params, 'contact', 'getquick'); |
839 | } | |
840 | ||
841 | /** | |
842 | * @deprecated api notice | |
a6c01b45 | 843 | * @return array |
16b10e64 | 844 | * Array of deprecated actions |
a14e9d08 CW |
845 | */ |
846 | function _civicrm_api3_contact_deprecation() { | |
847 | return array('getquick' => 'The "getquick" action is deprecated in favor of "getlist".'); | |
6a488035 TO |
848 | } |
849 | ||
850 | /** | |
851 | * Merges given pair of duplicate contacts. | |
852 | * | |
cf470720 TO |
853 | * @param array $params |
854 | * Input parameters. | |
6a488035 TO |
855 | * |
856 | * Allowed @params array keys are: | |
857 | * {int main_id main contact id with whom merge has to happen} | |
858 | * {int other_id duplicate contact which would be deleted after merge operation} | |
859 | * {string mode helps decide how to behave when there are conflicts. | |
860 | * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.} | |
861 | * {boolean auto_flip wether to let api decide which contact to retain and which to delete.} | |
862 | * | |
a6c01b45 | 863 | * @return array |
72b3a70c | 864 | * API Result Array |
6a488035 | 865 | * |
6a488035 TO |
866 | */ |
867 | function civicrm_api3_contact_merge($params) { | |
868 | $mode = CRM_Utils_Array::value('mode', $params, 'safe'); | |
869 | $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); | |
870 | ||
35671d00 TO |
871 | $dupePairs = array(array( |
872 | 'srcID' => CRM_Utils_Array::value('main_id', $params), | |
6a488035 TO |
873 | 'dstID' => CRM_Utils_Array::value('other_id', $params), |
874 | )); | |
875 | $result = CRM_Dedupe_Merger::merge($dupePairs, array(), $mode, $autoFlip); | |
876 | ||
877 | if ($result['is_error'] == 0) { | |
878 | return civicrm_api3_create_success(); | |
879 | } | |
880 | else { | |
881 | return civicrm_api3_create_error($result['messages']); | |
882 | } | |
883 | } | |
884 | ||
aa1b1481 | 885 | /** |
c490a46a | 886 | * @param array $params |
aa1b1481 | 887 | */ |
6a488035 TO |
888 | function _civicrm_api3_contact_proximity_spec(&$params) { |
889 | $params['latitude']['api.required'] = 1; | |
4c41ecb2 | 890 | $params['latitude']['title'] = 'Latitude'; |
6a488035 | 891 | $params['longitude']['api.required'] = 1; |
4c41ecb2 | 892 | $params['longitude']['title'] = 'Longitude'; |
6a488035 | 893 | $params['unit']['api.default'] = 'meter'; |
4c41ecb2 | 894 | $params['unit']['title'] = 'Unit of Measurement'; |
6a488035 TO |
895 | } |
896 | ||
aa1b1481 | 897 | /** |
c490a46a | 898 | * @param array $params |
aa1b1481 EM |
899 | * |
900 | * @return array | |
901 | * @throws Exception | |
902 | */ | |
6a488035 TO |
903 | function civicrm_api3_contact_proximity($params) { |
904 | $latitude = CRM_Utils_Array::value('latitude', $params); | |
905 | $longitude = CRM_Utils_Array::value('longitude', $params); | |
906 | $distance = CRM_Utils_Array::value('distance', $params); | |
907 | ||
908 | $unit = CRM_Utils_Array::value('unit', $params); | |
909 | ||
910 | // check and ensure that lat/long and distance are floats | |
911 | if ( | |
912 | !CRM_Utils_Rule::numeric($latitude) || | |
913 | !CRM_Utils_Rule::numeric($longitude) || | |
914 | !CRM_Utils_Rule::numeric($distance) | |
915 | ) { | |
916 | throw new Exception(ts('Latitude, Longitude and Distance should exist and be numeric')); | |
917 | } | |
918 | ||
919 | if ($unit == "mile") { | |
920 | $conversionFactor = 1609.344; | |
921 | } | |
922 | else { | |
923 | $conversionFactor = 1000; | |
924 | } | |
925 | //Distance in meters | |
926 | $distance = $distance * $conversionFactor; | |
927 | ||
928 | $whereClause = CRM_Contact_BAO_ProximityQuery::where($latitude, $longitude, $distance); | |
929 | ||
930 | $query = " | |
931 | SELECT civicrm_contact.id as contact_id, | |
932 | civicrm_contact.display_name as display_name | |
933 | FROM civicrm_contact | |
934 | LEFT JOIN civicrm_address ON civicrm_contact.id = civicrm_address.contact_id | |
935 | WHERE $whereClause | |
936 | "; | |
937 | ||
938 | $dao = CRM_Core_DAO::executeQuery($query); | |
939 | $contacts = array(); | |
940 | while ($dao->fetch()) { | |
941 | $contacts[] = $dao->toArray(); | |
942 | } | |
943 | ||
944 | return civicrm_api3_create_success($contacts, $params, 'contact', 'get_by_location', $dao); | |
945 | } | |
946 | ||
ff88d165 CW |
947 | |
948 | /** | |
a6c6059d | 949 | * @see _civicrm_api3_generic_getlist_params |
ff88d165 | 950 | * |
cf470720 TO |
951 | * @param $request |
952 | * Array. | |
ff88d165 CW |
953 | */ |
954 | function _civicrm_api3_contact_getlist_params(&$request) { | |
955 | // get the autocomplete options from settings | |
956 | $acpref = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
957 | CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
958 | 'contact_autocomplete_options' | |
959 | ) | |
960 | ); | |
961 | ||
962 | // get the option values for contact autocomplete | |
963 | $acOptions = CRM_Core_OptionGroup::values('contact_autocomplete_options', FALSE, FALSE, FALSE, NULL, 'name'); | |
964 | ||
965 | $list = array(); | |
966 | foreach ($acpref as $value) { | |
967 | if ($value && !empty($acOptions[$value])) { | |
968 | $list[] = $acOptions[$value]; | |
969 | } | |
970 | } | |
971 | // If we are doing quicksearch by a field other than name, make sure that field is added to results | |
972 | $field_name = CRM_Utils_String::munge($request['search_field']); | |
973 | // Unique name contact_id = id | |
974 | if ($field_name == 'contact_id') { | |
975 | $field_name = 'id'; | |
976 | } | |
977 | // phone_numeric should be phone | |
978 | $searchField = str_replace('_numeric', '', $field_name); | |
22e263ad | 979 | if (!in_array($searchField, $list)) { |
ff88d165 CW |
980 | $list[] = $searchField; |
981 | } | |
8250601e | 982 | $request['description_field'] = $list; |
54bee7df | 983 | $list[] = 'contact_type'; |
8250601e | 984 | $request['params']['return'] = array_unique(array_merge($list, $request['extra'])); |
ff88d165 CW |
985 | $request['params']['options']['sort'] = 'sort_name'; |
986 | // Contact api doesn't support array(LIKE => 'foo') syntax | |
609a8c53 | 987 | if (!empty($request['input'])) { |
fd816db5 | 988 | $request['params'][$request['search_field']] = $request['input']; |
609a8c53 | 989 | } |
ff88d165 CW |
990 | } |
991 | ||
992 | /** | |
a6c6059d | 993 | * @see _civicrm_api3_generic_getlist_output |
ff88d165 | 994 | * |
cf470720 TO |
995 | * @param $result |
996 | * Array. | |
997 | * @param $request | |
998 | * Array. | |
ff88d165 CW |
999 | * |
1000 | * @return array | |
1001 | */ | |
1002 | function _civicrm_api3_contact_getlist_output($result, $request) { | |
1003 | $output = array(); | |
1004 | if (!empty($result['values'])) { | |
88881f79 | 1005 | $addressFields = array_intersect(array('street_address', 'city', 'state_province', 'country'), $request['params']['return']); |
ff88d165 CW |
1006 | foreach ($result['values'] as $row) { |
1007 | $data = array( | |
1008 | 'id' => $row[$request['id_field']], | |
1009 | 'label' => $row[$request['label_field']], | |
88881f79 | 1010 | 'description' => array(), |
ff88d165 | 1011 | ); |
8250601e CW |
1012 | foreach ($request['description_field'] as $item) { |
1013 | if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) { | |
88881f79 | 1014 | $data['description'][] = $row[$item]; |
ff88d165 CW |
1015 | } |
1016 | } | |
88881f79 | 1017 | $address = array(); |
22e263ad | 1018 | foreach ($addressFields as $item) { |
88881f79 CW |
1019 | if (!empty($row[$item])) { |
1020 | $address[] = $row[$item]; | |
1021 | } | |
1022 | } | |
1023 | if ($address) { | |
1024 | $data['description'][] = implode(' ', $address); | |
1025 | } | |
ff88d165 CW |
1026 | if (!empty($request['image_field'])) { |
1027 | $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : ''; | |
54bee7df CW |
1028 | } |
1029 | else { | |
1030 | $data['icon_class'] = $row['contact_type']; | |
1031 | } | |
8250601e CW |
1032 | foreach ($request['extra'] as $field) { |
1033 | $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL; | |
1034 | } | |
ff88d165 CW |
1035 | $output[] = $data; |
1036 | } | |
1037 | } | |
1038 | return $output; | |
1039 | } |