From 64cafaa36b0f246ce5d5d8b331b0af383f72b9c7 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 7 Aug 2019 17:39:35 +1200 Subject: [PATCH] [REF] [Import] extract function that sets field metadata My thinking is that we want to migrate over to a new class (similar to export processor) that does a lot a metadata functions - perhaps it might be importMetadata. A big part of the reason it's all so crazy appears to be that the functions to wrangle the metadata are on the first class in the sequence (DataSource) and because they are not shared & do not have a shared parent they are passed around instead via the form set & get methods. In fact the metadata & array wrangling functions just need to be available - hmm sounding like a trait - ok next commit.... --- CRM/Contact/Import/Parser/Contact.php | 95 ++++++++++++++------------- CRM/Import/Parser.php | 27 ++++++++ 2 files changed, 78 insertions(+), 44 deletions(-) diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index f8b543c6c5..a93dfc1ae4 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -146,50 +146,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { * The initializer code, called before processing. */ public function init() { - $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); - } - - foreach ($fields as $name => $field) { + $this->setFieldMetadata(); + foreach ($this->getImportableFieldsMetadata() as $name => $field) { $this->addField($name, $field['title'], CRM_Utils_Array::value('type', $field), CRM_Utils_Array::value('headerPattern', $field), CRM_Utils_Array::value('dataPattern', $field), CRM_Utils_Array::value('hasLocationType', $field)); } @@ -2092,4 +2050,53 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Contact_Import_Parser { } + /** + * 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); + } + } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 6fab5498b9..3c71a3ca33 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -137,6 +137,33 @@ abstract class CRM_Import_Parser { */ protected $_fields; + /** + * Metadata for all available fields, keyed by unique name. + * + * This is intended to supercede $_fields which uses a special sauce format which + * importableFieldsMetadata uses the standard getfields type format. + * + * @var array + */ + protected $importableFieldsMetadata = []; + + /** + * Get metadata for all importable fields in std getfields style format. + * + * @return array + */ + public function getImportableFieldsMetadata(): array { + return $this->importableFieldsMetadata; + } + + /** + * Set metadata for all importable fields in std getfields style format. + * @param array $importableFieldsMetadata + */ + public function setImportableFieldsMetadata(array $importableFieldsMetadata) { + $this->importableFieldsMetadata = $importableFieldsMetadata; + } + /** * Array of the fields that are actually part of the import process * the position in the array also dictates their position in the import -- 2.25.1