Copy functions back from metadata trait to only caller
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 7 Dec 2023 03:16:46 +0000 (16:16 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 7 Dec 2023 03:17:39 +0000 (16:17 +1300)
Part of decommissioning the trait

CRM/Contact/Import/Parser/Contact.php

index 7004a436cd6dbea1252e03a64694598465b8c311..8fda4d76af0d98efba9f259d556b6c2993bf19be 100644 (file)
@@ -28,8 +28,6 @@ require_once 'api/v3/utils.php';
  */
 class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
 
-  use CRM_Contact_Import_MetadataTrait;
-
   private $externalIdentifiers = [];
 
   /**
@@ -309,7 +307,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
         !array_key_exists($customFieldID, $addressCustomFields)
       ) {
         // @todo - this can probably go....
-        if ($customFields[$customFieldID]['data_type'] == 'Boolean') {
+        if ($customFields[$customFieldID]['data_type'] === 'Boolean') {
           if (empty($val) && !is_numeric($val) && $this->isFillDuplicates()) {
             //retain earlier value when Import mode is `Fill`
             unset($params[$key]);
@@ -467,6 +465,26 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     }
   }
 
+  /**
+   * Get sorted available relationships.
+   *
+   * @return array
+   */
+  protected function getRelationships(): array {
+    $cacheKey = 'importable_contact_relationship_field_metadata' . $this->getContactType() . $this->getContactSubType();
+    if (Civi::cache('fields')->has($cacheKey)) {
+      return Civi::cache('fields')->get($cacheKey);
+    }
+    //Relationship importables
+    $relations = CRM_Contact_BAO_Relationship::getContactRelationshipType(
+      NULL, NULL, NULL, $this->getContactType(),
+      FALSE, 'label', TRUE, $this->getContactSubType()
+    );
+    asort($relations);
+    Civi::cache('fields')->set($cacheKey, $relations);
+    return $relations;
+  }
+
   /**
    * @param array $params
    *
@@ -748,7 +766,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
 
       if (is_numeric($locTypeId) &&
         !in_array($fieldName, $multiplFields) &&
-        substr($fieldName, 0, 7) != 'custom_'
+        substr($fieldName, 0, 7) !== 'custom_'
       ) {
         $index = $locTypeId;
 
@@ -1035,6 +1053,58 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     return TRUE;
   }
 
+  /**
+   * Get metadata for contact importable fields.
+   *
+   * @internal this function will be made private in the near future. It is
+   * currently used by a core form but should not be called directly & once fixed
+   * will be private.
+   *
+   * @return array
+   */
+  public function getContactImportMetadata(): array {
+    $cacheKey = 'importable_contact_field_metadata' . $this->getContactType() . $this->getContactSubType();
+    if (Civi::cache('fields')->has($cacheKey)) {
+      return Civi::cache('fields')->get($cacheKey);
+    }
+    $contactFields = CRM_Contact_BAO_Contact::importableFields($this->getContactType());
+    // exclude the address options disabled in the Address Settings
+    $fields = CRM_Core_BAO_Address::validateAddressOptions($contactFields);
+
+    //CRM-5125
+    //supporting import for contact subtypes
+    $csType = NULL;
+    if ($this->getContactSubType()) {
+      //custom fields for sub type
+      $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->getContactSubType());
+
+      if (!empty($subTypeFields)) {
+        foreach ($subTypeFields as $customSubTypeField => $details) {
+          $fields[$customSubTypeField] = $details;
+        }
+      }
+    }
+
+    foreach ($this->getRelationships() as $key => $var) {
+      [$type] = explode('_', $key);
+      $relationshipType[$key]['title'] = $var;
+      $relationshipType[$key]['headerPattern'] = '/' . preg_quote($var, '/') . '/';
+      $relationshipType[$key]['import'] = TRUE;
+      $relationshipType[$key]['relationship_type_id'] = $type;
+      $relationshipType[$key]['related'] = TRUE;
+    }
+
+    if (!empty($relationshipType)) {
+      $fields = array_merge($fields, [
+        'related' => [
+          'title' => ts('- related contact info -'),
+        ],
+      ], $relationshipType);
+    }
+    Civi::cache('fields')->set($cacheKey, $fields);
+    return $fields;
+  }
+
   /**
    * Format location block ready for importing.
    *