From 12370fbf3d004f631e169503299b0ee1ac36dfdc Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 7 Dec 2023 16:16:46 +1300 Subject: [PATCH] Copy functions back from metadata trait to only caller Part of decommissioning the trait --- CRM/Contact/Import/Parser/Contact.php | 78 +++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 7004a436cd..8fda4d76af 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -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. * -- 2.25.1