Skip to content

Commit

Permalink
Move function to the class that 'owns' it
Browse files Browse the repository at this point in the history
Following f54e87d#diff-5864d6a1d03dffb7699b930837c1c222b41fba16dad7d0a5f73b4923ac53dd42R198
neither of these functions are called from outside their class (I did a universe search)
so making them private (& in one case moving off the parent) means a universe seach won't be needed
next time
  • Loading branch information
eileenmcnaughton committed Jan 11, 2022
1 parent 84f3dc3 commit 2f8de76
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 225 deletions.
224 changes: 0 additions & 224 deletions CRM/Contact/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,230 +726,6 @@ public function setImportStatus(int $id, string $status, string $message): void
}
}

/**
* Format common params data to proper format to store.
*
* @param array $params
* Contain record values.
* @param array $formatted
* Array of formatted data.
* @param array $contactFields
* Contact DAO fields.
*/
public function formatCommonData($params, &$formatted, &$contactFields) {
$csType = [
CRM_Utils_Array::value('contact_type', $formatted),
];

//CRM-5125
//add custom fields for contact sub type
if (!empty($this->_contactSubType)) {
$csType = $this->_contactSubType;
}

if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $formatted)) {
$csType = $relCsType;
}

$customFields = CRM_Core_BAO_CustomField::getFields($formatted['contact_type'], FALSE, FALSE, $csType);

$addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address');
$customFields = $customFields + $addressCustomFields;

//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
$elements = [
'email_greeting_custom' => 'email_greeting',
'postal_greeting_custom' => 'postal_greeting',
'addressee_custom' => 'addressee',
];
foreach ($elements as $k => $v) {
if (array_key_exists($k, $params) && !(array_key_exists($v, $params))) {
$label = key(CRM_Core_OptionGroup::values($v, TRUE, NULL, NULL, 'AND v.name = "Customized"'));
$params[$v] = $label;
}
}

//format date first
$session = CRM_Core_Session::singleton();
$dateType = $session->get("dateTypes");
foreach ($params as $key => $val) {
$customFieldID = CRM_Core_BAO_CustomField::getKeyID($key);
if ($customFieldID &&
!array_key_exists($customFieldID, $addressCustomFields)
) {
//we should not update Date to null, CRM-4062
if ($val && ($customFields[$customFieldID]['data_type'] == 'Date')) {
//CRM-21267
CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $formatted, $dateType, $key);
}
elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') {
if (empty($val) && !is_numeric($val) && $this->_onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) {
//retain earlier value when Import mode is `Fill`
unset($params[$key]);
}
else {
$params[$key] = CRM_Utils_String::strtoboolstr($val);
}
}
}

if ($key == 'birth_date' && $val) {
CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
}
elseif ($key == 'deceased_date' && $val) {
CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key);
$params['is_deceased'] = 1;
}
elseif ($key == 'is_deceased' && $val) {
$params[$key] = CRM_Utils_String::strtoboolstr($val);
}
}

//now format custom data.
foreach ($params as $key => $field) {
if (is_array($field)) {
$isAddressCustomField = FALSE;
foreach ($field as $value) {
$break = FALSE;
if (is_array($value)) {
foreach ($value as $name => $testForEmpty) {
if ($addressCustomFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
$isAddressCustomField = TRUE;
break;
}
// check if $value does not contain IM provider or phoneType
if (($name !== 'phone_type_id' || $name !== 'provider_id') && ($testForEmpty === '' || $testForEmpty == NULL)) {
$break = TRUE;
break;
}
}
}
else {
$break = TRUE;
}

if (!$break) {
if (!empty($value['location_type_id'])) {
$this->formatLocationBlock($value, $formatted);
}
else {
// @todo - this is still reachable - e.g. import with related contact info like firstname,lastname,spouse-first-name,spouse-last-name,spouse-home-phone
CRM_Core_Error::deprecatedFunctionWarning('this is not expected to be reachable now');
$this->formatContactParameters($value, $formatted);
}
}
}
if (!$isAddressCustomField) {
continue;
}
}

$formatValues = [
$key => $field,
];

if (($key !== 'preferred_communication_method') && (array_key_exists($key, $contactFields))) {
// due to merging of individual table and
// contact table, we need to avoid
// preferred_communication_method forcefully
$formatValues['contact_type'] = $formatted['contact_type'];
}

if ($key == 'id' && isset($field)) {
$formatted[$key] = $field;
}
$this->formatContactParameters($formatValues, $formatted);

//Handling Custom Data
// note: Address custom fields will be handled separately inside formatContactParameters
if (($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) &&
array_key_exists($customFieldID, $customFields) &&
!array_key_exists($customFieldID, $addressCustomFields)
) {

$extends = $customFields[$customFieldID]['extends'] ?? NULL;
$htmlType = $customFields[$customFieldID]['html_type'] ?? NULL;
$dataType = $customFields[$customFieldID]['data_type'] ?? NULL;
$serialized = CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID]);

if (!$serialized && in_array($htmlType, ['Select', 'Radio', 'Autocomplete-Select']) && in_array($dataType, ['String', 'Int'])) {
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
foreach ($customOption as $customValue) {
$val = $customValue['value'] ?? NULL;
$label = strtolower($customValue['label'] ?? '');
$value = strtolower(trim($formatted[$key]));
if (($value == $label) || ($value == strtolower($val))) {
$params[$key] = $formatted[$key] = $val;
}
}
}
elseif ($serialized && !empty($formatted[$key]) && !empty($params[$key])) {
$mulValues = explode(',', $formatted[$key]);
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
$formatted[$key] = [];
$params[$key] = [];
foreach ($mulValues as $v1) {
foreach ($customOption as $v2) {
if ((strtolower($v2['label']) == strtolower(trim($v1))) ||
(strtolower($v2['value']) == strtolower(trim($v1)))
) {
if ($htmlType == 'CheckBox') {
$params[$key][$v2['value']] = $formatted[$key][$v2['value']] = 1;
}
else {
$params[$key][] = $formatted[$key][] = $v2['value'];
}
}
}
}
}
}
}

if (!empty($key) && ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) && array_key_exists($customFieldID, $customFields) &&
!array_key_exists($customFieldID, $addressCustomFields)
) {
// @todo calling api functions directly is not supported
_civicrm_api3_custom_format_params($params, $formatted, $extends);
}

// to check if not update mode and unset the fields with empty value.
if (!$this->_updateWithId && array_key_exists('custom', $formatted)) {
foreach ($formatted['custom'] as $customKey => $customvalue) {
if (empty($formatted['custom'][$customKey][-1]['is_required'])) {
$formatted['custom'][$customKey][-1]['is_required'] = $customFields[$customKey]['is_required'];
}
$emptyValue = $customvalue[-1]['value'] ?? NULL;
if (!isset($emptyValue)) {
unset($formatted['custom'][$customKey]);
}
}
}

// parse street address, CRM-5450
if ($this->_parseStreetAddress) {
if (array_key_exists('address', $formatted) && is_array($formatted['address'])) {
foreach ($formatted['address'] as $instance => & $address) {
$streetAddress = $address['street_address'] ?? NULL;
if (empty($streetAddress)) {
continue;
}
// parse address field.
$parsedFields = CRM_Core_BAO_Address::parseStreetAddress($streetAddress);

//street address consider to be parsed properly,
//If we get street_name and street_number.
if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
$parsedFields = array_fill_keys(array_keys($parsedFields), '');
}

// merge parse address w/ main address block.
$address = array_merge($address, $parsedFields);
}
}
}
}

/**
* Format contact parameters.
*
Expand Down
Loading

0 comments on commit 2f8de76

Please sign in to comment.