[REF] Relocate function from DeprecatedUtils to the class that actually calls it
[civicrm-core.git] / CRM / Utils / DeprecatedUtils.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /*
19 * These functions have been deprecated out of API v3 Utils folder as they are not part of the
20 * API. Calling API functions directly is not supported & these functions are not called by any
21 * part of the API so are not really part of the api
22 *
23 */
24
25 require_once 'api/v3/utils.php';
26
27 /**
28 * Check duplicate contacts based on de-dupe parameters.
29 *
30 * @param array $params
31 *
32 * @return array
33 */
34 function _civicrm_api3_deprecated_check_contact_dedupe($params) {
35 static $cIndieFields = NULL;
36 static $defaultLocationId = NULL;
37
38 $contactType = $params['contact_type'];
39 if ($cIndieFields == NULL) {
40 require_once 'CRM/Contact/BAO/Contact.php';
41 $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType);
42 $cIndieFields = $cTempIndieFields;
43
44 require_once "CRM/Core/BAO/LocationType.php";
45 $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
46
47 // set the value to default location id else set to 1
48 if (!$defaultLocationId = (int) $defaultLocation->id) {
49 $defaultLocationId = 1;
50 }
51 }
52
53 require_once 'CRM/Contact/BAO/Query.php';
54 $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields;
55
56 $contactFormatted = [];
57 foreach ($params as $key => $field) {
58 if ($field == NULL || $field === '') {
59 continue;
60 }
61 // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
62 // instead of soft credit contact.
63 if (is_array($field) && $key != "soft_credit") {
64 foreach ($field as $value) {
65 $break = FALSE;
66 if (is_array($value)) {
67 foreach ($value as $name => $testForEmpty) {
68 if ($name !== 'phone_type' &&
69 ($testForEmpty === '' || $testForEmpty == NULL)
70 ) {
71 $break = TRUE;
72 break;
73 }
74 }
75 }
76 else {
77 $break = TRUE;
78 }
79 if (!$break) {
80 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
81 }
82 }
83 continue;
84 }
85
86 $value = [$key => $field];
87
88 // check if location related field, then we need to add primary location type
89 if (in_array($key, $locationFields)) {
90 $value['location_type_id'] = $defaultLocationId;
91 }
92 elseif (array_key_exists($key, $cIndieFields)) {
93 $value['contact_type'] = $contactType;
94 }
95
96 _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
97 }
98
99 $contactFormatted['contact_type'] = $contactType;
100
101 return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
102 }
103
104 /**
105 * take the input parameter list as specified in the data model and
106 * convert it into the same format that we use in QF and BAO object
107 *
108 * @param array $params
109 * Associative array of property name/value.
110 * pairs to insert in new contact.
111 * @param array $values
112 * The reformatted properties that we can use internally.
113 *
114 * @param array|bool $create Is the formatted Values array going to
115 * be used for CRM_Activity_BAO_Activity::create()
116 *
117 * @return array|CRM_Error
118 */
119 function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
120 // copy all the activity fields as is
121 $fields = CRM_Activity_DAO_Activity::fields();
122 _civicrm_api3_store_values($fields, $params, $values);
123
124 require_once 'CRM/Core/OptionGroup.php';
125 $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
126
127 foreach ($params as $key => $value) {
128 // ignore empty values or empty arrays etc
129 if (CRM_Utils_System::isNull($value)) {
130 continue;
131 }
132
133 //Handling Custom Data
134 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
135 $values[$key] = $value;
136 $type = $customFields[$customFieldID]['html_type'];
137 if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) {
138 $values[$key] = CRM_Import_Parser::unserializeCustomValue($customFieldID, $value, $type);
139 }
140 elseif ($type == 'Select' || $type == 'Radio') {
141 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
142 foreach ($customOption as $customFldID => $customValue) {
143 $val = $customValue['value'] ?? NULL;
144 $label = $customValue['label'] ?? NULL;
145 $label = strtolower($label);
146 $value = strtolower(trim($value));
147 if (($value == $label) || ($value == strtolower($val))) {
148 $values[$key] = $val;
149 }
150 }
151 }
152 }
153
154 if ($key == 'target_contact_id') {
155 if (!CRM_Utils_Rule::integer($value)) {
156 return civicrm_api3_create_error("contact_id not valid: $value");
157 }
158 $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
159 if (!$contactID) {
160 return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
161 }
162 }
163 }
164 return NULL;
165 }
166
167 /**
168 * This function adds the contact variable in $values to the
169 * parameter list $params. For most cases, $values should have length 1. If
170 * the variable being added is a child of Location, a location_type_id must
171 * also be included. If it is a child of phone, a phone_type must be included.
172 *
173 * @param array $values
174 * The variable(s) to be added.
175 * @param array $params
176 * The structured parameter list.
177 *
178 * @return bool|CRM_Utils_Error
179 */
180 function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) {
181 // Crawl through the possible classes:
182 // Contact
183 // Individual
184 // Household
185 // Organization
186 // Location
187 // Address
188 // Email
189 // Phone
190 // IM
191 // Note
192 // Custom
193
194 // Cache the various object fields
195 static $fields = NULL;
196
197 if ($fields == NULL) {
198 $fields = [];
199 }
200
201 // first add core contact values since for other Civi modules they are not added
202 require_once 'CRM/Contact/BAO/Contact.php';
203 $contactFields = CRM_Contact_DAO_Contact::fields();
204 _civicrm_api3_store_values($contactFields, $values, $params);
205
206 if (isset($values['contact_type'])) {
207 // we're an individual/household/org property
208
209 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact::fields();
210
211 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
212 return TRUE;
213 }
214
215 if (isset($values['individual_prefix'])) {
216 if (!empty($params['prefix_id'])) {
217 $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
218 $params['prefix'] = $prefixes[$params['prefix_id']];
219 }
220 else {
221 $params['prefix'] = $values['individual_prefix'];
222 }
223 return TRUE;
224 }
225
226 if (isset($values['individual_suffix'])) {
227 if (!empty($params['suffix_id'])) {
228 $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
229 $params['suffix'] = $suffixes[$params['suffix_id']];
230 }
231 else {
232 $params['suffix'] = $values['individual_suffix'];
233 }
234 return TRUE;
235 }
236
237 // CRM-4575
238 if (isset($values['email_greeting'])) {
239 if (!empty($params['email_greeting_id'])) {
240 $emailGreetingFilter = [
241 'contact_type' => $params['contact_type'] ?? NULL,
242 'greeting_type' => 'email_greeting',
243 ];
244 $emailGreetings = CRM_Core_PseudoConstant::greeting($emailGreetingFilter);
245 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
246 }
247 else {
248 $params['email_greeting'] = $values['email_greeting'];
249 }
250
251 return TRUE;
252 }
253
254 if (isset($values['postal_greeting'])) {
255 if (!empty($params['postal_greeting_id'])) {
256 $postalGreetingFilter = [
257 'contact_type' => $params['contact_type'] ?? NULL,
258 'greeting_type' => 'postal_greeting',
259 ];
260 $postalGreetings = CRM_Core_PseudoConstant::greeting($postalGreetingFilter);
261 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
262 }
263 else {
264 $params['postal_greeting'] = $values['postal_greeting'];
265 }
266 return TRUE;
267 }
268
269 if (isset($values['addressee'])) {
270 $params['addressee'] = $values['addressee'];
271 return TRUE;
272 }
273
274 if (isset($values['gender'])) {
275 if (!empty($params['gender_id'])) {
276 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
277 $params['gender'] = $genders[$params['gender_id']];
278 }
279 else {
280 $params['gender'] = $values['gender'];
281 }
282 return TRUE;
283 }
284
285 if (!empty($values['preferred_communication_method'])) {
286 $comm = [];
287 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER);
288
289 $preffComm = explode(',', $values['preferred_communication_method']);
290 foreach ($preffComm as $v) {
291 $v = strtolower(trim($v));
292 if (array_key_exists($v, $pcm)) {
293 $comm[$pcm[$v]] = 1;
294 }
295 }
296
297 $params['preferred_communication_method'] = $comm;
298 return TRUE;
299 }
300
301 // format the website params.
302 if (!empty($values['url'])) {
303 static $websiteFields;
304 if (!is_array($websiteFields)) {
305 require_once 'CRM/Core/DAO/Website.php';
306 $websiteFields = CRM_Core_DAO_Website::fields();
307 }
308 if (!array_key_exists('website', $params) ||
309 !is_array($params['website'])
310 ) {
311 $params['website'] = [];
312 }
313
314 $websiteCount = count($params['website']);
315 _civicrm_api3_store_values($websiteFields, $values,
316 $params['website'][++$websiteCount]
317 );
318
319 return TRUE;
320 }
321
322 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
323 if (!empty($values['location_type_id'])) {
324 static $fields = NULL;
325 if ($fields == NULL) {
326 $fields = [];
327 }
328
329 foreach ([
330 'Phone',
331 'Email',
332 'IM',
333 'OpenID',
334 'Phone_Ext',
335 ] as $block) {
336 $name = strtolower($block);
337 if (!array_key_exists($name, $values)) {
338 continue;
339 }
340
341 if ($name == 'phone_ext') {
342 $block = 'Phone';
343 }
344
345 // block present in value array.
346 if (!array_key_exists($name, $params) || !is_array($params[$name])) {
347 $params[$name] = [];
348 }
349
350 if (!array_key_exists($block, $fields)) {
351 $className = "CRM_Core_DAO_$block";
352 $fields[$block] =& $className::fields();
353 }
354
355 $blockCnt = count($params[$name]);
356
357 // copy value to dao field name.
358 if ($name == 'im') {
359 $values['name'] = $values[$name];
360 }
361
362 _civicrm_api3_store_values($fields[$block], $values,
363 $params[$name][++$blockCnt]
364 );
365
366 if (empty($params['id']) && ($blockCnt == 1)) {
367 $params[$name][$blockCnt]['is_primary'] = TRUE;
368 }
369
370 // we only process single block at a time.
371 return TRUE;
372 }
373
374 // handle address fields.
375 if (!array_key_exists('address', $params) || !is_array($params['address'])) {
376 $params['address'] = [];
377 }
378
379 $addressCnt = 1;
380 foreach ($params['address'] as $cnt => $addressBlock) {
381 if (CRM_Utils_Array::value('location_type_id', $values) ==
382 CRM_Utils_Array::value('location_type_id', $addressBlock)
383 ) {
384 $addressCnt = $cnt;
385 break;
386 }
387 $addressCnt++;
388 }
389
390 if (!array_key_exists('Address', $fields)) {
391 $fields['Address'] = CRM_Core_DAO_Address::fields();
392 }
393
394 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
395 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
396 // the address in CRM_Core_BAO_Address::create method
397 if (!empty($values['location_type_id'])) {
398 static $customFields = [];
399 if (empty($customFields)) {
400 $customFields = CRM_Core_BAO_CustomField::getFields('Address');
401 }
402 // make a copy of values, as we going to make changes
403 $newValues = $values;
404 foreach ($values as $key => $val) {
405 $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
406 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
407 // mark an entry in fields array since we want the value of custom field to be copied
408 $fields['Address'][$key] = NULL;
409
410 $htmlType = $customFields[$customFieldID]['html_type'] ?? NULL;
411 if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID]) && $val) {
412 $mulValues = explode(',', $val);
413 $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
414 $newValues[$key] = [];
415 foreach ($mulValues as $v1) {
416 foreach ($customOption as $v2) {
417 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
418 (strtolower($v2['value']) == strtolower(trim($v1)))
419 ) {
420 if ($htmlType == 'CheckBox') {
421 $newValues[$key][$v2['value']] = 1;
422 }
423 else {
424 $newValues[$key][] = $v2['value'];
425 }
426 }
427 }
428 }
429 }
430 }
431 }
432 // consider new values
433 $values = $newValues;
434 }
435
436 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]);
437
438 $addressFields = [
439 'county',
440 'country',
441 'state_province',
442 'supplemental_address_1',
443 'supplemental_address_2',
444 'supplemental_address_3',
445 'StateProvince.name',
446 ];
447
448 foreach ($addressFields as $field) {
449 if (array_key_exists($field, $values)) {
450 if (!array_key_exists('address', $params)) {
451 $params['address'] = [];
452 }
453 $params['address'][$addressCnt][$field] = $values[$field];
454 }
455 }
456
457 if ($addressCnt == 1) {
458
459 $params['address'][$addressCnt]['is_primary'] = TRUE;
460 }
461 return TRUE;
462 }
463
464 if (isset($values['note'])) {
465 // add a note field
466 if (!isset($params['note'])) {
467 $params['note'] = [];
468 }
469 $noteBlock = count($params['note']) + 1;
470
471 $params['note'][$noteBlock] = [];
472 if (!isset($fields['Note'])) {
473 $fields['Note'] = CRM_Core_DAO_Note::fields();
474 }
475
476 // get the current logged in civicrm user
477 $session = CRM_Core_Session::singleton();
478 $userID = $session->get('userID');
479
480 if ($userID) {
481 $values['contact_id'] = $userID;
482 }
483
484 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
485
486 return TRUE;
487 }
488
489 // Check for custom field values
490
491 if (empty($fields['custom'])) {
492 $fields['custom'] = &CRM_Core_BAO_CustomField::getFields(CRM_Utils_Array::value('contact_type', $values),
493 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
494 );
495 }
496
497 foreach ($values as $key => $value) {
498 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
499 // check if it's a valid custom field id
500
501 if (!array_key_exists($customFieldID, $fields['custom'])) {
502 return civicrm_api3_create_error('Invalid custom field ID');
503 }
504 else {
505 $params[$key] = $value;
506 }
507 }
508 }
509 }
510
511 /**
512 *
513 * @param array $params
514 *
515 * @return array
516 * <type>
517 */
518 function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
519 $id = $params['id'] ?? NULL;
520 $externalId = $params['external_identifier'] ?? NULL;
521 if ($id || $externalId) {
522 $contact = new CRM_Contact_DAO_Contact();
523
524 $contact->id = $id;
525 $contact->external_identifier = $externalId;
526
527 if ($contact->find(TRUE)) {
528 if ($params['contact_type'] != $contact->contact_type) {
529 return civicrm_api3_create_error("Mismatched contact IDs OR Mismatched contact Types");
530 }
531
532 $error = CRM_Core_Error::createError("Found matching contacts: $contact->id",
533 CRM_Core_Error::DUPLICATE_CONTACT,
534 'Fatal', $contact->id
535 );
536 return civicrm_api3_create_error($error->pop());
537 }
538 }
539 else {
540 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised');
541
542 if (!empty($ids)) {
543 $ids = implode(',', $ids);
544 $error = CRM_Core_Error::createError("Found matching contacts: $ids",
545 CRM_Core_Error::DUPLICATE_CONTACT,
546 'Fatal', $ids
547 );
548 return civicrm_api3_create_error($error->pop());
549 }
550 }
551 return civicrm_api3_create_success(TRUE);
552 }
553
554 /**
555 * @deprecated - this is part of the import parser not the API & needs to be moved on out
556 *
557 * @param array $params
558 * @param $onDuplicate
559 *
560 * @return array|bool
561 * <type>
562 */
563 function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
564 require_once 'CRM/Event/Import/Parser.php';
565 if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) {
566 CRM_Core_Error::reset();
567 $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
568 if (civicrm_error($error)) {
569 return $error;
570 }
571 }
572 require_once "api/v3/Participant.php";
573 return civicrm_api3_participant_create($params);
574 }
575
576 /**
577 *
578 * @param array $params
579 *
580 * @param bool $checkDuplicate
581 *
582 * @return array|bool
583 * <type>
584 */
585 function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
586
587 // check if participant id is valid or not
588 if (!empty($params['id'])) {
589 $participant = new CRM_Event_BAO_Participant();
590 $participant->id = $params['id'];
591 if (!$participant->find(TRUE)) {
592 return civicrm_api3_create_error(ts('Participant id is not valid'));
593 }
594 }
595 require_once 'CRM/Contact/BAO/Contact.php';
596 // check if contact id is valid or not
597 if (!empty($params['contact_id'])) {
598 $contact = new CRM_Contact_BAO_Contact();
599 $contact->id = $params['contact_id'];
600 if (!$contact->find(TRUE)) {
601 return civicrm_api3_create_error(ts('Contact id is not valid'));
602 }
603 }
604
605 // check that event id is not an template
606 if (!empty($params['event_id'])) {
607 $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
608 if (!empty($isTemplate)) {
609 return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
610 }
611 }
612
613 $result = [];
614 if ($checkDuplicate) {
615 if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) {
616 $participantID = array_pop($result);
617
618 $error = CRM_Core_Error::createError("Found matching participant record.",
619 CRM_Core_Error::DUPLICATE_PARTICIPANT,
620 'Fatal', $participantID
621 );
622
623 return civicrm_api3_create_error($error->pop(),
624 [
625 'contactID' => $params['contact_id'],
626 'participantID' => $participantID,
627 ]
628 );
629 }
630 }
631 return TRUE;
632 }
633
634 /**
635 * @param array $params
636 * @param bool $dupeCheck
637 * @param null|int $dedupeRuleGroupID
638 *
639 * @throws \CRM_Core_Exception
640 */
641 function _civicrm_api3_deprecated_contact_check_params(
642 &$params,
643 $dupeCheck = TRUE,
644 $dedupeRuleGroupID = NULL) {
645
646 $requiredCheck = TRUE;
647
648 if (isset($params['id']) && is_numeric($params['id'])) {
649 $requiredCheck = FALSE;
650 }
651 if ($requiredCheck) {
652 if (isset($params['id'])) {
653 $required = ['Individual', 'Household', 'Organization'];
654 }
655 $required = [
656 'Individual' => [
657 ['first_name', 'last_name'],
658 'email',
659 ],
660 'Household' => [
661 'household_name',
662 ],
663 'Organization' => [
664 'organization_name',
665 ],
666 ];
667
668 // contact_type has a limited number of valid values
669 if (empty($params['contact_type'])) {
670 throw new CRM_Core_Exception("No Contact Type");
671 }
672 $fields = $required[$params['contact_type']] ?? NULL;
673 if ($fields == NULL) {
674 throw new CRM_Core_Exception("Invalid Contact Type: {$params['contact_type']}");
675 }
676
677 if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) {
678 if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) {
679 throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
680 }
681 }
682
683 if (empty($params['contact_id']) && !empty($params['id'])) {
684 $valid = FALSE;
685 $error = '';
686 foreach ($fields as $field) {
687 if (is_array($field)) {
688 $valid = TRUE;
689 foreach ($field as $element) {
690 if (empty($params[$element])) {
691 $valid = FALSE;
692 $error .= $element;
693 break;
694 }
695 }
696 }
697 else {
698 if (!empty($params[$field])) {
699 $valid = TRUE;
700 }
701 }
702 if ($valid) {
703 break;
704 }
705 }
706
707 if (!$valid) {
708 throw new CRM_Core_Exception("Required fields not found for {$params['contact_type']} : $error");
709 }
710 }
711 }
712
713 if ($dupeCheck) {
714 // @todo switch to using api version
715 // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
716 // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
717 $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array::value('check_permissions', $params), $dedupeRuleGroupID);
718 if ($ids != NULL) {
719 $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids),
720 CRM_Core_Error::DUPLICATE_CONTACT,
721 'Fatal', $ids
722 );
723 return civicrm_api3_create_error($error->pop());
724 }
725 }
726
727 // check for organisations with same name
728 if (!empty($params['current_employer'])) {
729 $organizationParams = ['organization_name' => $params['current_employer']];
730 $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
731
732 // check for mismatch employer name and id
733 if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
734 ) {
735 throw new CRM_Core_Exception('Employer name and Employer id Mismatch');
736 }
737
738 // show error if multiple organisation with same name exist
739 if (empty($params['employer_id']) && (count($dupeIds) > 1)
740 ) {
741 return civicrm_api3_create_error('Found more than one Organisation with same Name.');
742 }
743 }
744 }
745
746 /**
747 * @param $result
748 * @param int $activityTypeID
749 *
750 * @return array
751 * <type> $params
752 */
753 function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
754 // get ready for collecting data about activity to be created
755 $params = [];
756
757 $params['activity_type_id'] = $activityTypeID;
758
759 $params['status_id'] = 'Completed';
760 if (!empty($result['from']['id'])) {
761 $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
762 }
763 $params['target_contact_id'] = [];
764 $keys = ['to', 'cc', 'bcc'];
765 foreach ($keys as $key) {
766 if (is_array($result[$key])) {
767 foreach ($result[$key] as $key => $keyValue) {
768 if (!empty($keyValue['id'])) {
769 $params['target_contact_id'][] = $keyValue['id'];
770 }
771 }
772 }
773 }
774 $params['subject'] = $result['subject'];
775 $params['activity_date_time'] = $result['date'];
776 $params['details'] = $result['body'];
777
778 $numAttachments = Civi::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND;
779 for ($i = 1; $i <= $numAttachments; $i++) {
780 if (isset($result["attachFile_$i"])) {
781 $params["attachFile_$i"] = $result["attachFile_$i"];
782 }
783 else {
784 // No point looping 100 times if there's only one attachment
785 break;
786 }
787 }
788
789 return $params;
790 }