[REF] simple extraction of function to check required fields are present
authoreileen <emcnaughton@wikimedia.org>
Sun, 4 Aug 2019 23:44:05 +0000 (11:44 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 4 Aug 2019 23:50:24 +0000 (11:50 +1200)
CRM/Contribute/Import/Form/MapField.php

index e0f58c436e7733305ec56323f5ad6f7ad0147054..4b38a3c0cf1fa8cb46f108ce0fcb031e3fc29267 100644 (file)
  */
 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.
    */
@@ -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,
-              ]) . '<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) {