[REF] extract and share code to determine if required contact fields are present
authoreileen <emcnaughton@wikimedia.org>
Fri, 1 Jan 2021 22:21:10 +0000 (11:21 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 1 Jan 2021 22:23:39 +0000 (11:23 +1300)
CRM/Activity/Import/Form/MapField.php
CRM/Event/Import/Form/MapField.php
CRM/Import/Form/MapField.php

index a0688d9cffcc5d22456c4cf2894d53c86f625c9e..3d6e74f581068873fe99dc8b25de3bbbae6f5146 100644 (file)
@@ -262,30 +262,16 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField {
         '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 />';
             }
           }
index 50c3e1f11fd5e65cba91a63bf61fd0f06d7e9224..c7864e2aa5123b1aa97861082ec8ccafdb5abd1c 100644 (file)
@@ -277,7 +277,7 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
     $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) {
@@ -295,25 +295,12 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
         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;
@@ -322,9 +309,7 @@ class CRM_Event_Import_Form_MapField extends CRM_Import_Form_MapField {
               $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)) {
index 702d114555eaf4d1a5060901cc5ad76cb142ce5d..31212c0deeaee668dac2689f43d2ced3db5de316 100644 (file)
@@ -170,4 +170,36 @@ abstract class CRM_Import_Form_MapField extends CRM_Core_Form {
     $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 '';
+  }
+
 }