Skip to content

Commit

Permalink
[REF] simple extraction of function to check required fields are present
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Aug 4, 2019
1 parent 6c71cee commit fbb96b1
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions CRM/Contribute/Import/Form/MapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,56 @@
*/
class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField {

/**
* Check if required fields are present.
*
* @param CRM_Contribute_Import_Form_MapField $self
* @param string $contactORContributionId
* @param array $importKeys
* @param array $errors
* @param int $weightSum
* @param int $threshold
* @param string $fieldMessage
*
* @return array
*/
protected static function checkRequiredFields($self, string $contactORContributionId, array $importKeys, array $errors, int $weightSum, $threshold, string $fieldMessage): array {
// FIXME: should use the schema titles, not redeclare them
$requiredFields = [
$contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'),
'total_amount' => ts('Total Amount'),
'financial_type' => ts('Financial Type'),
];

foreach ($requiredFields as $field => $title) {
if (!in_array($field, $importKeys)) {
if (empty($errors['_qf_default'])) {
$errors['_qf_default'] = '';
}
if ($field == $contactORContributionId) {
if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
$self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
) {
$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]) . '<br />';
}
elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
!(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) ||
in_array('contribution_id', $importKeys)
)
) {
$errors['_qf_default'] .= ts('Invoice ID or Transaction ID or Contribution ID are required to match to the existing contribution records in Update mode.') . '<br />';
}
}
else {
$errors['_qf_default'] .= ts('Missing required field: %1', [
1 => $title,
]) . '<br />';
}
}
}
return $errors;
}

/**
* Set variables up before form is built.
*/
Expand Down Expand Up @@ -365,42 +415,7 @@ public static function formRule($fields, $files, $self) {
foreach ($ruleFields as $field => $weight) {
$fieldMessage .= ' ' . $field . '(weight ' . $weight . ')';
}

// FIXME: should use the schema titles, not redeclare them
$requiredFields = [
$contactORContributionId == 'contribution_id' ? 'contribution_id' : 'contribution_contact_id' => $contactORContributionId == 'contribution_id' ? ts('Contribution ID') : ts('Contact ID'),
'total_amount' => ts('Total Amount'),
'financial_type' => ts('Financial Type'),
];

foreach ($requiredFields as $field => $title) {
if (!in_array($field, $importKeys)) {
if (empty($errors['_qf_default'])) {
$errors['_qf_default'] = '';
}
if ($field == $contactORContributionId) {
if (!($weightSum >= $threshold || in_array('external_identifier', $importKeys)) &&
$self->_onDuplicate != CRM_Import_Parser::DUPLICATE_UPDATE
) {
$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,
]) . '<br />';
}
elseif ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE &&
!(in_array('invoice_id', $importKeys) || in_array('trxn_id', $importKeys) ||
in_array('contribution_id', $importKeys)
)
) {
$errors['_qf_default'] .= ts('Invoice ID or Transaction ID or Contribution ID are required to match to the existing contribution records in Update mode.') . '<br />';
}
}
else {
$errors['_qf_default'] .= ts('Missing required field: %1', [
1 => $title,
]) . '<br />';
}
}
}
$errors = self::checkRequiredFields($self, $contactORContributionId, $importKeys, $errors, $weightSum, $threshold, $fieldMessage);

//at least one field should be mapped during update.
if ($self->_onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) {
Expand Down

0 comments on commit fbb96b1

Please sign in to comment.