3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 abstract class CRM_Contact_Import_Parser
extends CRM_Import_Parser
{
35 protected $_tableName;
38 * Total number of lines in file
45 * Running total number of un-matched Contacts.
48 protected $_unMatchCount;
51 * Array of unmatched lines
57 * Total number of contacts with unparsed addresses
60 protected $_unparsedAddressCount;
63 * Filename of mismatch data
67 protected $_misMatchFilemName;
69 protected $_primaryKeyName;
70 protected $_statusFieldName;
80 * Dedupe rule group id to use if set
84 public $_dedupeRuleGroupID = NULL;
89 * @param string $tableName
90 * @param array $mapper
92 * @param int $contactType
93 * @param string $primaryKeyName
94 * @param string $statusFieldName
95 * @param int $onDuplicate
96 * @param int $statusID
97 * @param int $totalRowCount
98 * @param bool $doGeocodeAddress
100 * @param string $contactSubType
101 * @param int $dedupeRuleGroupID
108 $mode = self
::MODE_PREVIEW
,
109 $contactType = self
::CONTACT_INDIVIDUAL
,
110 $primaryKeyName = '_id',
111 $statusFieldName = '_status',
112 $onDuplicate = self
::DUPLICATE_SKIP
,
114 $totalRowCount = NULL,
115 $doGeocodeAddress = FALSE,
116 $timeout = CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
117 $contactSubType = NULL,
118 $dedupeRuleGroupID = NULL
121 // TODO: Make the timeout actually work
122 $this->_onDuplicate
= $onDuplicate;
123 $this->_dedupeRuleGroupID
= $dedupeRuleGroupID;
125 switch ($contactType) {
126 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
127 $this->_contactType
= 'Individual';
130 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
131 $this->_contactType
= 'Household';
134 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
135 $this->_contactType
= 'Organization';
138 $this->_contactSubType
= $contactSubType;
142 $this->_rowCount
= $this->_warningCount
= 0;
143 $this->_invalidRowCount
= $this->_validCount
= 0;
144 $this->_totalCount
= $this->_conflictCount
= 0;
147 $this->_warnings
= [];
148 $this->_conflicts
= [];
149 $this->_unparsedAddresses
= [];
151 $this->_tableName
= $tableName;
152 $this->_primaryKeyName
= $primaryKeyName;
153 $this->_statusFieldName
= $statusFieldName;
155 if ($mode == self
::MODE_MAPFIELD
) {
159 $this->_activeFieldCount
= count($this->_activeFields
);
162 if ($mode == self
::MODE_IMPORT
) {
163 //get the key of email field
164 foreach ($mapper as $key => $value) {
165 if (strtolower($value) == 'email') {
173 $this->progressImport($statusID);
174 $startTimestamp = $currTimestamp = $prevTimestamp = time();
176 // get the contents of the temp. import table
177 $query = "SELECT * FROM $tableName";
178 if ($mode == self
::MODE_IMPORT
) {
179 $query .= " WHERE $statusFieldName = 'NEW'";
181 $dao = new CRM_Core_DAO();
182 $db = $dao->getDatabaseConnection();
183 $result = $db->query($query);
185 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED
)) {
188 /* trim whitespace around the values */
189 foreach ($values as $k => $v) {
190 $values[$k] = trim($v, " \t\r\n");
192 if (CRM_Utils_System
::isNull($values)) {
196 $this->_totalCount++
;
198 if ($mode == self
::MODE_MAPFIELD
) {
199 $returnCode = $this->mapField($values);
201 elseif ($mode == self
::MODE_PREVIEW
) {
202 $returnCode = $this->preview($values);
204 elseif ($mode == self
::MODE_SUMMARY
) {
205 $returnCode = $this->summary($values);
207 elseif ($mode == self
::MODE_IMPORT
) {
208 //print "Running parser in import mode<br/>\n";
209 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
210 if ($statusID && (($this->_rowCount %
50) == 0)) {
211 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
215 $returnCode = self
::ERROR
;
218 // note that a line could be valid but still produce a warning
219 if ($returnCode & self
::VALID
) {
220 $this->_validCount++
;
221 if ($mode == self
::MODE_MAPFIELD
) {
222 $this->_rows
[] = $values;
223 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
227 if ($returnCode & self
::WARNING
) {
228 $this->_warningCount++
;
229 if ($this->_warningCount
< $this->_maxWarningCount
) {
230 $this->_warningCount
[] = $line;
234 if ($returnCode & self
::ERROR
) {
235 $this->_invalidRowCount++
;
236 array_unshift($values, $this->_rowCount
);
237 $this->_errors
[] = $values;
240 if ($returnCode & self
::CONFLICT
) {
241 $this->_conflictCount++
;
242 array_unshift($values, $this->_rowCount
);
243 $this->_conflicts
[] = $values;
246 if ($returnCode & self
::NO_MATCH
) {
247 $this->_unMatchCount++
;
248 array_unshift($values, $this->_rowCount
);
249 $this->_unMatch
[] = $values;
252 if ($returnCode & self
::DUPLICATE
) {
253 if ($returnCode & self
::MULTIPLE_DUPE
) {
254 /* TODO: multi-dupes should be counted apart from singles
255 * on non-skip action */
257 $this->_duplicateCount++
;
258 array_unshift($values, $this->_rowCount
);
259 $this->_duplicates
[] = $values;
260 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
261 $this->_validCount++
;
265 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
266 $this->_unparsedAddressCount++
;
267 array_unshift($values, $this->_rowCount
);
268 $this->_unparsedAddresses
[] = $values;
270 // we give the derived class a way of aborting the process
271 // note that the return code could be multiple code or'ed together
272 if ($returnCode & self
::STOP
) {
276 // if we are done processing the maxNumber of lines, break
277 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
281 // see if we've hit our timeout yet
282 /* if ( $the_thing_with_the_stuff ) {
287 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
288 $customHeaders = $mapper;
290 $customfields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
291 foreach ($customHeaders as $key => $value) {
292 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
293 $customHeaders[$key] = $customfields[$id][0];
297 if ($this->_invalidRowCount
) {
298 // removed view url for invlaid contacts
299 $headers = array_merge([
303 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
304 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
306 if ($this->_conflictCount
) {
307 $headers = array_merge([
311 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
312 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
314 if ($this->_duplicateCount
) {
315 $headers = array_merge([
317 ts('View Contact URL'),
320 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
321 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
323 if ($this->_unMatchCount
) {
324 $headers = array_merge([
329 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
330 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
332 if ($this->_unparsedAddressCount
) {
333 $headers = array_merge([
335 ts('Contact Edit URL'),
337 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
338 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
341 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
342 return $this->fini();
346 * Given a list of the importable field keys that the user has selected.
347 * set the active fields array to this list
349 * @param array $fieldKeys
350 * Mapped array of values.
352 public function setActiveFields($fieldKeys) {
353 $this->_activeFieldCount
= count($fieldKeys);
354 foreach ($fieldKeys as $key) {
355 if (empty($this->_fields
[$key])) {
356 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
359 $this->_activeFields
[] = clone($this->_fields
[$key]);
367 public function setActiveFieldLocationTypes($elements) {
368 for ($i = 0; $i < count($elements); $i++
) {
369 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
380 public function setActiveFieldPhoneTypes($elements) {
381 for ($i = 0; $i < count($elements); $i++
) {
382 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
389 public function setActiveFieldWebsiteTypes($elements) {
390 for ($i = 0; $i < count($elements); $i++
) {
391 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
396 * Set IM Service Provider type fields.
398 * @param array $elements
399 * IM service provider type ids.
401 public function setActiveFieldImProviders($elements) {
402 for ($i = 0; $i < count($elements); $i++
) {
403 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
410 public function setActiveFieldRelated($elements) {
411 for ($i = 0; $i < count($elements); $i++
) {
412 $this->_activeFields
[$i]->_related
= $elements[$i];
419 public function setActiveFieldRelatedContactType($elements) {
420 for ($i = 0; $i < count($elements); $i++
) {
421 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
428 public function setActiveFieldRelatedContactDetails($elements) {
429 for ($i = 0; $i < count($elements); $i++
) {
430 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
437 public function setActiveFieldRelatedContactLocType($elements) {
438 for ($i = 0; $i < count($elements); $i++
) {
439 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
444 * Set active field for related contact's phone type.
446 * @param array $elements
448 public function setActiveFieldRelatedContactPhoneType($elements) {
449 for ($i = 0; $i < count($elements); $i++
) {
450 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
457 public function setActiveFieldRelatedContactWebsiteType($elements) {
458 for ($i = 0; $i < count($elements); $i++
) {
459 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
464 * Set IM Service Provider type fields for related contacts.
466 * @param array $elements
467 * IM service provider type ids of related contact.
469 public function setActiveFieldRelatedContactImProvider($elements) {
470 for ($i = 0; $i < count($elements); $i++
) {
471 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
476 * Format the field values for input to the api.
479 * (reference ) associative array of name/value pairs
481 public function &getActiveFieldParams() {
484 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
485 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
489 if (isset($this->_activeFields
[$i]->_value
)) {
490 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
491 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
492 $params[$this->_activeFields
[$i]->_name
] = [];
496 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
497 'location_type_id' => $this->_activeFields
[$i]->_hasLocationType
,
500 if (isset($this->_activeFields
[$i]->_phoneType
)) {
501 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
504 // get IM service Provider type id
505 if (isset($this->_activeFields
[$i]->_imProvider
)) {
506 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
509 $params[$this->_activeFields
[$i]->_name
][] = $value;
511 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
513 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
514 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
517 $params[$this->_activeFields
[$i]->_name
][] = $value;
520 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
521 if (!isset($this->_activeFields
[$i]->_related
)) {
522 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
526 //minor fix for CRM-4062
527 if (isset($this->_activeFields
[$i]->_related
)) {
528 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
529 $params[$this->_activeFields
[$i]->_related
] = [];
532 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
533 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
536 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
537 if (!empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
]) &&
538 !is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])
540 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = [];
543 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
544 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
547 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
548 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
551 // get IM service Provider type id for related contact
552 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
553 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
556 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
558 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
559 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = [
560 'url' => $this->_activeFields
[$i]->_value
,
561 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
565 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
577 public function getColumnPatterns() {
579 foreach ($this->_fields
as $name => $field) {
580 $values[$name] = $field->_columnPattern
;
586 * @param string $name
589 * @param string $headerPattern
590 * @param string $dataPattern
591 * @param bool $hasLocationType
593 public function addField(
594 $name, $title, $type = CRM_Utils_Type
::T_INT
,
595 $headerPattern = '//', $dataPattern = '//',
596 $hasLocationType = FALSE
598 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
600 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
605 * Store parser values.
607 * @param CRM_Core_Session $store
611 public function set($store, $mode = self
::MODE_SUMMARY
) {
612 $store->set('rowCount', $this->_rowCount
);
613 $store->set('fields', $this->getSelectValues());
614 $store->set('fieldTypes', $this->getSelectTypes());
616 $store->set('columnPatterns', $this->getColumnPatterns());
617 $store->set('dataPatterns', $this->getDataPatterns());
618 $store->set('columnCount', $this->_activeFieldCount
);
620 $store->set('totalRowCount', $this->_totalCount
);
621 $store->set('validRowCount', $this->_validCount
);
622 $store->set('invalidRowCount', $this->_invalidRowCount
);
623 $store->set('conflictRowCount', $this->_conflictCount
);
624 $store->set('unMatchCount', $this->_unMatchCount
);
626 switch ($this->_contactType
) {
628 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
632 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
636 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
639 if ($this->_invalidRowCount
) {
640 $store->set('errorsFileName', $this->_errorFileName
);
642 if ($this->_conflictCount
) {
643 $store->set('conflictsFileName', $this->_conflictFileName
);
645 if (isset($this->_rows
) && !empty($this->_rows
)) {
646 $store->set('dataValues', $this->_rows
);
649 if ($this->_unMatchCount
) {
650 $store->set('mismatchFileName', $this->_misMatchFilemName
);
653 if ($mode == self
::MODE_IMPORT
) {
654 $store->set('duplicateRowCount', $this->_duplicateCount
);
655 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
656 if ($this->_duplicateCount
) {
657 $store->set('duplicatesFileName', $this->_duplicateFileName
);
659 if ($this->_unparsedAddressCount
) {
660 $store->set('errorsFileName', $this->_errorFileName
);
663 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
667 * Export data to a CSV file.
669 * @param string $fileName
670 * @param array $header
673 public static function exportCSV($fileName, $header, $data) {
675 if (file_exists($fileName) && !is_writable($fileName)) {
676 CRM_Core_Error
::movedSiteError($fileName);
678 //hack to remove '_status', '_statusMsg' and '_id' from error file
680 $dbRecordStatus = ['IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'];
681 foreach ($data as $rowCount => $rowValues) {
683 foreach ($rowValues as $key => $val) {
684 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
687 $errorValues[$rowCount][$key] = $val;
691 $data = $errorValues;
694 $fd = fopen($fileName, 'w');
696 foreach ($header as $key => $value) {
697 $header[$key] = "\"$value\"";
699 $config = CRM_Core_Config
::singleton();
700 $output[] = implode($config->fieldSeparator
, $header);
702 foreach ($data as $datum) {
703 foreach ($datum as $key => $value) {
704 $datum[$key] = "\"$value\"";
706 $output[] = implode($config->fieldSeparator
, $datum);
708 fwrite($fd, implode("\n", $output));
713 * Update the record with PK $id in the import database table.
716 * @param array $params
718 public function updateImportRecord($id, &$params) {
719 $statusFieldName = $this->_statusFieldName
;
720 $primaryKeyName = $this->_primaryKeyName
;
722 if ($statusFieldName && $primaryKeyName) {
723 $dao = new CRM_Core_DAO();
724 $db = $dao->getDatabaseConnection();
726 $query = "UPDATE $this->_tableName
727 SET $statusFieldName = ?,
728 ${statusFieldName}Msg = ?
729 WHERE $primaryKeyName = ?";
731 $params[$statusFieldName],
732 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
736 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
738 $db->query($query, $args);
743 * Format common params data to proper format to store.
745 * @param array $params
746 * Contain record values.
747 * @param array $formatted
748 * Array of formatted data.
749 * @param array $contactFields
750 * Contact DAO fields.
752 public function formatCommonData($params, &$formatted, &$contactFields) {
754 CRM_Utils_Array
::value('contact_type', $formatted),
758 //add custom fields for contact sub type
759 if (!empty($this->_contactSubType
)) {
760 $csType = $this->_contactSubType
;
763 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
764 $csType = $relCsType;
767 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
769 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
770 $customFields = $customFields +
$addressCustomFields;
772 //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
774 'email_greeting_custom' => 'email_greeting',
775 'postal_greeting_custom' => 'postal_greeting',
776 'addressee_custom' => 'addressee',
778 foreach ($elements as $k => $v) {
779 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
780 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
781 $params[$v] = $label;
786 $session = CRM_Core_Session
::singleton();
787 $dateType = $session->get("dateTypes");
788 foreach ($params as $key => $val) {
789 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
790 if ($customFieldID &&
791 !array_key_exists($customFieldID, $addressCustomFields)
793 //we should not update Date to null, CRM-4062
794 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
796 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
798 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
799 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
800 //retain earlier value when Import mode is `Fill`
801 unset($params[$key]);
804 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
809 if ($key == 'birth_date' && $val) {
810 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
812 elseif ($key == 'deceased_date' && $val) {
813 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
814 $params['is_deceased'] = 1;
816 elseif ($key == 'is_deceased' && $val) {
817 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
819 elseif ($key == 'gender') {
821 $params[$key] = $this->checkGender($val);
825 //now format custom data.
826 foreach ($params as $key => $field) {
827 if (is_array($field)) {
828 $isAddressCustomField = FALSE;
829 foreach ($field as $value) {
831 if (is_array($value)) {
832 foreach ($value as $name => $testForEmpty) {
833 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
834 $isAddressCustomField = TRUE;
837 // check if $value does not contain IM provider or phoneType
838 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
849 $this->formatContactParameters($value, $formatted);
852 if (!$isAddressCustomField) {
861 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
862 // due to merging of individual table and
863 // contact table, we need to avoid
864 // preferred_communication_method forcefully
865 $formatValues['contact_type'] = $formatted['contact_type'];
868 if ($key == 'id' && isset($field)) {
869 $formatted[$key] = $field;
871 $this->formatContactParameters($formatValues, $formatted);
873 //Handling Custom Data
874 // note: Address custom fields will be handled separately inside formatContactParameters
875 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
876 array_key_exists($customFieldID, $customFields) &&
877 !array_key_exists($customFieldID, $addressCustomFields)
880 $extends = CRM_Utils_Array
::value('extends', $customFields[$customFieldID]);
881 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
885 case 'Autocomplete-Select':
886 if ($customFields[$customFieldID]['data_type'] == 'String' ||
$customFields[$customFieldID]['data_type'] == 'Int') {
887 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
888 foreach ($customOption as $customValue) {
889 $val = CRM_Utils_Array
::value('value', $customValue);
890 $label = CRM_Utils_Array
::value('label', $customValue);
891 $label = strtolower($label);
892 $value = strtolower(trim($formatted[$key]));
893 if (($value == $label) ||
($value == strtolower($val))) {
894 $params[$key] = $formatted[$key] = $val;
903 if (!empty($formatted[$key]) && !empty($params[$key])) {
904 $mulValues = explode(',', $formatted[$key]);
905 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
906 $formatted[$key] = [];
908 foreach ($mulValues as $v1) {
909 foreach ($customOption as $v2) {
910 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
911 (strtolower($v2['value']) == strtolower(trim($v1)))
913 if ($htmlType == 'CheckBox') {
914 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
917 $params[$key][] = $formatted[$key][] = $v2['value'];
928 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
929 !array_key_exists($customFieldID, $addressCustomFields)
931 // @todo calling api functions directly is not supported
932 _civicrm_api3_custom_format_params($params, $formatted, $extends);
935 // to check if not update mode and unset the fields with empty value.
936 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
937 foreach ($formatted['custom'] as $customKey => $customvalue) {
938 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
939 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
941 $emptyValue = CRM_Utils_Array
::value('value', $customvalue[-1]);
942 if (!isset($emptyValue)) {
943 unset($formatted['custom'][$customKey]);
948 // parse street address, CRM-5450
949 if ($this->_parseStreetAddress
) {
950 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
951 foreach ($formatted['address'] as $instance => & $address) {
952 $streetAddress = CRM_Utils_Array
::value('street_address', $address);
953 if (empty($streetAddress)) {
956 // parse address field.
957 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
959 //street address consider to be parsed properly,
960 //If we get street_name and street_number.
961 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
962 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
965 // merge parse address w/ main address block.
966 $address = array_merge($address, $parsedFields);
973 * Format contact parameters.
975 * @todo this function needs re-writing & re-merging into the main function.
979 * @param array $values
980 * @param array $params
984 protected function formatContactParameters(&$values, &$params) {
985 // Crawl through the possible classes:
998 // Cache the various object fields
1001 // first add core contact values since for other Civi modules they are not added
1002 $contactFields = CRM_Contact_DAO_Contact
::fields();
1003 _civicrm_api3_store_values($contactFields, $values, $params);
1005 if (isset($values['contact_type'])) {
1006 // we're an individual/household/org property
1008 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
1010 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
1014 if (isset($values['individual_prefix'])) {
1015 if (!empty($params['prefix_id'])) {
1016 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1017 $params['prefix'] = $prefixes[$params['prefix_id']];
1020 $params['prefix'] = $values['individual_prefix'];
1025 if (isset($values['individual_suffix'])) {
1026 if (!empty($params['suffix_id'])) {
1027 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1028 $params['suffix'] = $suffixes[$params['suffix_id']];
1031 $params['suffix'] = $values['individual_suffix'];
1037 if (isset($values['email_greeting'])) {
1038 if (!empty($params['email_greeting_id'])) {
1039 $emailGreetingFilter = [
1040 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1041 'greeting_type' => 'email_greeting',
1043 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
1044 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1047 $params['email_greeting'] = $values['email_greeting'];
1053 if (isset($values['postal_greeting'])) {
1054 if (!empty($params['postal_greeting_id'])) {
1055 $postalGreetingFilter = [
1056 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1057 'greeting_type' => 'postal_greeting',
1059 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
1060 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1063 $params['postal_greeting'] = $values['postal_greeting'];
1068 if (isset($values['addressee'])) {
1069 if (!empty($params['addressee_id'])) {
1070 $addresseeFilter = [
1071 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1072 'greeting_type' => 'addressee',
1074 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
1075 $params['addressee'] = $addressee[$params['addressee_id']];
1078 $params['addressee'] = $values['addressee'];
1083 if (isset($values['gender'])) {
1084 if (!empty($params['gender_id'])) {
1085 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1086 $params['gender'] = $genders[$params['gender_id']];
1089 $params['gender'] = $values['gender'];
1094 if (!empty($values['preferred_communication_method'])) {
1096 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
1098 $preffComm = explode(',', $values['preferred_communication_method']);
1099 foreach ($preffComm as $v) {
1100 $v = strtolower(trim($v));
1101 if (array_key_exists($v, $pcm)) {
1102 $comm[$pcm[$v]] = 1;
1106 $params['preferred_communication_method'] = $comm;
1110 // format the website params.
1111 if (!empty($values['url'])) {
1112 static $websiteFields;
1113 if (!is_array($websiteFields)) {
1114 $websiteFields = CRM_Core_DAO_Website
::fields();
1116 if (!array_key_exists('website', $params) ||
1117 !is_array($params['website'])
1119 $params['website'] = [];
1122 $websiteCount = count($params['website']);
1123 _civicrm_api3_store_values($websiteFields, $values,
1124 $params['website'][++
$websiteCount]
1130 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1131 if (!empty($values['location_type_id'])) {
1132 return $this->formatLocationBlock($values, $params, $fields);
1135 if (isset($values['note'])) {
1137 if (!isset($params['note'])) {
1138 $params['note'] = [];
1140 $noteBlock = count($params['note']) +
1;
1142 $params['note'][$noteBlock] = [];
1143 if (!isset($fields['Note'])) {
1144 $fields['Note'] = CRM_Core_DAO_Note
::fields();
1147 // get the current logged in civicrm user
1148 $session = CRM_Core_Session
::singleton();
1149 $userID = $session->get('userID');
1152 $values['contact_id'] = $userID;
1155 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1160 // Check for custom field values
1162 if (empty($fields['custom'])) {
1163 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
1164 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1168 foreach ($values as $key => $value) {
1169 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1170 // check if it's a valid custom field id
1172 if (!array_key_exists($customFieldID, $fields['custom'])) {
1173 return civicrm_api3_create_error('Invalid custom field ID');
1176 $params[$key] = $value;
1184 * Format location block ready for importing.
1186 * There is some test coverage for this in CRM_Contact_Import_Parser_ContactTest
1187 * e.g. testImportPrimaryAddress.
1189 * @param array $values
1190 * @param array $params
1191 * @param array $fields
1195 protected function formatLocationBlock(&$values, &$params, $fields) {
1196 if (empty($values['location_type_id'])) {
1203 'openid' => 'OpenID',
1204 'phone_ext' => 'Phone',
1206 foreach ($blockTypes as $blockFieldName => $block) {
1207 if (!array_key_exists($blockFieldName, $values)) {
1211 // block present in value array.
1212 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
1213 $params[$blockFieldName] = [];
1216 if (!array_key_exists($block, $fields)) {
1217 $className = "CRM_Core_DAO_$block";
1218 $fields[$block] = $className::fields();
1221 $blockCnt = count($params[$blockFieldName]);
1223 // copy value to dao field name.
1224 if ($blockFieldName == 'im') {
1225 $values['name'] = $values[$blockFieldName];
1228 _civicrm_api3_store_values($fields[$block], $values,
1229 $params[$blockFieldName][++
$blockCnt]
1232 if ($values['location_type_id'] === 'Primary') {
1233 if (!empty($params['id'])) {
1234 $primary = civicrm_api3($block, 'get', [
1235 'return' => 'location_type_id',
1236 'contact_id' => $params['id'],
1241 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1242 $values['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1243 $values['is_primary'] = 1;
1246 if (empty($params['id']) && ($blockCnt == 1)) {
1247 $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
1250 // we only process single block at a time.
1254 // handle address fields.
1255 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1256 $params['address'] = [];
1259 if (!array_key_exists('Address', $fields)) {
1260 $fields['Address'] = CRM_Core_DAO_Address
::fields();
1263 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1264 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1265 // the address in CRM_Core_BAO_Address::create method
1266 if (!empty($values['location_type_id'])) {
1267 static $customFields = [];
1268 if (empty($customFields)) {
1269 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1271 // make a copy of values, as we going to make changes
1272 $newValues = $values;
1273 foreach ($values as $key => $val) {
1274 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1275 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1276 // mark an entry in fields array since we want the value of custom field to be copied
1277 $fields['Address'][$key] = NULL;
1279 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
1280 switch ($htmlType) {
1282 case 'Multi-Select':
1284 $mulValues = explode(',', $val);
1285 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1286 $newValues[$key] = [];
1287 foreach ($mulValues as $v1) {
1288 foreach ($customOption as $v2) {
1289 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1290 (strtolower($v2['value']) == strtolower(trim($v1)))
1292 if ($htmlType == 'CheckBox') {
1293 $newValues[$key][$v2['value']] = 1;
1296 $newValues[$key][] = $v2['value'];
1306 // consider new values
1307 $values = $newValues;
1310 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1316 'supplemental_address_1',
1317 'supplemental_address_2',
1318 'supplemental_address_3',
1319 'StateProvince.name',
1322 foreach ($addressFields as $field) {
1323 if (array_key_exists($field, $values)) {
1324 if (!array_key_exists('address', $params)) {
1325 $params['address'] = [];
1327 $params['address'][$values['location_type_id']][$field] = $values[$field];
1331 if ($values['location_type_id'] === 'Primary') {
1332 if (!empty($params['id'])) {
1333 $primary = civicrm_api3('Address', 'get', [
1334 'return' => 'location_type_id',
1335 'contact_id' => $params['id'],
1340 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1341 $params['address'][$values['location_type_id']]['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1342 $params['address'][$values['location_type_id']]['is_primary'] = 1;