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.
49 protected $_unMatchCount;
52 * Array of unmatched lines.
59 * Total number of contacts with unparsed addresses
62 protected $_unparsedAddressCount;
65 * Filename of mismatch data
69 protected $_misMatchFilemName;
71 protected $_primaryKeyName;
72 protected $_statusFieldName;
74 protected $fieldMetadata = [];
83 * Dedupe rule group id to use if set
87 public $_dedupeRuleGroupID = NULL;
92 * @param string $tableName
93 * @param array $mapper
95 * @param int $contactType
96 * @param string $primaryKeyName
97 * @param string $statusFieldName
98 * @param int $onDuplicate
99 * @param int $statusID
100 * @param int $totalRowCount
101 * @param bool $doGeocodeAddress
102 * @param int $timeout
103 * @param string $contactSubType
104 * @param int $dedupeRuleGroupID
111 $mode = self
::MODE_PREVIEW
,
112 $contactType = self
::CONTACT_INDIVIDUAL
,
113 $primaryKeyName = '_id',
114 $statusFieldName = '_status',
115 $onDuplicate = self
::DUPLICATE_SKIP
,
117 $totalRowCount = NULL,
118 $doGeocodeAddress = FALSE,
119 $timeout = CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
120 $contactSubType = NULL,
121 $dedupeRuleGroupID = NULL
124 // TODO: Make the timeout actually work
125 $this->_onDuplicate
= $onDuplicate;
126 $this->_dedupeRuleGroupID
= $dedupeRuleGroupID;
128 switch ($contactType) {
129 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
130 $this->_contactType
= 'Individual';
133 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
134 $this->_contactType
= 'Household';
137 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
138 $this->_contactType
= 'Organization';
141 $this->_contactSubType
= $contactSubType;
145 $this->_rowCount
= $this->_warningCount
= 0;
146 $this->_invalidRowCount
= $this->_validCount
= 0;
147 $this->_totalCount
= $this->_conflictCount
= 0;
150 $this->_warnings
= [];
151 $this->_conflicts
= [];
152 $this->_unparsedAddresses
= [];
154 $this->_tableName
= $tableName;
155 $this->_primaryKeyName
= $primaryKeyName;
156 $this->_statusFieldName
= $statusFieldName;
158 if ($mode == self
::MODE_MAPFIELD
) {
162 $this->_activeFieldCount
= count($this->_activeFields
);
165 if ($mode == self
::MODE_IMPORT
) {
166 //get the key of email field
167 foreach ($mapper as $key => $value) {
168 if (strtolower($value) == 'email') {
176 $this->progressImport($statusID);
177 $startTimestamp = $currTimestamp = $prevTimestamp = time();
179 // get the contents of the temp. import table
180 $query = "SELECT * FROM $tableName";
181 if ($mode == self
::MODE_IMPORT
) {
182 $query .= " WHERE $statusFieldName = 'NEW'";
184 $dao = new CRM_Core_DAO();
185 $db = $dao->getDatabaseConnection();
186 $result = $db->query($query);
188 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED
)) {
191 /* trim whitespace around the values */
192 foreach ($values as $k => $v) {
193 $values[$k] = trim($v, " \t\r\n");
195 if (CRM_Utils_System
::isNull($values)) {
199 $this->_totalCount++
;
201 if ($mode == self
::MODE_MAPFIELD
) {
202 $returnCode = $this->mapField($values);
204 elseif ($mode == self
::MODE_PREVIEW
) {
205 $returnCode = $this->preview($values);
207 elseif ($mode == self
::MODE_SUMMARY
) {
208 $returnCode = $this->summary($values);
210 elseif ($mode == self
::MODE_IMPORT
) {
211 //print "Running parser in import mode<br/>\n";
212 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
213 if ($statusID && (($this->_rowCount %
50) == 0)) {
214 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
218 $returnCode = self
::ERROR
;
221 // note that a line could be valid but still produce a warning
222 if ($returnCode & self
::VALID
) {
223 $this->_validCount++
;
224 if ($mode == self
::MODE_MAPFIELD
) {
225 $this->_rows
[] = $values;
226 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
230 if ($returnCode & self
::WARNING
) {
231 $this->_warningCount++
;
232 if ($this->_warningCount
< $this->_maxWarningCount
) {
233 $this->_warningCount
[] = $line;
237 if ($returnCode & self
::ERROR
) {
238 $this->_invalidRowCount++
;
239 array_unshift($values, $this->_rowCount
);
240 $this->_errors
[] = $values;
243 if ($returnCode & self
::CONFLICT
) {
244 $this->_conflictCount++
;
245 array_unshift($values, $this->_rowCount
);
246 $this->_conflicts
[] = $values;
249 if ($returnCode & self
::NO_MATCH
) {
250 $this->_unMatchCount++
;
251 array_unshift($values, $this->_rowCount
);
252 $this->_unMatch
[] = $values;
255 if ($returnCode & self
::DUPLICATE
) {
256 if ($returnCode & self
::MULTIPLE_DUPE
) {
257 /* TODO: multi-dupes should be counted apart from singles
258 * on non-skip action */
260 $this->_duplicateCount++
;
261 array_unshift($values, $this->_rowCount
);
262 $this->_duplicates
[] = $values;
263 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
264 $this->_validCount++
;
268 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
269 $this->_unparsedAddressCount++
;
270 array_unshift($values, $this->_rowCount
);
271 $this->_unparsedAddresses
[] = $values;
273 // we give the derived class a way of aborting the process
274 // note that the return code could be multiple code or'ed together
275 if ($returnCode & self
::STOP
) {
279 // if we are done processing the maxNumber of lines, break
280 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
284 // see if we've hit our timeout yet
285 /* if ( $the_thing_with_the_stuff ) {
290 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
291 $customHeaders = $mapper;
293 $customfields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
294 foreach ($customHeaders as $key => $value) {
295 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
296 $customHeaders[$key] = $customfields[$id][0];
300 if ($this->_invalidRowCount
) {
301 // removed view url for invlaid contacts
302 $headers = array_merge([
306 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
307 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
309 if ($this->_conflictCount
) {
310 $headers = array_merge([
314 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
315 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
317 if ($this->_duplicateCount
) {
318 $headers = array_merge([
320 ts('View Contact URL'),
323 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
324 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
326 if ($this->_unMatchCount
) {
327 $headers = array_merge([
332 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
333 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
335 if ($this->_unparsedAddressCount
) {
336 $headers = array_merge([
338 ts('Contact Edit URL'),
340 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
341 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
344 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
345 return $this->fini();
349 * Given a list of the importable field keys that the user has selected.
350 * set the active fields array to this list
352 * @param array $fieldKeys
353 * Mapped array of values.
355 public function setActiveFields($fieldKeys) {
356 $this->_activeFieldCount
= count($fieldKeys);
357 foreach ($fieldKeys as $key) {
358 if (empty($this->_fields
[$key])) {
359 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
362 $this->_activeFields
[] = clone($this->_fields
[$key]);
370 public function setActiveFieldLocationTypes($elements) {
371 for ($i = 0; $i < count($elements); $i++
) {
372 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
383 public function setActiveFieldPhoneTypes($elements) {
384 for ($i = 0; $i < count($elements); $i++
) {
385 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
392 public function setActiveFieldWebsiteTypes($elements) {
393 for ($i = 0; $i < count($elements); $i++
) {
394 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
399 * Set IM Service Provider type fields.
401 * @param array $elements
402 * IM service provider type ids.
404 public function setActiveFieldImProviders($elements) {
405 for ($i = 0; $i < count($elements); $i++
) {
406 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
413 public function setActiveFieldRelated($elements) {
414 for ($i = 0; $i < count($elements); $i++
) {
415 $this->_activeFields
[$i]->_related
= $elements[$i];
422 public function setActiveFieldRelatedContactType($elements) {
423 for ($i = 0; $i < count($elements); $i++
) {
424 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
431 public function setActiveFieldRelatedContactDetails($elements) {
432 for ($i = 0; $i < count($elements); $i++
) {
433 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
440 public function setActiveFieldRelatedContactLocType($elements) {
441 for ($i = 0; $i < count($elements); $i++
) {
442 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
447 * Set active field for related contact's phone type.
449 * @param array $elements
451 public function setActiveFieldRelatedContactPhoneType($elements) {
452 for ($i = 0; $i < count($elements); $i++
) {
453 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
460 public function setActiveFieldRelatedContactWebsiteType($elements) {
461 for ($i = 0; $i < count($elements); $i++
) {
462 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
467 * Set IM Service Provider type fields for related contacts.
469 * @param array $elements
470 * IM service provider type ids of related contact.
472 public function setActiveFieldRelatedContactImProvider($elements) {
473 for ($i = 0; $i < count($elements); $i++
) {
474 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
479 * Format the field values for input to the api.
482 * (reference ) associative array of name/value pairs
484 public function &getActiveFieldParams() {
487 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
488 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
492 if (isset($this->_activeFields
[$i]->_value
)) {
493 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
494 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
495 $params[$this->_activeFields
[$i]->_name
] = [];
499 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
500 'location_type_id' => $this->_activeFields
[$i]->_hasLocationType
,
503 if (isset($this->_activeFields
[$i]->_phoneType
)) {
504 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
507 // get IM service Provider type id
508 if (isset($this->_activeFields
[$i]->_imProvider
)) {
509 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
512 $params[$this->_activeFields
[$i]->_name
][] = $value;
514 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
516 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
517 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
520 $params[$this->_activeFields
[$i]->_name
][] = $value;
523 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
524 if (!isset($this->_activeFields
[$i]->_related
)) {
525 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
529 //minor fix for CRM-4062
530 if (isset($this->_activeFields
[$i]->_related
)) {
531 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
532 $params[$this->_activeFields
[$i]->_related
] = [];
535 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
536 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
539 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
540 if (!empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
]) &&
541 !is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])
543 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = [];
546 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
547 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
550 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
551 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
554 // get IM service Provider type id for related contact
555 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
556 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
559 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
561 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
562 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = [
563 'url' => $this->_activeFields
[$i]->_value
,
564 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
568 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
580 public function getColumnPatterns() {
582 foreach ($this->_fields
as $name => $field) {
583 $values[$name] = $field->_columnPattern
;
589 * @param string $name
592 * @param string $headerPattern
593 * @param string $dataPattern
594 * @param bool $hasLocationType
596 public function addField(
597 $name, $title, $type = CRM_Utils_Type
::T_INT
,
598 $headerPattern = '//', $dataPattern = '//',
599 $hasLocationType = FALSE
601 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
603 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
608 * Store parser values.
610 * @param CRM_Core_Session $store
614 public function set($store, $mode = self
::MODE_SUMMARY
) {
615 $store->set('rowCount', $this->_rowCount
);
616 $store->set('fields', $this->getSelectValues());
617 $store->set('fieldTypes', $this->getSelectTypes());
619 $store->set('columnPatterns', $this->getColumnPatterns());
620 $store->set('dataPatterns', $this->getDataPatterns());
621 $store->set('columnCount', $this->_activeFieldCount
);
623 $store->set('totalRowCount', $this->_totalCount
);
624 $store->set('validRowCount', $this->_validCount
);
625 $store->set('invalidRowCount', $this->_invalidRowCount
);
626 $store->set('conflictRowCount', $this->_conflictCount
);
627 $store->set('unMatchCount', $this->_unMatchCount
);
629 switch ($this->_contactType
) {
631 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
635 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
639 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
642 if ($this->_invalidRowCount
) {
643 $store->set('errorsFileName', $this->_errorFileName
);
645 if ($this->_conflictCount
) {
646 $store->set('conflictsFileName', $this->_conflictFileName
);
648 if (isset($this->_rows
) && !empty($this->_rows
)) {
649 $store->set('dataValues', $this->_rows
);
652 if ($this->_unMatchCount
) {
653 $store->set('mismatchFileName', $this->_misMatchFilemName
);
656 if ($mode == self
::MODE_IMPORT
) {
657 $store->set('duplicateRowCount', $this->_duplicateCount
);
658 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
659 if ($this->_duplicateCount
) {
660 $store->set('duplicatesFileName', $this->_duplicateFileName
);
662 if ($this->_unparsedAddressCount
) {
663 $store->set('errorsFileName', $this->_errorFileName
);
666 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
670 * Export data to a CSV file.
672 * @param string $fileName
673 * @param array $header
676 public static function exportCSV($fileName, $header, $data) {
678 if (file_exists($fileName) && !is_writable($fileName)) {
679 CRM_Core_Error
::movedSiteError($fileName);
681 //hack to remove '_status', '_statusMsg' and '_id' from error file
683 $dbRecordStatus = ['IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW'];
684 foreach ($data as $rowCount => $rowValues) {
686 foreach ($rowValues as $key => $val) {
687 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
690 $errorValues[$rowCount][$key] = $val;
694 $data = $errorValues;
697 $fd = fopen($fileName, 'w');
699 foreach ($header as $key => $value) {
700 $header[$key] = "\"$value\"";
702 $config = CRM_Core_Config
::singleton();
703 $output[] = implode($config->fieldSeparator
, $header);
705 foreach ($data as $datum) {
706 foreach ($datum as $key => $value) {
707 $datum[$key] = "\"$value\"";
709 $output[] = implode($config->fieldSeparator
, $datum);
711 fwrite($fd, implode("\n", $output));
716 * Update the record with PK $id in the import database table.
719 * @param array $params
721 public function updateImportRecord($id, &$params) {
722 $statusFieldName = $this->_statusFieldName
;
723 $primaryKeyName = $this->_primaryKeyName
;
725 if ($statusFieldName && $primaryKeyName) {
726 $dao = new CRM_Core_DAO();
727 $db = $dao->getDatabaseConnection();
729 $query = "UPDATE $this->_tableName
730 SET $statusFieldName = ?,
731 ${statusFieldName}Msg = ?
732 WHERE $primaryKeyName = ?";
734 $params[$statusFieldName],
735 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
739 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
741 $db->query($query, $args);
746 * Format common params data to proper format to store.
748 * @param array $params
749 * Contain record values.
750 * @param array $formatted
751 * Array of formatted data.
752 * @param array $contactFields
753 * Contact DAO fields.
755 public function formatCommonData($params, &$formatted, &$contactFields) {
757 CRM_Utils_Array
::value('contact_type', $formatted),
761 //add custom fields for contact sub type
762 if (!empty($this->_contactSubType
)) {
763 $csType = $this->_contactSubType
;
766 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
767 $csType = $relCsType;
770 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
772 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
773 $customFields = $customFields +
$addressCustomFields;
775 //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
777 'email_greeting_custom' => 'email_greeting',
778 'postal_greeting_custom' => 'postal_greeting',
779 'addressee_custom' => 'addressee',
781 foreach ($elements as $k => $v) {
782 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
783 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
784 $params[$v] = $label;
789 $session = CRM_Core_Session
::singleton();
790 $dateType = $session->get("dateTypes");
791 foreach ($params as $key => $val) {
792 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
793 if ($customFieldID &&
794 !array_key_exists($customFieldID, $addressCustomFields)
796 //we should not update Date to null, CRM-4062
797 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
799 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
801 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
802 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
803 //retain earlier value when Import mode is `Fill`
804 unset($params[$key]);
807 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
812 if ($key == 'birth_date' && $val) {
813 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
815 elseif ($key == 'deceased_date' && $val) {
816 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
817 $params['is_deceased'] = 1;
819 elseif ($key == 'is_deceased' && $val) {
820 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
822 elseif ($key == 'gender') {
824 $params[$key] = $this->checkGender($val);
828 //now format custom data.
829 foreach ($params as $key => $field) {
830 if (is_array($field)) {
831 $isAddressCustomField = FALSE;
832 foreach ($field as $value) {
834 if (is_array($value)) {
835 foreach ($value as $name => $testForEmpty) {
836 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
837 $isAddressCustomField = TRUE;
840 // check if $value does not contain IM provider or phoneType
841 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
852 if (!empty($value['location_type_id'])) {
853 $this->formatLocationBlock($value, $formatted);
856 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
857 $this->formatContactParameters($value, $formatted);
861 if (!$isAddressCustomField) {
870 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
871 // due to merging of individual table and
872 // contact table, we need to avoid
873 // preferred_communication_method forcefully
874 $formatValues['contact_type'] = $formatted['contact_type'];
877 if ($key == 'id' && isset($field)) {
878 $formatted[$key] = $field;
880 $this->formatContactParameters($formatValues, $formatted);
882 //Handling Custom Data
883 // note: Address custom fields will be handled separately inside formatContactParameters
884 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
885 array_key_exists($customFieldID, $customFields) &&
886 !array_key_exists($customFieldID, $addressCustomFields)
889 $extends = CRM_Utils_Array
::value('extends', $customFields[$customFieldID]);
890 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
894 case 'Autocomplete-Select':
895 if ($customFields[$customFieldID]['data_type'] == 'String' ||
$customFields[$customFieldID]['data_type'] == 'Int') {
896 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
897 foreach ($customOption as $customValue) {
898 $val = CRM_Utils_Array
::value('value', $customValue);
899 $label = CRM_Utils_Array
::value('label', $customValue);
900 $label = strtolower($label);
901 $value = strtolower(trim($formatted[$key]));
902 if (($value == $label) ||
($value == strtolower($val))) {
903 $params[$key] = $formatted[$key] = $val;
912 if (!empty($formatted[$key]) && !empty($params[$key])) {
913 $mulValues = explode(',', $formatted[$key]);
914 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
915 $formatted[$key] = [];
917 foreach ($mulValues as $v1) {
918 foreach ($customOption as $v2) {
919 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
920 (strtolower($v2['value']) == strtolower(trim($v1)))
922 if ($htmlType == 'CheckBox') {
923 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
926 $params[$key][] = $formatted[$key][] = $v2['value'];
937 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
938 !array_key_exists($customFieldID, $addressCustomFields)
940 // @todo calling api functions directly is not supported
941 _civicrm_api3_custom_format_params($params, $formatted, $extends);
944 // to check if not update mode and unset the fields with empty value.
945 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
946 foreach ($formatted['custom'] as $customKey => $customvalue) {
947 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
948 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
950 $emptyValue = CRM_Utils_Array
::value('value', $customvalue[-1]);
951 if (!isset($emptyValue)) {
952 unset($formatted['custom'][$customKey]);
957 // parse street address, CRM-5450
958 if ($this->_parseStreetAddress
) {
959 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
960 foreach ($formatted['address'] as $instance => & $address) {
961 $streetAddress = CRM_Utils_Array
::value('street_address', $address);
962 if (empty($streetAddress)) {
965 // parse address field.
966 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
968 //street address consider to be parsed properly,
969 //If we get street_name and street_number.
970 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
971 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
974 // merge parse address w/ main address block.
975 $address = array_merge($address, $parsedFields);
982 * Format contact parameters.
984 * @todo this function needs re-writing & re-merging into the main function.
988 * @param array $values
989 * @param array $params
993 protected function formatContactParameters(&$values, &$params) {
994 // Crawl through the possible classes:
1007 // Cache the various object fields
1008 static $fields = [];
1010 // first add core contact values since for other Civi modules they are not added
1011 $contactFields = CRM_Contact_DAO_Contact
::fields();
1012 _civicrm_api3_store_values($contactFields, $values, $params);
1014 if (isset($values['contact_type'])) {
1015 // we're an individual/household/org property
1017 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
1019 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
1023 if (isset($values['individual_prefix'])) {
1024 if (!empty($params['prefix_id'])) {
1025 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1026 $params['prefix'] = $prefixes[$params['prefix_id']];
1029 $params['prefix'] = $values['individual_prefix'];
1034 if (isset($values['individual_suffix'])) {
1035 if (!empty($params['suffix_id'])) {
1036 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1037 $params['suffix'] = $suffixes[$params['suffix_id']];
1040 $params['suffix'] = $values['individual_suffix'];
1046 if (isset($values['email_greeting'])) {
1047 if (!empty($params['email_greeting_id'])) {
1048 $emailGreetingFilter = [
1049 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1050 'greeting_type' => 'email_greeting',
1052 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
1053 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1056 $params['email_greeting'] = $values['email_greeting'];
1062 if (isset($values['postal_greeting'])) {
1063 if (!empty($params['postal_greeting_id'])) {
1064 $postalGreetingFilter = [
1065 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1066 'greeting_type' => 'postal_greeting',
1068 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
1069 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1072 $params['postal_greeting'] = $values['postal_greeting'];
1077 if (isset($values['addressee'])) {
1078 if (!empty($params['addressee_id'])) {
1079 $addresseeFilter = [
1080 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1081 'greeting_type' => 'addressee',
1083 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
1084 $params['addressee'] = $addressee[$params['addressee_id']];
1087 $params['addressee'] = $values['addressee'];
1092 if (isset($values['gender'])) {
1093 if (!empty($params['gender_id'])) {
1094 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1095 $params['gender'] = $genders[$params['gender_id']];
1098 $params['gender'] = $values['gender'];
1103 if (!empty($values['preferred_communication_method'])) {
1105 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
1107 $preffComm = explode(',', $values['preferred_communication_method']);
1108 foreach ($preffComm as $v) {
1109 $v = strtolower(trim($v));
1110 if (array_key_exists($v, $pcm)) {
1111 $comm[$pcm[$v]] = 1;
1115 $params['preferred_communication_method'] = $comm;
1119 // format the website params.
1120 if (!empty($values['url'])) {
1121 static $websiteFields;
1122 if (!is_array($websiteFields)) {
1123 $websiteFields = CRM_Core_DAO_Website
::fields();
1125 if (!array_key_exists('website', $params) ||
1126 !is_array($params['website'])
1128 $params['website'] = [];
1131 $websiteCount = count($params['website']);
1132 _civicrm_api3_store_values($websiteFields, $values,
1133 $params['website'][++
$websiteCount]
1139 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1140 if (!empty($values['location_type_id'])) {
1141 CRM_Core_Error
::deprecatedFunctionWarning('this is not expected to be reachable now');
1142 return $this->formatLocationBlock($values, $params);
1145 if (isset($values['note'])) {
1147 if (!isset($params['note'])) {
1148 $params['note'] = [];
1150 $noteBlock = count($params['note']) +
1;
1152 $params['note'][$noteBlock] = [];
1153 if (!isset($fields['Note'])) {
1154 $fields['Note'] = CRM_Core_DAO_Note
::fields();
1157 // get the current logged in civicrm user
1158 $session = CRM_Core_Session
::singleton();
1159 $userID = $session->get('userID');
1162 $values['contact_id'] = $userID;
1165 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1170 // Check for custom field values
1172 if (empty($fields['custom'])) {
1173 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
1174 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1178 foreach ($values as $key => $value) {
1179 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1180 // check if it's a valid custom field id
1182 if (!array_key_exists($customFieldID, $fields['custom'])) {
1183 return civicrm_api3_create_error('Invalid custom field ID');
1186 $params[$key] = $value;
1194 * Format location block ready for importing.
1196 * There is some test coverage for this in CRM_Contact_Import_Parser_ContactTest
1197 * e.g. testImportPrimaryAddress.
1199 * @param array $values
1200 * @param array $params
1204 protected function formatLocationBlock(&$values, &$params) {
1209 'openid' => 'OpenID',
1210 'phone_ext' => 'Phone',
1212 foreach ($blockTypes as $blockFieldName => $block) {
1213 if (!array_key_exists($blockFieldName, $values)) {
1217 // block present in value array.
1218 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
1219 $params[$blockFieldName] = [];
1222 $fields[$block] = $this->getMetadataForEntity($block);
1224 // copy value to dao field name.
1225 if ($blockFieldName == 'im') {
1226 $values['name'] = $values[$blockFieldName];
1229 _civicrm_api3_store_values($fields[$block], $values,
1230 $params[$blockFieldName][$values['location_type_id']]
1233 $this->fillPrimary($params[$blockFieldName][$values['location_type_id']], $values, $block, CRM_Utils_Array
::value('id', $params));
1235 if (empty($params['id']) && (count($params[$blockFieldName]) == 1)) {
1236 $params[$blockFieldName][$values['location_type_id']]['is_primary'] = TRUE;
1239 // we only process single block at a time.
1243 // handle address fields.
1244 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1245 $params['address'] = [];
1248 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1249 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1250 // the address in CRM_Core_BAO_Address::create method
1251 if (!empty($values['location_type_id'])) {
1252 static $customFields = [];
1253 if (empty($customFields)) {
1254 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1256 // make a copy of values, as we going to make changes
1257 $newValues = $values;
1258 foreach ($values as $key => $val) {
1259 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1260 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1262 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
1263 switch ($htmlType) {
1265 case 'Multi-Select':
1267 $mulValues = explode(',', $val);
1268 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1269 $newValues[$key] = [];
1270 foreach ($mulValues as $v1) {
1271 foreach ($customOption as $v2) {
1272 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1273 (strtolower($v2['value']) == strtolower(trim($v1)))
1275 if ($htmlType == 'CheckBox') {
1276 $newValues[$key][$v2['value']] = 1;
1279 $newValues[$key][] = $v2['value'];
1289 // consider new values
1290 $values = $newValues;
1293 $fields['Address'] = $this->getMetadataForEntity('Address');
1294 // @todo this is kinda replicated below....
1295 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1301 'supplemental_address_1',
1302 'supplemental_address_2',
1303 'supplemental_address_3',
1304 'StateProvince.name',
1306 foreach (array_keys($customFields) as $customFieldID) {
1307 $addressFields[] = 'custom_' . $customFieldID;
1310 foreach ($addressFields as $field) {
1311 if (array_key_exists($field, $values)) {
1312 if (!array_key_exists('address', $params)) {
1313 $params['address'] = [];
1315 $params['address'][$values['location_type_id']][$field] = $values[$field];
1319 $this->fillPrimary($params['address'][$values['location_type_id']], $values, 'address', CRM_Utils_Array
::value('id', $params));
1324 * Get the field metadata for the relevant entity.
1326 * @param string $entity
1330 protected function getMetadataForEntity($entity) {
1331 if (!isset($this->fieldMetadata
[$entity])) {
1332 $className = "CRM_Core_DAO_$entity";
1333 $this->fieldMetadata
[$entity] = $className::fields();
1335 return $this->fieldMetadata
[$entity];
1339 * Fill in the primary location.
1341 * If the contact has a primary address we update it. Otherwise
1342 * we add an address of the default location type.
1344 * @param array $params
1345 * Address block parameters
1346 * @param array $values
1348 * @param string $entity
1349 * - address, email, phone
1350 * @param int|null $contactID
1352 * @throws \CiviCRM_API3_Exception
1354 protected function fillPrimary(&$params, $values, $entity, $contactID) {
1355 if ($values['location_type_id'] === 'Primary') {
1357 $primary = civicrm_api3($entity, 'get', [
1358 'return' => 'location_type_id',
1359 'contact_id' => $contactID,
1364 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1365 $params['location_type_id'] = (int) (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1366 $params['is_primary'] = 1;