diff --git a/CRM/Activity/Import/Form/Preview.php b/CRM/Activity/Import/Form/Preview.php index 09e5bf2791c2..970ddeb0dd15 100644 --- a/CRM/Activity/Import/Form/Preview.php +++ b/CRM/Activity/Import/Form/Preview.php @@ -126,4 +126,13 @@ public function postProcess() { } } + /** + * @return CRM_Activity_Import_Parser_Activity + */ + protected function getParser(): CRM_Activity_Import_Parser_Activity { + $parser = new CRM_Activity_Import_Parser_Activity(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Contact/Import/Form/Preview.php b/CRM/Contact/Import/Form/Preview.php index 9f03057aee7e..6e34b5657aa4 100644 --- a/CRM/Contact/Import/Form/Preview.php +++ b/CRM/Contact/Import/Form/Preview.php @@ -241,26 +241,6 @@ public function postProcess() { $importJob->isComplete(); } - /** - * Get the mapped fields as an array of labels. - * - * e.g - * ['First Name', 'Employee Of - First Name', 'Home - Street Address'] - * - * @return array - * @throws \API_Exception - * @throws \CRM_Core_Exception - */ - protected function getMappedFieldLabels(): array { - $mapper = []; - $parser = new CRM_Contact_Import_Parser_Contact(); - $parser->setUserJobID($this->getUserJobID()); - foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) { - $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber)); - } - return $mapper; - } - /** * @return \CRM_Contact_Import_Parser_Contact */ diff --git a/CRM/Contribute/Import/Form/Preview.php b/CRM/Contribute/Import/Form/Preview.php index 97b645088189..67add1d6ab93 100644 --- a/CRM/Contribute/Import/Form/Preview.php +++ b/CRM/Contribute/Import/Form/Preview.php @@ -139,4 +139,13 @@ public function postProcess() { } } + /** + * @return \CRM_Contribute_Import_Parser_Contribution + */ + protected function getParser(): CRM_Contribute_Import_Parser_Contribution { + $parser = new CRM_Contribute_Import_Parser_Contribution(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Custom/Import/Form/Preview.php b/CRM/Custom/Import/Form/Preview.php index 6bee78ad9f2e..7b3fe5eaba0d 100644 --- a/CRM/Custom/Import/Form/Preview.php +++ b/CRM/Custom/Import/Form/Preview.php @@ -115,4 +115,13 @@ public function postProcess() { } } + /** + * @return CRM_Custom_Import_Parser_Api + */ + protected function getParser(): CRM_Custom_Import_Parser_Api { + $parser = new CRM_Custom_Import_Parser_Api(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Event/Import/Form/MapField.php b/CRM/Event/Import/Form/MapField.php index c4907fd2a6ee..409145e09455 100644 --- a/CRM/Event/Import/Form/MapField.php +++ b/CRM/Event/Import/Form/MapField.php @@ -430,4 +430,13 @@ public function postProcess() { $parser->set($this); } + /** + * @return CRM_Event_Import_Parser_Participant + */ + protected function getParser(): CRM_Event_Import_Parser_Participant { + $parser = new CRM_Event_Import_Parser_Participant(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Event/Import/Form/Preview.php b/CRM/Event/Import/Form/Preview.php index b5e0320d1ff5..db000ba2de49 100644 --- a/CRM/Event/Import/Form/Preview.php +++ b/CRM/Event/Import/Form/Preview.php @@ -128,4 +128,13 @@ public function postProcess() { } } + /** + * @return CRM_Event_Import_Parser_Participant + */ + protected function getParser(): CRM_Event_Import_Parser_Participant { + $parser = new CRM_Event_Import_Parser_Participant(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Import/Form/Preview.php b/CRM/Import/Form/Preview.php index 3ba704f33987..1c4b25dc354c 100644 --- a/CRM/Import/Form/Preview.php +++ b/CRM/Import/Form/Preview.php @@ -91,4 +91,23 @@ public function setStatusUrl() { $this->assign('statusUrl', $statusUrl); } + /** + * Get the mapped fields as an array of labels. + * + * e.g + * ['First Name', 'Employee Of - First Name', 'Home - Street Address'] + * + * @return array + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + protected function getMappedFieldLabels(): array { + $mapper = []; + $parser = $this->getParser(); + foreach ($this->getSubmittedValue('mapper') as $columnNumber => $mappedField) { + $mapper[$columnNumber] = $parser->getMappedFieldLabel($parser->getMappingFieldFromMapperInput($mappedField, 0, $columnNumber)); + } + return $mapper; + } + } diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index c8651ea4d23a..b0822ee31e57 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1370,4 +1370,27 @@ protected function validateCustomField($customFieldID, $value, array $fieldMetaD return NULL; } + /** + * Get the civicrm_mapping_field appropriate layout for the mapper input. + * + * For simple parsers (not contribution or contact) the input looks like + * ['first_name', 'custom_32'] + * and it is converted to + * + * ['name' => 'first_name', 'mapping_id' => 1, 'column_number' => 5], + * + * @param array $fieldMapping + * @param int $mappingID + * @param int $columnNumber + * + * @return array + */ + public function getMappingFieldFromMapperInput(array $fieldMapping, int $mappingID, int $columnNumber): array { + return [ + 'name' => $fieldMapping[0], + 'mapping_id' => $mappingID, + 'column_number' => $columnNumber, + ]; + } + } diff --git a/CRM/Member/Import/Form/MapField.php b/CRM/Member/Import/Form/MapField.php index b35a37eda07d..fcf541737e3a 100644 --- a/CRM/Member/Import/Form/MapField.php +++ b/CRM/Member/Import/Form/MapField.php @@ -459,4 +459,13 @@ public function postProcess() { $parser->set($this); } + /** + * @return \CRM_Member_Import_Parser_Membership + */ + protected function getParser(): CRM_Member_Import_Parser_Membership { + $parser = new CRM_Member_Import_Parser_Membership(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Member/Import/Form/Preview.php b/CRM/Member/Import/Form/Preview.php index e1226987d45e..cf3ccf31d915 100644 --- a/CRM/Member/Import/Form/Preview.php +++ b/CRM/Member/Import/Form/Preview.php @@ -148,4 +148,13 @@ public function postProcess() { } } + /** + * @return \CRM_Member_Import_Parser_Membership + */ + protected function getParser(): CRM_Member_Import_Parser_Membership { + $parser = new CRM_Member_Import_Parser_Membership(); + $parser->setUserJobID($this->getUserJobID()); + return $parser; + } + } diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index b484a964f8ee..a58f0b8c6ee9 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -424,15 +424,7 @@ public static function exportCSV($fileName, $header, $data) { * @return void */ public function init() { - $this->fieldMetadata = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE); - - foreach ($this->fieldMetadata as $name => $field) { - // @todo - we don't really need to do all this.... fieldMetadata is just fine to use as is. - $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); - $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); - $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); - $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); - } + $this->setFieldMetadata(); $this->_newMemberships = []; @@ -1045,4 +1037,23 @@ protected function isContactIDColumnPresent(): bool { return in_array('membership_contact_id', $this->_mapperKeys, TRUE); } + /** + * Set field metadata. + */ + protected function setFieldMetadata(): void { + if (empty($this->importableFieldsMetadata)) { + $metadata = CRM_Member_BAO_Membership::importableFields($this->_contactType, FALSE); + + foreach ($this->fieldMetadata as $name => $field) { + // @todo - we don't really need to do all this.... fieldMetadata is just fine to use as is. + $field['type'] = CRM_Utils_Array::value('type', $field, CRM_Utils_Type::T_INT); + $field['dataPattern'] = CRM_Utils_Array::value('dataPattern', $field, '//'); + $field['headerPattern'] = CRM_Utils_Array::value('headerPattern', $field, '//'); + $this->addField($name, $field['title'], $field['type'], $field['headerPattern'], $field['dataPattern']); + } + // We are consolidating on `importableFieldsMetadata` - but both still used. + $this->importableFieldsMetadata = $this->fieldMetadata = $metadata; + } + } + }