[Import][Ref] Calculate relationship type in parser
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 9 May 2022 12:16:23 +0000 (00:16 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 10 May 2022 00:19:16 +0000 (12:19 +1200)
This is being calculated
here
https://github.com/civicrm/civicrm-core/blob/c169525f72b1f956d58cec1e4a15b0f57de3eabf/CRM/Contact/Import/ImportJob.php#L132-L136

And this change moves the calculation into the
parser class so that, along with the other arrays
it can be removed as an input parameter

CRM/Contact/Import/Parser/Contact.php

index 8e369a0e779d76b873b0f6357903706ef453576f..a32f53832306eeae1f262748c5493202f1f17223 100644 (file)
@@ -30,7 +30,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
 
   protected $_mapperKeys = [];
   protected $_mapperRelated;
-  protected $_mapperRelatedContactType;
   protected $_mapperRelatedContactDetails;
   protected $_relationships;
 
@@ -168,7 +167,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     parent::__construct();
     $this->_mapperKeys = $mapperKeys;
     $this->_mapperRelated = &$mapperRelated;
-    $this->_mapperRelatedContactType = &$mapperRelatedContactType;
     $this->_mapperRelatedContactDetails = &$mapperRelatedContactDetails;
   }
 
@@ -183,7 +181,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     $this->_newContacts = [];
 
     $this->setActiveFields($this->_mapperKeys);
-    $this->setActiveFieldRelatedContactType($this->_mapperRelatedContactType);
 
     $this->_phoneIndex = -1;
     $this->_emailIndex = -1;
@@ -2595,15 +2592,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     }
   }
 
-  /**
-   * @param $elements
-   */
-  public function setActiveFieldRelatedContactType($elements) {
-    for ($i = 0; $i < count($elements); $i++) {
-      $this->_activeFields[$i]->_relatedContactType = $elements[$i];
-    }
-  }
-
   /**
    * Format the field values for input to the api.
    *
@@ -2623,7 +2611,7 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
       }
       $relatedContactFieldName = $relatedContactKey ? $mappedField['name'] : NULL;
       // RelatedContactType is not part of the mapping but rather calculated from the relationship.
-      $relatedContactType = $this->_activeFields[$i]->_relatedContactType;
+      $relatedContactType = $this->getRelatedContactType($mappedField['relationship_type_id'], $mappedField['relationship_direction']);
       $relatedContactLocationTypeID = $relatedContactKey ? $mappedField['location_type_id'] : NULL;
       $relatedContactWebsiteTypeID = $relatedContactKey ? $mappedField['website_type_id'] : NULL;
       $relatedContactIMProviderID = $relatedContactKey ? $mappedField['im_provider_id'] : NULL;
@@ -3503,4 +3491,28 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     return $mappedFields;
   }
 
+  /**
+   * Get the related contact type.
+   *
+   * @param int|null $relationshipTypeID
+   * @param int|string $relationshipDirection
+   *
+   * @return null|string
+   *
+   * @throws \API_Exception
+   */
+  protected function getRelatedContactType($relationshipTypeID, $relationshipDirection): ?string {
+    if (!$relationshipTypeID) {
+      return NULL;
+    }
+    $cacheKey = $relationshipTypeID . $relationshipDirection;
+    if (!isset(Civi::$statics[__CLASS__][$cacheKey])) {
+      $relationshipField = 'contact_type_' . substr($relationshipDirection, -1);
+      Civi::$statics[__CLASS__][$cacheKey] = RelationshipType::get(FALSE)
+        ->addWhere('id', '=', $relationshipTypeID)
+        ->addSelect($relationshipField)->execute()->first()[$relationshipField];
+    }
+    return Civi::$statics[__CLASS__][$cacheKey];
+  }
+
 }