3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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 // clean up memory from dao's
279 CRM_Core_DAO
::freeResult();
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(array(
305 $this->_errorFileName
= self
::errorFileName(self
::ERROR
);
306 self
::exportCSV($this->_errorFileName
, $headers, $this->_errors
);
308 if ($this->_conflictCount
) {
309 $headers = array_merge(array(
315 $this->_conflictFileName
= self
::errorFileName(self
::CONFLICT
);
316 self
::exportCSV($this->_conflictFileName
, $headers, $this->_conflicts
);
318 if ($this->_duplicateCount
) {
319 $headers = array_merge(array(
321 ts('View Contact URL'),
326 $this->_duplicateFileName
= self
::errorFileName(self
::DUPLICATE
);
327 self
::exportCSV($this->_duplicateFileName
, $headers, $this->_duplicates
);
329 if ($this->_unMatchCount
) {
330 $headers = array_merge(array(
337 $this->_misMatchFilemName
= self
::errorFileName(self
::NO_MATCH
);
338 self
::exportCSV($this->_misMatchFilemName
, $headers, $this->_unMatch
);
340 if ($this->_unparsedAddressCount
) {
341 $headers = array_merge(array(
343 ts('Contact Edit URL'),
347 $this->_errorFileName
= self
::errorFileName(self
::UNPARSED_ADDRESS_WARNING
);
348 self
::exportCSV($this->_errorFileName
, $headers, $this->_unparsedAddresses
);
351 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
352 return $this->fini();
356 * Given a list of the importable field keys that the user has selected.
357 * set the active fields array to this list
359 * @param array $fieldKeys
360 * Mapped array of values.
362 public function setActiveFields($fieldKeys) {
363 $this->_activeFieldCount
= count($fieldKeys);
364 foreach ($fieldKeys as $key) {
365 if (empty($this->_fields
[$key])) {
366 $this->_activeFields
[] = new CRM_Contact_Import_Field('', ts('- do not import -'));
369 $this->_activeFields
[] = clone($this->_fields
[$key]);
377 public function setActiveFieldLocationTypes($elements) {
378 for ($i = 0; $i < count($elements); $i++
) {
379 $this->_activeFields
[$i]->_hasLocationType
= $elements[$i];
389 public function setActiveFieldPhoneTypes($elements) {
390 for ($i = 0; $i < count($elements); $i++
) {
391 $this->_activeFields
[$i]->_phoneType
= $elements[$i];
398 public function setActiveFieldWebsiteTypes($elements) {
399 for ($i = 0; $i < count($elements); $i++
) {
400 $this->_activeFields
[$i]->_websiteType
= $elements[$i];
405 * Set IM Service Provider type fields.
407 * @param array $elements
408 * IM service provider type ids.
410 public function setActiveFieldImProviders($elements) {
411 for ($i = 0; $i < count($elements); $i++
) {
412 $this->_activeFields
[$i]->_imProvider
= $elements[$i];
419 public function setActiveFieldRelated($elements) {
420 for ($i = 0; $i < count($elements); $i++
) {
421 $this->_activeFields
[$i]->_related
= $elements[$i];
428 public function setActiveFieldRelatedContactType($elements) {
429 for ($i = 0; $i < count($elements); $i++
) {
430 $this->_activeFields
[$i]->_relatedContactType
= $elements[$i];
437 public function setActiveFieldRelatedContactDetails($elements) {
438 for ($i = 0; $i < count($elements); $i++
) {
439 $this->_activeFields
[$i]->_relatedContactDetails
= $elements[$i];
446 public function setActiveFieldRelatedContactLocType($elements) {
447 for ($i = 0; $i < count($elements); $i++
) {
448 $this->_activeFields
[$i]->_relatedContactLocType
= $elements[$i];
453 * Set active field for related contact's phone type.
455 * @param array $elements
457 public function setActiveFieldRelatedContactPhoneType($elements) {
458 for ($i = 0; $i < count($elements); $i++
) {
459 $this->_activeFields
[$i]->_relatedContactPhoneType
= $elements[$i];
466 public function setActiveFieldRelatedContactWebsiteType($elements) {
467 for ($i = 0; $i < count($elements); $i++
) {
468 $this->_activeFields
[$i]->_relatedContactWebsiteType
= $elements[$i];
473 * Set IM Service Provider type fields for related contacts.
475 * @param array $elements
476 * IM service provider type ids of related contact.
478 public function setActiveFieldRelatedContactImProvider($elements) {
479 for ($i = 0; $i < count($elements); $i++
) {
480 $this->_activeFields
[$i]->_relatedContactImProvider
= $elements[$i];
485 * Format the field values for input to the api.
488 * (reference ) associative array of name/value pairs
490 public function &getActiveFieldParams() {
493 for ($i = 0; $i < $this->_activeFieldCount
; $i++
) {
494 if ($this->_activeFields
[$i]->_name
== 'do_not_import') {
498 if (isset($this->_activeFields
[$i]->_value
)) {
499 if (isset($this->_activeFields
[$i]->_hasLocationType
)) {
500 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
501 $params[$this->_activeFields
[$i]->_name
] = array();
505 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
506 'location_type_id' => $this->_activeFields
[$i]->_hasLocationType
,
509 if (isset($this->_activeFields
[$i]->_phoneType
)) {
510 $value['phone_type_id'] = $this->_activeFields
[$i]->_phoneType
;
513 // get IM service Provider type id
514 if (isset($this->_activeFields
[$i]->_imProvider
)) {
515 $value['provider_id'] = $this->_activeFields
[$i]->_imProvider
;
518 $params[$this->_activeFields
[$i]->_name
][] = $value;
520 elseif (isset($this->_activeFields
[$i]->_websiteType
)) {
522 $this->_activeFields
[$i]->_name
=> $this->_activeFields
[$i]->_value
,
523 'website_type_id' => $this->_activeFields
[$i]->_websiteType
,
526 $params[$this->_activeFields
[$i]->_name
][] = $value;
529 if (!isset($params[$this->_activeFields
[$i]->_name
])) {
530 if (!isset($this->_activeFields
[$i]->_related
)) {
531 $params[$this->_activeFields
[$i]->_name
] = $this->_activeFields
[$i]->_value
;
535 //minor fix for CRM-4062
536 if (isset($this->_activeFields
[$i]->_related
)) {
537 if (!isset($params[$this->_activeFields
[$i]->_related
])) {
538 $params[$this->_activeFields
[$i]->_related
] = array();
541 if (!isset($params[$this->_activeFields
[$i]->_related
]['contact_type']) && !empty($this->_activeFields
[$i]->_relatedContactType
)) {
542 $params[$this->_activeFields
[$i]->_related
]['contact_type'] = $this->_activeFields
[$i]->_relatedContactType
;
545 if (isset($this->_activeFields
[$i]->_relatedContactLocType
) && !empty($this->_activeFields
[$i]->_value
)) {
546 if (!empty($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
]) &&
547 !is_array($params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
])
549 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = array();
552 $this->_activeFields
[$i]->_relatedContactDetails
=> $this->_activeFields
[$i]->_value
,
553 'location_type_id' => $this->_activeFields
[$i]->_relatedContactLocType
,
556 if (isset($this->_activeFields
[$i]->_relatedContactPhoneType
)) {
557 $value['phone_type_id'] = $this->_activeFields
[$i]->_relatedContactPhoneType
;
560 // get IM service Provider type id for related contact
561 if (isset($this->_activeFields
[$i]->_relatedContactImProvider
)) {
562 $value['provider_id'] = $this->_activeFields
[$i]->_relatedContactImProvider
;
565 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = $value;
567 elseif (isset($this->_activeFields
[$i]->_relatedContactWebsiteType
)) {
568 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
][] = array(
569 'url' => $this->_activeFields
[$i]->_value
,
570 'website_type_id' => $this->_activeFields
[$i]->_relatedContactWebsiteType
,
574 $params[$this->_activeFields
[$i]->_related
][$this->_activeFields
[$i]->_relatedContactDetails
] = $this->_activeFields
[$i]->_value
;
586 public function getColumnPatterns() {
588 foreach ($this->_fields
as $name => $field) {
589 $values[$name] = $field->_columnPattern
;
595 * @param string $name
598 * @param string $headerPattern
599 * @param string $dataPattern
600 * @param bool $hasLocationType
602 public function addField(
603 $name, $title, $type = CRM_Utils_Type
::T_INT
,
604 $headerPattern = '//', $dataPattern = '//',
605 $hasLocationType = FALSE
607 $this->_fields
[$name] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
609 $this->_fields
['doNotImport'] = new CRM_Contact_Import_Field($name, $title, $type, $headerPattern, $dataPattern, $hasLocationType);
614 * Store parser values.
616 * @param CRM_Core_Session $store
620 public function set($store, $mode = self
::MODE_SUMMARY
) {
621 $store->set('rowCount', $this->_rowCount
);
622 $store->set('fields', $this->getSelectValues());
623 $store->set('fieldTypes', $this->getSelectTypes());
625 $store->set('columnPatterns', $this->getColumnPatterns());
626 $store->set('dataPatterns', $this->getDataPatterns());
627 $store->set('columnCount', $this->_activeFieldCount
);
629 $store->set('totalRowCount', $this->_totalCount
);
630 $store->set('validRowCount', $this->_validCount
);
631 $store->set('invalidRowCount', $this->_invalidRowCount
);
632 $store->set('conflictRowCount', $this->_conflictCount
);
633 $store->set('unMatchCount', $this->_unMatchCount
);
635 switch ($this->_contactType
) {
637 $store->set('contactType', CRM_Import_Parser
::CONTACT_INDIVIDUAL
);
641 $store->set('contactType', CRM_Import_Parser
::CONTACT_HOUSEHOLD
);
645 $store->set('contactType', CRM_Import_Parser
::CONTACT_ORGANIZATION
);
648 if ($this->_invalidRowCount
) {
649 $store->set('errorsFileName', $this->_errorFileName
);
651 if ($this->_conflictCount
) {
652 $store->set('conflictsFileName', $this->_conflictFileName
);
654 if (isset($this->_rows
) && !empty($this->_rows
)) {
655 $store->set('dataValues', $this->_rows
);
658 if ($this->_unMatchCount
) {
659 $store->set('mismatchFileName', $this->_misMatchFilemName
);
662 if ($mode == self
::MODE_IMPORT
) {
663 $store->set('duplicateRowCount', $this->_duplicateCount
);
664 $store->set('unparsedAddressCount', $this->_unparsedAddressCount
);
665 if ($this->_duplicateCount
) {
666 $store->set('duplicatesFileName', $this->_duplicateFileName
);
668 if ($this->_unparsedAddressCount
) {
669 $store->set('errorsFileName', $this->_errorFileName
);
672 //echo "$this->_totalCount,$this->_invalidRowCount,$this->_conflictCount,$this->_duplicateCount";
676 * Export data to a CSV file.
678 * @param string $fileName
679 * @param array $header
682 public static function exportCSV($fileName, $header, $data) {
684 if (file_exists($fileName) && !is_writable($fileName)) {
685 CRM_Core_Error
::movedSiteError($fileName);
687 //hack to remove '_status', '_statusMsg' and '_id' from error file
688 $errorValues = array();
689 $dbRecordStatus = array('IMPORTED', 'ERROR', 'DUPLICATE', 'INVALID', 'NEW');
690 foreach ($data as $rowCount => $rowValues) {
692 foreach ($rowValues as $key => $val) {
693 if (in_array($val, $dbRecordStatus) && $count == (count($rowValues) - 3)) {
696 $errorValues[$rowCount][$key] = $val;
700 $data = $errorValues;
703 $fd = fopen($fileName, 'w');
705 foreach ($header as $key => $value) {
706 $header[$key] = "\"$value\"";
708 $config = CRM_Core_Config
::singleton();
709 $output[] = implode($config->fieldSeparator
, $header);
711 foreach ($data as $datum) {
712 foreach ($datum as $key => $value) {
713 $datum[$key] = "\"$value\"";
715 $output[] = implode($config->fieldSeparator
, $datum);
717 fwrite($fd, implode("\n", $output));
722 * Update the record with PK $id in the import database table.
725 * @param array $params
727 public function updateImportRecord($id, &$params) {
728 $statusFieldName = $this->_statusFieldName
;
729 $primaryKeyName = $this->_primaryKeyName
;
731 if ($statusFieldName && $primaryKeyName) {
732 $dao = new CRM_Core_DAO();
733 $db = $dao->getDatabaseConnection();
735 $query = "UPDATE $this->_tableName
736 SET $statusFieldName = ?,
737 ${statusFieldName}Msg = ?
738 WHERE $primaryKeyName = ?";
740 $params[$statusFieldName],
741 CRM_Utils_Array
::value("${statusFieldName}Msg", $params),
745 //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>";
747 $db->query($query, $args);
752 * Format common params data to proper format to store.
754 * @param array $params
755 * Contain record values.
756 * @param array $formatted
757 * Array of formatted data.
758 * @param array $contactFields
759 * Contact DAO fields.
761 public function formatCommonData($params, &$formatted, &$contactFields) {
763 CRM_Utils_Array
::value('contact_type', $formatted),
767 //add custom fields for contact sub type
768 if (!empty($this->_contactSubType
)) {
769 $csType = $this->_contactSubType
;
772 if ($relCsType = CRM_Utils_Array
::value('contact_sub_type', $formatted)) {
773 $csType = $relCsType;
776 $customFields = CRM_Core_BAO_CustomField
::getFields($formatted['contact_type'], FALSE, FALSE, $csType);
778 $addressCustomFields = CRM_Core_BAO_CustomField
::getFields('Address');
779 $customFields = $customFields +
$addressCustomFields;
781 //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
783 'email_greeting_custom' => 'email_greeting',
784 'postal_greeting_custom' => 'postal_greeting',
785 'addressee_custom' => 'addressee',
787 foreach ($elements as $k => $v) {
788 if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
789 $label = key(CRM_Core_OptionGroup
::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
790 $params[$v] = $label;
795 $session = CRM_Core_Session
::singleton();
796 $dateType = $session->get("dateTypes");
797 foreach ($params as $key => $val) {
798 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
799 if ($customFieldID &&
800 !array_key_exists($customFieldID, $addressCustomFields)
802 //we should not update Date to null, CRM-4062
803 if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
805 CRM_Contact_Import_Parser_Contact
::formatCustomDate($params, $formatted, $dateType, $key);
807 elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
808 if (empty($val) && !is_numeric($val) && $this->_onDuplicate
== CRM_Import_Parser
::DUPLICATE_FILL
) {
809 //retain earlier value when Import mode is `Fill`
810 unset($params[$key]);
813 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
818 if ($key == 'birth_date' && $val) {
819 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
821 elseif ($key == 'deceased_date' && $val) {
822 CRM_Utils_Date
::convertToDefaultDate($params, $dateType, $key);
823 $params['is_deceased'] = 1;
825 elseif ($key == 'is_deceased' && $val) {
826 $params[$key] = CRM_Utils_String
::strtoboolstr($val);
828 elseif ($key == 'gender') {
830 $params[$key] = $this->checkGender($val);
834 //now format custom data.
835 foreach ($params as $key => $field) {
836 if (is_array($field)) {
837 $isAddressCustomField = FALSE;
838 foreach ($field as $value) {
840 if (is_array($value)) {
841 foreach ($value as $name => $testForEmpty) {
842 if ($addressCustomFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
843 $isAddressCustomField = TRUE;
846 // check if $value does not contain IM provider or phoneType
847 if (($name !== 'phone_type_id' ||
$name !== 'provider_id') && ($testForEmpty === '' ||
$testForEmpty == NULL)) {
858 $this->formatContactParameters($value, $formatted);
861 if (!$isAddressCustomField) {
866 $formatValues = array(
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;
910 case 'AdvMulti-Select':
913 if (!empty($formatted[$key]) && !empty($params[$key])) {
914 $mulValues = explode(',', $formatted[$key]);
915 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
916 $formatted[$key] = array();
917 $params[$key] = array();
918 foreach ($mulValues as $v1) {
919 foreach ($customOption as $v2) {
920 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
921 (strtolower($v2['value']) == strtolower(trim($v1)))
923 if ($htmlType == 'CheckBox') {
924 $params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
927 $params[$key][] = $formatted[$key][] = $v2['value'];
938 if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
939 !array_key_exists($customFieldID, $addressCustomFields)
941 // @todo calling api functions directly is not supported
942 _civicrm_api3_custom_format_params($params, $formatted, $extends);
945 // to check if not update mode and unset the fields with empty value.
946 if (!$this->_updateWithId
&& array_key_exists('custom', $formatted)) {
947 foreach ($formatted['custom'] as $customKey => $customvalue) {
948 if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
949 $formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
951 $emptyValue = CRM_Utils_Array
::value('value', $customvalue[-1]);
952 if (!isset($emptyValue)) {
953 unset($formatted['custom'][$customKey]);
958 // parse street address, CRM-5450
959 if ($this->_parseStreetAddress
) {
960 if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
961 foreach ($formatted['address'] as $instance => & $address) {
962 $streetAddress = CRM_Utils_Array
::value('street_address', $address);
963 if (empty($streetAddress)) {
966 // parse address field.
967 $parsedFields = CRM_Core_BAO_Address
::parseStreetAddress($streetAddress);
969 //street address consider to be parsed properly,
970 //If we get street_name and street_number.
971 if (empty($parsedFields['street_name']) ||
empty($parsedFields['street_number'])) {
972 $parsedFields = array_fill_keys(array_keys($parsedFields), '');
975 // merge parse address w/ main address block.
976 $address = array_merge($address, $parsedFields);
983 * Format contact parameters.
985 * @todo this function needs re-writing & re-merging into the main function.
989 * @param array $values
990 * @param array $params
994 protected function formatContactParameters(&$values, &$params) {
995 // Crawl through the possible classes:
1008 // Cache the various object fields
1009 static $fields = array();
1011 // first add core contact values since for other Civi modules they are not added
1012 $contactFields = CRM_Contact_DAO_Contact
::fields();
1013 _civicrm_api3_store_values($contactFields, $values, $params);
1015 if (isset($values['contact_type'])) {
1016 // we're an individual/household/org property
1018 $fields[$values['contact_type']] = CRM_Contact_DAO_Contact
::fields();
1020 _civicrm_api3_store_values($fields[$values['contact_type']], $values, $params);
1024 if (isset($values['individual_prefix'])) {
1025 if (!empty($params['prefix_id'])) {
1026 $prefixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1027 $params['prefix'] = $prefixes[$params['prefix_id']];
1030 $params['prefix'] = $values['individual_prefix'];
1035 if (isset($values['individual_suffix'])) {
1036 if (!empty($params['suffix_id'])) {
1037 $suffixes = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1038 $params['suffix'] = $suffixes[$params['suffix_id']];
1041 $params['suffix'] = $values['individual_suffix'];
1047 if (isset($values['email_greeting'])) {
1048 if (!empty($params['email_greeting_id'])) {
1049 $emailGreetingFilter = array(
1050 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1051 'greeting_type' => 'email_greeting',
1053 $emailGreetings = CRM_Core_PseudoConstant
::greeting($emailGreetingFilter);
1054 $params['email_greeting'] = $emailGreetings[$params['email_greeting_id']];
1057 $params['email_greeting'] = $values['email_greeting'];
1063 if (isset($values['postal_greeting'])) {
1064 if (!empty($params['postal_greeting_id'])) {
1065 $postalGreetingFilter = array(
1066 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1067 'greeting_type' => 'postal_greeting',
1069 $postalGreetings = CRM_Core_PseudoConstant
::greeting($postalGreetingFilter);
1070 $params['postal_greeting'] = $postalGreetings[$params['postal_greeting_id']];
1073 $params['postal_greeting'] = $values['postal_greeting'];
1078 if (isset($values['addressee'])) {
1079 if (!empty($params['addressee_id'])) {
1080 $addresseeFilter = array(
1081 'contact_type' => CRM_Utils_Array
::value('contact_type', $params),
1082 'greeting_type' => 'addressee',
1084 $addressee = CRM_Core_PseudoConstant
::addressee($addresseeFilter);
1085 $params['addressee'] = $addressee[$params['addressee_id']];
1088 $params['addressee'] = $values['addressee'];
1093 if (isset($values['gender'])) {
1094 if (!empty($params['gender_id'])) {
1095 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
1096 $params['gender'] = $genders[$params['gender_id']];
1099 $params['gender'] = $values['gender'];
1104 if (!empty($values['preferred_communication_method'])) {
1106 $pcm = array_change_key_case(array_flip(CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'preferred_communication_method')), CASE_LOWER
);
1108 $preffComm = explode(',', $values['preferred_communication_method']);
1109 foreach ($preffComm as $v) {
1110 $v = strtolower(trim($v));
1111 if (array_key_exists($v, $pcm)) {
1112 $comm[$pcm[$v]] = 1;
1116 $params['preferred_communication_method'] = $comm;
1120 // format the website params.
1121 if (!empty($values['url'])) {
1122 static $websiteFields;
1123 if (!is_array($websiteFields)) {
1124 $websiteFields = CRM_Core_DAO_Website
::fields();
1126 if (!array_key_exists('website', $params) ||
1127 !is_array($params['website'])
1129 $params['website'] = array();
1132 $websiteCount = count($params['website']);
1133 _civicrm_api3_store_values($websiteFields, $values,
1134 $params['website'][++
$websiteCount]
1140 // get the formatted location blocks into params - w/ 3.0 format, CRM-4605
1141 if (!empty($values['location_type_id'])) {
1142 $blockTypes = array(
1146 'openid' => 'OpenID',
1147 'phone_ext' => 'Phone',
1149 foreach ($blockTypes as $blockFieldName => $block) {
1150 if (!array_key_exists($blockFieldName, $values)) {
1154 // block present in value array.
1155 if (!array_key_exists($blockFieldName, $params) ||
!is_array($params[$blockFieldName])) {
1156 $params[$blockFieldName] = array();
1159 if (!array_key_exists($block, $fields)) {
1160 $className = "CRM_Core_DAO_$block";
1161 $fields[$block] = $className::fields();
1164 $blockCnt = count($params[$blockFieldName]);
1166 // copy value to dao field name.
1167 if ($blockFieldName == 'im') {
1168 $values['name'] = $values[$blockFieldName];
1171 _civicrm_api3_store_values($fields[$block], $values,
1172 $params[$blockFieldName][++
$blockCnt]
1175 if ($values['location_type_id'] === 'Primary') {
1176 if (!empty($params['id'])) {
1177 $primary = civicrm_api3($block, 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1));
1179 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1180 $values['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1181 $values['is_primary'] = 1;
1184 if (empty($params['id']) && ($blockCnt == 1)) {
1185 $params[$blockFieldName][$blockCnt]['is_primary'] = TRUE;
1188 // we only process single block at a time.
1192 // handle address fields.
1193 if (!array_key_exists('address', $params) ||
!is_array($params['address'])) {
1194 $params['address'] = array();
1197 if (!array_key_exists('Address', $fields)) {
1198 $fields['Address'] = CRM_Core_DAO_Address
::fields();
1201 // Note: we doing multiple value formatting here for address custom fields, plus putting into right format.
1202 // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving
1203 // the address in CRM_Core_BAO_Address::create method
1204 if (!empty($values['location_type_id'])) {
1205 static $customFields = array();
1206 if (empty($customFields)) {
1207 $customFields = CRM_Core_BAO_CustomField
::getFields('Address');
1209 // make a copy of values, as we going to make changes
1210 $newValues = $values;
1211 foreach ($values as $key => $val) {
1212 $customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key);
1213 if ($customFieldID && array_key_exists($customFieldID, $customFields)) {
1214 // mark an entry in fields array since we want the value of custom field to be copied
1215 $fields['Address'][$key] = NULL;
1217 $htmlType = CRM_Utils_Array
::value('html_type', $customFields[$customFieldID]);
1218 switch ($htmlType) {
1220 case 'AdvMulti-Select':
1221 case 'Multi-Select':
1223 $mulValues = explode(',', $val);
1224 $customOption = CRM_Core_BAO_CustomOption
::getCustomOption($customFieldID, TRUE);
1225 $newValues[$key] = array();
1226 foreach ($mulValues as $v1) {
1227 foreach ($customOption as $v2) {
1228 if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
1229 (strtolower($v2['value']) == strtolower(trim($v1)))
1231 if ($htmlType == 'CheckBox') {
1232 $newValues[$key][$v2['value']] = 1;
1235 $newValues[$key][] = $v2['value'];
1245 // consider new values
1246 $values = $newValues;
1249 _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$values['location_type_id']]);
1251 $addressFields = array(
1255 'supplemental_address_1',
1256 'supplemental_address_2',
1257 'supplemental_address_3',
1258 'StateProvince.name',
1261 foreach ($addressFields as $field) {
1262 if (array_key_exists($field, $values)) {
1263 if (!array_key_exists('address', $params)) {
1264 $params['address'] = array();
1266 $params['address'][$values['location_type_id']][$field] = $values[$field];
1270 if ($values['location_type_id'] === 'Primary') {
1271 if (!empty($params['id'])) {
1272 $primary = civicrm_api3('Address', 'get', array('return' => 'location_type_id', 'contact_id' => $params['id'], 'is_primary' => 1, 'sequential' => 1));
1274 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
1275 $params['address'][$values['location_type_id']]['location_type_id'] = (isset($primary) && $primary['count']) ?
$primary['values'][0]['location_type_id'] : $defaultLocationType->id
;
1276 $params['address'][$values['location_type_id']]['is_primary'] = 1;
1282 if (isset($values['note'])) {
1284 if (!isset($params['note'])) {
1285 $params['note'] = array();
1287 $noteBlock = count($params['note']) +
1;
1289 $params['note'][$noteBlock] = array();
1290 if (!isset($fields['Note'])) {
1291 $fields['Note'] = CRM_Core_DAO_Note
::fields();
1294 // get the current logged in civicrm user
1295 $session = CRM_Core_Session
::singleton();
1296 $userID = $session->get('userID');
1299 $values['contact_id'] = $userID;
1302 _civicrm_api3_store_values($fields['Note'], $values, $params['note'][$noteBlock]);
1307 // Check for custom field values
1309 if (empty($fields['custom'])) {
1310 $fields['custom'] = &CRM_Core_BAO_CustomField
::getFields(CRM_Utils_Array
::value('contact_type', $values),
1311 FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE
1315 foreach ($values as $key => $value) {
1316 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($key)) {
1317 // check if it's a valid custom field id
1319 if (!array_key_exists($customFieldID, $fields['custom'])) {
1320 return civicrm_api3_create_error('Invalid custom field ID');
1323 $params[$key] = $value;