3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
29 * These functions have been deprecated out of API v3 Utils folder as they are not part of the
30 * API. Calling API functions directly is not supported & these functions are not called by any
31 * part of the API so are not really part of the api
35 require_once 'api/v3/utils.php';
38 * take the input parameter list as specified in the data model and
39 * convert it into the same format that we use in QF and BAO object
41 * @param array $params Associative array of property name/value
42 * pairs to insert in new contact.
43 * @param array $values The reformatted properties that we can use internally
45 * @param array|bool $create Is the formatted Values array going to
46 * be used for CRM_vent_BAO_Participant:create()
48 * @return array|CRM_Error
51 function _civicrm_api3_deprecated_participant_formatted_param($params, &$values, $create = FALSE) {
52 $fields = CRM_Event_DAO_Participant
::fields();
53 _civicrm_api3_store_values($fields, $params, $values);
55 require_once 'CRM/Core/OptionGroup.php';
56 $customFields = CRM_Core_BAO_CustomField
::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
58 foreach ($params as $key => $value) {
59 // ignore empty values or empty arrays etc
60 if (CRM_Utils_System
::isNull($value)) {
64 //Handling Custom Data
65 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
66 $values[$key] = $value;
67 $type = $customFields[$customFieldID]['html_type'];
68 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
69 $mulValues = explode(',', $value);
70 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
71 $values[$key] = array();
72 foreach ($mulValues as $v1) {
73 foreach ($customOption as $customValueID => $customLabel) {
74 $customValue = $customLabel['value'];
75 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
76 (strtolower(trim($customValue)) == strtolower(trim($v1)))
78 if ($type == 'CheckBox') {
79 $values[$key][$customValue] = 1;
82 $values[$key][] = $customValue;
88 elseif ($type == 'Select' ||
$type == 'Radio') {
89 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
90 foreach ($customOption as $customFldID => $customValue) {
91 $val = CRM_Utils_Array
::value('value', $customValue);
92 $label = CRM_Utils_Array
::value('label', $customValue);
93 $label = strtolower($label);
94 $value = strtolower(trim($value));
95 if (($value == $label) ||
($value == strtolower($val))) {
103 case 'participant_contact_id':
104 if (!CRM_Utils_Rule
::integer($value)) {
105 return civicrm_api3_create_error("contact_id not valid: $value");
107 $dao = new CRM_Core_DAO();
109 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
113 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
115 $values['contact_id'] = $values['participant_contact_id'];
116 unset($values['participant_contact_id']);
119 case 'participant_register_date':
120 if (!CRM_Utils_Rule
::dateTime($value)) {
121 return civicrm_api3_create_error("$key not a valid date: $value");
126 $id = CRM_Core_DAO
::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title');
127 $values['event_id'] = $id;
131 if (!CRM_Utils_Rule
::integer($value)) {
132 return civicrm_api3_create_error("Event ID is not valid: $value");
134 $dao = new CRM_Core_DAO();
136 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value",
140 return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value.");
144 case 'participant_status_id':
145 if (!CRM_Utils_Rule
::integer($value)) {
146 return civicrm_api3_create_error("Event Status ID is not valid: $value");
150 case 'participant_status':
151 $status = CRM_Event_PseudoConstant
::participantStatus();
152 $values['participant_status_id'] = CRM_Utils_Array
::key($value, $status);;
155 case 'participant_role_id':
156 case 'participant_role':
157 $role = CRM_Event_PseudoConstant
::participantRole();
158 $participantRoles = explode(",", $value);
159 foreach ($participantRoles as $k => $v) {
161 if ($key == 'participant_role') {
162 $participantRoles[$k] = CRM_Utils_Array
::key($v, $role);
165 $participantRoles[$k] = $v;
168 require_once 'CRM/Core/DAO.php';
169 $values['role_id'] = implode(CRM_Core_DAO
::VALUE_SEPARATOR
, $participantRoles);
170 unset($values[$key]);
178 if (array_key_exists('participant_note', $params)) {
179 $values['participant_note'] = $params['participant_note'];
183 // CRM_Event_BAO_Participant::create() handles register_date,
184 // status_id and source. So, if $values contains
185 // participant_register_date, participant_status_id or participant_source,
186 // convert it to register_date, status_id or source
188 'participant_register_date' => 'register_date',
189 'participant_source' => 'source',
190 'participant_status_id' => 'status_id',
191 'participant_role_id' => 'role_id',
192 'participant_fee_level' => 'fee_level',
193 'participant_fee_amount' => 'fee_amount',
194 'participant_id' => 'id',
197 foreach ($changes as $orgVal => $changeVal) {
198 if (isset($values[$orgVal])) {
199 $values[$changeVal] = $values[$orgVal];
200 unset($values[$orgVal]);
209 * take the input parameter list as specified in the data model and
210 * convert it into the same format that we use in QF and BAO object
212 * @param array $params Associative array of property name/value
213 * pairs to insert in new contact.
214 * @param array $values The reformatted properties that we can use internally
217 * @param bool $create
218 * @param null $onDuplicate
220 * @return array|CRM_Error
223 function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = Null) {
224 // copy all the contribution fields as is
226 $fields = CRM_Contribute_DAO_Contribution
::fields();
228 _civicrm_api3_store_values($fields, $params, $values);
230 require_once 'CRM/Core/OptionGroup.php';
231 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
233 foreach ($params as $key => $value) {
234 // ignore empty values or empty arrays etc
235 if (CRM_Utils_System
::isNull($value)) {
239 //Handling Custom Data
240 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
241 $values[$key] = $value;
242 $type = $customFields[$customFieldID]['html_type'];
243 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
244 $mulValues = explode(',', $value);
245 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
246 $values[$key] = array();
247 foreach ($mulValues as $v1) {
248 foreach ($customOption as $customValueID => $customLabel) {
249 $customValue = $customLabel['value'];
250 if ((strtolower($customLabel['label']) == strtolower(trim($v1))) ||
251 (strtolower($customValue) == strtolower(trim($v1)))
253 if ($type == 'CheckBox') {
254 $values[$key][$customValue] = 1;
257 $values[$key][] = $customValue;
263 elseif ($type == 'Select' ||
$type == 'Radio' ||
264 ($type == 'Autocomplete-Select' &&
265 $customFields[$customFieldID]['data_type'] == 'String'
268 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
269 foreach ($customOption as $customFldID => $customValue) {
270 $val = CRM_Utils_Array
::value('value', $customValue);
271 $label = CRM_Utils_Array
::value('label', $customValue);
272 $label = strtolower($label);
273 $value = strtolower(trim($value));
274 if (($value == $label) ||
($value == strtolower($val))) {
275 $values[$key] = $val;
282 case 'contribution_contact_id':
283 if (!CRM_Utils_Rule
::integer($value)) {
284 return civicrm_api3_create_error("contact_id not valid: $value");
286 $dao = new CRM_Core_DAO();
288 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
292 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
295 $values['contact_id'] = $values['contribution_contact_id'];
296 unset($values['contribution_contact_id']);
300 //import contribution record according to select contact type
301 require_once 'CRM/Contact/DAO/Contact.php';
302 $contactType = new CRM_Contact_DAO_Contact();
303 //when insert mode check contact id or external identifier
304 if (!empty($params['contribution_contact_id']) ||
!empty($params['external_identifier'])) {
305 if (!empty($params['contribution_contact_id'])) {
306 $contactType->id
= CRM_Utils_Array
::value('contribution_contact_id', $params);
308 elseif (!empty($params['external_identifier'])) {
309 $contactType->external_identifier
= $params['external_identifier'];
311 if ($contactType->find(TRUE)) {
312 if ($params['contact_type'] != $contactType->contact_type
) {
313 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
317 elseif (!empty($params['contribution_id']) ||
!empty($params['trxn_id']) ||
!empty($params['invoice_id'])) {
318 //when update mode check contribution id or trxn id or
320 $contactId = new CRM_Contribute_DAO_Contribution();
321 if (!empty($params['contribution_id'])) {
322 $contactId->id
= $params['contribution_id'];
324 elseif (!empty($params['trxn_id'])) {
325 $contactId->trxn_id
= $params['trxn_id'];
327 elseif (!empty($params['invoice_id'])) {
328 $contactId->invoice_id
= $params['invoice_id'];
330 if ($contactId->find(TRUE)) {
331 $contactType->id
= $contactId->contact_id
;
332 if ($contactType->find(TRUE)) {
333 if ($params['contact_type'] != $contactType->contact_type
) {
334 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
340 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
341 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
344 return civicrm_api3_create_error("Empty Contact and External ID. Row was skipped.");
352 case 'thankyou_date':
353 if (!CRM_Utils_Rule
::dateTime($value)) {
354 return civicrm_api3_create_error("$key not a valid date: $value");
358 case 'non_deductible_amount':
362 if (!CRM_Utils_Rule
::money($value)) {
363 return civicrm_api3_create_error("$key not a valid amount: $value");
368 if (!CRM_Utils_Rule
::currencyCode($value)) {
369 return civicrm_api3_create_error("currency not a valid code: $value");
373 case 'financial_type':
374 require_once 'CRM/Contribute/PseudoConstant.php';
375 $contriTypes = CRM_Contribute_PseudoConstant
::financialType();
376 foreach ($contriTypes as $val => $type) {
377 if (strtolower($value) == strtolower($type)) {
378 $values['financial_type_id'] = $val;
382 if (empty($values['financial_type_id'])) {
383 return civicrm_api3_create_error("Financial Type is not valid: $value");
387 case 'payment_instrument':
388 require_once 'CRM/Core/OptionGroup.php';
389 $values['payment_instrument_id'] = CRM_Core_OptionGroup
::getValue('payment_instrument', $value);
390 if (empty($values['payment_instrument_id'])) {
391 return civicrm_api3_create_error("Payment Instrument is not valid: $value");
395 case 'contribution_status_id':
396 require_once 'CRM/Core/OptionGroup.php';
397 if (!$values['contribution_status_id'] = CRM_Core_OptionGroup
::getValue('contribution_status', $value)) {
398 return civicrm_api3_create_error("Contribution Status is not valid: $value");
403 //import contribution record according to select contact type
404 // validate contact id and external identifier.
405 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
406 if (isset($params[$key]) && is_array($params[$key])) {
407 foreach ($params[$key] as $softKey => $softParam) {
408 $contactId = CRM_Utils_Array
::value('contact_id', $softParam);
409 $externalId = CRM_Utils_Array
::value('external_identifier', $softParam);
410 $email = CRM_Utils_Array
::value('email', $softParam);
411 if ($contactId ||
$externalId) {
412 require_once 'CRM/Contact/DAO/Contact.php';
413 $contact = new CRM_Contact_DAO_Contact();
414 $contact->id
= $contactId;
415 $contact->external_identifier
= $externalId;
417 if (!$contact->find(TRUE)) {
418 $errorMsg = $contactId ?
ts("Soft Credit ContactID - $contactId doesn't exist. Row was skipped.") : ts("Provided Soft Credit External Identifier - $externalIddoesn't exist. Row was skipped.");
422 return civicrm_api3_create_error($errorMsg, $value[$key]);
425 // finally get soft credit contact id.
426 $values[$key][$softKey] = $softParam;
427 $values[$key][$softKey]['contact_id'] = $contact->id
;
430 if (!CRM_Utils_Rule
::email($email)) {
431 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
434 // get the contact id from duplicate contact rule, if more than one contact is returned
435 // we should return error, since current interface allows only one-one mapping
436 $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']);
437 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
438 if (!$checkDedupe['is_error']) {
439 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
442 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
443 if (count($matchingContactIds) > 1) {
444 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
446 elseif (count($matchingContactIds) == 1) {
447 $contactId = $matchingContactIds[0];
448 unset($softParam['email']);
449 $values[$key][$softKey] = $softParam +
array('contact_id' => $contactId);
457 case 'pledge_payment':
460 //giving respect to pledge_payment flag.
461 if (empty($params['pledge_payment'])) {
465 //get total amount of from import fields
466 $totalAmount = CRM_Utils_Array
::value('total_amount', $params);
468 $onDuplicate = CRM_Utils_Array
::value('onDuplicate', $params);
470 //we need to get contact id $contributionContactID to
471 //retrieve pledge details as well as to validate pledge ID
473 //first need to check for update mode
474 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
475 ($params['contribution_id'] ||
$params['trxn_id'] ||
$params['invoice_id'])
477 $contribution = new CRM_Contribute_DAO_Contribution();
478 if ($params['contribution_id']) {
479 $contribution->id
= $params['contribution_id'];
481 elseif ($params['trxn_id']) {
482 $contribution->trxn_id
= $params['trxn_id'];
484 elseif ($params['invoice_id']) {
485 $contribution->invoice_id
= $params['invoice_id'];
488 if ($contribution->find(TRUE)) {
489 $contributionContactID = $contribution->contact_id
;
491 $totalAmount = $contribution->total_amount
;
495 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
499 // first get the contact id for given contribution record.
500 if (!empty($params['contribution_contact_id'])) {
501 $contributionContactID = $params['contribution_contact_id'];
503 elseif (!empty($params['external_identifier'])) {
504 require_once 'CRM/Contact/DAO/Contact.php';
505 $contact = new CRM_Contact_DAO_Contact();
506 $contact->external_identifier
= $params['external_identifier'];
507 if ($contact->find(TRUE)) {
508 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id
;
511 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
515 // we need to get contribution contact using de dupe
516 $error = _civicrm_api3_deprecated_check_contact_dedupe($params);
518 if (isset($error['error_message']['params'][0])) {
519 $matchedIDs = explode(',', $error['error_message']['params'][0]);
521 // check if only one contact is found
522 if (count($matchedIDs) > 1) {
523 return civicrm_api3_create_error($error['error_message']['message'], 'pledge_payment');
526 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
530 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
535 if (!empty($params['pledge_id'])) {
536 if (CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
537 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment');
539 $values['pledge_id'] = $params['pledge_id'];
542 //check if there are any pledge related to this contact, with payments pending or in progress
543 require_once 'CRM/Pledge/BAO/Pledge.php';
544 $pledgeDetails = CRM_Pledge_BAO_Pledge
::getContactPledges($contributionContactID);
546 if (empty($pledgeDetails)) {
547 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.', 'pledge_payment');
549 elseif (count($pledgeDetails) > 1) {
550 return civicrm_api3_create_error('This contact has more than one open pledge. Unable to determine which pledge to apply the contribution to. Contribution row was skipped.', 'pledge_payment');
553 // this mean we have only one pending / in progress pledge
554 $values['pledge_id'] = $pledgeDetails[0];
557 //we need to check if oldest payment amount equal to contribution amount
558 require_once 'CRM/Pledge/BAO/PledgePayment.php';
559 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($values['pledge_id']);
561 if ($pledgePaymentDetails['amount'] == $totalAmount) {
562 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
565 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', 'pledge_payment');
574 if (array_key_exists('note', $params)) {
575 $values['note'] = $params['note'];
579 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
580 // So, if $values contains contribution_source, convert it to source
581 $changes = array('contribution_source' => 'source');
583 foreach ($changes as $orgVal => $changeVal) {
584 if (isset($values[$orgVal])) {
585 $values[$changeVal] = $values[$orgVal];
586 unset($values[$orgVal]);
595 * Function to check duplicate contacts based on de-deupe parameters
597 function _civicrm_api3_deprecated_check_contact_dedupe($params) {
598 static $cIndieFields = NULL;
599 static $defaultLocationId = NULL;
601 $contactType = $params['contact_type'];
602 if ($cIndieFields == NULL) {
603 require_once 'CRM/Contact/BAO/Contact.php';
604 $cTempIndieFields = CRM_Contact_BAO_Contact
::importableFields($contactType);
605 $cIndieFields = $cTempIndieFields;
607 require_once "CRM/Core/BAO/LocationType.php";
608 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
610 //set the value to default location id else set to 1
611 if (!$defaultLocationId = (int)$defaultLocation->id
) {
612 $defaultLocationId = 1;
616 require_once 'CRM/Contact/BAO/Query.php';
617 $locationFields = CRM_Contact_BAO_Query
::$_locationSpecificFields;
619 $contactFormatted = array();
620 foreach ($params as $key => $field) {
621 if ($field == NULL ||
$field === '') {
624 if (is_array($field)) {
625 foreach ($field as $value) {
627 if (is_array($value)) {
628 foreach ($value as $name => $testForEmpty) {
629 if ($name !== 'phone_type' &&
630 ($testForEmpty === '' ||
$testForEmpty == NULL)
641 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
647 $value = array($key => $field);
649 // check if location related field, then we need to add primary location type
650 if (in_array($key, $locationFields)) {
651 $value['location_type_id'] = $defaultLocationId;
653 elseif (array_key_exists($key, $cIndieFields)) {
654 $value['contact_type'] = $contactType;
657 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
660 $contactFormatted['contact_type'] = $contactType;
662 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
666 * take the input parameter list as specified in the data model and
667 * convert it into the same format that we use in QF and BAO object
669 * @param array $params Associative array of property name/value
670 * pairs to insert in new contact.
671 * @param array $values The reformatted properties that we can use internally
673 * @param array|bool $create Is the formatted Values array going to
674 * be used for CRM_Activity_BAO_Activity::create()
676 * @return array|CRM_Error
679 function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
680 // copy all the activity fields as is
681 $fields = CRM_Activity_DAO_Activity
::fields();
682 _civicrm_api3_store_values($fields, $params, $values);
684 require_once 'CRM/Core/OptionGroup.php';
685 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
687 foreach ($params as $key => $value) {
688 // ignore empty values or empty arrays etc
689 if (CRM_Utils_System
::isNull($value)) {
693 //Handling Custom Data
694 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
695 $values[$key] = $value;
696 $type = $customFields[$customFieldID]['html_type'];
697 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
698 $mulValues = explode(',', $value);
699 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
700 $values[$key] = array();
701 foreach ($mulValues as $v1) {
702 foreach ($customOption as $customValueID => $customLabel) {
703 $customValue = $customLabel['value'];
704 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
705 (strtolower(trim($customValue)) == strtolower(trim($v1)))
707 if ($type == 'CheckBox') {
708 $values[$key][$customValue] = 1;
711 $values[$key][] = $customValue;
717 elseif ($type == 'Select' ||
$type == 'Radio') {
718 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
719 foreach ($customOption as $customFldID => $customValue) {
720 $val = CRM_Utils_Array
::value('value', $customValue);
721 $label = CRM_Utils_Array
::value('label', $customValue);
722 $label = strtolower($label);
723 $value = strtolower(trim($value));
724 if (($value == $label) ||
($value == strtolower($val))) {
725 $values[$key] = $val;
731 if ($key == 'target_contact_id') {
732 if (!CRM_Utils_Rule
::integer($value)) {
733 return civicrm_api3_create_error("contact_id not valid: $value");
735 $contactID = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
737 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
745 * This function adds the contact variable in $values to the
746 * parameter list $params. For most cases, $values should have length 1. If
747 * the variable being added is a child of Location, a location_type_id must
748 * also be included. If it is a child of phone, a phone_type must be included.
750 * @param array $values The variable(s) to be added
751 * @param array $params The structured parameter list
753 * @return bool|CRM_Utils_Error
756 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
757 /* Crawl through the possible classes:
771 /* Cache the various object fields */
772 static $fields = NULL;
774 if ($fields == NULL) {
778 //first add core contact values since for other Civi modules they are not added
779 require_once 'CRM/Contact/BAO/Contact.php';
780 $contactFields = CRM_Contact_DAO_Contact
::fields();
781 _civicrm_api3_store_values($contactFields, $values, $params);
783 if (isset($values['contact_type'])) {
784 /* we're an individual/household/org property */
786 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
788 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
792 if (isset($values['individual_prefix'])) {
793 if (!empty($params['prefix_id'])) {
794 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
795 $params['prefix'] = $prefixes[$params['prefix_id']];
798 $params['prefix'] = $values['individual_prefix'];
803 if (isset($values['individual_suffix'])) {
804 if (!empty($params['suffix_id'])) {
805 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
806 $params['suffix'] = $suffixes[$params['suffix_id']];
809 $params['suffix'] = $values['individual_suffix'];
815 if (isset($values['email_greeting'])) {
816 if (!empty($params['email_greeting_id'])) {
817 $emailGreetingFilter = array(
818 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
819 'greeting_type' => 'email_greeting',
821 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
822 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
825 $params['email_greeting'] = $values['email_greeting'];
831 if (isset($values['postal_greeting'])) {
832 if (!empty($params['postal_greeting_id'])) {
833 $postalGreetingFilter = array(
834 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
835 'greeting_type' => 'postal_greeting',
837 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
838 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
841 $params['postal_greeting'] = $values['postal_greeting'];
846 if (isset($values['addressee'])) {
847 if (!empty($params['addressee_id'])) {
848 $addresseeFilter = array(
849 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
850 'greeting_type' => 'addressee',
852 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
853 $params['addressee'] = $addressee[$params['addressee_id']];
856 $params['addressee'] = $values['addressee'];
861 if (isset($values['gender'])) {
862 if (!empty($params['gender_id'])) {
863 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
864 $params['gender'] = $genders[$params['gender_id']];
867 $params['gender'] = $values['gender'];
872 if (isset($values['preferred_communication_method'])) {
874 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
876 $preffComm = explode(',', $values['preferred_communication_method']);
877 foreach ($preffComm as $v) {
878 $v = strtolower(trim($v));
879 if (array_key_exists($v, $pcm)) {
884 $params['preferred_communication_method'] = $comm;
888 //format the website params.
889 if (!empty($values['url'])) {
890 static $websiteFields;
891 if (!is_array($websiteFields)) {
892 require_once 'CRM/Core/DAO/Website.php';
893 $websiteFields = CRM_Core_DAO_Website
::fields();
895 if (!array_key_exists('website', $params) ||
896 !is_array($params['website'])
898 $params['website'] = array();
901 $websiteCount = count($params['website']);
902 _civicrm_api3_store_values($websiteFields, $values,
903 $params['website'][++
$websiteCount]
909 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
910 if (!empty($values['location_type_id'])) {
911 _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params);
915 if (isset($values['note'])) {
916 /* add a note field */
917 if (!isset($params['note'])) {
918 $params['note'] = array();
920 $noteBlock = count($params['note']) +
1;
922 $params['note'][$noteBlock] = array();
923 if (!isset($fields['Note'])) {
924 $fields['Note'] = CRM_Core_DAO_Note
::fields();
927 // get the current logged in civicrm user
928 $session = CRM_Core_Session
::singleton();
929 $userID = $session->get('userID');
932 $values['contact_id'] = $userID;
935 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
940 /* Check for custom field values */
942 if (empty($fields['custom'])) {
943 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
944 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
948 foreach ($values as $key => $value) {
949 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
950 /* check if it's a valid custom field id */
952 if (!array_key_exists($customFieldID, $fields['custom'])) {
953 return civicrm_api3_create_error('Invalid custom field ID');
956 $params[$key] = $value;
963 * This function format location blocks w/ v3.0 format.
965 * @param array $values The variable(s) to be added
966 * @param array $params The structured parameter list
971 function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params) {
972 static $fields = NULL;
973 if ($fields == NULL) {
978 'Phone', 'Email', 'IM', 'OpenID','Phone_Ext') as $block) {
979 $name = strtolower($block);
980 if (!array_key_exists($name, $values)) {
984 if($name == 'phone_ext'){
988 // block present in value array.
989 if (!array_key_exists($name, $params) ||
!is_array($params[$name])) {
990 $params[$name] = array();
993 if (!array_key_exists($block, $fields)) {
994 $className = "CRM_Core_DAO_$block";
995 $fields[$block] =& $className::fields( );
998 $blockCnt = count($params[$name]);
1000 // copy value to dao field name.
1001 if ($name == 'im') {
1002 $values['name'] = $values[$name];
1005 _civicrm_api3_store_values($fields[$block], $values,
1006 $params[$name][++
$blockCnt]
1009 if (empty($params['id']) && ($blockCnt == 1)) {
1010 $params[$name][$blockCnt]['is_primary'] = TRUE;
1013 // we only process single block at a time.
1017 // handle address fields.
1018 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1019 $params['address'] = array();
1023 foreach ($params['address'] as $cnt => $addressBlock) {
1024 if (CRM_Utils_Array
::value('location_type_id', $values) ==
1025 CRM_Utils_Array
::value('location_type_id', $addressBlock)
1033 if (!array_key_exists('Address', $fields)) {
1034 require_once 'CRM/Core/DAO/Address.php';
1035 $fields['Address'] = CRM_Core_DAO_Address
::fields();
1038 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1039 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1040 // the address in CRM_Core_BAO_Address::create method
1041 if (!empty($values['location_type_id'])) {
1042 static $customFields = array();
1043 if (empty($customFields)) {
1044 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1046 // make a copy of values, as we going to make changes
1047 $newValues = $values;
1048 foreach ($values as $key => $val) {
1049 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1050 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1051 // mark an entry in fields array since we want the value of custom field to be copied
1052 $fields['Address'][$key] = null;
1054 $htmlType = CRM_Utils_Array
::value( 'html_type', $customFields[$customFieldID] );
1055 switch ( $htmlType ) {
1057 case 'AdvMulti-Select':
1058 case 'Multi-Select':
1060 $mulValues = explode( ',', $val );
1061 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption( $customFieldID, true );
1062 $newValues[$key] = array( );
1063 foreach ( $mulValues as $v1 ) {
1064 foreach ( $customOption as $v2 ) {
1065 if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) ||
1066 ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) {
1067 if ( $htmlType == 'CheckBox' ) {
1068 $newValues[$key][$v2['value']] = 1;
1070 $newValues[$key][] = $v2['value'];
1080 // consider new values
1081 $values = $newValues;
1084 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
1086 $addressFields = array(
1087 'county', 'country', 'state_province',
1088 'supplemental_address_1', 'supplemental_address_2',
1089 'StateProvince.name',
1092 foreach ($addressFields as $field) {
1093 if (array_key_exists($field, $values)) {
1094 if (!array_key_exists('address', $params)) {
1095 $params['address'] = array();
1097 $params['address'][$addressCnt][$field] = $values[$field];
1101 if ($addressCnt == 1) {
1103 $params['address'][$addressCnt]['is_primary'] = TRUE;
1111 * @param <type> $params
1113 * @return array <type>
1115 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
1116 $id = CRM_Utils_Array
::value('id', $params);
1117 $externalId = CRM_Utils_Array
::value('external_identifier', $params);
1118 if ($id ||
$externalId) {
1119 $contact = new CRM_Contact_DAO_Contact();
1122 $contact->external_identifier
= $externalId;
1124 if ($contact->find(TRUE)) {
1125 if ($params['contact_type'] != $contact->contact_type
) {
1126 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
1129 $error = CRM_Core_Error
::createError("Found matching contacts: $contact->id",
1130 CRM_Core_Error
::DUPLICATE_CONTACT
,
1131 'Fatal', $contact->id
1133 return civicrm_api3_create_error($error->pop());
1137 require_once 'CRM/Dedupe/Finder.php';
1138 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
1139 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised');
1142 $ids = implode(',', $ids);
1143 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
1144 CRM_Core_Error
::DUPLICATE_CONTACT
,
1147 return civicrm_api3_create_error($error->pop());
1150 return civicrm_api3_create_success(TRUE);
1154 * Validate a formatted contact parameter list.
1156 * @param array $params Structured parameter list (as in crm_format_params)
1158 * @return bool|CRM_Core_Error
1161 function _civicrm_api3_deprecated_validate_formatted_contact(&$params) {
1162 /* Look for offending email addresses */
1164 if (array_key_exists('email', $params)) {
1165 foreach ($params['email'] as $count => $values) {
1166 if (!is_array($values)) {
1169 if ($email = CRM_Utils_Array
::value('email', $values)) {
1170 //validate each email
1171 if (!CRM_Utils_Rule
::email($email)) {
1172 return civicrm_api3_create_error('No valid email address');
1175 //check for loc type id.
1176 if (empty($values['location_type_id'])) {
1177 return civicrm_api3_create_error('Location Type Id missing.');
1183 /* Validate custom data fields */
1184 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
1185 foreach ($params['custom'] as $key => $custom) {
1186 if (is_array($custom)) {
1187 foreach ($custom as $fieldId => $value) {
1188 $valid = CRM_Core_BAO_CustomValue
::typecheck(CRM_Utils_Array
::value('type', $value),
1189 CRM_Utils_Array
::value('value', $value)
1191 if (!$valid && $value['is_required']) {
1192 return civicrm_api3_create_error('Invalid value for custom field \'' .
1193 CRM_Utils_Array
::value('name', $custom) . '\''
1196 if (CRM_Utils_Array
::value('type', $custom) == 'Date') {
1197 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
1204 return civicrm_api3_create_success(TRUE);
1209 * @deprecated - this is part of the import parser not the API & needs to be moved on out
1212 * @param $onDuplicate
1214 * @internal param $ <type> $params
1215 * @internal param $ <type> $onDuplicate
1217 * @return array|bool <type>
1219 function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
1220 require_once 'CRM/Event/Import/Parser.php';
1221 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
1222 CRM_Core_Error
::reset();
1223 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
1224 if (civicrm_error($error)) {
1228 require_once "api/v3/Participant.php";
1229 return civicrm_api3_participant_create($params);
1234 * @param <type> $params
1236 * @param bool $checkDuplicate
1238 * @return array|bool <type>
1240 function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
1242 //check if participant id is valid or not
1243 if (!empty($params['id'])) {
1244 $participant = new CRM_Event_BAO_Participant();
1245 $participant->id
= $params['id'];
1246 if (!$participant->find(TRUE)) {
1247 return civicrm_api3_create_error(ts('Participant id is not valid'));
1250 require_once 'CRM/Contact/BAO/Contact.php';
1251 //check if contact id is valid or not
1252 if (!empty($params['contact_id'])) {
1253 $contact = new CRM_Contact_BAO_Contact();
1254 $contact->id
= $params['contact_id'];
1255 if (!$contact->find(TRUE)) {
1256 return civicrm_api3_create_error(ts('Contact id is not valid'));
1260 //check that event id is not an template
1261 if (!empty($params['event_id'])) {
1262 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
1263 if (!empty($isTemplate)) {
1264 return civicrm_api3_create_error(ts('Event templates are not meant to be registered'));
1269 if ($checkDuplicate) {
1270 if (CRM_Event_BAO_Participant
::checkDuplicate($params, $result)) {
1271 $participantID = array_pop($result);
1273 $error = CRM_Core_Error
::createError("Found matching participant record.",
1274 CRM_Core_Error
::DUPLICATE_PARTICIPANT
,
1275 'Fatal', $participantID
1278 return civicrm_api3_create_error($error->pop(),
1280 'contactID' => $params['contact_id'],
1281 'participantID' => $participantID,
1290 * Ensure that we have the right input parameters for custom data
1292 * @param array $params Associative array of property name/value
1293 * pairs to insert in new contact.
1294 * @param string $csType contact subtype if exists/passed.
1296 * @return null on success, error message otherwise
1299 function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = NULL) {
1300 empty($csType) ?
$onlyParent = TRUE : $onlyParent = FALSE;
1302 require_once 'CRM/Core/BAO/CustomField.php';
1303 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'],
1313 foreach ($params as $key => $value) {
1314 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1315 /* check if it's a valid custom field id */
1316 if (!array_key_exists($customFieldID, $customFields)) {
1318 $errorMsg = "Invalid Custom Field Contact Type: {$params['contact_type']}";
1319 if (!empty($csType)) {
1320 $errorMsg .= " or Mismatched SubType: " . implode(', ', (array)$csType);
1322 return civicrm_api3_create_error($errorMsg);
1330 * @param bool $dupeCheck
1331 * @param bool $dupeErrorArray
1332 * @param bool $requiredCheck
1333 * @param null $dedupeRuleGroupID
1335 * @return array|null
1337 function _civicrm_api3_deprecated_contact_check_params(
1340 $dupeErrorArray = FALSE,
1341 $requiredCheck = TRUE,
1342 $dedupeRuleGroupID = NULL) {
1343 if (isset($params['id']) && is_numeric($params['id'])) {
1344 $requiredCheck = FALSE;
1346 if ($requiredCheck) {
1347 if (isset($params['id'])) {
1348 $required = array('Individual', 'Household', 'Organization');
1351 'Individual' => array(
1352 array('first_name', 'last_name'),
1355 'Household' => array(
1358 'Organization' => array(
1359 'organization_name',
1364 // contact_type has a limited number of valid values
1365 if(empty($params['contact_type'])) {
1366 return civicrm_api3_create_error("No Contact Type");
1368 $fields = CRM_Utils_Array
::value($params['contact_type'], $required);
1369 if ($fields == NULL) {
1370 return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}");
1373 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
1374 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
1375 return civicrm_api3_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType));
1379 if (empty($params['contact_id']) && !empty($params['id'])) {
1382 foreach ($fields as $field) {
1383 if (is_array($field)) {
1385 foreach ($field as $element) {
1386 if (empty($params[$element])) {
1394 if (!empty($params[$field])) {
1404 return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error");
1410 // check for record already existing
1411 require_once 'CRM/Dedupe/Finder.php';
1412 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
1415 // setting 'check_permission' here means that the dedupe checking will be carried out even if the
1416 // person does not have permission to carry out de-dupes
1417 // this is similar to the front end form
1418 if (isset($params['check_permission'])) {
1419 $dedupeParams['check_permission'] = $params['check_permission'];
1422 $ids = implode(',', CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID));
1425 if ($dupeErrorArray) {
1426 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
1427 CRM_Core_Error
::DUPLICATE_CONTACT
,
1430 return civicrm_api3_create_error($error->pop());
1433 return civicrm_api3_create_error("Found matching contacts: $ids");
1437 //check for organisations with same name
1438 if (!empty($params['current_employer'])) {
1439 $organizationParams = array();
1440 $organizationParams['organization_name'] = $params['current_employer'];
1442 require_once 'CRM/Dedupe/Finder.php';
1443 $dedupParams = CRM_Dedupe_Finder
::formatParams($organizationParams, 'Organization');
1445 $dedupParams['check_permission'] = FALSE;
1446 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupParams, 'Organization', 'Supervised');
1448 // check for mismatch employer name and id
1449 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
1451 return civicrm_api3_create_error('Employer name and Employer id Mismatch');
1454 // show error if multiple organisation with same name exist
1455 if (empty($params['employer_id']) && (count($dupeIds) > 1)
1457 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
1467 * @param $activityTypeID
1469 * @internal param $ <type> $result
1470 * @internal param $ <type> $activityTypeID
1472 * @return array <type> $params
1474 function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
1475 // get ready for collecting data about activity to be created
1478 $params['activity_type_id'] = $activityTypeID;
1480 $params['status_id'] = 2;
1481 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
1482 $params['target_contact_id'] = array();
1483 $keys = array('to', 'cc', 'bcc');
1484 foreach ($keys as $key) {
1485 if (is_array($result[$key])) {
1486 foreach ($result[$key] as $key => $keyValue) {
1487 if (!empty($keyValue['id'])) {
1488 $params['target_contact_id'][] = $keyValue['id'];
1493 $params['subject'] = $result['subject'];
1494 $params['activity_date_time'] = $result['date'];
1495 $params['details'] = $result['body'];
1497 for ($i = 1; $i <= 5; $i++
) {
1498 if (isset($result["attachFile_$i"])) {
1499 $params["attachFile_$i"] = $result["attachFile_$i"];