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 $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 * @return array|CRM_Error
220 function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = Null) {
221 // copy all the contribution fields as is
223 $fields = CRM_Contribute_DAO_Contribution
::fields();
225 _civicrm_api3_store_values($fields, $params, $values);
227 require_once 'CRM/Core/OptionGroup.php';
228 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
230 foreach ($params as $key => $value) {
231 // ignore empty values or empty arrays etc
232 if (CRM_Utils_System
::isNull($value)) {
236 //Handling Custom Data
237 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
238 $values[$key] = $value;
239 $type = $customFields[$customFieldID]['html_type'];
240 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
241 $mulValues = explode(',', $value);
242 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
243 $values[$key] = array();
244 foreach ($mulValues as $v1) {
245 foreach ($customOption as $customValueID => $customLabel) {
246 $customValue = $customLabel['value'];
247 if ((strtolower($customLabel['label']) == strtolower(trim($v1))) ||
248 (strtolower($customValue) == strtolower(trim($v1)))
250 if ($type == 'CheckBox') {
251 $values[$key][$customValue] = 1;
254 $values[$key][] = $customValue;
260 elseif ($type == 'Select' ||
$type == 'Radio' ||
261 ($type == 'Autocomplete-Select' &&
262 $customFields[$customFieldID]['data_type'] == 'String'
265 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
266 foreach ($customOption as $customFldID => $customValue) {
267 $val = CRM_Utils_Array
::value('value', $customValue);
268 $label = CRM_Utils_Array
::value('label', $customValue);
269 $label = strtolower($label);
270 $value = strtolower(trim($value));
271 if (($value == $label) ||
($value == strtolower($val))) {
272 $values[$key] = $val;
279 case 'contribution_contact_id':
280 if (!CRM_Utils_Rule
::integer($value)) {
281 return civicrm_api3_create_error("contact_id not valid: $value");
283 $dao = new CRM_Core_DAO();
285 $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
289 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
292 $values['contact_id'] = $values['contribution_contact_id'];
293 unset($values['contribution_contact_id']);
297 //import contribution record according to select contact type
298 require_once 'CRM/Contact/DAO/Contact.php';
299 $contactType = new CRM_Contact_DAO_Contact();
300 //when insert mode check contact id or external identifier
301 if (!empty($params['contribution_contact_id']) ||
!empty($params['external_identifier'])) {
302 if (!empty($params['contribution_contact_id'])) {
303 $contactType->id
= CRM_Utils_Array
::value('contribution_contact_id', $params);
305 elseif (!empty($params['external_identifier'])) {
306 $contactType->external_identifier
= $params['external_identifier'];
308 if ($contactType->find(TRUE)) {
309 if ($params['contact_type'] != $contactType->contact_type
) {
310 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
314 elseif ($params['contribution_id'] ||
$params['trxn_id'] ||
$params['invoice_id']) {
315 //when update mode check contribution id or trxn id or
317 $contactId = new CRM_Contribute_DAO_Contribution();
318 if ($params['contribution_id']) {
319 $contactId->id
= $params['contribution_id'];
321 elseif ($params['trxn_id']) {
322 $contactId->trxn_id
= $params['trxn_id'];
324 elseif ($params['invoice_id']) {
325 $contactId->invoice_id
= $params['invoice_id'];
327 if ($contactId->find(TRUE)) {
328 $contactType->id
= $contactId->contact_id
;
329 if ($contactType->find(TRUE)) {
330 if ($params['contact_type'] != $contactType->contact_type
) {
331 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
341 case 'thankyou_date':
342 if (!CRM_Utils_Rule
::dateTime($value)) {
343 return civicrm_api3_create_error("$key not a valid date: $value");
347 case 'non_deductible_amount':
351 if (!CRM_Utils_Rule
::money($value)) {
352 return civicrm_api3_create_error("$key not a valid amount: $value");
357 if (!CRM_Utils_Rule
::currencyCode($value)) {
358 return civicrm_api3_create_error("currency not a valid code: $value");
362 case 'financial_type':
363 require_once 'CRM/Contribute/PseudoConstant.php';
364 $contriTypes = CRM_Contribute_PseudoConstant
::financialType();
365 foreach ($contriTypes as $val => $type) {
366 if (strtolower($value) == strtolower($type)) {
367 $values['financial_type_id'] = $val;
371 if (empty($values['financial_type_id'])) {
372 return civicrm_api3_create_error("Financial Type is not valid: $value");
376 case 'payment_instrument':
377 require_once 'CRM/Core/OptionGroup.php';
378 $values['payment_instrument_id'] = CRM_Core_OptionGroup
::getValue('payment_instrument', $value);
379 if (empty($values['payment_instrument_id'])) {
380 return civicrm_api3_create_error("Payment Instrument is not valid: $value");
384 case 'contribution_status_id':
385 require_once 'CRM/Core/OptionGroup.php';
386 if (!$values['contribution_status_id'] = CRM_Core_OptionGroup
::getValue('contribution_status', $value)) {
387 return civicrm_api3_create_error("Contribution Status is not valid: $value");
392 //import contribution record according to select contact type
393 // validate contact id and external identifier.
394 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
395 if (!isset($params['contribution_id']) && empty($params['contribution_id']) && $onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
) {
396 $errorMsg = ts("Empty Contribution Id. Row was skipped.");
397 return civicrm_api3_create_error($errorMsg, $value[$key]);
399 elseif (!isset($params['contribution_contact_id']) && empty($params['contribution_contact_id']) && $onDuplicate != CRM_Import_Parser
::DUPLICATE_UPDATE
) {
400 $errorMsg = ts("Empty Contact Id. Row was skipped.");
401 return civicrm_api3_create_error($errorMsg, $value[$key]);
403 if (isset($params[$key]) && is_array($params[$key])) {
404 foreach ($params[$key] as $softKey => $softParam) {
405 $contactId = CRM_Utils_Array
::value('contact_id', $softParam);
406 $externalId = CRM_Utils_Array
::value('external_identifier', $softParam);
407 $email = CRM_Utils_Array
::value('email', $softParam);
408 if ($contactId ||
$externalId) {
409 require_once 'CRM/Contact/DAO/Contact.php';
410 $contact = new CRM_Contact_DAO_Contact();
411 $contact->id
= $contactId;
412 $contact->external_identifier
= $externalId;
414 if (!$contact->find(TRUE)) {
415 $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.");
419 return civicrm_api3_create_error($errorMsg, $value[$key]);
422 // finally get soft credit contact id.
423 $values[$key][$softKey] = $softParam;
424 $values[$key][$softKey]['contact_id'] = $contact->id
;
427 if (!CRM_Utils_Rule
::email($email)) {
428 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
431 // get the contact id from duplicate contact rule, if more than one contact is returned
432 // we should return error, since current interface allows only one-one mapping
433 $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']);
434 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
435 if (!$checkDedupe['is_error']) {
436 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
439 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
440 if (count($matchingContactIds) > 1) {
441 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
443 elseif (count($matchingContactIds) == 1) {
444 $contactId = $matchingContactIds[0];
445 unset($softParam['email']);
446 $values[$key][$softKey] = $softParam +
array('contact_id' => $contactId);
454 case 'pledge_payment':
457 //giving respect to pledge_payment flag.
458 if (empty($params['pledge_payment'])) {
462 //get total amount of from import fields
463 $totalAmount = CRM_Utils_Array
::value('total_amount', $params);
465 $onDuplicate = CRM_Utils_Array
::value('onDuplicate', $params);
467 //we need to get contact id $contributionContactID to
468 //retrieve pledge details as well as to validate pledge ID
470 //first need to check for update mode
471 if ($onDuplicate == CRM_Import_Parser
::DUPLICATE_UPDATE
&&
472 ($params['contribution_id'] ||
$params['trxn_id'] ||
$params['invoice_id'])
474 $contribution = new CRM_Contribute_DAO_Contribution();
475 if ($params['contribution_id']) {
476 $contribution->id
= $params['contribution_id'];
478 elseif ($params['trxn_id']) {
479 $contribution->trxn_id
= $params['trxn_id'];
481 elseif ($params['invoice_id']) {
482 $contribution->invoice_id
= $params['invoice_id'];
485 if ($contribution->find(TRUE)) {
486 $contributionContactID = $contribution->contact_id
;
488 $totalAmount = $contribution->total_amount
;
492 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
496 // first get the contact id for given contribution record.
497 if (!empty($params['contribution_contact_id'])) {
498 $contributionContactID = $params['contribution_contact_id'];
500 elseif (!empty($params['external_identifier'])) {
501 require_once 'CRM/Contact/DAO/Contact.php';
502 $contact = new CRM_Contact_DAO_Contact();
503 $contact->external_identifier
= $params['external_identifier'];
504 if ($contact->find(TRUE)) {
505 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id
;
508 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
512 // we need to get contribution contact using de dupe
513 $error = _civicrm_api3_deprecated_check_contact_dedupe($params);
515 if (isset($error['error_message']['params'][0])) {
516 $matchedIDs = explode(',', $error['error_message']['params'][0]);
518 // check if only one contact is found
519 if (count($matchedIDs) > 1) {
520 return civicrm_api3_create_error($error['error_message']['message'], 'pledge_payment');
523 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
527 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.', 'pledge_payment');
532 if (!empty($params['pledge_id'])) {
533 if (CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
534 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.', 'pledge_payment');
536 $values['pledge_id'] = $params['pledge_id'];
539 //check if there are any pledge related to this contact, with payments pending or in progress
540 require_once 'CRM/Pledge/BAO/Pledge.php';
541 $pledgeDetails = CRM_Pledge_BAO_Pledge
::getContactPledges($contributionContactID);
543 if (empty($pledgeDetails)) {
544 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.', 'pledge_payment');
546 elseif (count($pledgeDetails) > 1) {
547 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');
550 // this mean we have only one pending / in progress pledge
551 $values['pledge_id'] = $pledgeDetails[0];
554 //we need to check if oldest payment amount equal to contribution amount
555 require_once 'CRM/Pledge/BAO/PledgePayment.php';
556 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($values['pledge_id']);
558 if ($pledgePaymentDetails['amount'] == $totalAmount) {
559 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
562 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.', 'pledge_payment');
571 if (array_key_exists('note', $params)) {
572 $values['note'] = $params['note'];
576 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
577 // So, if $values contains contribution_source, convert it to source
578 $changes = array('contribution_source' => 'source');
580 foreach ($changes as $orgVal => $changeVal) {
581 if (isset($values[$orgVal])) {
582 $values[$changeVal] = $values[$orgVal];
583 unset($values[$orgVal]);
592 * Function to check duplicate contacts based on de-deupe parameters
594 function _civicrm_api3_deprecated_check_contact_dedupe($params) {
595 static $cIndieFields = NULL;
596 static $defaultLocationId = NULL;
598 $contactType = $params['contact_type'];
599 if ($cIndieFields == NULL) {
600 require_once 'CRM/Contact/BAO/Contact.php';
601 $cTempIndieFields = CRM_Contact_BAO_Contact
::importableFields($contactType);
602 $cIndieFields = $cTempIndieFields;
604 require_once "CRM/Core/BAO/LocationType.php";
605 $defaultLocation = CRM_Core_BAO_LocationType
::getDefault();
607 //set the value to default location id else set to 1
608 if (!$defaultLocationId = (int)$defaultLocation->id
) {
609 $defaultLocationId = 1;
613 require_once 'CRM/Contact/BAO/Query.php';
614 $locationFields = CRM_Contact_BAO_Query
::$_locationSpecificFields;
616 $contactFormatted = array();
617 foreach ($params as $key => $field) {
618 if ($field == NULL ||
$field === '') {
621 if (is_array($field)) {
622 foreach ($field as $value) {
624 if (is_array($value)) {
625 foreach ($value as $name => $testForEmpty) {
626 if ($name !== 'phone_type' &&
627 ($testForEmpty === '' ||
$testForEmpty == NULL)
638 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
644 $value = array($key => $field);
646 // check if location related field, then we need to add primary location type
647 if (in_array($key, $locationFields)) {
648 $value['location_type_id'] = $defaultLocationId;
650 elseif (array_key_exists($key, $cIndieFields)) {
651 $value['contact_type'] = $contactType;
654 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
657 $contactFormatted['contact_type'] = $contactType;
659 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
663 * take the input parameter list as specified in the data model and
664 * convert it into the same format that we use in QF and BAO object
666 * @param array $params Associative array of property name/value
667 * pairs to insert in new contact.
668 * @param array $values The reformatted properties that we can use internally
670 * @param array $create Is the formatted Values array going to
671 * be used for CRM_Activity_BAO_Activity::create()
673 * @return array|CRM_Error
676 function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
677 // copy all the activity fields as is
678 $fields = CRM_Activity_DAO_Activity
::fields();
679 _civicrm_api3_store_values($fields, $params, $values);
681 require_once 'CRM/Core/OptionGroup.php';
682 $customFields = CRM_Core_BAO_CustomField
::getFields('Activity');
684 foreach ($params as $key => $value) {
685 // ignore empty values or empty arrays etc
686 if (CRM_Utils_System
::isNull($value)) {
690 //Handling Custom Data
691 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
692 $values[$key] = $value;
693 $type = $customFields[$customFieldID]['html_type'];
694 if ($type == 'CheckBox' ||
$type == 'Multi-Select') {
695 $mulValues = explode(',', $value);
696 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
697 $values[$key] = array();
698 foreach ($mulValues as $v1) {
699 foreach ($customOption as $customValueID => $customLabel) {
700 $customValue = $customLabel['value'];
701 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
702 (strtolower(trim($customValue)) == strtolower(trim($v1)))
704 if ($type == 'CheckBox') {
705 $values[$key][$customValue] = 1;
708 $values[$key][] = $customValue;
714 elseif ($type == 'Select' ||
$type == 'Radio') {
715 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
716 foreach ($customOption as $customFldID => $customValue) {
717 $val = CRM_Utils_Array
::value('value', $customValue);
718 $label = CRM_Utils_Array
::value('label', $customValue);
719 $label = strtolower($label);
720 $value = strtolower(trim($value));
721 if (($value == $label) ||
($value == strtolower($val))) {
722 $values[$key] = $val;
728 if ($key == 'target_contact_id') {
729 if (!CRM_Utils_Rule
::integer($value)) {
730 return civicrm_api3_create_error("contact_id not valid: $value");
732 $contactID = CRM_Core_DAO
::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
734 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
742 * This function adds the contact variable in $values to the
743 * parameter list $params. For most cases, $values should have length 1. If
744 * the variable being added is a child of Location, a location_type_id must
745 * also be included. If it is a child of phone, a phone_type must be included.
747 * @param array $values The variable(s) to be added
748 * @param array $params The structured parameter list
750 * @return bool|CRM_Utils_Error
753 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
754 /* Crawl through the possible classes:
768 /* Cache the various object fields */
769 static $fields = NULL;
771 if ($fields == NULL) {
775 //first add core contact values since for other Civi modules they are not added
776 require_once 'CRM/Contact/BAO/Contact.php';
777 $contactFields = CRM_Contact_DAO_Contact
::fields();
778 _civicrm_api3_store_values($contactFields, $values, $params);
780 if (isset($values['contact_type'])) {
781 /* we're an individual/household/org property */
783 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
785 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
789 if (isset($values['individual_prefix'])) {
790 if (!empty($params['prefix_id'])) {
791 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
792 $params['prefix'] = $prefixes[$params['prefix_id']];
795 $params['prefix'] = $values['individual_prefix'];
800 if (isset($values['individual_suffix'])) {
801 if (!empty($params['suffix_id'])) {
802 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
803 $params['suffix'] = $suffixes[$params['suffix_id']];
806 $params['suffix'] = $values['individual_suffix'];
812 if (isset($values['email_greeting'])) {
813 if (!empty($params['email_greeting_id'])) {
814 $emailGreetingFilter = array(
815 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
816 'greeting_type' => 'email_greeting',
818 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
819 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
822 $params['email_greeting'] = $values['email_greeting'];
828 if (isset($values['postal_greeting'])) {
829 if (!empty($params['postal_greeting_id'])) {
830 $postalGreetingFilter = array(
831 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
832 'greeting_type' => 'postal_greeting',
834 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
835 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
838 $params['postal_greeting'] = $values['postal_greeting'];
843 if (isset($values['addressee'])) {
844 if (!empty($params['addressee_id'])) {
845 $addresseeFilter = array(
846 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
847 'greeting_type' => 'addressee',
849 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
850 $params['addressee'] = $addressee[$params['addressee_id']];
853 $params['addressee'] = $values['addressee'];
858 if (isset($values['gender'])) {
859 if (!empty($params['gender_id'])) {
860 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
861 $params['gender'] = $genders[$params['gender_id']];
864 $params['gender'] = $values['gender'];
869 if (isset($values['preferred_communication_method'])) {
871 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
873 $preffComm = explode(',', $values['preferred_communication_method']);
874 foreach ($preffComm as $v) {
875 $v = strtolower(trim($v));
876 if (array_key_exists($v, $pcm)) {
881 $params['preferred_communication_method'] = $comm;
885 //format the website params.
886 if (!empty($values['url'])) {
887 static $websiteFields;
888 if (!is_array($websiteFields)) {
889 require_once 'CRM/Core/DAO/Website.php';
890 $websiteFields = CRM_Core_DAO_Website
::fields();
892 if (!array_key_exists('website', $params) ||
893 !is_array($params['website'])
895 $params['website'] = array();
898 $websiteCount = count($params['website']);
899 _civicrm_api3_store_values($websiteFields, $values,
900 $params['website'][++
$websiteCount]
906 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
907 if (!empty($values['location_type_id'])) {
908 _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params);
912 if (isset($values['note'])) {
913 /* add a note field */
914 if (!isset($params['note'])) {
915 $params['note'] = array();
917 $noteBlock = count($params['note']) +
1;
919 $params['note'][$noteBlock] = array();
920 if (!isset($fields['Note'])) {
921 $fields['Note'] = CRM_Core_DAO_Note
::fields();
924 // get the current logged in civicrm user
925 $session = CRM_Core_Session
::singleton();
926 $userID = $session->get('userID');
929 $values['contact_id'] = $userID;
932 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
937 /* Check for custom field values */
939 if (empty($fields['custom'])) {
940 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
941 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
945 foreach ($values as $key => $value) {
946 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
947 /* check if it's a valid custom field id */
949 if (!array_key_exists($customFieldID, $fields['custom'])) {
950 return civicrm_api3_create_error('Invalid custom field ID');
953 $params[$key] = $value;
960 * This function format location blocks w/ v3.0 format.
962 * @param array $values The variable(s) to be added
963 * @param array $params The structured parameter list
968 function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params) {
969 static $fields = NULL;
970 if ($fields == NULL) {
975 'Phone', 'Email', 'IM', 'OpenID','Phone_Ext') as $block) {
976 $name = strtolower($block);
977 if (!array_key_exists($name, $values)) {
981 if($name == 'phone_ext'){
985 // block present in value array.
986 if (!array_key_exists($name, $params) ||
!is_array($params[$name])) {
987 $params[$name] = array();
990 if (!array_key_exists($block, $fields)) {
991 $className = "CRM_Core_DAO_$block";
992 $fields[$block] =& $className::fields( );
995 $blockCnt = count($params[$name]);
997 // copy value to dao field name.
999 $values['name'] = $values[$name];
1002 _civicrm_api3_store_values($fields[$block], $values,
1003 $params[$name][++
$blockCnt]
1006 if (empty($params['id']) && ($blockCnt == 1)) {
1007 $params[$name][$blockCnt]['is_primary'] = TRUE;
1010 // we only process single block at a time.
1014 // handle address fields.
1015 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1016 $params['address'] = array();
1020 foreach ($params['address'] as $cnt => $addressBlock) {
1021 if (CRM_Utils_Array
::value('location_type_id', $values) ==
1022 CRM_Utils_Array
::value('location_type_id', $addressBlock)
1030 if (!array_key_exists('Address', $fields)) {
1031 require_once 'CRM/Core/DAO/Address.php';
1032 $fields['Address'] = CRM_Core_DAO_Address
::fields();
1035 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1036 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1037 // the address in CRM_Core_BAO_Address::create method
1038 if (!empty($values['location_type_id'])) {
1039 static $customFields = array();
1040 if (empty($customFields)) {
1041 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1043 // make a copy of values, as we going to make changes
1044 $newValues = $values;
1045 foreach ($values as $key => $val) {
1046 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1047 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1048 // mark an entry in fields array since we want the value of custom field to be copied
1049 $fields['Address'][$key] = null;
1051 $htmlType = CRM_Utils_Array
::value( 'html_type', $customFields[$customFieldID] );
1052 switch ( $htmlType ) {
1054 case 'AdvMulti-Select':
1055 case 'Multi-Select':
1057 $mulValues = explode( ',', $val );
1058 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption( $customFieldID, true );
1059 $newValues[$key] = array( );
1060 foreach ( $mulValues as $v1 ) {
1061 foreach ( $customOption as $v2 ) {
1062 if ( ( strtolower( $v2['label'] ) == strtolower( trim( $v1 ) ) ) ||
1063 ( strtolower( $v2['value'] ) == strtolower( trim( $v1 ) ) ) ) {
1064 if ( $htmlType == 'CheckBox' ) {
1065 $newValues[$key][$v2['value']] = 1;
1067 $newValues[$key][] = $v2['value'];
1077 // consider new values
1078 $values = $newValues;
1081 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
1083 $addressFields = array(
1084 'county', 'country', 'state_province',
1085 'supplemental_address_1', 'supplemental_address_2',
1086 'StateProvince.name',
1089 foreach ($addressFields as $field) {
1090 if (array_key_exists($field, $values)) {
1091 if (!array_key_exists('address', $params)) {
1092 $params['address'] = array();
1094 $params['address'][$addressCnt][$field] = $values[$field];
1098 if ($addressCnt == 1) {
1100 $params['address'][$addressCnt]['is_primary'] = TRUE;
1108 * @param <type> $params
1112 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
1113 $id = CRM_Utils_Array
::value('id', $params);
1114 $externalId = CRM_Utils_Array
::value('external_identifier', $params);
1115 if ($id ||
$externalId) {
1116 $contact = new CRM_Contact_DAO_Contact();
1119 $contact->external_identifier
= $externalId;
1121 if ($contact->find(TRUE)) {
1122 if ($params['contact_type'] != $contact->contact_type
) {
1123 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
1126 $error = CRM_Core_Error
::createError("Found matching contacts: $contact->id",
1127 CRM_Core_Error
::DUPLICATE_CONTACT
,
1128 'Fatal', $contact->id
1130 return civicrm_api3_create_error($error->pop());
1134 require_once 'CRM/Dedupe/Finder.php';
1135 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
1136 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised');
1139 $ids = implode(',', $ids);
1140 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
1141 CRM_Core_Error
::DUPLICATE_CONTACT
,
1144 return civicrm_api3_create_error($error->pop());
1147 return civicrm_api3_create_success(TRUE);
1151 * Validate a formatted contact parameter list.
1153 * @param array $params Structured parameter list (as in crm_format_params)
1155 * @return bool|CRM_Core_Error
1158 function _civicrm_api3_deprecated_validate_formatted_contact(&$params) {
1159 /* Look for offending email addresses */
1161 if (array_key_exists('email', $params)) {
1162 foreach ($params['email'] as $count => $values) {
1163 if (!is_array($values)) {
1166 if ($email = CRM_Utils_Array
::value('email', $values)) {
1167 //validate each email
1168 if (!CRM_Utils_Rule
::email($email)) {
1169 return civicrm_api3_create_error('No valid email address');
1172 //check for loc type id.
1173 if (empty($values['location_type_id'])) {
1174 return civicrm_api3_create_error('Location Type Id missing.');
1180 /* Validate custom data fields */
1181 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
1182 foreach ($params['custom'] as $key => $custom) {
1183 if (is_array($custom)) {
1184 foreach ($custom as $fieldId => $value) {
1185 $valid = CRM_Core_BAO_CustomValue
::typecheck(CRM_Utils_Array
::value('type', $value),
1186 CRM_Utils_Array
::value('value', $value)
1189 return civicrm_api3_create_error('Invalid value for custom field \'' .
1190 CRM_Utils_Array
::value('name', $custom) . '\''
1193 if (CRM_Utils_Array
::value('type', $custom) == 'Date') {
1194 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
1201 return civicrm_api3_create_success(TRUE);
1207 * @deprecated - this is part of the import parser not the API & needs to be moved on out
1209 * @param <type> $params
1210 * @param <type> $onDuplicate
1214 function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
1215 require_once 'CRM/Event/Import/Parser.php';
1216 if ($onDuplicate != CRM_Import_Parser
::DUPLICATE_NOCHECK
) {
1217 CRM_Core_Error
::reset();
1218 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
1219 if (civicrm_error($error)) {
1223 require_once "api/v3/Participant.php";
1224 return civicrm_api3_participant_create($params);
1229 * @param <type> $params
1233 function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
1235 //check if participant id is valid or not
1236 if (!empty($params['id'])) {
1237 $participant = new CRM_Event_BAO_Participant();
1238 $participant->id
= $params['id'];
1239 if (!$participant->find(TRUE)) {
1240 return civicrm_api3_create_error(ts('Participant id is not valid'));
1243 require_once 'CRM/Contact/BAO/Contact.php';
1244 //check if contact id is valid or not
1245 if (!empty($params['contact_id'])) {
1246 $contact = new CRM_Contact_BAO_Contact();
1247 $contact->id
= $params['contact_id'];
1248 if (!$contact->find(TRUE)) {
1249 return civicrm_api3_create_error(ts('Contact id is not valid'));
1253 //check that event id is not an template
1254 if (!empty($params['event_id'])) {
1255 $isTemplate = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
1256 if (!empty($isTemplate)) {
1257 return civicrm_api3_create_error(ts('Event templates are not meant to be registered'));
1262 if ($checkDuplicate) {
1263 if (CRM_Event_BAO_Participant
::checkDuplicate($params, $result)) {
1264 $participantID = array_pop($result);
1266 $error = CRM_Core_Error
::createError("Found matching participant record.",
1267 CRM_Core_Error
::DUPLICATE_PARTICIPANT
,
1268 'Fatal', $participantID
1271 return civicrm_api3_create_error($error->pop(),
1273 'contactID' => $params['contact_id'],
1274 'participantID' => $participantID,
1283 * Ensure that we have the right input parameters for custom data
1285 * @param array $params Associative array of property name/value
1286 * pairs to insert in new contact.
1287 * @param string $csType contact subtype if exists/passed.
1289 * @return null on success, error message otherwise
1292 function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = NULL) {
1293 empty($csType) ?
$onlyParent = TRUE : $onlyParent = FALSE;
1295 require_once 'CRM/Core/BAO/CustomField.php';
1296 $customFields = CRM_Core_BAO_CustomField
::getFields($params['contact_type'],
1306 foreach ($params as $key => $value) {
1307 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1308 /* check if it's a valid custom field id */
1309 if (!array_key_exists($customFieldID, $customFields)) {
1311 $errorMsg = "Invalid Custom Field Contact Type: {$params['contact_type']}";
1312 if (!empty($csType)) {
1313 $errorMsg .= " or Mismatched SubType: " . implode(', ', (array)$csType);
1315 return civicrm_api3_create_error($errorMsg);
1321 function _civicrm_api3_deprecated_contact_check_params(
1324 $dupeErrorArray = FALSE,
1325 $requiredCheck = TRUE,
1326 $dedupeRuleGroupID = NULL) {
1327 if (isset($params['id']) && is_numeric($params['id'])) {
1328 $requiredCheck = FALSE;
1330 if ($requiredCheck) {
1331 if (isset($params['id'])) {
1332 $required = array('Individual', 'Household', 'Organization');
1335 'Individual' => array(
1336 array('first_name', 'last_name'),
1339 'Household' => array(
1342 'Organization' => array(
1343 'organization_name',
1348 // contact_type has a limited number of valid values
1349 if(empty($params['contact_type'])) {
1350 return civicrm_api3_create_error("No Contact Type");
1352 $fields = CRM_Utils_Array
::value($params['contact_type'], $required);
1353 if ($fields == NULL) {
1354 return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}");
1357 if ($csType = CRM_Utils_Array
::value('contact_sub_type', $params)) {
1358 if (!(CRM_Contact_BAO_ContactType
::isExtendsContactType($csType, $params['contact_type']))) {
1359 return civicrm_api3_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType));
1363 if (empty($params['contact_id']) && !empty($params['id'])) {
1366 foreach ($fields as $field) {
1367 if (is_array($field)) {
1369 foreach ($field as $element) {
1370 if (empty($params[$element])) {
1378 if (!empty($params[$field])) {
1388 return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error");
1394 // check for record already existing
1395 require_once 'CRM/Dedupe/Finder.php';
1396 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, $params['contact_type']);
1399 // setting 'check_permission' here means that the dedupe checking will be carried out even if the
1400 // person does not have permission to carry out de-dupes
1401 // this is similar to the front end form
1402 if (isset($params['check_permission'])) {
1403 $dedupeParams['check_permission'] = $params['check_permission'];
1406 $ids = implode(',', CRM_Dedupe_Finder
::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array(), $dedupeRuleGroupID));
1409 if ($dupeErrorArray) {
1410 $error = CRM_Core_Error
::createError("Found matching contacts: $ids",
1411 CRM_Core_Error
::DUPLICATE_CONTACT
,
1414 return civicrm_api3_create_error($error->pop());
1417 return civicrm_api3_create_error("Found matching contacts: $ids");
1421 //check for organisations with same name
1422 if (!empty($params['current_employer'])) {
1423 $organizationParams = array();
1424 $organizationParams['organization_name'] = $params['current_employer'];
1426 require_once 'CRM/Dedupe/Finder.php';
1427 $dedupParams = CRM_Dedupe_Finder
::formatParams($organizationParams, 'Organization');
1429 $dedupParams['check_permission'] = FALSE;
1430 $dupeIds = CRM_Dedupe_Finder
::dupesByParams($dedupParams, 'Organization', 'Supervised');
1432 // check for mismatch employer name and id
1433 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
1435 return civicrm_api3_create_error('Employer name and Employer id Mismatch');
1438 // show error if multiple organisation with same name exist
1439 if (empty($params['employer_id']) && (count($dupeIds) > 1)
1441 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
1450 * @param <type> $result
1451 * @param <type> $activityTypeID
1453 * @return <type> $params
1455 function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
1456 // get ready for collecting data about activity to be created
1459 $params['activity_type_id'] = $activityTypeID;
1461 $params['status_id'] = 2;
1462 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
1463 $params['target_contact_id'] = array();
1464 $keys = array('to', 'cc', 'bcc');
1465 foreach ($keys as $key) {
1466 if (is_array($result[$key])) {
1467 foreach ($result[$key] as $key => $keyValue) {
1468 if (!empty($keyValue['id'])) {
1469 $params['target_contact_id'][] = $keyValue['id'];
1474 $params['subject'] = $result['subject'];
1475 $params['activity_date_time'] = $result['date'];
1476 $params['details'] = $result['body'];
1478 for ($i = 1; $i <= 5; $i++
) {
1479 if (isset($result["attachFile_$i"])) {
1480 $params["attachFile_$i"] = $result["attachFile_$i"];