Merge pull request #23709 from eileenmcnaughton/buttons
[civicrm-core.git] / CRM / Import / ImportProcessor.php
index 52357e2899f3fdb69247b92c0c8461c11045525f..fcdb58255bd968fd38681ab870d55d0ce3350c84 100644 (file)
@@ -1,5 +1,8 @@
 <?php
 
+use Civi\Api4\Mapping;
+use Civi\Api4\MappingField;
+
 /**
  * Class CRM_Import_ImportProcessor.
  *
@@ -420,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;
@@ -447,6 +435,40 @@ class CRM_Import_ImportProcessor {
    * @throws \CiviCRM_API3_Exception
    */
   protected function loadSavedMapping() {
+    $fields = civicrm_api3('MappingField', 'get', [
+      'mapping_id' => $this->getMappingID(),
+      'options' => ['limit' => 0],
+    ])['values'];
+    foreach ($fields as $index => $field) {
+      $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';
+      }
+    }
+    $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.
+   *
+   * This is preserved as a copy the upgrade script can use - since the
+   * upgrade allows the other to be 'fixed'.
+   *
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function legacyLoadSavedMapping() {
     $fields = civicrm_api3('MappingField', 'get', [
       'mapping_id' => $this->getMappingID(),
       'options' => ['limit' => 0],
@@ -525,68 +547,70 @@ class CRM_Import_ImportProcessor {
   }
 
   /**
-   * Get the relevant js for quickform.
+   * Get the defaults for the column from the saved mapping.
    *
    * @param int $column
    *
-   * @return string
+   * @return array
    * @throws \CiviCRM_API3_Exception
    */
-  public function getQuickFormJSForField($column) {
-    $columnNumbersToHide = [];
+  public function getSavedQuickformDefaultsForColumn($column) {
+    $fieldMapping = [];
+
+    // $sel1 is either unmapped, a relationship or a target field.
     if ($this->getFieldName($column) === 'do_not_import') {
-      $columnNumbersToHide = [1, 2, 3];
+      return $fieldMapping;
     }
-    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;
-      }
+
+    if ($this->getValidRelationshipKey($column)) {
+      $fieldMapping[] = $this->getValidRelationshipKey($column);
     }
-    else {
-      if (!$this->getLocationTypeID($column) && !$this->getWebsiteTypeID($column)) {
-        $columnNumbersToHide[] = 1;
-      }
-      if (!$this->getPhoneOrIMTypeID($column)) {
-        $columnNumbersToHide[] = 2;
-      }
-      $columnNumbersToHide[] = 3;
+
+    // $sel1
+    $fieldMapping[] = $this->getFieldName($column);
+
+    // $sel2
+    if ($this->getWebsiteTypeID($column)) {
+      $fieldMapping[] = $this->getWebsiteTypeID($column);
+    }
+    elseif ($this->getLocationTypeID($column)) {
+      $fieldMapping[] = $this->getLocationTypeID($column);
     }
 
-    $jsClauses = [];
-    foreach ($columnNumbersToHide as $columnNumber) {
-      $jsClauses[] = $this->getFormName() . "['mapper[$column][" . $columnNumber . "]'].style.display = 'none';";
+    // $sel3
+    if ($this->getPhoneOrIMTypeID($column)) {
+      $fieldMapping[] = $this->getPhoneOrIMTypeID($column);
     }
-    return empty($jsClauses) ? '' : implode("\n", $jsClauses) . "\n";
+    return $fieldMapping;
   }
 
   /**
-   * Get the defaults for the column from the saved mapping.
-   *
-   * @param int $column
+   * This exists for use in the FiveFifty Upgrade
    *
-   * @return array
-   * @throws \CiviCRM_API3_Exception
+   * @throws \API_Exception|\CiviCRM_API3_Exception
    */
-  public function getSavedQuickformDefaultsForColumn($column) {
-    if ($this->getFieldName($column) === 'do_not_import') {
-      return [];
-    }
-    if ($this->getValidRelationshipKey($column)) {
-      if ($this->getWebsiteTypeID($column)) {
-        return [$this->getValidRelationshipKey($column), $this->getFieldName($column), $this->getWebsiteTypeID($column)];
+  public static function convertSavedFields(): void {
+    $mappings = Mapping::get(FALSE)
+      ->setSelect(['id', 'contact_type'])
+      ->addWhere('mapping_type_id:name', '=', 'Import Contact')
+      ->execute();
+
+    foreach ($mappings as $mapping) {
+      $processor = new CRM_Import_ImportProcessor();
+      $processor->setMappingID($mapping['id']);
+      $processor->setMetadata(CRM_Contact_BAO_Contact::importableFields('All'));
+      $processor->legacyLoadSavedMapping();;
+      foreach ($processor->getMappingFields() as $field) {
+        // The if is mostly precautionary against running this more than once
+        // - which is common in dev if not live...
+        if ($field['name']) {
+          MappingField::update(FALSE)
+            ->setValues(['name' => $field['name']])
+            ->addWhere('id', '=', $field['id'])
+            ->execute();
+        }
       }
-      return [$this->getValidRelationshipKey($column), $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
-    }
-    if ($this->getWebsiteTypeID($column)) {
-      return [$this->getFieldName($column), $this->getWebsiteTypeID($column)];
     }
-    return [(string) $this->getFieldName($column), $this->getLocationTypeID($column), $this->getPhoneOrIMTypeID($column)];
   }
 
 }