[REF] Import - generate js on the processor class
authoreileen <emcnaughton@wikimedia.org>
Sun, 1 Sep 2019 22:10:54 +0000 (10:10 +1200)
committereileen <emcnaughton@wikimedia.org>
Sun, 1 Sep 2019 22:10:54 +0000 (10:10 +1200)
This moves the js calculation to the processor class for some of the function and simplifies it
so that the concatenation is done at the end.

Test cover is very solid on this

CRM/Contact/Import/Form/MapField.php
CRM/Import/ImportProcessor.php

index e78e2ea8f2306fa4b74170cdba0fc3187750b7d3..58423b71a8a34c3ee66ab6eafe6a897ba38855c7 100644 (file)
@@ -917,26 +917,17 @@ class CRM_Contact_Import_Form_MapField extends CRM_Import_Form_MapField {
           $mappingHeader = $processor->getFieldName($i);
           $websiteTypeId = $processor->getWebsiteTypeID($i);
           $locationId = $processor->getLocationTypeID($i);
-          $phoneType = $processor->getPhoneTypeID($i);
-          $imProvider = $processor->getIMProviderID($i);
           $typeId = $processor->getPhoneOrIMTypeID($i);
 
           if ($websiteTypeId) {
             $defaults["mapper[$i]"] = [$mappingHeader, $websiteTypeId];
           }
           else {
-            if (!$locationId) {
-              $js .= "{$formName}['mapper[$i][1]'].style.display = 'none';\n";
-            }
             //default for IM/phone without related contact
             $defaults["mapper[$i]"] = [$mappingHeader ?? '', $locationId, $typeId];
           }
 
-          if ((!$phoneType) && (!$imProvider)) {
-            $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n";
-          }
-
-          $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n";
+          $js .= $processor->getQuickFormJSForField($i);
 
           $jsSet = TRUE;
         }
index 534226a162ba780e43fccc5852d20e4f1134787b..535e641f8b2abf75aadbf8b95afa18531ae86610 100644 (file)
@@ -478,4 +478,30 @@ class CRM_Import_ImportProcessor {
     return !empty($this->getValidRelationships()[$key]) ? TRUE : FALSE;
   }
 
+  /**
+   * Get the relevant js for quickform.
+   *
+   * @param int $column
+   *
+   * @return string
+   * @throws \CiviCRM_API3_Exception
+   */
+  public function getQuickFormJSForField($column) {
+    $columnNumbersToHide = [];
+
+    if (!$this->getLocationTypeID($column) && !$this->getWebsiteTypeID($column)) {
+      $columnNumbersToHide[] = 1;
+    }
+    if (!$this->getPhoneOrIMTypeID($column)) {
+      $columnNumbersToHide[] = 2;
+    }
+    $columnNumbersToHide[] = 3;
+
+    $jsClauses = [];
+    foreach ($columnNumbersToHide as $columnNumber) {
+      $jsClauses[] = $this->getFormName() . "['mapper[$column][" . $columnNumber . "]'].style.display = 'none';";
+    }
+    return implode("\n", $jsClauses) . "\n";
+  }
+
 }