From 0bf0fdc701064fcd8be3ff55e0b2a82eda120ca3 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 8 Sep 2022 17:13:39 +1200 Subject: [PATCH] [REF] Cleanup code to getAvailableFields on Contribution MapField --- CRM/Contribute/Import/Form/MapField.php | 54 +++++++++++++++---------- CRM/Import/Form/MapField.php | 2 +- CRM/Import/Parser.php | 10 ++--- 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index 4222dba205..55a8197f8e 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -30,29 +30,16 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { //CRM-2219 removing other required fields since for updation only //invoice id or trxn id or contribution id is required. if ($this->isUpdateExisting()) { - $remove = [ - 'contribution_contact_id', - 'email', - 'first_name', - 'last_name', - 'external_identifier', - ]; - foreach ($remove as $value) { - unset($this->_mapperFields[$value]); - } - //modify field title only for update mode. CRM-3245 foreach ([ 'contribution_id', 'invoice_id', 'trxn_id', ] as $key) { - $this->_mapperFields[$key] .= ' (match to contribution record)'; $highlightedFields[] = $key; } } elseif ($this->isSkipExisting()) { - unset($this->_mapperFields['contribution_id']); $highlightedFieldsArray = [ 'contribution_contact_id', 'email', @@ -65,9 +52,6 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { } } - // modify field title for contribution status - $this->_mapperFields['contribution_status_id'] = ts('Contribution Status'); - $this->assign('highlightedFields', $highlightedFields); } @@ -96,12 +80,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { 'formRule', ], $this); - $sel1 = $this->_mapperFields; - - if (!$this->isUpdateExisting()) { - unset($sel1['id']); - unset($sel1['contribution_id']); - } + $sel1 = $this->getAvailableFields(); $softCreditFields['contact_id'] = ts('Contact ID'); $softCreditFields['external_identifier'] = ts('External ID'); @@ -129,6 +108,37 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { $this->addFormButtons(); } + /** + * Get the fields available for import selection. + * + * @return array + * e.g ['first_name' => 'First Name', 'last_name' => 'Last Name'.... + * + * @throws \API_Exception + */ + protected function getAvailableFields(): array { + $return = []; + foreach ($this->getFields() as $name => $field) { + if ($name === 'id' && $this->isSkipExisting()) { + // Duplicates are being skipped so id matching is not available. + continue; + } + if ($this->isUpdateExisting() && in_array($name, ['contribution_contact_id', 'email', 'first_name', 'last_name', 'external_identifier', 'email_primary.email'], TRUE)) { + continue; + } + if ($this->isUpdateExisting() && in_array($name, ['contribution_id', 'invoice_id', 'trxn_id'], TRUE)) { + $field['title'] .= (' ' . ts('(match to contribution record)')); + } + if (($field['entity'] ?? '') === 'Contact' && $this->isFilterContactFields() && empty($field['match_rule'])) { + // Filter out metadata that is intended for create & update - this is not available in the quick-form + // but is now loaded in the Parser for the LexIM variant. + continue; + } + $return[$name] = $field['html']['label'] ?? $field['title']; + } + return $return; + } + /** * Global validation rules for the form. * diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index b938e7d080..cac5ea42f0 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -511,7 +511,7 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { protected function guessMappingBasedOnColumns(string $columnHeader): string { $headerPatterns = $this->getHeaderPatterns(); // do array search first to see if has mapped key - $columnKey = array_search($columnHeader, $this->_mapperFields, TRUE); + $columnKey = array_search($columnHeader, $this->getAvailableFields(), TRUE); if ($columnKey && empty($this->_fieldUsed[$columnKey])) { $this->_fieldUsed[$columnKey] = TRUE; return $columnKey; diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index d55bc0c0e9..64a7558fca 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -324,7 +324,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { $contactFields[$fieldName]['match_rule'] = $this->getDefaultRuleForContactType($contactType); } - $contactFields['external_identifier']['title'] .= ts('(match to contact)'); + $contactFields['external_identifier']['title'] .= (' ' . ts('(match to contact)')); $contactFields['external_identifier']['match_rule'] = '*'; return $contactFields; } @@ -1421,7 +1421,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { * @throws \API_Exception */ private function getActionForEntity(string $entity): string { - return $this->getUserJob()['metadata']['entity_metadata'][$entity]['action'] ?? $this->getImportEntities()[$entity]['default_action']; + return $this->getUserJob()['metadata']['entity_metadata'][$entity]['action'] ?? ($this->getImportEntities()[$entity]['default_action'] ?? ''); } /** @@ -2391,7 +2391,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { // Set entity to contact as primary fields used in Contact actions $field['entity'] = 'Contact'; $field['name'] = 'address_primary.' . $fieldName; - $field['contact_type'] = ['Individual', 'Organization', 'Household']; + $field['contact_type'] = ['Individual' => 'Individual', 'Organization' => 'Organization', 'Household' => 'Household']; $prefixedFields[$prefix . 'address_primary.' . $fieldName] = $field; } @@ -2405,7 +2405,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { foreach ($phoneFields as $fieldName => $field) { $field['entity'] = 'Contact'; $field['name'] = 'phone_primary.' . $fieldName; - $field['contact_type'] = ['Individual', 'Organization', 'Household']; + $field['contact_type'] = ['Individual' => 'Individual', 'Organization' => 'Organization', 'Household' => 'Household']; $prefixedFields[$prefix . 'phone_primary.' . $fieldName] = $field; } @@ -2420,7 +2420,7 @@ abstract class CRM_Import_Parser implements UserJobInterface { foreach ($emailFields as $fieldName => $field) { $field['entity'] = 'Contact'; $field['name'] = 'email_primary.' . $fieldName; - $field['contact_type'] = ['Individual', 'Organization', 'Household']; + $field['contact_type'] = ['Individual' => 'Individual', 'Organization' => 'Organization', 'Household' => 'Household']; $prefixedFields[$prefix . 'email_primary.' . $fieldName] = $field; } return $prefixedFields; -- 2.25.1