CRM-21120 Add environment check for existence of mcrypt function
[civicrm-core.git] / CRM / Utils / DeprecatedUtils.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
e70a7fc0 26 */
6a488035 27
50bfb460
SB
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
50bfb460
SB
32 */
33
6a488035
TO
34/*
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
38 *
39 */
40
6a488035
TO
41require_once 'api/v3/utils.php';
42
6a488035
TO
43/**
44 * take the input parameter list as specified in the data model and
45 * convert it into the same format that we use in QF and BAO object
46 *
77855840
TO
47 * @param array $params
48 * Associative array of property name/value.
6a488035 49 * pairs to insert in new contact.
77855840
TO
50 * @param array $values
51 * The reformatted properties that we can use internally.
6a488035
TO
52 * '
53 *
f4aaa82a
EM
54 * @param bool $create
55 * @param null $onDuplicate
56 *
6a488035 57 * @return array|CRM_Error
6a488035 58 */
e7292422 59function _civicrm_api3_deprecated_formatted_param($params, &$values, $create = FALSE, $onDuplicate = NULL) {
6a488035
TO
60 // copy all the contribution fields as is
61
62 $fields = CRM_Contribute_DAO_Contribution::fields();
63
64 _civicrm_api3_store_values($fields, $params, $values);
65
66 require_once 'CRM/Core/OptionGroup.php';
67 $customFields = CRM_Core_BAO_CustomField::getFields('Contribution', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE);
68
69 foreach ($params as $key => $value) {
70 // ignore empty values or empty arrays etc
71 if (CRM_Utils_System::isNull($value)) {
72 continue;
73 }
74
50bfb460 75 // Handling Custom Data
6a488035
TO
76 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
77 $values[$key] = $value;
78 $type = $customFields[$customFieldID]['html_type'];
79 if ($type == 'CheckBox' || $type == 'Multi-Select') {
91bb24a7 80 $mulValues = explode(',', $value);
6a488035
TO
81 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
82 $values[$key] = array();
83 foreach ($mulValues as $v1) {
84 foreach ($customOption as $customValueID => $customLabel) {
85 $customValue = $customLabel['value'];
86 if ((strtolower($customLabel['label']) == strtolower(trim($v1))) ||
87 (strtolower($customValue) == strtolower(trim($v1)))
88 ) {
89 if ($type == 'CheckBox') {
90 $values[$key][$customValue] = 1;
91 }
92 else {
93 $values[$key][] = $customValue;
94 }
95 }
96 }
97 }
98 }
99 elseif ($type == 'Select' || $type == 'Radio' ||
100 ($type == 'Autocomplete-Select' &&
101 $customFields[$customFieldID]['data_type'] == 'String'
102 )
103 ) {
104 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
105 foreach ($customOption as $customFldID => $customValue) {
91bb24a7 106 $val = CRM_Utils_Array::value('value', $customValue);
6a488035
TO
107 $label = CRM_Utils_Array::value('label', $customValue);
108 $label = strtolower($label);
109 $value = strtolower(trim($value));
110 if (($value == $label) || ($value == strtolower($val))) {
111 $values[$key] = $val;
112 }
113 }
114 }
115 }
116
117 switch ($key) {
118 case 'contribution_contact_id':
119 if (!CRM_Utils_Rule::integer($value)) {
120 return civicrm_api3_create_error("contact_id not valid: $value");
121 }
91bb24a7 122 $dao = new CRM_Core_DAO();
6a488035 123 $qParams = array();
7bc2eb38 124 $svq = $dao->singleValueQuery("SELECT is_deleted FROM civicrm_contact WHERE id = $value",
6a488035
TO
125 $qParams
126 );
7bc2eb38 127 if (!isset($svq)) {
6a488035 128 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
0db6c3e1 129 }
4c9b6178 130 elseif ($svq == 1) {
7bc2eb38 131 return civicrm_api3_create_error("Invalid Contact ID: contact_id $value is a soft-deleted contact.");
6a488035
TO
132 }
133
134 $values['contact_id'] = $values['contribution_contact_id'];
135 unset($values['contribution_contact_id']);
136 break;
137
138 case 'contact_type':
50bfb460 139 // import contribution record according to select contact type
6a488035
TO
140 require_once 'CRM/Contact/DAO/Contact.php';
141 $contactType = new CRM_Contact_DAO_Contact();
0dbc73f0 142 $contactId = CRM_Utils_Array::value('contribution_contact_id', $params);
143 $externalId = CRM_Utils_Array::value('external_identifier', $params);
144 $email = CRM_Utils_Array::value('email', $params);
145 //when insert mode check contact id or external identifier
146 if ($contactId || $externalId) {
147 $contactType->id = $contactId;
148 $contactType->external_identifier = $externalId;
6a488035
TO
149 if ($contactType->find(TRUE)) {
150 if ($params['contact_type'] != $contactType->contact_type) {
151 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
152 }
153 }
154 }
0dbc73f0 155 elseif ($email) {
156 if (!CRM_Utils_Rule::email($email)) {
157 return civicrm_api3_create_error("Invalid email address $email provided. Row was skipped");
158 }
159
160 // get the contact id from duplicate contact rule, if more than one contact is returned
161 // we should return error, since current interface allows only one-one mapping
162 $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']);
163 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
164 if (!$checkDedupe['is_error']) {
165 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email. Row was skipped");
166 }
167 else {
168 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
169 if (count($matchingContactIds) > 1) {
170 return civicrm_api3_create_error("Invalid email address(duplicate) $email. Row was skipped");
171 }
172 elseif (count($matchingContactIds) == 1) {
173 $params['contribution_contact_id'] = $matchingContactIds[0];
174 }
175 }
176 }
c9ea7d07 177 elseif (!empty($params['contribution_id']) || !empty($params['trxn_id']) || !empty($params['invoice_id'])) {
50bfb460
SB
178 // when update mode check contribution id or trxn id or
179 // invoice id
6a488035 180 $contactId = new CRM_Contribute_DAO_Contribution();
c411c28a 181 if (!empty($params['contribution_id'])) {
6a488035
TO
182 $contactId->id = $params['contribution_id'];
183 }
c411c28a 184 elseif (!empty($params['trxn_id'])) {
6a488035
TO
185 $contactId->trxn_id = $params['trxn_id'];
186 }
c411c28a 187 elseif (!empty($params['invoice_id'])) {
6a488035
TO
188 $contactId->invoice_id = $params['invoice_id'];
189 }
190 if ($contactId->find(TRUE)) {
191 $contactType->id = $contactId->contact_id;
192 if ($contactType->find(TRUE)) {
193 if ($params['contact_type'] != $contactType->contact_type) {
194 return civicrm_api3_create_error("Contact Type is wrong: $contactType->contact_type");
195 }
196 }
197 }
198 }
c9ea7d07
AS
199 else {
200 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
8b8e183b 201 return civicrm_api3_create_error("Empty Contribution and Invoice and Transaction ID. Row was skipped.");
c9ea7d07 202 }
c9ea7d07 203 }
6a488035
TO
204 break;
205
206 case 'receive_date':
207 case 'cancel_date':
208 case 'receipt_date':
209 case 'thankyou_date':
210 if (!CRM_Utils_Rule::dateTime($value)) {
211 return civicrm_api3_create_error("$key not a valid date: $value");
212 }
213 break;
214
215 case 'non_deductible_amount':
216 case 'total_amount':
217 case 'fee_amount':
218 case 'net_amount':
219 if (!CRM_Utils_Rule::money($value)) {
220 return civicrm_api3_create_error("$key not a valid amount: $value");
221 }
222 break;
223
224 case 'currency':
225 if (!CRM_Utils_Rule::currencyCode($value)) {
226 return civicrm_api3_create_error("currency not a valid code: $value");
227 }
228 break;
229
230 case 'financial_type':
231 require_once 'CRM/Contribute/PseudoConstant.php';
232 $contriTypes = CRM_Contribute_PseudoConstant::financialType();
233 foreach ($contriTypes as $val => $type) {
234 if (strtolower($value) == strtolower($type)) {
235 $values['financial_type_id'] = $val;
236 break;
237 }
238 }
a7488080 239 if (empty($values['financial_type_id'])) {
6a488035
TO
240 return civicrm_api3_create_error("Financial Type is not valid: $value");
241 }
242 break;
243
244 case 'payment_instrument':
676159c9 245 require_once 'CRM/Core/PseudoConstant.php';
246 $values['payment_instrument_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'payment_instrument_id', $value);
a7488080 247 if (empty($values['payment_instrument_id'])) {
6a488035
TO
248 return civicrm_api3_create_error("Payment Instrument is not valid: $value");
249 }
250 break;
251
252 case 'contribution_status_id':
676159c9 253 require_once 'CRM/Core/PseudoConstant.php';
254 if (!$values['contribution_status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $value)) {
6a488035
TO
255 return civicrm_api3_create_error("Contribution Status is not valid: $value");
256 }
257 break;
258
6a488035 259 case 'soft_credit':
50bfb460 260 // import contribution record according to select contact type
6a488035 261 // validate contact id and external identifier.
377fa510 262 $value[$key] = $mismatchContactType = $softCreditContactIds = '';
1221efe9 263 if (isset($params[$key]) && is_array($params[$key])) {
264 foreach ($params[$key] as $softKey => $softParam) {
265 $contactId = CRM_Utils_Array::value('contact_id', $softParam);
266 $externalId = CRM_Utils_Array::value('external_identifier', $softParam);
377fa510 267 $email = CRM_Utils_Array::value('email', $softParam);
1221efe9 268 if ($contactId || $externalId) {
269 require_once 'CRM/Contact/DAO/Contact.php';
270 $contact = new CRM_Contact_DAO_Contact();
271 $contact->id = $contactId;
272 $contact->external_identifier = $externalId;
1221efe9 273 $errorMsg = NULL;
274 if (!$contact->find(TRUE)) {
7b99ead3
CW
275 $field = $contactId ? ts('Contact ID') : ts('External ID');
276 $errorMsg = ts("Soft Credit %1 - %2 doesn't exist. Row was skipped.",
277 array(1 => $field, 2 => $contactId ? $contactId : $externalId));
1221efe9 278 }
6a488035 279
1221efe9 280 if ($errorMsg) {
216a6a5b 281 return civicrm_api3_create_error($errorMsg);
1221efe9 282 }
6a488035 283
1221efe9 284 // finally get soft credit contact id.
285 $values[$key][$softKey] = $softParam;
286 $values[$key][$softKey]['contact_id'] = $contact->id;
6a488035 287 }
377fa510 288 elseif ($email) {
289 if (!CRM_Utils_Rule::email($email)) {
290 return civicrm_api3_create_error("Invalid email address $email provided for Soft Credit. Row was skipped");
291 }
292
1221efe9 293 // get the contact id from duplicate contact rule, if more than one contact is returned
294 // we should return error, since current interface allows only one-one mapping
377fa510 295 $emailParams = array('email' => $email, 'contact_type' => $params['contact_type']);
296 $checkDedupe = _civicrm_api3_deprecated_duplicate_formatted_contact($emailParams);
297 if (!$checkDedupe['is_error']) {
298 return civicrm_api3_create_error("Invalid email address(doesn't exist) $email for Soft Credit. Row was skipped");
1221efe9 299 }
300 else {
377fa510 301 $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]);
302 if (count($matchingContactIds) > 1) {
3d4fb61c 303 return civicrm_api3_create_error("Invalid email address(duplicate) $email for Soft Credit. Row was skipped");
377fa510 304 }
305 elseif (count($matchingContactIds) == 1) {
e7292422 306 $contactId = $matchingContactIds[0];
377fa510 307 unset($softParam['email']);
308 $values[$key][$softKey] = $softParam + array('contact_id' => $contactId);
309 }
1221efe9 310 }
6a488035
TO
311 }
312 }
e7292422
TO
313 }
314 break;
6a488035
TO
315
316 case 'pledge_payment':
317 case 'pledge_id':
318
50bfb460 319 // giving respect to pledge_payment flag.
a7488080 320 if (empty($params['pledge_payment'])) {
6a488035
TO
321 continue;
322 }
323
50bfb460 324 // get total amount of from import fields
6a488035
TO
325 $totalAmount = CRM_Utils_Array::value('total_amount', $params);
326
327 $onDuplicate = CRM_Utils_Array::value('onDuplicate', $params);
328
50bfb460
SB
329 // we need to get contact id $contributionContactID to
330 // retrieve pledge details as well as to validate pledge ID
6a488035 331
50bfb460 332 // first need to check for update mode
a05662ef 333 if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
6a488035
TO
334 ($params['contribution_id'] || $params['trxn_id'] || $params['invoice_id'])
335 ) {
336 $contribution = new CRM_Contribute_DAO_Contribution();
337 if ($params['contribution_id']) {
338 $contribution->id = $params['contribution_id'];
339 }
340 elseif ($params['trxn_id']) {
341 $contribution->trxn_id = $params['trxn_id'];
342 }
343 elseif ($params['invoice_id']) {
344 $contribution->invoice_id = $params['invoice_id'];
345 }
346
347 if ($contribution->find(TRUE)) {
348 $contributionContactID = $contribution->contact_id;
349 if (!$totalAmount) {
350 $totalAmount = $contribution->total_amount;
351 }
352 }
353 else {
216a6a5b 354 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
6a488035
TO
355 }
356 }
357 else {
358 // first get the contact id for given contribution record.
a7488080 359 if (!empty($params['contribution_contact_id'])) {
6a488035
TO
360 $contributionContactID = $params['contribution_contact_id'];
361 }
a7488080 362 elseif (!empty($params['external_identifier'])) {
6a488035
TO
363 require_once 'CRM/Contact/DAO/Contact.php';
364 $contact = new CRM_Contact_DAO_Contact();
365 $contact->external_identifier = $params['external_identifier'];
366 if ($contact->find(TRUE)) {
367 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $contact->id;
368 }
369 else {
216a6a5b 370 return civicrm_api3_create_error('No match found for specified contact in pledge payment data. Row was skipped.');
6a488035
TO
371 }
372 }
373 else {
50bfb460 374 // we need to get contribution contact using de dupe
6a488035
TO
375 $error = _civicrm_api3_deprecated_check_contact_dedupe($params);
376
377 if (isset($error['error_message']['params'][0])) {
378 $matchedIDs = explode(',', $error['error_message']['params'][0]);
379
380 // check if only one contact is found
381 if (count($matchedIDs) > 1) {
216a6a5b 382 return civicrm_api3_create_error($error['error_message']['message']);
6a488035
TO
383 }
384 else {
385 $contributionContactID = $params['contribution_contact_id'] = $values['contribution_contact_id'] = $matchedIDs[0];
386 }
387 }
388 else {
216a6a5b 389 return civicrm_api3_create_error('No match found for specified contact in contribution data. Row was skipped.');
6a488035
TO
390 }
391 }
392 }
393
a7488080 394 if (!empty($params['pledge_id'])) {
6a488035 395 if (CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $params['pledge_id'], 'contact_id') != $contributionContactID) {
216a6a5b 396 return civicrm_api3_create_error('Invalid Pledge ID provided. Contribution row was skipped.');
6a488035
TO
397 }
398 $values['pledge_id'] = $params['pledge_id'];
399 }
400 else {
50bfb460 401 // check if there are any pledge related to this contact, with payments pending or in progress
6a488035
TO
402 require_once 'CRM/Pledge/BAO/Pledge.php';
403 $pledgeDetails = CRM_Pledge_BAO_Pledge::getContactPledges($contributionContactID);
404
405 if (empty($pledgeDetails)) {
216a6a5b 406 return civicrm_api3_create_error('No open pledges found for this contact. Contribution row was skipped.');
6a488035
TO
407 }
408 elseif (count($pledgeDetails) > 1) {
216a6a5b 409 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.');
6a488035
TO
410 }
411
412 // this mean we have only one pending / in progress pledge
413 $values['pledge_id'] = $pledgeDetails[0];
414 }
415
50bfb460 416 // we need to check if oldest payment amount equal to contribution amount
6a488035
TO
417 require_once 'CRM/Pledge/BAO/PledgePayment.php';
418 $pledgePaymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($values['pledge_id']);
419
420 if ($pledgePaymentDetails['amount'] == $totalAmount) {
421 $values['pledge_payment_id'] = $pledgePaymentDetails['id'];
422 }
423 else {
216a6a5b 424 return civicrm_api3_create_error('Contribution and Pledge Payment amount mismatch for this record. Contribution row was skipped.');
6a488035
TO
425 }
426 break;
427
428 default:
429 break;
430 }
431 }
432
433 if (array_key_exists('note', $params)) {
434 $values['note'] = $params['note'];
435 }
436
437 if ($create) {
438 // CRM_Contribute_BAO_Contribution::add() handles contribution_source
439 // So, if $values contains contribution_source, convert it to source
440 $changes = array('contribution_source' => 'source');
441
442 foreach ($changes as $orgVal => $changeVal) {
443 if (isset($values[$orgVal])) {
444 $values[$changeVal] = $values[$orgVal];
445 unset($values[$orgVal]);
446 }
447 }
448 }
449
450 return NULL;
451}
452
453/**
ea3ddccf 454 * Check duplicate contacts based on de-dupe parameters.
455 *
456 * @param array $params
457 *
458 * @return array
6a488035
TO
459 */
460function _civicrm_api3_deprecated_check_contact_dedupe($params) {
461 static $cIndieFields = NULL;
462 static $defaultLocationId = NULL;
463
464 $contactType = $params['contact_type'];
465 if ($cIndieFields == NULL) {
466 require_once 'CRM/Contact/BAO/Contact.php';
467 $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType);
468 $cIndieFields = $cTempIndieFields;
469
470 require_once "CRM/Core/BAO/LocationType.php";
471 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
472
50bfb460 473 // set the value to default location id else set to 1
e7292422 474 if (!$defaultLocationId = (int) $defaultLocation->id) {
6a488035
TO
475 $defaultLocationId = 1;
476 }
477 }
478
479 require_once 'CRM/Contact/BAO/Query.php';
480 $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields;
481
482 $contactFormatted = array();
483 foreach ($params as $key => $field) {
484 if ($field == NULL || $field === '') {
485 continue;
486 }
6ce097e8
JF
487 // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
488 // instead of soft credit contact.
489 if (is_array($field) && $key != "soft_credit") {
6a488035
TO
490 foreach ($field as $value) {
491 $break = FALSE;
492 if (is_array($value)) {
493 foreach ($value as $name => $testForEmpty) {
494 if ($name !== 'phone_type' &&
495 ($testForEmpty === '' || $testForEmpty == NULL)
496 ) {
497 $break = TRUE;
498 break;
499 }
500 }
501 }
502 else {
503 $break = TRUE;
504 }
505 if (!$break) {
506 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
507 }
508 }
509 continue;
510 }
511
512 $value = array($key => $field);
513
514 // check if location related field, then we need to add primary location type
515 if (in_array($key, $locationFields)) {
516 $value['location_type_id'] = $defaultLocationId;
517 }
518 elseif (array_key_exists($key, $cIndieFields)) {
519 $value['contact_type'] = $contactType;
520 }
521
522 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
523 }
524
525 $contactFormatted['contact_type'] = $contactType;
526
527 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
528}
529
530/**
531 * take the input parameter list as specified in the data model and
532 * convert it into the same format that we use in QF and BAO object
533 *
77855840
TO
534 * @param array $params
535 * Associative array of property name/value.
6a488035 536 * pairs to insert in new contact.
77855840
TO
537 * @param array $values
538 * The reformatted properties that we can use internally.
6a488035 539 *
f4aaa82a 540 * @param array|bool $create Is the formatted Values array going to
6a488035
TO
541 * be used for CRM_Activity_BAO_Activity::create()
542 *
543 * @return array|CRM_Error
6a488035
TO
544 */
545function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
546 // copy all the activity fields as is
547 $fields = CRM_Activity_DAO_Activity::fields();
548 _civicrm_api3_store_values($fields, $params, $values);
549
550 require_once 'CRM/Core/OptionGroup.php';
551 $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
552
553 foreach ($params as $key => $value) {
554 // ignore empty values or empty arrays etc
555 if (CRM_Utils_System::isNull($value)) {
556 continue;
557 }
558
559 //Handling Custom Data
560 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
561 $values[$key] = $value;
562 $type = $customFields[$customFieldID]['html_type'];
563 if ($type == 'CheckBox' || $type == 'Multi-Select') {
91bb24a7 564 $mulValues = explode(',', $value);
6a488035
TO
565 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
566 $values[$key] = array();
567 foreach ($mulValues as $v1) {
568 foreach ($customOption as $customValueID => $customLabel) {
569 $customValue = $customLabel['value'];
570 if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) ||
571 (strtolower(trim($customValue)) == strtolower(trim($v1)))
572 ) {
573 if ($type == 'CheckBox') {
574 $values[$key][$customValue] = 1;
575 }
576 else {
577 $values[$key][] = $customValue;
578 }
579 }
580 }
581 }
582 }
583 elseif ($type == 'Select' || $type == 'Radio') {
584 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
585 foreach ($customOption as $customFldID => $customValue) {
91bb24a7 586 $val = CRM_Utils_Array::value('value', $customValue);
6a488035
TO
587 $label = CRM_Utils_Array::value('label', $customValue);
588 $label = strtolower($label);
589 $value = strtolower(trim($value));
590 if (($value == $label) || ($value == strtolower($val))) {
591 $values[$key] = $val;
592 }
593 }
594 }
595 }
596
597 if ($key == 'target_contact_id') {
598 if (!CRM_Utils_Rule::integer($value)) {
599 return civicrm_api3_create_error("contact_id not valid: $value");
600 }
601 $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
602 if (!$contactID) {
603 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
604 }
605 }
606 }
607 return NULL;
608}
609
610/**
611 * This function adds the contact variable in $values to the
612 * parameter list $params. For most cases, $values should have length 1. If
613 * the variable being added is a child of Location, a location_type_id must
614 * also be included. If it is a child of phone, a phone_type must be included.
615 *
77855840
TO
616 * @param array $values
617 * The variable(s) to be added.
618 * @param array $params
619 * The structured parameter list.
6a488035
TO
620 *
621 * @return bool|CRM_Utils_Error
6a488035
TO
622 */
623function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
50bfb460
SB
624 // Crawl through the possible classes:
625 // Contact
626 // Individual
627 // Household
628 // Organization
629 // Location
630 // Address
631 // Email
632 // Phone
633 // IM
634 // Note
635 // Custom
636
637 // Cache the various object fields
6a488035
TO
638 static $fields = NULL;
639
640 if ($fields == NULL) {
641 $fields = array();
642 }
643
50bfb460 644 // first add core contact values since for other Civi modules they are not added
6a488035
TO
645 require_once 'CRM/Contact/BAO/Contact.php';
646 $contactFields = CRM_Contact_DAO_Contact::fields();
647 _civicrm_api3_store_values($contactFields, $values, $params);
648
649 if (isset($values['contact_type'])) {
50bfb460 650 // we're an individual/household/org property
6a488035 651
6a488035
TO
652 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields();
653
654 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
655 return TRUE;
656 }
657
658 if (isset($values['individual_prefix'])) {
a7488080 659 if (!empty($params['prefix_id'])) {
91bb24a7 660 $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
6a488035
TO
661 $params['prefix'] = $prefixes[$params['prefix_id']];
662 }
663 else {
664 $params['prefix'] = $values['individual_prefix'];
665 }
666 return TRUE;
667 }
668
669 if (isset($values['individual_suffix'])) {
a7488080 670 if (!empty($params['suffix_id'])) {
91bb24a7 671 $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
6a488035
TO
672 $params['suffix'] = $suffixes[$params['suffix_id']];
673 }
674 else {
675 $params['suffix'] = $values['individual_suffix'];
676 }
677 return TRUE;
678 }
679
50bfb460 680 // CRM-4575
6a488035 681 if (isset($values['email_greeting'])) {
a7488080 682 if (!empty($params['email_greeting_id'])) {
91bb24a7 683 $emailGreetingFilter = array(
684 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
6a488035
TO
685 'greeting_type' => 'email_greeting',
686 );
687 $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter);
688 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
689 }
690 else {
691 $params['email_greeting'] = $values['email_greeting'];
692 }
693
694 return TRUE;
695 }
696
697 if (isset($values['postal_greeting'])) {
a7488080 698 if (!empty($params['postal_greeting_id'])) {
91bb24a7 699 $postalGreetingFilter = array(
700 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
6a488035
TO
701 'greeting_type' => 'postal_greeting',
702 );
703 $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter);
704 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
705 }
706 else {
707 $params['postal_greeting'] = $values['postal_greeting'];
708 }
709 return TRUE;
710 }
711
712 if (isset($values['addressee'])) {
a7488080 713 if (!empty($params['addressee_id'])) {
91bb24a7 714 $addresseeFilter = array(
715 'contact_type' => CRM_Utils_Array::value('contact_type', $params),
6a488035
TO
716 'greeting_type' => 'addressee',
717 );
718 $addressee = CRM_Core_PseudoConstant::addressee($addresseeFilter);
719 $params['addressee'] = $addressee[$params['addressee_id']];
720 }
721 else {
722 $params['addressee'] = $values['addressee'];
723 }
724 return TRUE;
725 }
726
727 if (isset($values['gender'])) {
a7488080 728 if (!empty($params['gender_id'])) {
91bb24a7 729 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
6a488035
TO
730 $params['gender'] = $genders[$params['gender_id']];
731 }
732 else {
733 $params['gender'] = $values['gender'];
734 }
735 return TRUE;
736 }
737
df5ad245 738 if (!empty($values['preferred_communication_method'])) {
91bb24a7 739 $comm = array();
740 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER);
6a488035
TO
741
742 $preffComm = explode(',', $values['preferred_communication_method']);
743 foreach ($preffComm as $v) {
744 $v = strtolower(trim($v));
745 if (array_key_exists($v, $pcm)) {
746 $comm[$pcm[$v]] = 1;
747 }
748 }
749
750 $params['preferred_communication_method'] = $comm;
751 return TRUE;
752 }
753
50bfb460 754 // format the website params.
a7488080 755 if (!empty($values['url'])) {
6a488035
TO
756 static $websiteFields;
757 if (!is_array($websiteFields)) {
758 require_once 'CRM/Core/DAO/Website.php';
759 $websiteFields = CRM_Core_DAO_Website::fields();
760 }
761 if (!array_key_exists('website', $params) ||
762 !is_array($params['website'])
763 ) {
764 $params['website'] = array();
765 }
766
767 $websiteCount = count($params['website']);
768 _civicrm_api3_store_values($websiteFields, $values,
769 $params['website'][++$websiteCount]
770 );
771
772 return TRUE;
773 }
774
775 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
a7488080 776 if (!empty($values['location_type_id'])) {
c193e786 777 static $fields = NULL;
778 if ($fields == NULL) {
779 $fields = array();
780 }
781
782 foreach (array(
783 'Phone',
784 'Email',
785 'IM',
786 'OpenID',
787 'Phone_Ext',
788 ) as $block) {
789 $name = strtolower($block);
790 if (!array_key_exists($name, $values)) {
791 continue;
792 }
793
794 if ($name == 'phone_ext') {
795 $block = 'Phone';
796 }
797
798 // block present in value array.
799 if (!array_key_exists($name, $params) || !is_array($params[$name])) {
800 $params[$name] = array();
801 }
802
803 if (!array_key_exists($block, $fields)) {
804 $className = "CRM_Core_DAO_$block";
805 $fields[$block] =& $className::fields();
806 }
807
808 $blockCnt = count($params[$name]);
809
810 // copy value to dao field name.
811 if ($name == 'im') {
812 $values['name'] = $values[$name];
813 }
814
815 _civicrm_api3_store_values($fields[$block], $values,
816 $params[$name][++$blockCnt]
817 );
818
819 if (empty($params['id']) && ($blockCnt == 1)) {
820 $params[$name][$blockCnt]['is_primary'] = TRUE;
821 }
822
823 // we only process single block at a time.
824 return TRUE;
825 }
826
827 // handle address fields.
828 if (!array_key_exists('address', $params) || !is_array($params['address'])) {
829 $params['address'] = array();
830 }
831
832 $addressCnt = 1;
833 foreach ($params['address'] as $cnt => $addressBlock) {
834 if (CRM_Utils_Array::value('location_type_id', $values) ==
835 CRM_Utils_Array::value('location_type_id', $addressBlock)
836 ) {
837 $addressCnt = $cnt;
838 break;
839 }
840 $addressCnt++;
841 }
842
843 if (!array_key_exists('Address', $fields)) {
844 $fields['Address'] = CRM_Core_DAO_Address::fields();
845 }
846
847 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
848 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
849 // the address in CRM_Core_BAO_Address::create method
850 if (!empty($values['location_type_id'])) {
851 static $customFields = array();
852 if (empty($customFields)) {
853 $customFields = CRM_Core_BAO_CustomField::getFields('Address');
854 }
855 // make a copy of values, as we going to make changes
856 $newValues = $values;
857 foreach ($values as $key => $val) {
858 $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
859 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
860 // mark an entry in fields array since we want the value of custom field to be copied
861 $fields['Address'][$key] = NULL;
862
863 $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]);
864 switch ($htmlType) {
865 case 'CheckBox':
866 case 'AdvMulti-Select':
867 case 'Multi-Select':
868 if ($val) {
869 $mulValues = explode(',', $val);
870 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
871 $newValues[$key] = array();
872 foreach ($mulValues as $v1) {
873 foreach ($customOption as $v2) {
874 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
875 (strtolower($v2['value']) == strtolower(trim($v1)))
876 ) {
877 if ($htmlType == 'CheckBox') {
878 $newValues[$key][$v2['value']] = 1;
879 }
880 else {
881 $newValues[$key][] = $v2['value'];
882 }
883 }
884 }
885 }
886 }
887 break;
888 }
889 }
890 }
891 // consider new values
892 $values = $newValues;
893 }
894
895 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
896
897 $addressFields = array(
898 'county',
899 'country',
900 'state_province',
901 'supplemental_address_1',
902 'supplemental_address_2',
903 'supplemental_address_3',
904 'StateProvince.name',
905 );
906
907 foreach ($addressFields as $field) {
908 if (array_key_exists($field, $values)) {
909 if (!array_key_exists('address', $params)) {
910 $params['address'] = array();
911 }
912 $params['address'][$addressCnt][$field] = $values[$field];
913 }
914 }
915
916 if ($addressCnt == 1) {
917
918 $params['address'][$addressCnt]['is_primary'] = TRUE;
919 }
6a488035
TO
920 return TRUE;
921 }
922
923 if (isset($values['note'])) {
50bfb460 924 // add a note field
6a488035
TO
925 if (!isset($params['note'])) {
926 $params['note'] = array();
927 }
928 $noteBlock = count($params['note']) + 1;
929
930 $params['note'][$noteBlock] = array();
931 if (!isset($fields['Note'])) {
932 $fields['Note'] = CRM_Core_DAO_Note::fields();
933 }
934
935 // get the current logged in civicrm user
936 $session = CRM_Core_Session::singleton();
937 $userID = $session->get('userID');
938
939 if ($userID) {
940 $values['contact_id'] = $userID;
941 }
942
943 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
944
945 return TRUE;
946 }
947
50bfb460 948 // Check for custom field values
6a488035 949
a7488080 950 if (empty($fields['custom'])) {
6a488035
TO
951 $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values),
952 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
953 );
954 }
955
956 foreach ($values as $key => $value) {
957 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
50bfb460 958 // check if it's a valid custom field id
6a488035 959
6a488035
TO
960 if (!array_key_exists($customFieldID, $fields['custom'])) {
961 return civicrm_api3_create_error('Invalid custom field ID');
962 }
963 else {
964 $params[$key] = $value;
965 }
966 }
967 }
968}
969
6a488035
TO
970/**
971 *
72b3a70c 972 * @param array $params
6a488035 973 *
a6c01b45 974 * @return array
353ffa53 975 * <type>
6a488035
TO
976 */
977function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
978 $id = CRM_Utils_Array::value('id', $params);
979 $externalId = CRM_Utils_Array::value('external_identifier', $params);
980 if ($id || $externalId) {
981 $contact = new CRM_Contact_DAO_Contact();
982
983 $contact->id = $id;
984 $contact->external_identifier = $externalId;
985
986 if ($contact->find(TRUE)) {
987 if ($params['contact_type'] != $contact->contact_type) {
988 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
989 }
990
991 $error = CRM_Core_Error::createError("Found matching contacts: $contact->id",
992 CRM_Core_Error::DUPLICATE_CONTACT,
993 'Fatal', $contact->id
994 );
995 return civicrm_api3_create_error($error->pop());
996 }
997 }
998 else {
03b40a7b 999 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised');
6a488035
TO
1000
1001 if (!empty($ids)) {
1002 $ids = implode(',', $ids);
1003 $error = CRM_Core_Error::createError("Found matching contacts: $ids",
1004 CRM_Core_Error::DUPLICATE_CONTACT,
1005 'Fatal', $ids
1006 );
1007 return civicrm_api3_create_error($error->pop());
1008 }
1009 }
1010 return civicrm_api3_create_success(TRUE);
1011}
1012
1013/**
1014 * Validate a formatted contact parameter list.
1015 *
77855840
TO
1016 * @param array $params
1017 * Structured parameter list (as in crm_format_params).
6a488035
TO
1018 *
1019 * @return bool|CRM_Core_Error
6a488035
TO
1020 */
1021function _civicrm_api3_deprecated_validate_formatted_contact(&$params) {
50bfb460 1022 // Look for offending email addresses
6a488035 1023
6a488035
TO
1024 if (array_key_exists('email', $params)) {
1025 foreach ($params['email'] as $count => $values) {
1026 if (!is_array($values)) {
1027 continue;
1028 }
1029 if ($email = CRM_Utils_Array::value('email', $values)) {
50bfb460 1030 // validate each email
6a488035
TO
1031 if (!CRM_Utils_Rule::email($email)) {
1032 return civicrm_api3_create_error('No valid email address');
1033 }
1034
50bfb460 1035 // check for loc type id.
a7488080 1036 if (empty($values['location_type_id'])) {
6a488035
TO
1037 return civicrm_api3_create_error('Location Type Id missing.');
1038 }
1039 }
1040 }
1041 }
1042
50bfb460 1043 // Validate custom data fields
6a488035
TO
1044 if (array_key_exists('custom', $params) && is_array($params['custom'])) {
1045 foreach ($params['custom'] as $key => $custom) {
1046 if (is_array($custom)) {
1047 foreach ($custom as $fieldId => $value) {
1048 $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value),
1049 CRM_Utils_Array::value('value', $value)
1050 );
70dd449f 1051 if (!$valid && $value['is_required']) {
6a488035
TO
1052 return civicrm_api3_create_error('Invalid value for custom field \'' .
1053 CRM_Utils_Array::value('name', $custom) . '\''
1054 );
1055 }
1056 if (CRM_Utils_Array::value('type', $custom) == 'Date') {
1057 $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
1058 }
1059 }
1060 }
1061 }
1062 }
1063
1064 return civicrm_api3_create_success(TRUE);
1065}
1066
6a488035 1067
6a488035
TO
1068/**
1069 * @deprecated - this is part of the import parser not the API & needs to be moved on out
1070 *
c490a46a 1071 * @param array $params
f4aaa82a 1072 * @param $onDuplicate
6a488035 1073 *
72b3a70c
CW
1074 * @return array|bool
1075 * <type>
6a488035
TO
1076 */
1077function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
1078 require_once 'CRM/Event/Import/Parser.php';
a05662ef 1079 if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) {
6a488035
TO
1080 CRM_Core_Error::reset();
1081 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
1082 if (civicrm_error($error)) {
1083 return $error;
1084 }
1085 }
1086 require_once "api/v3/Participant.php";
1087 return civicrm_api3_participant_create($params);
1088}
1089
1090/**
1091 *
72b3a70c 1092 * @param array $params
6a488035 1093 *
f4aaa82a
EM
1094 * @param bool $checkDuplicate
1095 *
72b3a70c
CW
1096 * @return array|bool
1097 * <type>
6a488035
TO
1098 */
1099function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
1100
50bfb460 1101 // check if participant id is valid or not
a7488080 1102 if (!empty($params['id'])) {
6a488035
TO
1103 $participant = new CRM_Event_BAO_Participant();
1104 $participant->id = $params['id'];
1105 if (!$participant->find(TRUE)) {
1106 return civicrm_api3_create_error(ts('Participant id is not valid'));
1107 }
1108 }
1109 require_once 'CRM/Contact/BAO/Contact.php';
50bfb460 1110 // check if contact id is valid or not
a7488080 1111 if (!empty($params['contact_id'])) {
6a488035
TO
1112 $contact = new CRM_Contact_BAO_Contact();
1113 $contact->id = $params['contact_id'];
1114 if (!$contact->find(TRUE)) {
1115 return civicrm_api3_create_error(ts('Contact id is not valid'));
1116 }
1117 }
1118
50bfb460 1119 // check that event id is not an template
a7488080 1120 if (!empty($params['event_id'])) {
6a488035
TO
1121 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
1122 if (!empty($isTemplate)) {
3770b22a 1123 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
6a488035
TO
1124 }
1125 }
1126
1127 $result = array();
1128 if ($checkDuplicate) {
1129 if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) {
1130 $participantID = array_pop($result);
1131
1132 $error = CRM_Core_Error::createError("Found matching participant record.",
1133 CRM_Core_Error::DUPLICATE_PARTICIPANT,
1134 'Fatal', $participantID
1135 );
1136
1137 return civicrm_api3_create_error($error->pop(),
1138 array(
1139 'contactID' => $params['contact_id'],
1140 'participantID' => $participantID,
1141 )
1142 );
1143 }
1144 }
1145 return TRUE;
1146}
1147
1148/**
1149 * Ensure that we have the right input parameters for custom data
1150 *
77855840
TO
1151 * @param array $params
1152 * Associative array of property name/value.
6a488035 1153 * pairs to insert in new contact.
77855840
TO
1154 * @param string $csType
1155 * Contact subtype if exists/passed.
6a488035 1156 *
a6c01b45 1157 * @return null
353ffa53 1158 * on success, error message otherwise
6a488035
TO
1159 */
1160function _civicrm_api3_deprecated_contact_check_custom_params($params, $csType = NULL) {
1161 empty($csType) ? $onlyParent = TRUE : $onlyParent = FALSE;
1162
1163 require_once 'CRM/Core/BAO/CustomField.php';
1164 $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'],
1165 FALSE,
1166 FALSE,
1167 $csType,
1168 NULL,
1169 $onlyParent,
1170 FALSE,
1171 FALSE
1172 );
1173
1174 foreach ($params as $key => $value) {
1175 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
50bfb460 1176 // check if it's a valid custom field id
6a488035
TO
1177 if (!array_key_exists($customFieldID, $customFields)) {
1178
1179 $errorMsg = "Invalid Custom Field Contact Type: {$params['contact_type']}";
1180 if (!empty($csType)) {
e7292422 1181 $errorMsg .= " or Mismatched SubType: " . implode(', ', (array) $csType);
6a488035
TO
1182 }
1183 return civicrm_api3_create_error($errorMsg);
1184 }
1185 }
1186 }
1187}
1188
5bc392e6 1189/**
c490a46a 1190 * @param array $params
5bc392e6 1191 * @param bool $dupeCheck
100fef9d 1192 * @param int $dedupeRuleGroupID
5bc392e6
EM
1193 *
1194 * @return array|null
1195 */
109ff2ac
DL
1196function _civicrm_api3_deprecated_contact_check_params(
1197 &$params,
1198 $dupeCheck = TRUE,
109ff2ac 1199 $dedupeRuleGroupID = NULL) {
ffec8e23 1200
1201 $requiredCheck = TRUE;
1202
6a488035
TO
1203 if (isset($params['id']) && is_numeric($params['id'])) {
1204 $requiredCheck = FALSE;
1205 }
1206 if ($requiredCheck) {
1207 if (isset($params['id'])) {
1208 $required = array('Individual', 'Household', 'Organization');
1209 }
1210 $required = array(
1211 'Individual' => array(
1212 array('first_name', 'last_name'),
1213 'email',
1214 ),
1215 'Household' => array(
1216 'household_name',
1217 ),
1218 'Organization' => array(
1219 'organization_name',
1220 ),
1221 );
1222
6a488035 1223 // contact_type has a limited number of valid values
22e263ad 1224 if (empty($params['contact_type'])) {
81fc1677
E
1225 return civicrm_api3_create_error("No Contact Type");
1226 }
6a488035
TO
1227 $fields = CRM_Utils_Array::value($params['contact_type'], $required);
1228 if ($fields == NULL) {
1229 return civicrm_api3_create_error("Invalid Contact Type: {$params['contact_type']}");
1230 }
1231
1232 if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) {
1233 if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) {
e7292422 1234 return civicrm_api3_create_error("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
6a488035
TO
1235 }
1236 }
1237
8cc574cf 1238 if (empty($params['contact_id']) && !empty($params['id'])) {
6a488035
TO
1239 $valid = FALSE;
1240 $error = '';
1241 foreach ($fields as $field) {
1242 if (is_array($field)) {
1243 $valid = TRUE;
1244 foreach ($field as $element) {
a7488080 1245 if (empty($params[$element])) {
6a488035
TO
1246 $valid = FALSE;
1247 $error .= $element;
1248 break;
1249 }
1250 }
1251 }
1252 else {
a7488080 1253 if (!empty($params[$field])) {
6a488035
TO
1254 $valid = TRUE;
1255 }
1256 }
1257 if ($valid) {
1258 break;
1259 }
1260 }
1261
1262 if (!$valid) {
1263 return civicrm_api3_create_error("Required fields not found for {$params['contact_type']} : $error");
1264 }
1265 }
1266 }
1267
1268 if ($dupeCheck) {
03b40a7b 1269 // @todo switch to using api version
eb5f7260 1270 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
1271 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
03b40a7b 1272 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', array(), CRM_Utils_Array::value('check_permissions', $params, $dedupeRuleGroupID));
6a488035 1273 if ($ids != NULL) {
ffec8e23 1274 $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids),
1275 CRM_Core_Error::DUPLICATE_CONTACT,
1276 'Fatal', $ids
1277 );
1278 return civicrm_api3_create_error($error->pop());
6a488035
TO
1279 }
1280 }
1281
50bfb460 1282 // check for organisations with same name
a7488080 1283 if (!empty($params['current_employer'])) {
03b40a7b 1284 $organizationParams = array('organization_name' => $params['current_employer']);
1285 $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', array(), FALSE);
6a488035
TO
1286
1287 // check for mismatch employer name and id
a7488080 1288 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
6a488035
TO
1289 ) {
1290 return civicrm_api3_create_error('Employer name and Employer id Mismatch');
1291 }
1292
1293 // show error if multiple organisation with same name exist
a7488080 1294 if (empty($params['employer_id']) && (count($dupeIds) > 1)
6a488035
TO
1295 ) {
1296 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
1297 }
1298 }
1299
1300 return NULL;
1301}
1302
109ff2ac 1303/**
f4aaa82a 1304 * @param $result
100fef9d 1305 * @param int $activityTypeID
f4aaa82a 1306 *
a6c01b45 1307 * @return array
353ffa53 1308 * <type> $params
109ff2ac
DL
1309 */
1310function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
1311 // get ready for collecting data about activity to be created
1312 $params = array();
1313
1314 $params['activity_type_id'] = $activityTypeID;
1315
040073c9 1316 $params['status_id'] = 'Completed';
3ba820ea
CB
1317 if (!empty($result['from']['id'])) {
1318 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
1319 }
109ff2ac
DL
1320 $params['target_contact_id'] = array();
1321 $keys = array('to', 'cc', 'bcc');
1322 foreach ($keys as $key) {
1323 if (is_array($result[$key])) {
1324 foreach ($result[$key] as $key => $keyValue) {
1325 if (!empty($keyValue['id'])) {
1326 $params['target_contact_id'][] = $keyValue['id'];
1327 }
1328 }
1329 }
1330 }
1331 $params['subject'] = $result['subject'];
1332 $params['activity_date_time'] = $result['date'];
1333 $params['details'] = $result['body'];
1334
1335 for ($i = 1; $i <= 5; $i++) {
1336 if (isset($result["attachFile_$i"])) {
1337 $params["attachFile_$i"] = $result["attachFile_$i"];
1338 }
1339 }
1340
1341 return $params;
1342}