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

[REF] extract and share code to determine if required contact fields are present #19302

Merged
merged 1 commit into from
Jan 5, 2021
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
20 changes: 3 additions & 17 deletions CRM/Activity/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,30 +262,16 @@ public static function formRule($fields) {
'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
. '<br />';
}
}
Expand Down
23 changes: 4 additions & 19 deletions CRM/Event/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public static function formRule($fields, $files, $self) {
$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) {
Expand All @@ -295,25 +295,12 @@ public static function formRule($fields, $files, $self) {
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;
Expand All @@ -322,9 +309,7 @@ public static function formRule($fields, $files, $self) {
$errors['_qf_default'] .= ts('Missing required field: Provide Participant ID') . '<br />';
}
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.') . '<br />';
$errors['_qf_default'] .= ts('Missing required contact matching fields.') . " $contactFieldsBelowWeightMessage " . ' ' . ts('Or Provide Contact ID or External ID.') . '<br />';
}
}
elseif (!in_array('event_title', $importKeys)) {
Expand Down
32 changes: 32 additions & 0 deletions CRM/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,36 @@ protected function buildSavedMappingFields($savedMappingID) {
$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 '';
}

}