3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 abstract class CRM_Contact_Import_Parser
extends CRM_Import_Parser
{
19 protected $_tableName;
22 * Total number of lines in file
29 * Running total number of un-matched Contacts.
33 protected $_unMatchCount;
36 * Array of unmatched lines.
43 * Total number of contacts with unparsed addresses
46 protected $_unparsedAddressCount;
49 * Filename of mismatch data
53 protected $_misMatchFilemName;
55 protected $_primaryKeyName;
56 protected $_statusFieldName;
58 protected $fieldMetadata = [];
67 * Dedupe rule group id to use if set
71 public $_dedupeRuleGroupID = NULL;
76 * @param string $tableName
77 * @param array $mapper
79 * @param int $contactType
80 * @param string $primaryKeyName
81 * @param string $statusFieldName
82 * @param int $onDuplicate
83 * @param int $statusID
84 * @param int $totalRowCount
85 * @param bool $doGeocodeAddress
87 * @param string $contactSubType
88 * @param int $dedupeRuleGroupID
95 $mode = self
::MODE_PREVIEW
,
96 $contactType = self
::CONTACT_INDIVIDUAL
,
97 $primaryKeyName = '_id',
98 $statusFieldName = '_status',
99 $onDuplicate = self
::DUPLICATE_SKIP
,
101 $totalRowCount = NULL,
102 $doGeocodeAddress = FALSE,
103 $timeout = CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
104 $contactSubType = NULL,
105 $dedupeRuleGroupID = NULL
108 // TODO: Make the timeout actually work
109 $this->_onDuplicate
= $onDuplicate;
110 $this->_dedupeRuleGroupID
= $dedupeRuleGroupID;
112 switch ($contactType) {
113 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
114 $this->_contactType
= 'Individual';
117 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
118 $this->_contactType
= 'Household';
121 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
122 $this->_contactType
= 'Organization';
125 $this->_contactSubType
= $contactSubType;
129 $this->_rowCount
= $this->_warningCount
= 0;
130 $this->_invalidRowCount
= $this->_validCount
= 0;
131 $this->_totalCount
= $this->_conflictCount
= 0;
134 $this->_warnings
= [];
135 $this->_conflicts
= [];
136 $this->_unparsedAddresses
= [];
138 $this->_tableName
= $tableName;
139 $this->_primaryKeyName
= $primaryKeyName;
140 $this->_statusFieldName
= $statusFieldName;
142 if ($mode == self
::MODE_MAPFIELD
) {
146 $this->_activeFieldCount
= count($this->_activeFields
);
149 if ($mode == self
::MODE_IMPORT
) {
150 //get the key of email field
151 foreach ($mapper as $key => $value) {
152 if (strtolower($value) == 'email') {
160 $this->progressImport($statusID);
161 $startTimestamp = $currTimestamp = $prevTimestamp = time();
163 // get the contents of the temp. import table
164 $query = "SELECT * FROM $tableName";
165 if ($mode == self
::MODE_IMPORT
) {
166 $query .= " WHERE $statusFieldName = 'NEW'";
169 $result = CRM_Core_DAO
::executeQuery($query);
171 while ($result->fetch()) {
172 $values = array_values($result->toArray());
175 /* trim whitespace around the values */
176 foreach ($values as $k => $v) {
177 $values[$k] = trim($v, " \t\r\n");
179 if (CRM_Utils_System
::isNull($values)) {
183 $this->_totalCount++
;
185 if ($mode == self
::MODE_MAPFIELD
) {
186 $returnCode = $this->mapField($values);
188 elseif ($mode == self
::MODE_PREVIEW
) {
189 $returnCode = $this->preview($values);
191 elseif ($mode == self
::MODE_SUMMARY
) {
192 $returnCode = $this->summary($values);
194 elseif ($mode == self
::MODE_IMPORT
) {
195 //print "Running parser in import mode<br/>\n";
196 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
197 if ($statusID && (($this->_rowCount %
50) == 0)) {
198 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
202 $returnCode = self
::ERROR
;
205 // note that a line could be valid but still produce a warning
206 if ($returnCode & self
::VALID
) {
207 $this->_validCount++
;
208 if ($mode == self
::MODE_MAPFIELD
) {
209 $this->_rows
[] = $values;
210 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
214 if ($returnCode & self
::WARNING
) {
215 $this->_warningCount++
;
216 if ($this->_warningCount
< $this->_maxWarningCount
) {
217 $this->_warningCount
[] = $line;
221 if ($returnCode & self
::ERROR
) {
222 $this->_invalidRowCount++
;
223 array_unshift($values, $this->_rowCount
);
224 $this->_errors
[] = $values;
227 if ($returnCode & self
::CONFLICT
) {
228 $this->_conflictCount++
;
229 array_unshift($values, $this->_rowCount
);
230 $this->_conflicts
[] = $values;
233 if ($returnCode & self
::NO_MATCH
) {
234 $this->_unMatchCount++
;
235 array_unshift($values, $this->_rowCount
);
236 $this->_unMatch
[] = $values;
239 if ($returnCode & self
::DUPLICATE
) {
240 if ($returnCode & self
::MULTIPLE_DUPE
) {
241 /* TODO: multi-dupes should be counted apart from singles
242 * on non-skip action */
244 $this->_duplicateCount++
;
245 array_unshift($values, $this->_rowCount
);
246 $this->_duplicates
[] = $values;
247 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
248 $this->_validCount++
;
252 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
253 $this->_unparsedAddressCount++
;
254 array_unshift($values, $this->_rowCount
);
255 $this->_unparsedAddresses
[] = $values;
257 // we give the derived class a way of aborting the process
258 // note that the return code could be multiple code or'ed together
259 if ($returnCode & self
::STOP
) {
263 // if we are done processing the maxNumber of lines, break
264 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
268 // see if we've hit our timeout yet
269 /* if ( $the_thing_with_the_stuff ) {
274 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
275 $customHeaders = $mapper;
277 $customfields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
278 foreach ($customHeaders as $key => $value) {
279 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
280 $customHeaders[$key] = $customfields[$id][0];
284 if ($this->_invalidRowCount
) {
285 // removed view url for invlaid contacts
286 $headers = array_merge([
290 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
291 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
293 if ($this->_conflictCount
) {
294 $headers = array_merge([
298 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
299 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
301 if ($this->_duplicateCount
) {
302 $headers = array_merge([
304 ts('View Contact URL'),
307 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
308 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
310 if ($this->_unMatchCount
) {
311 $headers = array_merge([
316 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
317 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
319 if ($this->_unparsedAddressCount
) {
320 $headers = array_merge([
322 ts('Contact Edit URL'),
324 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
325 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
328 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
329 return $this->fini();
333 * Given a list of the importable field keys that the user has selected.
334 * set the active fields array to this list
336 * @param array $fieldKeys
337 * Mapped array of values.
339 public function setActiveFields($fieldKeys) {
340 $this->_activeFieldCount
= count($fieldKeys);
341 foreach ($fieldKeys as $key) {
342 if (empty($this->_fields
[$key])) {
343 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
346 $this->_activeFields
[] = clone($this->_fields
[$key]);
354 public function setActiveFieldLocationTypes($elements) {
355 for ($i = 0; $i < count($elements); $i++
) {
356 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
367 public function setActiveFieldPhoneTypes($elements) {
368 for ($i = 0; $i < count($elements); $i++
) {
369 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
376 public function setActiveFieldWebsiteTypes($elements) {
377 for ($i = 0; $i < count($elements); $i++
) {
378 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
383 * Set IM Service Provider type fields.
385 * @param array $elements
386 * IM service provider type ids.
388 public function setActiveFieldImProviders($elements) {
389 for ($i = 0; $i < count($elements); $i++
) {
390 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
397 public function setActiveFieldRelated($elements) {
398 for ($i = 0; $i < count($elements); $i++
) {
399 $this->_activeFields
[$i]->_related
= $elements[$i];
406 public function setActiveFieldRelatedContactType($elements) {
407 for ($i = 0; $i < count($elements); $i++
) {
408 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
415 public function setActiveFieldRelatedContactDetails($elements) {
416 for ($i = 0; $i < count($elements); $i++
) {
417 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
424 public function setActiveFieldRelatedContactLocType($elements) {
425 for ($i = 0; $i < count($elements); $i++
) {
426 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
431 * Set active field for related contact's phone type.
433 * @param array $elements
435 public function setActiveFieldRelatedContactPhoneType($elements) {
436 for ($i = 0; $i < count($elements); $i++
) {
437 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
444 public function setActiveFieldRelatedContactWebsiteType($elements) {
445 for ($i = 0; $i < count($elements); $i++
) {
446 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
451 * Set IM Service Provider type fields for related contacts.
453 * @param array $elements
454 * IM service provider type ids of related contact.
456 public function setActiveFieldRelatedContactImProvider($elements) {
457 for ($i = 0; $i < count($elements); $i++
) {
458 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
463 * Format the field values for input to the api.
466 * (reference ) associative array of name/value pairs
468 public function &getActiveFieldParams() {
471 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
472 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
476 if (isset($this->_activeFields
[$i]->_value
)) {
477 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
478 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
479 $params[$this->_activeFields
[$i]->_name
] = [];
483 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
484 'location_type_id' => $this->_activeFields
[$i]->_hasLocationType
,
487 if (isset($this->_activeFields
[$i]->_phoneType
)) {
488 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
491 // get IM service Provider type id
492 if (isset($this->_activeFields
[$i]->_imProvider
)) {
493 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
496 $params[$this->_activeFields
[$i]->_name
][] = $value;
498 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
500 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
501 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
504 $params[$this->_activeFields
[$i]->_name
][] = $value;
507 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
508 if (!isset($this->_activeFields
[$i]->_related
)) {
509 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
513 //minor fix for CRM-4062
514 if (isset($this->_activeFields
[$i]->_related
)) {
515 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
516 $params[$this->_activeFields
[$i]->_related
] = [];
519 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
520 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
523 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
524 if (!empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
]) &&
525 !is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])
527 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = [];
530 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
531 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
534 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
535 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
538 // get IM service Provider type id for related contact
539 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
540 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
543 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
545 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
546 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = [
547 'url' => $this->_activeFields
[$i]->_value
,
548 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
551 elseif (empty($this->_activeFields
[$i]->_value
) && isset($this->_activeFields
[$i]->_relatedContactLocType
)) {
552 if (empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])) {
553 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = [];
557 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
569 public function getColumnPatterns() {
570 CRM_Core_Error
::deprecatedFunctionWarning('no longer used- use CRM_Contact_Import_MetadataTrait');
572 foreach ($this->_fields
as $name => $field) {
573 $values[$name] = $field->_columnPattern
;
579 * @param string $name
582 * @param string $headerPattern
583 * @param string $dataPattern
584 * @param bool $hasLocationType
586 public function addField(
587 $name, $title, $type = CRM_Utils_Type
::T_INT
,
588 $headerPattern = '//', $dataPattern = '//',
589 $hasLocationType = FALSE
591 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
593 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
598 * Store parser values.
600 * @param CRM_Core_Session $store
604 public function set($store, $mode = self
::MODE_SUMMARY
) {
605 $store->set('rowCount', $this->_rowCount
);
606 $store->set('fields', $this->getSelectValues());
607 $store->set('fieldTypes', $this->getSelectTypes());
609 $store->set('columnCount', $this->_activeFieldCount
);
611 $store->set('totalRowCount', $this->_totalCount
);
612 $store->set('validRowCount', $this->_validCount
);
613 $store->set('invalidRowCount', $this->_invalidRowCount
);
614 $store->set('conflictRowCount', $this->_conflictCount
);
615 $store->set('unMatchCount', $this->_unMatchCount
);
617 switch ($this->_contactType
) {
619 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
623 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
627 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
630 if ($this->_invalidRowCount
) {
631 $store->set('errorsFileName', $this->_errorFileName
);
633 if ($this->_conflictCount
) {
634 $store->set('conflictsFileName', $this->_conflictFileName
);
636 if (isset($this->_rows
) && !empty($this->_rows
)) {
637 $store->set('dataValues', $this->_rows
);
640 if ($this->_unMatchCount
) {
641 $store->set('mismatchFileName', $this->_misMatchFilemName
);
644 if ($mode == self
::MODE_IMPORT
) {
645 $store->set('duplicateRowCount', $this->_duplicateCount
);
646 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
647 if ($this->_duplicateCount
) {
648 $store->set('duplicatesFileName', $this->_duplicateFileName
);
650 if ($this->_unparsedAddressCount
) {
651 $store->set('errorsFileName', $this->_errorFileName
);
654 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
658 * Export data to a CSV file.
660 * @param string $fileName
661 * @param array $header
664 public static function exportCSV($fileName, $header, $data) {
666 if (file_exists($fileName) && !is_writable($fileName)) {
667 CRM_Core_Error
::movedSiteError($fileName);
669 //hack to remove '_status', '_statusMsg' and '_id' from error file
671 $dbRecordStatus = ['IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'];
672 foreach ($data as $rowCount => $rowValues) {
674 foreach ($rowValues as $key => $val) {
675 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
678 $errorValues[$rowCount][$key] = $val;
682 $data = $errorValues;
685 $fd = fopen($fileName, 'w');
687 foreach ($header as $key => $value) {
688 $header[$key] = "\"$value\"";
690 $config = CRM_Core_Config
::singleton();
691 $output[] = implode($config->fieldSeparator
, $header);
693 foreach ($data as $datum) {
694 foreach ($datum as $key => $value) {
695 $datum[$key] = "\"$value\"";
697 $output[] = implode($config->fieldSeparator
, $datum);
699 fwrite($fd, implode("\n", $output));
704 * Update the record with PK $id in the import database table.
707 * @param array $params
709 public function updateImportRecord($id, &$params) {
710 $statusFieldName = $this->_statusFieldName
;
711 $primaryKeyName = $this->_primaryKeyName
;
713 if ($statusFieldName && $primaryKeyName) {
714 $dao = new CRM_Core_DAO();
715 $db = $dao->getDatabaseConnection();
717 $query = "UPDATE $this->_tableName
718 SET $statusFieldName = ?,
719 ${statusFieldName}Msg = ?
720 WHERE $primaryKeyName = ?";
722 $params[$statusFieldName],
723 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
727 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
729 $db->query($query, $args);
734 * Format common params data to proper format to store.
736 * @param array $params
737 * Contain record values.
738 * @param array $formatted
739 * Array of formatted data.
740 * @param array $contactFields
741 * Contact DAO fields.
743 public function formatCommonData($params, &$formatted, &$contactFields) {
745 CRM_Utils_Array
::value('contact_type', $formatted),
749 //add custom fields for contact sub type
750 if (!empty($this->_contactSubType
)) {
751 $csType = $this->_contactSubType
;
754 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
755 $csType = $relCsType;
758 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
760 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
761 $customFields = $customFields +
$addressCustomFields;
763 //if a Custom Email Greeting, Custom Postal Greeting or Custom Addressee is mapped, and no "Greeting / Addressee Type ID" is provided, then automatically set the type = Customized, CRM-4575
765 'email_greeting_custom' => 'email_greeting',
766 'postal_greeting_custom' => 'postal_greeting',
767 'addressee_custom' => 'addressee',
769 foreach ($elements as $k => $v) {
770 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
771 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
772 $params[$v] = $label;
777 $session = CRM_Core_Session
::singleton();
778 $dateType = $session->get("dateTypes");
779 foreach ($params as $key => $val) {
780 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
781 if ($customFieldID &&
782 !array_key_exists($customFieldID, $addressCustomFields)
784 //we should not update Date to null, CRM-4062
785 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
787 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
789 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
790 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
791 //retain earlier value when Import mode is `Fill`
792 unset($params[$key]);
795 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
800 if ($key == 'birth_date' && $val) {
801 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
803 elseif ($key == 'deceased_date' && $val) {
804 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
805 $params['is_deceased'] = 1;
807 elseif ($key == 'is_deceased' && $val) {
808 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
812 //now format custom data.
813 foreach ($params as $key => $field) {
814 if (is_array($field)) {
815 $isAddressCustomField = FALSE;
816 foreach ($field as $value) {
818 if (is_array($value)) {
819 foreach ($value as $name => $testForEmpty) {
820 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
821 $isAddressCustomField = TRUE;
824 // check if $value does not contain IM provider or phoneType
825 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
836 if (!empty($value['location_type_id'])) {
837 $this->formatLocationBlock($value, $formatted);
840 // @todo - this is still reachable - e.g. import with related contact info like firstname,lastname,spouse-first-name,spouse-last-name,spouse-home-phone
841 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
842 $this->formatContactParameters($value, $formatted);
846 if (!$isAddressCustomField) {
855 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
856 // due to merging of individual table and
857 // contact table, we need to avoid
858 // preferred_communication_method forcefully
859 $formatValues['contact_type'] = $formatted['contact_type'];
862 if ($key == 'id' && isset($field)) {
863 $formatted[$key] = $field;
865 $this->formatContactParameters($formatValues, $formatted);
867 //Handling Custom Data
868 // note: Address custom fields will be handled separately inside formatContactParameters
869 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
870 array_key_exists($customFieldID, $customFields) &&
871 !array_key_exists($customFieldID, $addressCustomFields)
874 $extends = $customFields[$customFieldID]['extends'] ??
NULL;
875 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
876 $dataType = $customFields[$customFieldID]['data_type'] ??
NULL;
877 $serialized = CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]);
879 if (!$serialized && in_array($htmlType, ['Select', 'Radio', 'Autocomplete-Select']) && in_array($dataType, ['String', 'Int'])) {
880 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
881 foreach ($customOption as $customValue) {
882 $val = $customValue['value'] ??
NULL;
883 $label = strtolower($customValue['label'] ??
'');
884 $value = strtolower(trim($formatted[$key]));
885 if (($value == $label) ||
($value == strtolower($val))) {
886 $params[$key] = $formatted[$key] = $val;
890 elseif ($serialized && !empty($formatted[$key]) && !empty($params[$key])) {
891 $mulValues = explode(',', $formatted[$key]);
892 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
893 $formatted[$key] = [];
895 foreach ($mulValues as $v1) {
896 foreach ($customOption as $v2) {
897 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
898 (strtolower($v2['value']) == strtolower(trim($v1)))
900 if ($htmlType == 'CheckBox') {
901 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
904 $params[$key][] = $formatted[$key][] = $v2['value'];
913 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
914 !array_key_exists($customFieldID, $addressCustomFields)
916 // @todo calling api functions directly is not supported
917 _civicrm_api3_custom_format_params($params, $formatted, $extends);
920 // to check if not update mode and unset the fields with empty value.
921 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
922 foreach ($formatted['custom'] as $customKey => $customvalue) {
923 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
924 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
926 $emptyValue = $customvalue[-1]['value'] ??
NULL;
927 if (!isset($emptyValue)) {
928 unset($formatted['custom'][$customKey]);
933 // parse street address, CRM-5450
934 if ($this->_parseStreetAddress
) {
935 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
936 foreach ($formatted['address'] as $instance => & $address) {
937 $streetAddress = $address['street_address'] ??
NULL;
938 if (empty($streetAddress)) {
941 // parse address field.
942 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
944 //street address consider to be parsed properly,
945 //If we get street_name and street_number.
946 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
947 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
950 // merge parse address w/ main address block.
951 $address = array_merge($address, $parsedFields);
958 * Format contact parameters.
960 * @todo this function needs re-writing & re-merging into the main function.
964 * @param array $values
965 * @param array $params
969 protected function formatContactParameters(&$values, &$params) {
970 // Crawl through the possible classes:
983 // first add core contact values since for other Civi modules they are not added
984 $contactFields = CRM_Contact_DAO_Contact
::fields();
985 _civicrm_api3_store_values($contactFields, $values, $params);
987 if (isset($values['contact_type'])) {
988 // we're an individual/household/org property
990 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
992 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
996 // Cache the various object fields
997 // @todo - remove this after confirming this is just a compilation of other-wise-cached fields.
1000 if (isset($values['individual_prefix'])) {
1001 if (!empty($params['prefix_id'])) {
1002 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1003 $params['prefix'] = $prefixes[$params['prefix_id']];
1006 $params['prefix'] = $values['individual_prefix'];
1011 if (isset($values['individual_suffix'])) {
1012 if (!empty($params['suffix_id'])) {
1013 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1014 $params['suffix'] = $suffixes[$params['suffix_id']];
1017 $params['suffix'] = $values['individual_suffix'];
1023 if (isset($values['email_greeting'])) {
1024 if (!empty($params['email_greeting_id'])) {
1025 $emailGreetingFilter = [
1026 'contact_type' => $params['contact_type'] ??
NULL,
1027 'greeting_type' => 'email_greeting',
1029 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
1030 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1033 $params['email_greeting'] = $values['email_greeting'];
1039 if (isset($values['postal_greeting'])) {
1040 if (!empty($params['postal_greeting_id'])) {
1041 $postalGreetingFilter = [
1042 'contact_type' => $params['contact_type'] ??
NULL,
1043 'greeting_type' => 'postal_greeting',
1045 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
1046 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1049 $params['postal_greeting'] = $values['postal_greeting'];
1054 if (isset($values['addressee'])) {
1055 if (!empty($params['addressee_id'])) {
1056 $addresseeFilter = [
1057 'contact_type' => $params['contact_type'] ??
NULL,
1058 'greeting_type' => 'addressee',
1060 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
1061 $params['addressee'] = $addressee[$params['addressee_id']];
1064 $params['addressee'] = $values['addressee'];
1069 if (isset($values['gender'])) {
1070 if (!empty($params['gender_id'])) {
1071 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1072 $params['gender'] = $genders[$params['gender_id']];
1075 $params['gender'] = $values['gender'];
1080 if (!empty($values['preferred_communication_method'])) {
1082 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
1084 $preffComm = explode(',', $values['preferred_communication_method']);
1085 foreach ($preffComm as $v) {
1086 $v = strtolower(trim($v));
1087 if (array_key_exists($v, $pcm)) {
1088 $comm[$pcm[$v]] = 1;
1092 $params['preferred_communication_method'] = $comm;
1096 // format the website params.
1097 if (!empty($values['url'])) {
1098 static $websiteFields;
1099 if (!is_array($websiteFields)) {
1100 $websiteFields = CRM_Core_DAO_Website
::fields();
1102 if (!array_key_exists('website', $params) ||
1103 !is_array($params['website'])
1105 $params['website'] = [];
1108 $websiteCount = count($params['website']);
1109 _civicrm_api3_store_values($websiteFields, $values,
1110 $params['website'][++
$websiteCount]
1116 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1117 if (!empty($values['location_type_id'])) {
1118 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
1119 return $this->formatLocationBlock($values, $params);
1122 if (isset($values['note'])) {
1124 if (!isset($params['note'])) {
1125 $params['note'] = [];
1127 $noteBlock = count($params['note']) +
1;
1129 $params['note'][$noteBlock] = [];
1130 if (!isset($fields['Note'])) {
1131 $fields['Note'] = CRM_Core_DAO_Note
::fields();
1134 // get the current logged in civicrm user
1135 $session = CRM_Core_Session
::singleton();
1136 $userID = $session->get('userID');
1139 $values['contact_id'] = $userID;
1142 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1147 // Check for custom field values
1148 $customFields = CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
1149 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1152 foreach ($values as $key => $value) {
1153 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1154 // check if it's a valid custom field id
1156 if (!array_key_exists($customFieldID, $customFields)) {
1157 return civicrm_api3_create_error('Invalid custom field ID');
1160 $params[$key] = $value;
1168 * Format location block ready for importing.
1170 * There is some test coverage for this in CRM_Contact_Import_Parser_ContactTest
1171 * e.g. testImportPrimaryAddress.
1173 * @param array $values
1174 * @param array $params
1178 protected function formatLocationBlock(&$values, &$params) {
1183 'openid' => 'OpenID',
1184 'phone_ext' => 'Phone',
1186 foreach ($blockTypes as $blockFieldName => $block) {
1187 if (!array_key_exists($blockFieldName, $values)) {
1190 $blockIndex = $values['location_type_id'] . (!empty($values['phone_type_id']) ?
'_' . $values['phone_type_id'] : '');
1192 // block present in value array.
1193 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
1194 $params[$blockFieldName] = [];
1197 $fields[$block] = $this->getMetadataForEntity($block);
1199 // copy value to dao field name.
1200 if ($blockFieldName == 'im') {
1201 $values['name'] = $values[$blockFieldName];
1204 _civicrm_api3_store_values($fields[$block], $values,
1205 $params[$blockFieldName][$blockIndex]
1208 $this->fillPrimary($params[$blockFieldName][$blockIndex], $values, $block, CRM_Utils_Array
::value('id', $params));
1210 if (empty($params['id']) && (count($params[$blockFieldName]) == 1)) {
1211 $params[$blockFieldName][$blockIndex]['is_primary'] = TRUE;
1214 // we only process single block at a time.
1218 // handle address fields.
1219 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1220 $params['address'] = [];
1223 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1224 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1225 // the address in CRM_Core_BAO_Address::create method
1226 if (!empty($values['location_type_id'])) {
1227 static $customFields = [];
1228 if (empty($customFields)) {
1229 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1231 // make a copy of values, as we going to make changes
1232 $newValues = $values;
1233 foreach ($values as $key => $val) {
1234 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1235 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1237 $htmlType = $customFields[$customFieldID]['html_type'] ??
NULL;
1238 if (CRM_Core_BAO_CustomField
::isSerialized($customFields[$customFieldID]) && $val) {
1239 $mulValues = explode(',', $val);
1240 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1241 $newValues[$key] = [];
1242 foreach ($mulValues as $v1) {
1243 foreach ($customOption as $v2) {
1244 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1245 (strtolower($v2['value']) == strtolower(trim($v1)))
1247 if ($htmlType == 'CheckBox') {
1248 $newValues[$key][$v2['value']] = 1;
1251 $newValues[$key][] = $v2['value'];
1259 // consider new values
1260 $values = $newValues;
1263 $fields['Address'] = $this->getMetadataForEntity('Address');
1264 // @todo this is kinda replicated below....
1265 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1271 'supplemental_address_1',
1272 'supplemental_address_2',
1273 'supplemental_address_3',
1274 'StateProvince.name',
1276 foreach (array_keys($customFields) as $customFieldID) {
1277 $addressFields[] = 'custom_' . $customFieldID;
1280 foreach ($addressFields as $field) {
1281 if (array_key_exists($field, $values)) {
1282 if (!array_key_exists('address', $params)) {
1283 $params['address'] = [];
1285 $params['address'][$values['location_type_id']][$field] = $values[$field];
1289 $this->fillPrimary($params['address'][$values['location_type_id']], $values, 'address', CRM_Utils_Array
::value('id', $params));
1294 * Get the field metadata for the relevant entity.
1296 * @param string $entity
1300 protected function getMetadataForEntity($entity) {
1301 if (!isset($this->fieldMetadata
[$entity])) {
1302 $className = "CRM_Core_DAO_$entity";
1303 $this->fieldMetadata
[$entity] = $className::fields();
1305 return $this->fieldMetadata
[$entity];
1309 * Fill in the primary location.
1311 * If the contact has a primary address we update it. Otherwise
1312 * we add an address of the default location type.
1314 * @param array $params
1315 * Address block parameters
1316 * @param array $values
1318 * @param string $entity
1319 * - address, email, phone
1320 * @param int|null $contactID
1322 * @throws \CiviCRM_API3_Exception
1324 protected function fillPrimary(&$params, $values, $entity, $contactID) {
1325 if ($values['location_type_id'] === 'Primary') {
1327 $primary = civicrm_api3($entity, 'get', [
1328 'return' => 'location_type_id',
1329 'contact_id' => $contactID,
1334 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1335 $params['location_type_id'] = (int) (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1336 $params['is_primary'] = 1;