From be3c70347605c2bae82493031184bef1876f7bfe Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 5 Aug 2019 11:44:05 +1200 Subject: [PATCH] [REF] simple extraction of function to check required fields are present --- CRM/Contribute/Import/Form/MapField.php | 85 ++++++++++++++----------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index e0f58c436e..4b38a3c0cf 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -36,6 +36,54 @@ */ 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]) . '
'; + } + 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.') . '
'; + } + } + else { + $errors['_qf_default'] .= ts('Missing required field: %1', [1 => $title]) . '
'; + } + } + } + return $errors; + } + /** * Set variables up before form is built. */ @@ -365,42 +413,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { 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, - ]) . '
'; - } - 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.') . '
'; - } - } - else { - $errors['_qf_default'] .= ts('Missing required field: %1', [ - 1 => $title, - ]) . '
'; - } - } - } + $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) { -- 2.25.1