Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importer - Remove unused functions (isErrorInCustomData, getSubtypes, validateCustomField) #24280

Merged
merged 3 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 0 additions & 149 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1518,81 +1518,6 @@ protected function getContactMatchingFields(): array {
return $fields;
}

/**
* @param $customFieldID
* @param $value
* @param array $fieldMetaData
* @param $dateType
*
* @return ?string
*/
protected function validateCustomField($customFieldID, $value, array $fieldMetaData, $dateType): ?string {
/* validate the data against the CF type */

if ($value) {
$dataType = $fieldMetaData['data_type'];
$htmlType = $fieldMetaData['html_type'];
$isSerialized = CRM_Core_BAO_CustomField::isSerialized($fieldMetaData);
if ($dataType === 'Date') {
$params = ['date_field' => $value];
if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'date_field')) {
return NULL;
}
return $fieldMetaData['label'];
}
elseif ($dataType === 'Boolean') {
if (CRM_Utils_String::strtoboolstr($value) === FALSE) {
return $fieldMetaData['label'] . '::' . $fieldMetaData['groupTitle'];
}
}
// need not check for label filed import
$selectHtmlTypes = [
'CheckBox',
'Select',
'Radio',
];
if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) || $dataType == 'Boolean' || $dataType == 'ContactReference') {
$valid = CRM_Core_BAO_CustomValue::typecheck($dataType, $value);
if (!$valid) {
return $fieldMetaData['label'];
}
}

// check for values for custom fields for checkboxes and multiselect
if ($isSerialized && $dataType != 'ContactReference') {
$mulValues = array_filter(explode(',', str_replace('|', ',', trim($value))), 'strlen');
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
foreach ($mulValues as $v1) {

$flag = FALSE;
foreach ($customOption as $v2) {
if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) || (strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
$flag = TRUE;
}
}

if (!$flag) {
return $fieldMetaData['label'];
}
}
}
elseif ($htmlType == 'Select' || ($htmlType == 'Radio' && $dataType != 'Boolean')) {
$customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
$flag = FALSE;
foreach ($customOption as $v2) {
if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) || (strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
$flag = TRUE;
}
}
if (!$flag) {
return $fieldMetaData['label'];
}
}
}

return NULL;
}

/**
* Get the entity for the given field.
*
Expand Down Expand Up @@ -1915,80 +1840,6 @@ public static function runJob(\CRM_Queue_TaskContext $taskContext, int $userJobI
return TRUE;
}

/**
* Check if an error in custom data.
*
* @deprecated all of this is duplicated if getTransformedValue is used.
*
* @param array $params
* @param string $errorMessage
* A string containing all the error-fields.
*
* @param null $csType
*/
public function isErrorInCustomData($params, &$errorMessage, $csType = NULL) {
$dateType = CRM_Core_Session::singleton()->get("dateTypes");
$errors = [];

if (!empty($params['contact_sub_type'])) {
$csType = $params['contact_sub_type'] ?? NULL;
}

if (empty($params['contact_type'])) {
$params['contact_type'] = 'Individual';
}

// get array of subtypes - CRM-18708
if (in_array($csType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) {
$csType = $this->getSubtypes($params['contact_type']);
}

if (is_array($csType)) {
// fetch custom fields for every subtype and add it to $customFields array
// CRM-18708
$customFields = [];
foreach ($csType as $cType) {
$customFields += CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $cType);
}
}
else {
$customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType);
}

foreach ($params as $key => $value) {
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
//For address custom fields, we do get actual custom field value as an inner array of
//values so need to modify
if (!array_key_exists($customFieldID, $customFields)) {
return ts('field ID');
}
/* check if it's a valid custom field id */
$errors[] = $this->validateCustomField($customFieldID, $value, $customFields[$customFieldID], $dateType);
}
}
if ($errors) {
$errorMessage .= ($errorMessage ? '; ' : '') . implode('; ', array_filter($errors));
}
}

/**
* get subtypes given the contact type
*
* @param string $contactType
* @return array $subTypes
*/
protected function getSubtypes($contactType) {
$subTypes = [];
$types = CRM_Contact_BAO_ContactType::subTypeInfo($contactType);

if (count($types) > 0) {
foreach ($types as $type) {
$subTypes[] = $type['name'];
}
}
return $subTypes;
}

/**
* Update the status of the import row to reflect the processing outcome.
*
Expand Down
18 changes: 0 additions & 18 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,24 +973,6 @@ public function testImportPrimaryAddressUpdate(): void {
$this->callAPISuccessGetSingle('Contact', $contactValues);
}

/**
* Test the determination of whether a custom field is valid.
*/
public function testCustomFieldValidation(): void {
$errorMessage = '';
$customGroup = $this->customGroupCreate([
'extends' => 'Contact',
'title' => 'ABC',
]);
$customField = $this->customFieldOptionValueCreate($customGroup, 'fieldABC', ['html_type' => 'Select', 'serialize' => 1]);
$params = [
'custom_' . $customField['id'] => 'Label1|Label2',
];
$parser = new CRM_Contact_Import_Parser_Contact();
$parser->isErrorInCustomData($params, $errorMessage);
$this->assertEquals(NULL, $errorMessage);
}

/**
* Test the import validation.
*
Expand Down