*/
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.
*/
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) {