From 40a48df041b5490a3c17680d7d0d785bdbc189be Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 10 May 2022 00:16:23 +1200 Subject: [PATCH] [Import][Ref] Calculate relationship type in parser 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 | 38 ++++++++++++++++++--------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 8e369a0e77..a32f538323 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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]; + } + } -- 2.25.1