3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * These functions have been deprecated out of API v3 Utils folder as they are not part of the
20 * API. Calling API functions directly is not supported & these functions are not called by any
21 * part of the API so are not really part of the api
25 require_once 'api/v3/utils.php';
28 * This function adds the contact variable in $values to the
29 * parameter list $params. For most cases, $values should have length 1. If
30 * the variable being added is a child of Location, a location_type_id must
31 * also be included. If it is a child of phone, a phone_type must be included.
33 * @param array $values
34 * The variable(s) to be added.
35 * @param array $params
36 * The structured parameter list.
38 * @return bool|CRM_Utils_Error
40 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
41 // Crawl through the possible classes:
54 // Cache the various object fields
55 static $fields = NULL;
57 if ($fields == NULL) {
61 // first add core contact values since for other Civi modules they are not added
62 require_once 'CRM/Contact/BAO/Contact.php';
63 $contactFields = CRM_Contact_DAO_Contact
::fields();
64 _civicrm_api3_store_values($contactFields, $values, $params);
66 if (isset($values['contact_type'])) {
67 // we're an individual/household/org property
69 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
71 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
75 if (isset($values['individual_prefix'])) {
76 if (!empty($params['prefix_id'])) {
77 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
78 $params['prefix'] = $prefixes[$params['prefix_id']];
81 $params['prefix'] = $values['individual_prefix'];
86 if (isset($values['individual_suffix'])) {
87 if (!empty($params['suffix_id'])) {
88 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
89 $params['suffix'] = $suffixes[$params['suffix_id']];
92 $params['suffix'] = $values['individual_suffix'];
98 if (isset($values['email_greeting'])) {
99 if (!empty($params['email_greeting_id'])) {
100 $emailGreetingFilter = [
101 'contact_type' => $params['contact_type'] ??
NULL,
102 'greeting_type' => 'email_greeting',
104 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
105 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
108 $params['email_greeting'] = $values['email_greeting'];
114 if (isset($values['postal_greeting'])) {
115 if (!empty($params['postal_greeting_id'])) {
116 $postalGreetingFilter = [
117 'contact_type' => $params['contact_type'] ??
NULL,
118 'greeting_type' => 'postal_greeting',
120 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
121 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
124 $params['postal_greeting'] = $values['postal_greeting'];
129 if (isset($values['addressee'])) {
130 $params['addressee'] = $values['addressee'];
134 if (isset($values['gender'])) {
135 if (!empty($params['gender_id'])) {
136 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
137 $params['gender'] = $genders[$params['gender_id']];
140 $params['gender'] = $values['gender'];
145 if (!empty($values['preferred_communication_method'])) {
147 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
149 $preffComm = explode(',', $values['preferred_communication_method']);
150 foreach ($preffComm as $v) {
151 $v = strtolower(trim($v));
152 if (array_key_exists($v, $pcm)) {
157 $params['preferred_communication_method'] = $comm;
161 // format the website params.
162 if (!empty($values['url'])) {
163 static $websiteFields;
164 if (!is_array($websiteFields)) {
165 require_once 'CRM/Core/DAO/Website.php';
166 $websiteFields = CRM_Core_DAO_Website
::fields();
168 if (!array_key_exists('website', $params) ||
169 !is_array($params['website'])
171 $params['website'] = [];
174 $websiteCount = count($params['website']);
175 _civicrm_api3_store_values($websiteFields, $values,
176 $params['website'][++
$websiteCount]
182 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
183 if (!empty($values['location_type_id'])) {
184 static $fields = NULL;
185 if ($fields == NULL) {
196 $name = strtolower($block);
197 if (!array_key_exists($name, $values)) {
201 if ($name == 'phone_ext') {
205 // block present in value array.
206 if (!array_key_exists($name, $params) ||
!is_array($params[$name])) {
210 if (!array_key_exists($block, $fields)) {
211 $className = "CRM_Core_DAO_$block";
212 $fields[$block] =& $className::fields();
215 $blockCnt = count($params[$name]);
217 // copy value to dao field name.
219 $values['name'] = $values[$name];
222 _civicrm_api3_store_values($fields[$block], $values,
223 $params[$name][++
$blockCnt]
226 if (empty($params['id']) && ($blockCnt == 1)) {
227 $params[$name][$blockCnt]['is_primary'] = TRUE;
230 // we only process single block at a time.
234 // handle address fields.
235 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
236 $params['address'] = [];
240 foreach ($params['address'] as $cnt => $addressBlock) {
241 if (CRM_Utils_Array
::value('location_type_id', $values) ==
242 CRM_Utils_Array
::value('location_type_id', $addressBlock)
250 if (!array_key_exists('Address', $fields)) {
251 $fields['Address'] = CRM_Core_DAO_Address
::fields();
254 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
255 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
256 // the address in CRM_Core_BAO_Address::create method
257 if (!empty($values['location_type_id'])) {
258 static $customFields = [];
259 if (empty($customFields)) {
260 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
262 // make a copy of values, as we going to make changes
263 $newValues = $values;
264 foreach ($values as $key => $val) {
265 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
266 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
267 // mark an entry in fields array since we want the value of custom field to be copied
268 $fields['Address'][$key] = NULL;
270 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
271 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]) && $val) {
272 $mulValues = explode(',', $val);
273 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
274 $newValues[$key] = [];
275 foreach ($mulValues as $v1) {
276 foreach ($customOption as $v2) {
277 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
278 (strtolower($v2['value']) == strtolower(trim($v1)))
280 if ($htmlType == 'CheckBox') {
281 $newValues[$key][$v2['value']] = 1;
284 $newValues[$key][] = $v2['value'];
292 // consider new values
293 $values = $newValues;
296 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
302 'supplemental_address_1',
303 'supplemental_address_2',
304 'supplemental_address_3',
305 'StateProvince.name',
308 foreach ($addressFields as $field) {
309 if (array_key_exists($field, $values)) {
310 if (!array_key_exists('address', $params)) {
311 $params['address'] = [];
313 $params['address'][$addressCnt][$field] = $values[$field];
317 if ($addressCnt == 1) {
319 $params['address'][$addressCnt]['is_primary'] = TRUE;
324 if (isset($values['note'])) {
326 if (!isset($params['note'])) {
327 $params['note'] = [];
329 $noteBlock = count($params['note']) +
1;
331 $params['note'][$noteBlock] = [];
332 if (!isset($fields['Note'])) {
333 $fields['Note'] = CRM_Core_DAO_Note
::fields();
336 // get the current logged in civicrm user
337 $session = CRM_Core_Session
::singleton();
338 $userID = $session->get('userID');
341 $values['contact_id'] = $userID;
344 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
349 // Check for custom field values
351 if (empty($fields['custom'])) {
352 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
353 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
357 foreach ($values as $key => $value) {
358 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
359 // check if it's a valid custom field id
361 if (!array_key_exists($customFieldID, $fields['custom'])) {
362 return civicrm_api3_create_error('Invalid custom field ID');
365 $params[$key] = $value;
373 * @param array $params
378 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
379 $id = $params['id'] ??
NULL;
380 $externalId = $params['external_identifier'] ??
NULL;
381 if ($id ||
$externalId) {
382 $contact = new CRM_Contact_DAO_Contact();
385 $contact->external_identifier
= $externalId;
387 if ($contact->find(TRUE)) {
388 if ($params['contact_type'] != $contact->contact_type
) {
389 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
392 $error = CRM_Core_Error
::createError("Found matching contacts: $contact->id",
393 CRM_Core_Error
::DUPLICATE_CONTACT
,
394 'Fatal', $contact->id
396 return civicrm_api3_create_error($error->pop());
400 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised');
403 $ids = implode(',', $ids);
404 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
405 CRM_Core_Error
::DUPLICATE_CONTACT
,
408 return civicrm_api3_create_error($error->pop());
411 return civicrm_api3_create_success(TRUE);