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.
47 protected $_unMatchCount;
50 * Array of unmatched lines
55 * Total number of contacts with unparsed addresses
57 protected $_unparsedAddressCount;
60 * Filename of mismatch data
64 protected $_misMatchFilemName;
66 protected $_primaryKeyName;
67 protected $_statusFieldName;
77 * Dedupe rule group id to use if set
81 public $_dedupeRuleGroupID = NULL;
86 * @param string $tableName
87 * @param array $mapper
89 * @param int $contactType
90 * @param string $primaryKeyName
91 * @param string $statusFieldName
92 * @param int $onDuplicate
93 * @param int $statusID
94 * @param int $totalRowCount
95 * @param bool $doGeocodeAddress
97 * @param string $contactSubType
98 * @param int $dedupeRuleGroupID
105 $mode = self
::MODE_PREVIEW
,
106 $contactType = self
::CONTACT_INDIVIDUAL
,
107 $primaryKeyName = '_id',
108 $statusFieldName = '_status',
109 $onDuplicate = self
::DUPLICATE_SKIP
,
111 $totalRowCount = NULL,
112 $doGeocodeAddress = FALSE,
113 $timeout = CRM_Contact_Import_Parser
::DEFAULT_TIMEOUT
,
114 $contactSubType = NULL,
115 $dedupeRuleGroupID = NULL
118 // TODO: Make the timeout actually work
119 $this->_onDuplicate
= $onDuplicate;
120 $this->_dedupeRuleGroupID
= $dedupeRuleGroupID;
122 switch ($contactType) {
123 case CRM_Import_Parser
::CONTACT_INDIVIDUAL
:
124 $this->_contactType
= 'Individual';
127 case CRM_Import_Parser
::CONTACT_HOUSEHOLD
:
128 $this->_contactType
= 'Household';
131 case CRM_Import_Parser
::CONTACT_ORGANIZATION
:
132 $this->_contactType
= 'Organization';
135 $this->_contactSubType
= $contactSubType;
139 $this->_rowCount
= $this->_warningCount
= 0;
140 $this->_invalidRowCount
= $this->_validCount
= 0;
141 $this->_totalCount
= $this->_conflictCount
= 0;
143 $this->_errors
= array();
144 $this->_warnings
= array();
145 $this->_conflicts
= array();
146 $this->_unparsedAddresses
= array();
148 $this->_tableName
= $tableName;
149 $this->_primaryKeyName
= $primaryKeyName;
150 $this->_statusFieldName
= $statusFieldName;
152 if ($mode == self
::MODE_MAPFIELD
) {
153 $this->_rows
= array();
156 $this->_activeFieldCount
= count($this->_activeFields
);
159 if ($mode == self
::MODE_IMPORT
) {
160 //get the key of email field
161 foreach ($mapper as $key => $value) {
162 if (strtolower($value) == 'email') {
170 $this->progressImport($statusID);
171 $startTimestamp = $currTimestamp = $prevTimestamp = time();
173 // get the contents of the temp. import table
174 $query = "SELECT * FROM $tableName";
175 if ($mode == self
::MODE_IMPORT
) {
176 $query .= " WHERE $statusFieldName = 'NEW'";
178 $dao = new CRM_Core_DAO();
179 $db = $dao->getDatabaseConnection();
180 $result = $db->query($query);
182 while ($values = $result->fetchRow(DB_FETCHMODE_ORDERED
)) {
185 /* trim whitespace around the values */
186 foreach ($values as $k => $v) {
187 $values[$k] = trim($v, " \t\r\n");
189 if (CRM_Utils_System
::isNull($values)) {
193 $this->_totalCount++
;
195 if ($mode == self
::MODE_MAPFIELD
) {
196 $returnCode = $this->mapField($values);
198 elseif ($mode == self
::MODE_PREVIEW
) {
199 $returnCode = $this->preview($values);
201 elseif ($mode == self
::MODE_SUMMARY
) {
202 $returnCode = $this->summary($values);
204 elseif ($mode == self
::MODE_IMPORT
) {
205 //print "Running parser in import mode<br/>\n";
206 $returnCode = $this->import($onDuplicate, $values, $doGeocodeAddress);
207 if ($statusID && (($this->_rowCount %
50) == 0)) {
208 $prevTimestamp = $this->progressImport($statusID, FALSE, $startTimestamp, $prevTimestamp, $totalRowCount);
212 $returnCode = self
::ERROR
;
215 // note that a line could be valid but still produce a warning
216 if ($returnCode & self
::VALID
) {
217 $this->_validCount++
;
218 if ($mode == self
::MODE_MAPFIELD
) {
219 $this->_rows
[] = $values;
220 $this->_activeFieldCount
= max($this->_activeFieldCount
, count($values));
224 if ($returnCode & self
::WARNING
) {
225 $this->_warningCount++
;
226 if ($this->_warningCount
< $this->_maxWarningCount
) {
227 $this->_warningCount
[] = $line;
231 if ($returnCode & self
::ERROR
) {
232 $this->_invalidRowCount++
;
233 array_unshift($values, $this->_rowCount
);
234 $this->_errors
[] = $values;
237 if ($returnCode & self
::CONFLICT
) {
238 $this->_conflictCount++
;
239 array_unshift($values, $this->_rowCount
);
240 $this->_conflicts
[] = $values;
243 if ($returnCode & self
::NO_MATCH
) {
244 $this->_unMatchCount++
;
245 array_unshift($values, $this->_rowCount
);
246 $this->_unMatch
[] = $values;
249 if ($returnCode & self
::DUPLICATE
) {
250 if ($returnCode & self
::MULTIPLE_DUPE
) {
251 /* TODO: multi-dupes should be counted apart from singles
252 * on non-skip action */
254 $this->_duplicateCount++
;
255 array_unshift($values, $this->_rowCount
);
256 $this->_duplicates
[] = $values;
257 if ($onDuplicate != self
::DUPLICATE_SKIP
) {
258 $this->_validCount++
;
262 if ($returnCode & self
::UNPARSED_ADDRESS_WARNING
) {
263 $this->_unparsedAddressCount++
;
264 array_unshift($values, $this->_rowCount
);
265 $this->_unparsedAddresses
[] = $values;
267 // we give the derived class a way of aborting the process
268 // note that the return code could be multiple code or'ed together
269 if ($returnCode & self
::STOP
) {
273 // if we are done processing the maxNumber of lines, break
274 if ($this->_maxLinesToProcess
> 0 && $this->_validCount
>= $this->_maxLinesToProcess
) {
278 // see if we've hit our timeout yet
279 /* if ( $the_thing_with_the_stuff ) {
284 if ($mode == self
::MODE_PREVIEW ||
$mode == self
::MODE_IMPORT
) {
285 $customHeaders = $mapper;
287 $customfields = CRM_Core_BAO_CustomField
::getFields($this->_contactType
);
288 foreach ($customHeaders as $key => $value) {
289 if ($id = CRM_Core_BAO_CustomField
::getKeyID($value)) {
290 $customHeaders[$key] = $customfields[$id][0];
294 if ($this->_invalidRowCount
) {
295 // removed view url for invlaid contacts
296 $headers = array_merge(array(
302 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
303 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
305 if ($this->_conflictCount
) {
306 $headers = array_merge(array(
312 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
313 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
315 if ($this->_duplicateCount
) {
316 $headers = array_merge(array(
318 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(array(
334 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
335 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
337 if ($this->_unparsedAddressCount
) {
338 $headers = array_merge(array(
340 ts('Contact Edit URL'),
344 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
345 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
348 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
349 return $this->fini();
353 * Given a list of the importable field keys that the user has selected.
354 * set the active fields array to this list
356 * @param array $fieldKeys
357 * Mapped array of values.
359 public function setActiveFields($fieldKeys) {
360 $this->_activeFieldCount
= count($fieldKeys);
361 foreach ($fieldKeys as $key) {
362 if (empty($this->_fields
[$key])) {
363 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
366 $this->_activeFields
[] = clone($this->_fields
[$key]);
374 public function setActiveFieldLocationTypes($elements) {
375 for ($i = 0; $i < count($elements); $i++
) {
376 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
386 public function setActiveFieldPhoneTypes($elements) {
387 for ($i = 0; $i < count($elements); $i++
) {
388 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
395 public function setActiveFieldWebsiteTypes($elements) {
396 for ($i = 0; $i < count($elements); $i++
) {
397 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
402 * Set IM Service Provider type fields.
404 * @param array $elements
405 * IM service provider type ids.
407 public function setActiveFieldImProviders($elements) {
408 for ($i = 0; $i < count($elements); $i++
) {
409 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
416 public function setActiveFieldRelated($elements) {
417 for ($i = 0; $i < count($elements); $i++
) {
418 $this->_activeFields
[$i]->_related
= $elements[$i];
425 public function setActiveFieldRelatedContactType($elements) {
426 for ($i = 0; $i < count($elements); $i++
) {
427 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
434 public function setActiveFieldRelatedContactDetails($elements) {
435 for ($i = 0; $i < count($elements); $i++
) {
436 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
443 public function setActiveFieldRelatedContactLocType($elements) {
444 for ($i = 0; $i < count($elements); $i++
) {
445 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
450 * Set active field for related contact's phone type.
452 * @param array $elements
454 public function setActiveFieldRelatedContactPhoneType($elements) {
455 for ($i = 0; $i < count($elements); $i++
) {
456 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
463 public function setActiveFieldRelatedContactWebsiteType($elements) {
464 for ($i = 0; $i < count($elements); $i++
) {
465 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
470 * Set IM Service Provider type fields for related contacts.
472 * @param array $elements
473 * IM service provider type ids of related contact.
475 public function setActiveFieldRelatedContactImProvider($elements) {
476 for ($i = 0; $i < count($elements); $i++
) {
477 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
482 * Format the field values for input to the api.
485 * (reference ) associative array of name/value pairs
487 public function &getActiveFieldParams() {
490 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
491 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
495 if (isset($this->_activeFields
[$i]->_value
)) {
496 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
497 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
498 $params[$this->_activeFields
[$i]->_name
] = array();
502 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
503 'location_type_id' => $this->_activeFields
[$i]->_hasLocationType
,
506 if (isset($this->_activeFields
[$i]->_phoneType
)) {
507 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
510 // get IM service Provider type id
511 if (isset($this->_activeFields
[$i]->_imProvider
)) {
512 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
515 $params[$this->_activeFields
[$i]->_name
][] = $value;
517 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
519 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
520 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
523 $params[$this->_activeFields
[$i]->_name
][] = $value;
526 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
527 if (!isset($this->_activeFields
[$i]->_related
)) {
528 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
532 //minor fix for CRM-4062
533 if (isset($this->_activeFields
[$i]->_related
)) {
534 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
535 $params[$this->_activeFields
[$i]->_related
] = array();
538 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
539 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
542 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
543 if (!empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
]) &&
544 !is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])
546 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = array();
549 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
550 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
553 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
554 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
557 // get IM service Provider type id for related contact
558 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
559 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
562 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
564 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
565 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = array(
566 'url' => $this->_activeFields
[$i]->_value
,
567 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
571 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
583 public function getColumnPatterns() {
585 foreach ($this->_fields
as $name => $field) {
586 $values[$name] = $field->_columnPattern
;
592 * @param string $name
595 * @param string $headerPattern
596 * @param string $dataPattern
597 * @param bool $hasLocationType
599 public function addField(
600 $name, $title, $type = CRM_Utils_Type
::T_INT
,
601 $headerPattern = '//', $dataPattern = '//',
602 $hasLocationType = FALSE
604 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
606 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
611 * Store parser values.
613 * @param CRM_Core_Session $store
617 public function set($store, $mode = self
::MODE_SUMMARY
) {
618 $store->set('rowCount', $this->_rowCount
);
619 $store->set('fields', $this->getSelectValues());
620 $store->set('fieldTypes', $this->getSelectTypes());
622 $store->set('columnPatterns', $this->getColumnPatterns());
623 $store->set('dataPatterns', $this->getDataPatterns());
624 $store->set('columnCount', $this->_activeFieldCount
);
626 $store->set('totalRowCount', $this->_totalCount
);
627 $store->set('validRowCount', $this->_validCount
);
628 $store->set('invalidRowCount', $this->_invalidRowCount
);
629 $store->set('conflictRowCount', $this->_conflictCount
);
630 $store->set('unMatchCount', $this->_unMatchCount
);
632 switch ($this->_contactType
) {
634 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
638 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
642 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
645 if ($this->_invalidRowCount
) {
646 $store->set('errorsFileName', $this->_errorFileName
);
648 if ($this->_conflictCount
) {
649 $store->set('conflictsFileName', $this->_conflictFileName
);
651 if (isset($this->_rows
) && !empty($this->_rows
)) {
652 $store->set('dataValues', $this->_rows
);
655 if ($this->_unMatchCount
) {
656 $store->set('mismatchFileName', $this->_misMatchFilemName
);
659 if ($mode == self
::MODE_IMPORT
) {
660 $store->set('duplicateRowCount', $this->_duplicateCount
);
661 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
662 if ($this->_duplicateCount
) {
663 $store->set('duplicatesFileName', $this->_duplicateFileName
);
665 if ($this->_unparsedAddressCount
) {
666 $store->set('errorsFileName', $this->_errorFileName
);
669 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
673 * Export data to a CSV file.
675 * @param string $fileName
676 * @param array $header
679 public static function exportCSV($fileName, $header, $data) {
681 if (file_exists($fileName) && !is_writable($fileName)) {
682 CRM_Core_Error
::movedSiteError($fileName);
684 //hack to remove '_status', '_statusMsg' and '_id' from error file
685 $errorValues = array();
686 $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW');
687 foreach ($data as $rowCount => $rowValues) {
689 foreach ($rowValues as $key => $val) {
690 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
693 $errorValues[$rowCount][$key] = $val;
697 $data = $errorValues;
700 $fd = fopen($fileName, 'w');
702 foreach ($header as $key => $value) {
703 $header[$key] = "\"$value\"";
705 $config = CRM_Core_Config
::singleton();
706 $output[] = implode($config->fieldSeparator
, $header);
708 foreach ($data as $datum) {
709 foreach ($datum as $key => $value) {
710 $datum[$key] = "\"$value\"";
712 $output[] = implode($config->fieldSeparator
, $datum);
714 fwrite($fd, implode("\n", $output));
719 * Update the record with PK $id in the import database table.
722 * @param array $params
724 public function updateImportRecord($id, &$params) {
725 $statusFieldName = $this->_statusFieldName
;
726 $primaryKeyName = $this->_primaryKeyName
;
728 if ($statusFieldName && $primaryKeyName) {
729 $dao = new CRM_Core_DAO();
730 $db = $dao->getDatabaseConnection();
732 $query = "UPDATE $this->_tableName
733 SET $statusFieldName = ?,
734 ${statusFieldName}Msg = ?
735 WHERE $primaryKeyName = ?";
737 $params[$statusFieldName],
738 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
742 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
744 $db->query($query, $args);
749 * Format common params data to proper format to store.
751 * @param array $params
752 * Contain record values.
753 * @param array $formatted
754 * Array of formatted data.
755 * @param array $contactFields
756 * Contact DAO fields.
758 public function formatCommonData($params, &$formatted, &$contactFields) {
760 CRM_Utils_Array
::value('contact_type', $formatted),
764 //add custom fields for contact sub type
765 if (!empty($this->_contactSubType
)) {
766 $csType = $this->_contactSubType
;
769 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
770 $csType = $relCsType;
773 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
775 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
776 $customFields = $customFields +
$addressCustomFields;
778 //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
780 'email_greeting_custom' => 'email_greeting',
781 'postal_greeting_custom' => 'postal_greeting',
782 'addressee_custom' => 'addressee',
784 foreach ($elements as $k => $v) {
785 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
786 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
787 $params[$v] = $label;
792 $session = CRM_Core_Session
::singleton();
793 $dateType = $session->get("dateTypes");
794 foreach ($params as $key => $val) {
795 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
796 if ($customFieldID &&
797 !array_key_exists($customFieldID, $addressCustomFields)
799 //we should not update Date to null, CRM-4062
800 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
802 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
804 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
805 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
806 //retain earlier value when Import mode is `Fill`
807 unset($params[$key]);
810 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
815 if ($key == 'birth_date' && $val) {
816 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
818 elseif ($key == 'deceased_date' && $val) {
819 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
820 $params['is_deceased'] = 1;
822 elseif ($key == 'is_deceased' && $val) {
823 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
825 elseif ($key == 'gender') {
827 $params[$key] = $this->checkGender($val);
831 //now format custom data.
832 foreach ($params as $key => $field) {
833 if (is_array($field)) {
834 $isAddressCustomField = FALSE;
835 foreach ($field as $value) {
837 if (is_array($value)) {
838 foreach ($value as $name => $testForEmpty) {
839 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
840 $isAddressCustomField = TRUE;
843 // check if $value does not contain IM provider or phoneType
844 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
855 $this->formatContactParameters($value, $formatted);
858 if (!$isAddressCustomField) {
863 $formatValues = array(
867 if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
868 // due to merging of individual table and
869 // contact table, we need to avoid
870 // preferred_communication_method forcefully
871 $formatValues['contact_type'] = $formatted['contact_type'];
874 if ($key == 'id' && isset($field)) {
875 $formatted[$key] = $field;
877 $this->formatContactParameters($formatValues, $formatted);
879 //Handling Custom Data
880 // note: Address custom fields will be handled separately inside formatContactParameters
881 if (($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) &&
882 array_key_exists($customFieldID, $customFields) &&
883 !array_key_exists($customFieldID, $addressCustomFields)
886 $extends = CRM_Utils_Array
::value('extends', $customFields[$customFieldID]);
887 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
891 case 'Autocomplete-Select':
892 if ($customFields[$customFieldID]['data_type'] == 'String' ||
$customFields[$customFieldID]['data_type'] == 'Int') {
893 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
894 foreach ($customOption as $customValue) {
895 $val = CRM_Utils_Array
::value('value', $customValue);
896 $label = CRM_Utils_Array
::value('label', $customValue);
897 $label = strtolower($label);
898 $value = strtolower(trim($formatted[$key]));
899 if (($value == $label) ||
($value == strtolower($val))) {
900 $params[$key] = $formatted[$key] = $val;
909 if (!empty($formatted[$key]) && !empty($params[$key])) {
910 $mulValues = explode(',', $formatted[$key]);
911 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
912 $formatted[$key] = array();
913 $params[$key] = array();
914 foreach ($mulValues as $v1) {
915 foreach ($customOption as $v2) {
916 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
917 (strtolower($v2['value']) == strtolower(trim($v1)))
919 if ($htmlType == 'CheckBox') {
920 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
923 $params[$key][] = $formatted[$key][] = $v2['value'];
934 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
935 !array_key_exists($customFieldID, $addressCustomFields)
937 // @todo calling api functions directly is not supported
938 _civicrm_api3_custom_format_params($params, $formatted, $extends);
941 // to check if not update mode and unset the fields with empty value.
942 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
943 foreach ($formatted['custom'] as $customKey => $customvalue) {
944 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
945 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
947 $emptyValue = CRM_Utils_Array
::value('value', $customvalue[-1]);
948 if (!isset($emptyValue)) {
949 unset($formatted['custom'][$customKey]);
954 // parse street address, CRM-5450
955 if ($this->_parseStreetAddress
) {
956 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
957 foreach ($formatted['address'] as $instance => & $address) {
958 $streetAddress = CRM_Utils_Array
::value('street_address', $address);
959 if (empty($streetAddress)) {
962 // parse address field.
963 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
965 //street address consider to be parsed properly,
966 //If we get street_name and street_number.
967 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
968 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
971 // merge parse address w/ main address block.
972 $address = array_merge($address, $parsedFields);
979 * Format contact parameters.
981 * @todo this function needs re-writing & re-merging into the main function.
985 * @param array $values
986 * @param array $params
990 protected function formatContactParameters(&$values, &$params) {
991 // Crawl through the possible classes:
1004 // Cache the various object fields
1005 static $fields = array();
1007 // first add core contact values since for other Civi modules they are not added
1008 $contactFields = CRM_Contact_DAO_Contact
::fields();
1009 _civicrm_api3_store_values($contactFields, $values, $params);
1011 if (isset($values['contact_type'])) {
1012 // we're an individual/household/org property
1014 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
1016 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
1020 if (isset($values['individual_prefix'])) {
1021 if (!empty($params['prefix_id'])) {
1022 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1023 $params['prefix'] = $prefixes[$params['prefix_id']];
1026 $params['prefix'] = $values['individual_prefix'];
1031 if (isset($values['individual_suffix'])) {
1032 if (!empty($params['suffix_id'])) {
1033 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1034 $params['suffix'] = $suffixes[$params['suffix_id']];
1037 $params['suffix'] = $values['individual_suffix'];
1043 if (isset($values['email_greeting'])) {
1044 if (!empty($params['email_greeting_id'])) {
1045 $emailGreetingFilter = array(
1046 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1047 'greeting_type' => 'email_greeting',
1049 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
1050 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1053 $params['email_greeting'] = $values['email_greeting'];
1059 if (isset($values['postal_greeting'])) {
1060 if (!empty($params['postal_greeting_id'])) {
1061 $postalGreetingFilter = array(
1062 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1063 'greeting_type' => 'postal_greeting',
1065 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
1066 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1069 $params['postal_greeting'] = $values['postal_greeting'];
1074 if (isset($values['addressee'])) {
1075 if (!empty($params['addressee_id'])) {
1076 $addresseeFilter = array(
1077 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1078 'greeting_type' => 'addressee',
1080 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
1081 $params['addressee'] = $addressee[$params['addressee_id']];
1084 $params['addressee'] = $values['addressee'];
1089 if (isset($values['gender'])) {
1090 if (!empty($params['gender_id'])) {
1091 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1092 $params['gender'] = $genders[$params['gender_id']];
1095 $params['gender'] = $values['gender'];
1100 if (!empty($values['preferred_communication_method'])) {
1102 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
1104 $preffComm = explode(',', $values['preferred_communication_method']);
1105 foreach ($preffComm as $v) {
1106 $v = strtolower(trim($v));
1107 if (array_key_exists($v, $pcm)) {
1108 $comm[$pcm[$v]] = 1;
1112 $params['preferred_communication_method'] = $comm;
1116 // format the website params.
1117 if (!empty($values['url'])) {
1118 static $websiteFields;
1119 if (!is_array($websiteFields)) {
1120 $websiteFields = CRM_Core_DAO_Website
::fields();
1122 if (!array_key_exists('website', $params) ||
1123 !is_array($params['website'])
1125 $params['website'] = array();
1128 $websiteCount = count($params['website']);
1129 _civicrm_api3_store_values($websiteFields, $values,
1130 $params['website'][++
$websiteCount]
1136 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1137 if (!empty($values['location_type_id'])) {
1138 $blockTypes = array(
1142 'openid' => 'OpenID',
1143 'phone_ext' => 'Phone',
1145 foreach ($blockTypes as $blockFieldName => $block) {
1146 if (!array_key_exists($blockFieldName, $values)) {
1150 // block present in value array.
1151 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
1152 $params[$blockFieldName] = array();
1155 if (!array_key_exists($block, $fields)) {
1156 $className = "CRM_Core_DAO_$block";
1157 $fields[$block] = $className::fields();
1160 $blockCnt = count($params[$blockFieldName]);
1162 // copy value to dao field name.
1163 if ($blockFieldName == 'im') {
1164 $values['name'] = $values[$blockFieldName];
1167 _civicrm_api3_store_values($fields[$block], $values,
1168 $params[$blockFieldName][++
$blockCnt]
1171 if ($values['location_type_id'] === 'Primary') {
1172 if (!empty($params['id'])) {
1173 $primary = civicrm_api3($block, 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1));
1175 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1176 $values['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1177 $values['is_primary'] = 1;
1180 if (empty($params['id']) && ($blockCnt == 1)) {
1181 $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
1184 // we only process single block at a time.
1188 // handle address fields.
1189 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1190 $params['address'] = array();
1193 if (!array_key_exists('Address', $fields)) {
1194 $fields['Address'] = CRM_Core_DAO_Address
::fields();
1197 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1198 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1199 // the address in CRM_Core_BAO_Address::create method
1200 if (!empty($values['location_type_id'])) {
1201 static $customFields = array();
1202 if (empty($customFields)) {
1203 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1205 // make a copy of values, as we going to make changes
1206 $newValues = $values;
1207 foreach ($values as $key => $val) {
1208 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1209 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1210 // mark an entry in fields array since we want the value of custom field to be copied
1211 $fields['Address'][$key] = NULL;
1213 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
1214 switch ($htmlType) {
1216 case 'Multi-Select':
1218 $mulValues = explode(',', $val);
1219 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1220 $newValues[$key] = array();
1221 foreach ($mulValues as $v1) {
1222 foreach ($customOption as $v2) {
1223 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1224 (strtolower($v2['value']) == strtolower(trim($v1)))
1226 if ($htmlType == 'CheckBox') {
1227 $newValues[$key][$v2['value']] = 1;
1230 $newValues[$key][] = $v2['value'];
1240 // consider new values
1241 $values = $newValues;
1244 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1246 $addressFields = array(
1250 'supplemental_address_1',
1251 'supplemental_address_2',
1252 'supplemental_address_3',
1253 'StateProvince.name',
1256 foreach ($addressFields as $field) {
1257 if (array_key_exists($field, $values)) {
1258 if (!array_key_exists('address', $params)) {
1259 $params['address'] = array();
1261 $params['address'][$values['location_type_id']][$field] = $values[$field];
1265 if ($values['location_type_id'] === 'Primary') {
1266 if (!empty($params['id'])) {
1267 $primary = civicrm_api3('Address', 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1));
1269 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1270 $params['address'][$values['location_type_id']]['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1271 $params['address'][$values['location_type_id']]['is_primary'] = 1;
1277 if (isset($values['note'])) {
1279 if (!isset($params['note'])) {
1280 $params['note'] = array();
1282 $noteBlock = count($params['note']) +
1;
1284 $params['note'][$noteBlock] = array();
1285 if (!isset($fields['Note'])) {
1286 $fields['Note'] = CRM_Core_DAO_Note
::fields();
1289 // get the current logged in civicrm user
1290 $session = CRM_Core_Session
::singleton();
1291 $userID = $session->get('userID');
1294 $values['contact_id'] = $userID;
1297 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1302 // Check for custom field values
1304 if (empty($fields['custom'])) {
1305 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
1306 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1310 foreach ($values as $key => $value) {
1311 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1312 // check if it's a valid custom field id
1314 if (!array_key_exists($customFieldID, $fields['custom'])) {
1315 return civicrm_api3_create_error('Invalid custom field ID');
1318 $params[$key] = $value;