Merge pull request #23709 from eileenmcnaughton/buttons
[civicrm-core.git] / CRM / Import / ImportProcessor.php
index 12b38d6b5326111dea9b890365eac8c9d96c9ede..fcdb58255bd968fd38681ab870d55d0ce3350c84 100644 (file)
@@ -423,22 +423,7 @@ class CRM_Import_ImportProcessor {
    * @throws \CiviCRM_API3_Exception
    */
   public function getImporterObject() {
-    $importer = new CRM_Contact_Import_Parser_Contact(
-      $this->getFieldNames(),
-      $this->getFieldLocationTypes(),
-      $this->getFieldPhoneTypes(),
-      $this->getFieldIMProviderTypes(),
-      // @todo - figure out related mappings.
-      // $mapperRelated = [], $mapperRelatedContactType = [], $mapperRelatedContactDetails = [], $mapperRelatedContactLocType = [], $mapperRelatedContactPhoneType = [], $mapperRelatedContactImProvider = [],
-      [],
-      [],
-      [],
-      [],
-      [],
-      [],
-      $this->getFieldWebsiteTypes()
-      // $mapperRelatedContactWebsiteType = []
-    );
+    $importer = new CRM_Contact_Import_Parser_Contact($this->getFieldNames());
     $importer->setUserJobID($this->getUserJobID());
     $importer->init();
     return $importer;
@@ -455,7 +440,7 @@ class CRM_Import_ImportProcessor {
       'options' => ['limit' => 0],
     ])['values'];
     foreach ($fields as $index => $field) {
-      $fieldSpec = $this->getMetadata()[$fields[$index]['name']];
+      $fieldSpec = $this->getFieldMetadata($field['name']);
       $fields[$index]['label'] = $fieldSpec['title'];
       if (empty($field['location_type_id']) && !empty($fieldSpec['hasLocationType'])) {
         $fields[$index]['location_type_id'] = 'Primary';
@@ -464,6 +449,17 @@ class CRM_Import_ImportProcessor {
     $this->mappingFields = $this->rekeyBySortedColumnNumbers($fields);
   }
 
+  /**
+   * Get the metadata for the field.
+   *
+   * @param string $fieldName
+   *
+   * @return array
+   */
+  protected function getFieldMetadata(string $fieldName): array {
+    return $this->getMetadata()[$fieldName] ?? CRM_Contact_BAO_Contact::importableFields('All')[$fieldName];
+  }
+
   /**
    * Load the mapping from the database into the pre-5.50 format.
    *
@@ -550,47 +546,6 @@ class CRM_Import_ImportProcessor {
     return !empty($this->getValidRelationships()[$key]);
   }
 
-  /**
-   * Get the relevant js for quickform.
-   *
-   * @param int $column
-   *
-   * @return string
-   * @throws \CiviCRM_API3_Exception
-   */
-  public function getQuickFormJSForField($column) {
-    $columnNumbersToHide = [];
-    if ($this->getFieldName($column) === 'do_not_import') {
-      $columnNumbersToHide = [1, 2, 3];
-    }
-    elseif ($this->getRelationshipKey($column)) {
-      if (!$this->getWebsiteTypeID($column) && !$this->getLocationTypeID($column)) {
-        $columnNumbersToHide[] = 2;
-      }
-      if (!$this->getFieldName($column)) {
-        $columnNumbersToHide[] = 1;
-      }
-      if (!$this->getPhoneOrIMTypeID($column)) {
-        $columnNumbersToHide[] = 3;
-      }
-    }
-    else {
-      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 empty($jsClauses) ? '' : implode("\n", $jsClauses) . "\n";
-  }
-
   /**
    * Get the defaults for the column from the saved mapping.
    *
@@ -600,19 +555,33 @@ class CRM_Import_ImportProcessor {
    * @throws \CiviCRM_API3_Exception
    */
   public function getSavedQuickformDefaultsForColumn($column) {
+    $fieldMapping = [];
+
+    // $sel1 is either unmapped, a relationship or a target field.
     if ($this->getFieldName($column) === 'do_not_import') {
-      return [];
+      return $fieldMapping;
     }
+
     if ($this->getValidRelationshipKey($column)) {
-      if ($this->getWebsiteTypeID($column)) {
-        return [$this->getValidRelationshipKey($column), $this->getFieldName($column), $this->getWebsiteTypeID($column)];
-      }
-      return [$this->getValidRelationshipKey($column), $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
+      $fieldMapping[] = $this->getValidRelationshipKey($column);
     }
+
+    // $sel1
+    $fieldMapping[] = $this->getFieldName($column);
+
+    // $sel2
     if ($this->getWebsiteTypeID($column)) {
-      return [$this->getFieldName($column), $this->getWebsiteTypeID($column)];
+      $fieldMapping[] = $this->getWebsiteTypeID($column);
+    }
+    elseif ($this->getLocationTypeID($column)) {
+      $fieldMapping[] = $this->getLocationTypeID($column);
+    }
+
+    // $sel3
+    if ($this->getPhoneOrIMTypeID($column)) {
+      $fieldMapping[] = $this->getPhoneOrIMTypeID($column);
     }
-    return [(string) $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
+    return $fieldMapping;
   }
 
   /**