3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * These functions have been deprecated out of API v3 Utils folder as they are not part of the
36 * API. Calling API functions directly is not supported & these functions are not called by any
37 * part of the API so are not really part of the api
41 require_once 'api/v3/utils.php';
44 * Check duplicate contacts based on de-dupe parameters.
46 * @param array $params
50 function _civicrm_api3_deprecated_check_contact_dedupe($params) {
51 static $cIndieFields = NULL;
52 static $defaultLocationId = NULL;
54 $contactType = $params['contact_type'];
55 if ($cIndieFields == NULL) {
56 require_once 'CRM/Contact/BAO/Contact.php';
57 $cTempIndieFields = CRM_Contact_BAO_Contact
::importableFields($contactType);
58 $cIndieFields = $cTempIndieFields;
60 require_once "CRM/Core/BAO/LocationType.php";
61 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
63 // set the value to default location id else set to 1
64 if (!$defaultLocationId = (int) $defaultLocation->id
) {
65 $defaultLocationId = 1;
69 require_once 'CRM/Contact/BAO/Query.php';
70 $locationFields = CRM_Contact_BAO_Query
::$_locationSpecificFields;
72 $contactFormatted = [];
73 foreach ($params as $key => $field) {
74 if ($field == NULL ||
$field === '') {
77 // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
78 // instead of soft credit contact.
79 if (is_array($field) && $key != "soft_credit") {
80 foreach ($field as $value) {
82 if (is_array($value)) {
83 foreach ($value as $name => $testForEmpty) {
84 if ($name !== 'phone_type' &&
85 ($testForEmpty === '' ||
$testForEmpty == NULL)
96 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
102 $value = [$key => $field];
104 // check if location related field, then we need to add primary location type
105 if (in_array($key, $locationFields)) {
106 $value['location_type_id'] = $defaultLocationId;
108 elseif (array_key_exists($key, $cIndieFields)) {
109 $value['contact_type'] = $contactType;
112 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
115 $contactFormatted['contact_type'] = $contactType;
117 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
121 * take the input parameter list as specified in the data model and
122 * convert it into the same format that we use in QF and BAO object
124 * @param array $params
125 * Associative array of property name/value.
126 * pairs to insert in new contact.
127 * @param array $values
128 * The reformatted properties that we can use internally.
130 * @param array|bool $create Is the formatted Values array going to
131 * be used for CRM_Activity_BAO_Activity::create()
133 * @return array|CRM_Error
135 function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
136 // copy all the activity fields as is
137 $fields = CRM_Activity_DAO_Activity
::fields();
138 _civicrm_api3_store_values($fields, $params, $values);
140 require_once 'CRM/Core/OptionGroup.php';
141 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
143 foreach ($params as $key => $value) {
144 // ignore empty values or empty arrays etc
145 if (CRM_Utils_System
::isNull($value)) {
149 //Handling Custom Data
150 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
151 $values[$key] = $value;
152 $type = $customFields[$customFieldID]['html_type'];
153 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
154 $mulValues = explode(',', $value);
155 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
157 foreach ($mulValues as $v1) {
158 foreach ($customOption as $customValueID => $customLabel) {
159 $customValue = $customLabel['value'];
160 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
161 (strtolower(trim($customValue)) == strtolower(trim($v1)))
163 if ($type == 'CheckBox') {
164 $values[$key][$customValue] = 1;
167 $values[$key][] = $customValue;
173 elseif ($type == 'Select' ||
$type == 'Radio') {
174 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
175 foreach ($customOption as $customFldID => $customValue) {
176 $val = CRM_Utils_Array
::value('value', $customValue);
177 $label = CRM_Utils_Array
::value('label', $customValue);
178 $label = strtolower($label);
179 $value = strtolower(trim($value));
180 if (($value == $label) ||
($value == strtolower($val))) {
181 $values[$key] = $val;
187 if ($key == 'target_contact_id') {
188 if (!CRM_Utils_Rule
::integer($value)) {
189 return civicrm_api3_create_error("contact_id not valid: $value");
191 $contactID = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
193 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
201 * This function adds the contact variable in $values to the
202 * parameter list $params. For most cases, $values should have length 1. If
203 * the variable being added is a child of Location, a location_type_id must
204 * also be included. If it is a child of phone, a phone_type must be included.
206 * @param array $values
207 * The variable(s) to be added.
208 * @param array $params
209 * The structured parameter list.
211 * @return bool|CRM_Utils_Error
213 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
214 // Crawl through the possible classes:
227 // Cache the various object fields
228 static $fields = NULL;
230 if ($fields == NULL) {
234 // first add core contact values since for other Civi modules they are not added
235 require_once 'CRM/Contact/BAO/Contact.php';
236 $contactFields = CRM_Contact_DAO_Contact
::fields();
237 _civicrm_api3_store_values($contactFields, $values, $params);
239 if (isset($values['contact_type'])) {
240 // we're an individual/household/org property
242 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
244 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
248 if (isset($values['individual_prefix'])) {
249 if (!empty($params['prefix_id'])) {
250 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
251 $params['prefix'] = $prefixes[$params['prefix_id']];
254 $params['prefix'] = $values['individual_prefix'];
259 if (isset($values['individual_suffix'])) {
260 if (!empty($params['suffix_id'])) {
261 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
262 $params['suffix'] = $suffixes[$params['suffix_id']];
265 $params['suffix'] = $values['individual_suffix'];
271 if (isset($values['email_greeting'])) {
272 if (!empty($params['email_greeting_id'])) {
273 $emailGreetingFilter = [
274 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
275 'greeting_type' => 'email_greeting',
277 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
278 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
281 $params['email_greeting'] = $values['email_greeting'];
287 if (isset($values['postal_greeting'])) {
288 if (!empty($params['postal_greeting_id'])) {
289 $postalGreetingFilter = [
290 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
291 'greeting_type' => 'postal_greeting',
293 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
294 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
297 $params['postal_greeting'] = $values['postal_greeting'];
302 if (isset($values['addressee'])) {
303 if (!empty($params['addressee_id'])) {
305 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
306 'greeting_type' => 'addressee',
308 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
309 $params['addressee'] = $addressee[$params['addressee_id']];
312 $params['addressee'] = $values['addressee'];
317 if (isset($values['gender'])) {
318 if (!empty($params['gender_id'])) {
319 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
320 $params['gender'] = $genders[$params['gender_id']];
323 $params['gender'] = $values['gender'];
328 if (!empty($values['preferred_communication_method'])) {
330 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
332 $preffComm = explode(',', $values['preferred_communication_method']);
333 foreach ($preffComm as $v) {
334 $v = strtolower(trim($v));
335 if (array_key_exists($v, $pcm)) {
340 $params['preferred_communication_method'] = $comm;
344 // format the website params.
345 if (!empty($values['url'])) {
346 static $websiteFields;
347 if (!is_array($websiteFields)) {
348 require_once 'CRM/Core/DAO/Website.php';
349 $websiteFields = CRM_Core_DAO_Website
::fields();
351 if (!array_key_exists('website', $params) ||
352 !is_array($params['website'])
354 $params['website'] = [];
357 $websiteCount = count($params['website']);
358 _civicrm_api3_store_values($websiteFields, $values,
359 $params['website'][++
$websiteCount]
365 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
366 if (!empty($values['location_type_id'])) {
367 static $fields = NULL;
368 if ($fields == NULL) {
379 $name = strtolower($block);
380 if (!array_key_exists($name, $values)) {
384 if ($name == 'phone_ext') {
388 // block present in value array.
389 if (!array_key_exists($name, $params) ||
!is_array($params[$name])) {
393 if (!array_key_exists($block, $fields)) {
394 $className = "CRM_Core_DAO_$block";
395 $fields[$block] =& $className::fields();
398 $blockCnt = count($params[$name]);
400 // copy value to dao field name.
402 $values['name'] = $values[$name];
405 _civicrm_api3_store_values($fields[$block], $values,
406 $params[$name][++
$blockCnt]
409 if (empty($params['id']) && ($blockCnt == 1)) {
410 $params[$name][$blockCnt]['is_primary'] = TRUE;
413 // we only process single block at a time.
417 // handle address fields.
418 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
419 $params['address'] = [];
423 foreach ($params['address'] as $cnt => $addressBlock) {
424 if (CRM_Utils_Array
::value('location_type_id', $values) ==
425 CRM_Utils_Array
::value('location_type_id', $addressBlock)
433 if (!array_key_exists('Address', $fields)) {
434 $fields['Address'] = CRM_Core_DAO_Address
::fields();
437 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
438 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
439 // the address in CRM_Core_BAO_Address::create method
440 if (!empty($values['location_type_id'])) {
441 static $customFields = [];
442 if (empty($customFields)) {
443 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
445 // make a copy of values, as we going to make changes
446 $newValues = $values;
447 foreach ($values as $key => $val) {
448 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
449 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
450 // mark an entry in fields array since we want the value of custom field to be copied
451 $fields['Address'][$key] = NULL;
453 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
458 $mulValues = explode(',', $val);
459 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
460 $newValues[$key] = [];
461 foreach ($mulValues as $v1) {
462 foreach ($customOption as $v2) {
463 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
464 (strtolower($v2['value']) == strtolower(trim($v1)))
466 if ($htmlType == 'CheckBox') {
467 $newValues[$key][$v2['value']] = 1;
470 $newValues[$key][] = $v2['value'];
480 // consider new values
481 $values = $newValues;
484 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
490 'supplemental_address_1',
491 'supplemental_address_2',
492 'supplemental_address_3',
493 'StateProvince.name',
496 foreach ($addressFields as $field) {
497 if (array_key_exists($field, $values)) {
498 if (!array_key_exists('address', $params)) {
499 $params['address'] = [];
501 $params['address'][$addressCnt][$field] = $values[$field];
505 if ($addressCnt == 1) {
507 $params['address'][$addressCnt]['is_primary'] = TRUE;
512 if (isset($values['note'])) {
514 if (!isset($params['note'])) {
515 $params['note'] = [];
517 $noteBlock = count($params['note']) +
1;
519 $params['note'][$noteBlock] = [];
520 if (!isset($fields['Note'])) {
521 $fields['Note'] = CRM_Core_DAO_Note
::fields();
524 // get the current logged in civicrm user
525 $session = CRM_Core_Session
::singleton();
526 $userID = $session->get('userID');
529 $values['contact_id'] = $userID;
532 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
537 // Check for custom field values
539 if (empty($fields['custom'])) {
540 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
541 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
545 foreach ($values as $key => $value) {
546 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
547 // check if it's a valid custom field id
549 if (!array_key_exists($customFieldID, $fields['custom'])) {
550 return civicrm_api3_create_error('Invalid custom field ID');
553 $params[$key] = $value;
561 * @param array $params
566 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
567 $id = CRM_Utils_Array
::value('id', $params);
568 $externalId = CRM_Utils_Array
::value('external_identifier', $params);
569 if ($id ||
$externalId) {
570 $contact = new CRM_Contact_DAO_Contact();
573 $contact->external_identifier
= $externalId;
575 if ($contact->find(TRUE)) {
576 if ($params['contact_type'] != $contact->contact_type
) {
577 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
580 $error = CRM_Core_Error
::createError("Found matching contacts: $contact->id",
581 CRM_Core_Error
::DUPLICATE_CONTACT
,
582 'Fatal', $contact->id
584 return civicrm_api3_create_error($error->pop());
588 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised');
591 $ids = implode(',', $ids);
592 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
593 CRM_Core_Error
::DUPLICATE_CONTACT
,
596 return civicrm_api3_create_error($error->pop());
599 return civicrm_api3_create_success(TRUE);
603 * Validate a formatted contact parameter list.
605 * @param array $params
606 * Structured parameter list (as in crm_format_params).
608 * @return bool|CRM_Core_Error
610 function _civicrm_api3_deprecated_validate_formatted_contact(&$params) {
611 // Look for offending email addresses
613 if (array_key_exists('email', $params)) {
614 foreach ($params['email'] as $count => $values) {
615 if (!is_array($values)) {
618 if ($email = CRM_Utils_Array
::value('email', $values)) {
619 // validate each email
620 if (!CRM_Utils_Rule
::email($email)) {
621 return civicrm_api3_create_error('No valid email address');
624 // check for loc type id.
625 if (empty($values['location_type_id'])) {
626 return civicrm_api3_create_error('Location Type Id missing.');
632 // Validate custom data fields
633 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
634 foreach ($params['custom'] as $key => $custom) {
635 if (is_array($custom)) {
636 foreach ($custom as $fieldId => $value) {
637 $valid = CRM_Core_BAO_CustomValue
::typecheck(CRM_Utils_Array
::value('type', $value),
638 CRM_Utils_Array
::value('value', $value)
640 if (!$valid && $value['is_required']) {
641 return civicrm_api3_create_error('Invalid value for custom field \'' .
642 CRM_Utils_Array
::value('name', $custom) . '\''
645 if (CRM_Utils_Array
::value('type', $custom) == 'Date') {
646 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
653 return civicrm_api3_create_success(TRUE);
657 * @deprecated - this is part of the import parser not the API & needs to be moved on out
659 * @param array $params
660 * @param $onDuplicate
665 function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
666 require_once 'CRM/Event/Import/Parser.php';
667 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
668 CRM_Core_Error
::reset();
669 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
670 if (civicrm_error($error)) {
674 require_once "api/v3/Participant.php";
675 return civicrm_api3_participant_create($params);
680 * @param array $params
682 * @param bool $checkDuplicate
687 function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
689 // check if participant id is valid or not
690 if (!empty($params['id'])) {
691 $participant = new CRM_Event_BAO_Participant();
692 $participant->id
= $params['id'];
693 if (!$participant->find(TRUE)) {
694 return civicrm_api3_create_error(ts('Participant id is not valid'));
697 require_once 'CRM/Contact/BAO/Contact.php';
698 // check if contact id is valid or not
699 if (!empty($params['contact_id'])) {
700 $contact = new CRM_Contact_BAO_Contact();
701 $contact->id
= $params['contact_id'];
702 if (!$contact->find(TRUE)) {
703 return civicrm_api3_create_error(ts('Contact id is not valid'));
707 // check that event id is not an template
708 if (!empty($params['event_id'])) {
709 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
710 if (!empty($isTemplate)) {
711 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
716 if ($checkDuplicate) {
717 if (CRM_Event_BAO_Participant
::checkDuplicate($params, $result)) {
718 $participantID = array_pop($result);
720 $error = CRM_Core_Error
::createError("Found matching participant record.",
721 CRM_Core_Error
::DUPLICATE_PARTICIPANT
,
722 'Fatal', $participantID
725 return civicrm_api3_create_error($error->pop(),
727 'contactID' => $params['contact_id'],
728 'participantID' => $participantID,
737 * @param array $params
738 * @param bool $dupeCheck
739 * @param int $dedupeRuleGroupID
743 function _civicrm_api3_deprecated_contact_check_params(
746 $dedupeRuleGroupID = NULL) {
748 $requiredCheck = TRUE;
750 if (isset($params['id']) && is_numeric($params['id'])) {
751 $requiredCheck = FALSE;
753 if ($requiredCheck) {
754 if (isset($params['id'])) {
755 $required = ['Individual', 'Household', 'Organization'];
759 ['first_name', 'last_name'],
770 // contact_type has a limited number of valid values
771 if (empty($params['contact_type'])) {
772 return civicrm_api3_create_error("No Contact Type");
774 $fields = CRM_Utils_Array
::value($params['contact_type'], $required);
775 if ($fields == NULL) {
776 return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}");
779 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
780 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
781 return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
785 if (empty($params['contact_id']) && !empty($params['id'])) {
788 foreach ($fields as $field) {
789 if (is_array($field)) {
791 foreach ($field as $element) {
792 if (empty($params[$element])) {
800 if (!empty($params[$field])) {
810 return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error");
816 // @todo switch to using api version
817 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
818 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
819 $ids = CRM_Contact_BAO_Contact
::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array
::value('check_permissions', $params), $dedupeRuleGroupID);
821 $error = CRM_Core_Error
::createError("Found matching contacts: " . implode(',', $ids),
822 CRM_Core_Error
::DUPLICATE_CONTACT
,
825 return civicrm_api3_create_error($error->pop());
829 // check for organisations with same name
830 if (!empty($params['current_employer'])) {
831 $organizationParams = ['organization_name' => $params['current_employer']];
832 $dupeIds = CRM_Contact_BAO_Contact
::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
834 // check for mismatch employer name and id
835 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
837 return civicrm_api3_create_error('Employer name and Employer id Mismatch');
840 // show error if multiple organisation with same name exist
841 if (empty($params['employer_id']) && (count($dupeIds) > 1)
843 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
852 * @param int $activityTypeID
857 function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
858 // get ready for collecting data about activity to be created
861 $params['activity_type_id'] = $activityTypeID;
863 $params['status_id'] = 'Completed';
864 if (!empty($result['from']['id'])) {
865 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
867 $params['target_contact_id'] = [];
868 $keys = ['to', 'cc', 'bcc'];
869 foreach ($keys as $key) {
870 if (is_array($result[$key])) {
871 foreach ($result[$key] as $key => $keyValue) {
872 if (!empty($keyValue['id'])) {
873 $params['target_contact_id'][] = $keyValue['id'];
878 $params['subject'] = $result['subject'];
879 $params['activity_date_time'] = $result['date'];
880 $params['details'] = $result['body'];
882 $numAttachments = Civi
::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File
::DEFAULT_MAX_ATTACHMENTS_BACKEND
;
883 for ($i = 1; $i <= $numAttachments; $i++
) {
884 if (isset($result["attachFile_$i"])) {
885 $params["attachFile_$i"] = $result["attachFile_$i"];
888 // No point looping 100 times if there's only one attachment