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 * Check duplicate contacts based on de-dupe parameters.
30 * @param array $params
34 function _civicrm_api3_deprecated_check_contact_dedupe($params) {
35 static $cIndieFields = NULL;
36 static $defaultLocationId = NULL;
38 $contactType = $params['contact_type'];
39 if ($cIndieFields == NULL) {
40 require_once 'CRM/Contact/BAO/Contact.php';
41 $cTempIndieFields = CRM_Contact_BAO_Contact
::importableFields($contactType);
42 $cIndieFields = $cTempIndieFields;
44 require_once "CRM/Core/BAO/LocationType.php";
45 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
47 // set the value to default location id else set to 1
48 if (!$defaultLocationId = (int) $defaultLocation->id
) {
49 $defaultLocationId = 1;
53 require_once 'CRM/Contact/BAO/Query.php';
54 $locationFields = CRM_Contact_BAO_Query
::$_locationSpecificFields;
56 $contactFormatted = [];
57 foreach ($params as $key => $field) {
58 if ($field == NULL ||
$field === '') {
61 // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
62 // instead of soft credit contact.
63 if (is_array($field) && $key != "soft_credit") {
64 foreach ($field as $value) {
66 if (is_array($value)) {
67 foreach ($value as $name => $testForEmpty) {
68 if ($name !== 'phone_type' &&
69 ($testForEmpty === '' ||
$testForEmpty == NULL)
80 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
86 $value = [$key => $field];
88 // check if location related field, then we need to add primary location type
89 if (in_array($key, $locationFields)) {
90 $value['location_type_id'] = $defaultLocationId;
92 elseif (array_key_exists($key, $cIndieFields)) {
93 $value['contact_type'] = $contactType;
96 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
99 $contactFormatted['contact_type'] = $contactType;
101 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
105 * take the input parameter list as specified in the data model and
106 * convert it into the same format that we use in QF and BAO object
108 * @param array $params
109 * Associative array of property name/value.
110 * pairs to insert in new contact.
111 * @param array $values
112 * The reformatted properties that we can use internally.
114 * @param array|bool $create Is the formatted Values array going to
115 * be used for CRM_Activity_BAO_Activity::create()
117 * @return array|CRM_Error
119 function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
120 // copy all the activity fields as is
121 $fields = CRM_Activity_DAO_Activity
::fields();
122 _civicrm_api3_store_values($fields, $params, $values);
124 require_once 'CRM/Core/OptionGroup.php';
125 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
127 foreach ($params as $key => $value) {
128 // ignore empty values or empty arrays etc
129 if (CRM_Utils_System
::isNull($value)) {
133 //Handling Custom Data
134 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
135 $values[$key] = $value;
136 $type = $customFields[$customFieldID]['html_type'];
137 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID])) {
138 $values[$key] = CRM_Import_Parser
::unserializeCustomValue($customFieldID, $value, $type);
140 elseif ($type == 'Select' ||
$type == 'Radio') {
141 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
142 foreach ($customOption as $customFldID => $customValue) {
143 $val = $customValue['value'] ??
NULL;
144 $label = $customValue['label'] ??
NULL;
145 $label = strtolower($label);
146 $value = strtolower(trim($value));
147 if (($value == $label) ||
($value == strtolower($val))) {
148 $values[$key] = $val;
154 if ($key == 'target_contact_id') {
155 if (!CRM_Utils_Rule
::integer($value)) {
156 return civicrm_api3_create_error("contact_id not valid: $value");
158 $contactID = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
160 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
168 * This function adds the contact variable in $values to the
169 * parameter list $params. For most cases, $values should have length 1. If
170 * the variable being added is a child of Location, a location_type_id must
171 * also be included. If it is a child of phone, a phone_type must be included.
173 * @param array $values
174 * The variable(s) to be added.
175 * @param array $params
176 * The structured parameter list.
178 * @return bool|CRM_Utils_Error
180 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
181 // Crawl through the possible classes:
194 // Cache the various object fields
195 static $fields = NULL;
197 if ($fields == NULL) {
201 // first add core contact values since for other Civi modules they are not added
202 require_once 'CRM/Contact/BAO/Contact.php';
203 $contactFields = CRM_Contact_DAO_Contact
::fields();
204 _civicrm_api3_store_values($contactFields, $values, $params);
206 if (isset($values['contact_type'])) {
207 // we're an individual/household/org property
209 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
211 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
215 if (isset($values['individual_prefix'])) {
216 if (!empty($params['prefix_id'])) {
217 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
218 $params['prefix'] = $prefixes[$params['prefix_id']];
221 $params['prefix'] = $values['individual_prefix'];
226 if (isset($values['individual_suffix'])) {
227 if (!empty($params['suffix_id'])) {
228 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
229 $params['suffix'] = $suffixes[$params['suffix_id']];
232 $params['suffix'] = $values['individual_suffix'];
238 if (isset($values['email_greeting'])) {
239 if (!empty($params['email_greeting_id'])) {
240 $emailGreetingFilter = [
241 'contact_type' => $params['contact_type'] ??
NULL,
242 'greeting_type' => 'email_greeting',
244 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
245 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
248 $params['email_greeting'] = $values['email_greeting'];
254 if (isset($values['postal_greeting'])) {
255 if (!empty($params['postal_greeting_id'])) {
256 $postalGreetingFilter = [
257 'contact_type' => $params['contact_type'] ??
NULL,
258 'greeting_type' => 'postal_greeting',
260 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
261 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
264 $params['postal_greeting'] = $values['postal_greeting'];
269 if (isset($values['addressee'])) {
270 $params['addressee'] = $values['addressee'];
274 if (isset($values['gender'])) {
275 if (!empty($params['gender_id'])) {
276 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
277 $params['gender'] = $genders[$params['gender_id']];
280 $params['gender'] = $values['gender'];
285 if (!empty($values['preferred_communication_method'])) {
287 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
289 $preffComm = explode(',', $values['preferred_communication_method']);
290 foreach ($preffComm as $v) {
291 $v = strtolower(trim($v));
292 if (array_key_exists($v, $pcm)) {
297 $params['preferred_communication_method'] = $comm;
301 // format the website params.
302 if (!empty($values['url'])) {
303 static $websiteFields;
304 if (!is_array($websiteFields)) {
305 require_once 'CRM/Core/DAO/Website.php';
306 $websiteFields = CRM_Core_DAO_Website
::fields();
308 if (!array_key_exists('website', $params) ||
309 !is_array($params['website'])
311 $params['website'] = [];
314 $websiteCount = count($params['website']);
315 _civicrm_api3_store_values($websiteFields, $values,
316 $params['website'][++
$websiteCount]
322 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
323 if (!empty($values['location_type_id'])) {
324 static $fields = NULL;
325 if ($fields == NULL) {
336 $name = strtolower($block);
337 if (!array_key_exists($name, $values)) {
341 if ($name == 'phone_ext') {
345 // block present in value array.
346 if (!array_key_exists($name, $params) ||
!is_array($params[$name])) {
350 if (!array_key_exists($block, $fields)) {
351 $className = "CRM_Core_DAO_$block";
352 $fields[$block] =& $className::fields();
355 $blockCnt = count($params[$name]);
357 // copy value to dao field name.
359 $values['name'] = $values[$name];
362 _civicrm_api3_store_values($fields[$block], $values,
363 $params[$name][++
$blockCnt]
366 if (empty($params['id']) && ($blockCnt == 1)) {
367 $params[$name][$blockCnt]['is_primary'] = TRUE;
370 // we only process single block at a time.
374 // handle address fields.
375 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
376 $params['address'] = [];
380 foreach ($params['address'] as $cnt => $addressBlock) {
381 if (CRM_Utils_Array
::value('location_type_id', $values) ==
382 CRM_Utils_Array
::value('location_type_id', $addressBlock)
390 if (!array_key_exists('Address', $fields)) {
391 $fields['Address'] = CRM_Core_DAO_Address
::fields();
394 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
395 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
396 // the address in CRM_Core_BAO_Address::create method
397 if (!empty($values['location_type_id'])) {
398 static $customFields = [];
399 if (empty($customFields)) {
400 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
402 // make a copy of values, as we going to make changes
403 $newValues = $values;
404 foreach ($values as $key => $val) {
405 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
406 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
407 // mark an entry in fields array since we want the value of custom field to be copied
408 $fields['Address'][$key] = NULL;
410 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
411 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]) && $val) {
412 $mulValues = explode(',', $val);
413 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
414 $newValues[$key] = [];
415 foreach ($mulValues as $v1) {
416 foreach ($customOption as $v2) {
417 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
418 (strtolower($v2['value']) == strtolower(trim($v1)))
420 if ($htmlType == 'CheckBox') {
421 $newValues[$key][$v2['value']] = 1;
424 $newValues[$key][] = $v2['value'];
432 // consider new values
433 $values = $newValues;
436 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
442 'supplemental_address_1',
443 'supplemental_address_2',
444 'supplemental_address_3',
445 'StateProvince.name',
448 foreach ($addressFields as $field) {
449 if (array_key_exists($field, $values)) {
450 if (!array_key_exists('address', $params)) {
451 $params['address'] = [];
453 $params['address'][$addressCnt][$field] = $values[$field];
457 if ($addressCnt == 1) {
459 $params['address'][$addressCnt]['is_primary'] = TRUE;
464 if (isset($values['note'])) {
466 if (!isset($params['note'])) {
467 $params['note'] = [];
469 $noteBlock = count($params['note']) +
1;
471 $params['note'][$noteBlock] = [];
472 if (!isset($fields['Note'])) {
473 $fields['Note'] = CRM_Core_DAO_Note
::fields();
476 // get the current logged in civicrm user
477 $session = CRM_Core_Session
::singleton();
478 $userID = $session->get('userID');
481 $values['contact_id'] = $userID;
484 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
489 // Check for custom field values
491 if (empty($fields['custom'])) {
492 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
493 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
497 foreach ($values as $key => $value) {
498 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
499 // check if it's a valid custom field id
501 if (!array_key_exists($customFieldID, $fields['custom'])) {
502 return civicrm_api3_create_error('Invalid custom field ID');
505 $params[$key] = $value;
513 * @param array $params
518 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
519 $id = $params['id'] ??
NULL;
520 $externalId = $params['external_identifier'] ??
NULL;
521 if ($id ||
$externalId) {
522 $contact = new CRM_Contact_DAO_Contact();
525 $contact->external_identifier
= $externalId;
527 if ($contact->find(TRUE)) {
528 if ($params['contact_type'] != $contact->contact_type
) {
529 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
532 $error = CRM_Core_Error
::createError("Found matching contacts: $contact->id",
533 CRM_Core_Error
::DUPLICATE_CONTACT
,
534 'Fatal', $contact->id
536 return civicrm_api3_create_error($error->pop());
540 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised');
543 $ids = implode(',', $ids);
544 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
545 CRM_Core_Error
::DUPLICATE_CONTACT
,
548 return civicrm_api3_create_error($error->pop());
551 return civicrm_api3_create_success(TRUE);
555 * @deprecated - this is part of the import parser not the API & needs to be moved on out
557 * @param array $params
558 * @param $onDuplicate
563 function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
564 require_once 'CRM/Event/Import/Parser.php';
565 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
566 CRM_Core_Error
::reset();
567 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
568 if (civicrm_error($error)) {
572 require_once "api/v3/Participant.php";
573 return civicrm_api3_participant_create($params);
578 * @param array $params
580 * @param bool $checkDuplicate
585 function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
587 // check if participant id is valid or not
588 if (!empty($params['id'])) {
589 $participant = new CRM_Event_BAO_Participant();
590 $participant->id
= $params['id'];
591 if (!$participant->find(TRUE)) {
592 return civicrm_api3_create_error(ts('Participant id is not valid'));
595 require_once 'CRM/Contact/BAO/Contact.php';
596 // check if contact id is valid or not
597 if (!empty($params['contact_id'])) {
598 $contact = new CRM_Contact_BAO_Contact();
599 $contact->id
= $params['contact_id'];
600 if (!$contact->find(TRUE)) {
601 return civicrm_api3_create_error(ts('Contact id is not valid'));
605 // check that event id is not an template
606 if (!empty($params['event_id'])) {
607 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
608 if (!empty($isTemplate)) {
609 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
614 if ($checkDuplicate) {
615 if (CRM_Event_BAO_Participant
::checkDuplicate($params, $result)) {
616 $participantID = array_pop($result);
618 $error = CRM_Core_Error
::createError("Found matching participant record.",
619 CRM_Core_Error
::DUPLICATE_PARTICIPANT
,
620 'Fatal', $participantID
623 return civicrm_api3_create_error($error->pop(),
625 'contactID' => $params['contact_id'],
626 'participantID' => $participantID,
635 * @param array $params
636 * @param bool $dupeCheck
637 * @param null|int $dedupeRuleGroupID
639 * @throws \CRM_Core_Exception
641 function _civicrm_api3_deprecated_contact_check_params(
644 $dedupeRuleGroupID = NULL) {
646 $requiredCheck = TRUE;
648 if (isset($params['id']) && is_numeric($params['id'])) {
649 $requiredCheck = FALSE;
651 if ($requiredCheck) {
652 if (isset($params['id'])) {
653 $required = ['Individual', 'Household', 'Organization'];
657 ['first_name', 'last_name'],
668 // contact_type has a limited number of valid values
669 if (empty($params['contact_type'])) {
670 throw new CRM_Core_Exception("No Contact Type");
672 $fields = $required[$params['contact_type']] ??
NULL;
673 if ($fields == NULL) {
674 throw new CRM_Core_Exception("Invalid Contact Type: {$params['contact_type']}");
677 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
678 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
679 throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
683 if (empty($params['contact_id']) && !empty($params['id'])) {
686 foreach ($fields as $field) {
687 if (is_array($field)) {
689 foreach ($field as $element) {
690 if (empty($params[$element])) {
698 if (!empty($params[$field])) {
708 throw new CRM_Core_Exception("Required fields not found for {$params['contact_type']} : $error");
714 // @todo switch to using api version
715 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
716 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
717 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array
::value('check_permissions', $params), $dedupeRuleGroupID);
719 $error = CRM_Core_Error
::createError("Found matching contacts: " . implode(',', $ids),
720 CRM_Core_Error
::DUPLICATE_CONTACT
,
723 return civicrm_api3_create_error($error->pop());
727 // check for organisations with same name
728 if (!empty($params['current_employer'])) {
729 $organizationParams = ['organization_name' => $params['current_employer']];
730 $dupeIds = CRM_Contact_BAO_Contact
::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
732 // check for mismatch employer name and id
733 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
735 throw new CRM_Core_Exception('Employer name and Employer id Mismatch');
738 // show error if multiple organisation with same name exist
739 if (empty($params['employer_id']) && (count($dupeIds) > 1)
741 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
748 * @param int $activityTypeID
753 function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
754 // get ready for collecting data about activity to be created
757 $params['activity_type_id'] = $activityTypeID;
759 $params['status_id'] = 'Completed';
760 if (!empty($result['from']['id'])) {
761 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
763 $params['target_contact_id'] = [];
764 $keys = ['to', 'cc', 'bcc'];
765 foreach ($keys as $key) {
766 if (is_array($result[$key])) {
767 foreach ($result[$key] as $key => $keyValue) {
768 if (!empty($keyValue['id'])) {
769 $params['target_contact_id'][] = $keyValue['id'];
774 $params['subject'] = $result['subject'];
775 $params['activity_date_time'] = $result['date'];
776 $params['details'] = $result['body'];
778 $numAttachments = Civi
::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File
::DEFAULT_MAX_ATTACHMENTS_BACKEND
;
779 for ($i = 1; $i <= $numAttachments; $i++
) {
780 if (isset($result["attachFile_$i"])) {
781 $params["attachFile_$i"] = $result["attachFile_$i"];
784 // No point looping 100 times if there's only one attachment