From: eileen Date: Fri, 1 Jan 2021 22:21:10 +0000 (+1300) Subject: [REF] extract and share code to determine if required contact fields are present X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=02a237ce1d3bce1e1f60a86dabc7468c0da05da4;p=civicrm-core.git [REF] extract and share code to determine if required contact fields are present --- diff --git a/CRM/Activity/Import/Form/MapField.php b/CRM/Activity/Import/Form/MapField.php index a0688d9cff..3d6e74f581 100644 --- a/CRM/Activity/Import/Form/MapField.php +++ b/CRM/Activity/Import/Form/MapField.php @@ -262,30 +262,16 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { 'activity_type_id' => ts('Activity Type ID'), ]; - $params = [ - 'used' => 'Unsupervised', - 'contact_type' => 'Individual', - ]; - list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); - $weightSum = 0; - foreach ($importKeys as $key => $val) { - if (array_key_exists($val, $ruleFields)) { - $weightSum += $ruleFields[$val]; - } - } - foreach ($ruleFields as $field => $weight) { - $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; - } + $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields('Individual', $importKeys); foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'target_contact_id') { - if ($weightSum >= $threshold || in_array('external_identifier', $importKeys)) { + if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys)) { continue; } else { $errors['_qf_default'] .= ts('Missing required contact matching fields.') - . $fieldMessage . ' ' - . ts('(Sum of all weights should be greater than or equal to threshold: %1).', [1 => $threshold]) + . $contactFieldsBelowWeightMessage . '
'; } } diff --git a/CRM/Event/Import/Form/MapField.php b/CRM/Event/Import/Form/MapField.php index 50c3e1f11f..c7864e2aa5 100644 --- a/CRM/Event/Import/Form/MapField.php +++ b/CRM/Event/Import/Form/MapField.php @@ -277,7 +277,7 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField { $errors = []; // define so we avoid notices below $errors['_qf_default'] = ''; - $fieldMessage = NULL; + $contactFieldsBelowWeightMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = []; foreach ($fields['mapper'] as $mapperPart) { @@ -295,25 +295,12 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField { CRM_Import_Parser::CONTACT_HOUSEHOLD => 'Household', CRM_Import_Parser::CONTACT_ORGANIZATION => 'Organization', ); - $params = array( - 'used' => 'Unsupervised', - 'contact_type' => $contactTypes[$contactTypeId], - ); - list($ruleFields, $threshold) = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight($params); - $weightSum = 0; - foreach ($importKeys as $key => $val) { - if (array_key_exists($val, $ruleFields)) { - $weightSum += $ruleFields[$val]; - } - } - foreach ($ruleFields as $field => $weight) { - $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; - } + $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields($contactTypes[$contactTypeId], $importKeys); foreach ($requiredFields as $field => $title) { if (!in_array($field, $importKeys)) { if ($field == 'participant_contact_id') { - if ($weightSum >= $threshold || in_array('external_identifier', $importKeys) || + if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys) || in_array('participant_id', $importKeys) ) { continue; @@ -322,9 +309,7 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField { $errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '
'; } else { - $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $fieldMessage " . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array( - 1 => $threshold, - )) . ' ' . ts('Or Provide Contact ID or External ID.') . '
'; + $errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $contactFieldsBelowWeightMessage " . ' ' . ts('Or Provide Contact ID or External ID.') . '
'; } } elseif (!in_array('event_title', $importKeys)) { diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 702d114555..31212c0dee 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -170,4 +170,36 @@ abstract class CRM_Import_Form_MapField extends CRM_Core_Form { $this->addElement('checkbox', 'saveMapping', $saveDetailsName, NULL, ['onclick' => "showSaveDetails(this)"]); } + /** + * Validate that sufficient fields have been supplied to match to a contact. + * + * @param string $contactType + * @param array $importKeys + * + * @return string + * Message if insufficient fields are present. Empty string otherwise. + */ + protected static function validateRequiredContactMatchFields(string $contactType, array $importKeys): string { + [$ruleFields, $threshold] = CRM_Dedupe_BAO_RuleGroup::dedupeRuleFieldsWeight([ + 'used' => 'Unsupervised', + 'contact_type' => $contactType, + ]); + $weightSum = 0; + foreach ($importKeys as $key => $val) { + if (array_key_exists($val, $ruleFields)) { + $weightSum += $ruleFields[$val]; + } + } + $fieldMessage = ''; + foreach ($ruleFields as $field => $weight) { + $fieldMessage .= ' ' . $field . '(weight ' . $weight . ')'; + } + if ($weightSum < $threshold) { + return $fieldMessage . ' ' . ts('(Sum of all weights should be greater than or equal to threshold: %1).', array( + 1 => $threshold, + )); + } + return ''; + } + }