From 5484728757de4c85d44305a92bfa2aa35351fbce Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 12 Jan 2022 00:50:11 +1300 Subject: [PATCH] Move function to the class that 'owns' it Following https://github.com/civicrm/civicrm-core/commit/f54e87d9cf1e216036adf92f744cdbac3c1fe79e#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 --- CRM/Contact/Import/Parser.php | 224 -------------------------- CRM/Contact/Import/Parser/Contact.php | 224 ++++++++++++++++++++++++++ CRM/Custom/Import/Parser/Api.php | 2 +- 3 files changed, 225 insertions(+), 225 deletions(-) diff --git a/CRM/Contact/Import/Parser.php b/CRM/Contact/Import/Parser.php index 99932d242a..cc354036cb 100644 --- a/CRM/Contact/Import/Parser.php +++ b/CRM/Contact/Import/Parser.php @@ -726,230 +726,6 @@ abstract class CRM_Contact_Import_Parser extends CRM_Import_Parser { } } - /** - * 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. * diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 8399c5fb40..5fd6af3b72 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -957,6 +957,230 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); } + /** + * 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. + */ + private 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); + } + } + } + } + /** * Get the array of successfully imported contact id's * diff --git a/CRM/Custom/Import/Parser/Api.php b/CRM/Custom/Import/Parser/Api.php index bff037f88f..28320ca624 100644 --- a/CRM/Custom/Import/Parser/Api.php +++ b/CRM/Custom/Import/Parser/Api.php @@ -195,7 +195,7 @@ class CRM_Custom_Import_Parser_Api extends CRM_Custom_Import_Parser { * @param array $formatted * Array of formatted data. */ - public function formatCommonData($params, &$formatted) { + private function formatCommonData($params, &$formatted) { $customFields = CRM_Core_BAO_CustomField::getFields(NULL); -- 2.25.1