From c67a47bd50fe0853db782f14fffd6a082b7de312 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 12 Aug 2019 10:53:17 +1200 Subject: [PATCH] Extract metadata definition into a trait. This metadata needs to be shared between multiple import forms. They do not have a common parent. Curently this is done via calculating once & passing via quickform. However, the formats are convoluted & hard to read. Information that is user generated should be calculated per form - not so much metadata --- CRM/Contact/Import/MetadataTrait.php | 62 +++++++++++++++++++++++++++ CRM/Contact/Import/Parser/Contact.php | 47 ++------------------ 2 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 CRM/Contact/Import/MetadataTrait.php diff --git a/CRM/Contact/Import/MetadataTrait.php b/CRM/Contact/Import/MetadataTrait.php new file mode 100644 index 000000000000..bf5c5143b2f5 --- /dev/null +++ b/CRM/Contact/Import/MetadataTrait.php @@ -0,0 +1,62 @@ +_contactType); + // 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 (!empty($this->_contactSubType)) { + //custom fields for sub type + $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType); + + if (!empty($subTypeFields)) { + foreach ($subTypeFields as $customSubTypeField => $details) { + $fields[$customSubTypeField] = $details; + } + } + } + + //Relationship importables + $this->_relationships = $relations + = CRM_Contact_BAO_Relationship::getContactRelationshipType( + NULL, NULL, NULL, $this->_contactType, + FALSE, 'label', TRUE, $this->_contactSubType + ); + asort($relations); + + foreach ($relations as $key => $var) { + list($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); + } + return $fields; + } + +} diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index a93dfc1ae400..cb19b6515ad8 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -37,6 +37,9 @@ * class to parse contact csv files */ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { + + use CRM_Contact_Import_MetadataTrait; + protected $_mapperKeys = []; protected $_mapperLocType = []; protected $_mapperPhoneType; @@ -2054,49 +2057,7 @@ public static function getParameterForParser($count) { * Set field metadata. */ protected function setFieldMetadata() { - $contactFields = CRM_Contact_BAO_Contact::importableFields($this->_contactType); - // 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 (!empty($this->_contactSubType)) { - //custom fields for sub type - $subTypeFields = CRM_Core_BAO_CustomField::getFieldsForImport($this->_contactSubType); - - if (!empty($subTypeFields)) { - foreach ($subTypeFields as $customSubTypeField => $details) { - $fields[$customSubTypeField] = $details; - } - } - } - - //Relationship importables - $this->_relationships = $relations - = CRM_Contact_BAO_Relationship::getContactRelationshipType( - NULL, NULL, NULL, $this->_contactType, - FALSE, 'label', TRUE, $this->_contactSubType - ); - asort($relations); - - foreach ($relations as $key => $var) { - list($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); - } - $this->setImportableFieldsMetadata($fields); + $this->setImportableFieldsMetadata($this->getContactImportMetadata()); } }