From af494ae17c489607f9796c7ef8069ae5dffd6994 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 24 Aug 2022 17:50:35 +1200 Subject: [PATCH] [REF] Extract function to determine defaults in Contribution.MapField --- CRM/Contribute/Import/Form/MapField.php | 71 ++++++++++++------------- CRM/Import/Form/MapField.php | 19 +++++++ 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/CRM/Contribute/Import/Form/MapField.php b/CRM/Contribute/Import/Form/MapField.php index 5c57063f71..de58e3347c 100644 --- a/CRM/Contribute/Import/Form/MapField.php +++ b/CRM/Contribute/Import/Form/MapField.php @@ -134,18 +134,10 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { //-------- end of saved mapping stuff --------- - $defaults = []; - $mapperKeys = array_keys($this->_mapperFields); - $hasHeaders = $this->getSubmittedValue('skipColumnHeader'); - $headerPatterns = $this->getHeaderPatterns(); $mapperKeysValues = $this->getSubmittedValue('mapper'); $columnHeaders = $this->getColumnHeaders(); $fieldMappings = $this->getFieldMappings(); - /* Initialize all field usages to false */ - foreach ($mapperKeys as $key) { - $this->_fieldUsed[$key] = FALSE; - } $sel1 = $this->_mapperFields; if (!$this->isUpdateExisting()) { @@ -185,17 +177,8 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { $js .= "{$formName}['mapper[$i][2]'].style.display = 'none';\n"; $js .= "{$formName}['mapper[$i][3]'].style.display = 'none';\n"; - $defaults["mapper[$i]"] = [ - $fieldMapping['name'], - $softField, - // Since the soft credit type id is not stored we can't load it here. - '', - ]; $jsSet = TRUE; } - else { - $defaults["mapper[$i]"] = []; - } if (!$jsSet) { for ($k = 1; $k < 4; $k++) { $js .= "{$formName}['mapper[$i][$k]'].style.display = 'none';\n"; @@ -205,30 +188,11 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { else { // this load section to help mapping if we ran out of saved columns when doing Load Mapping $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; - - if ($hasHeaders) { - $defaults["mapper[$i]"] = [$this->defaultFromHeader($columnHeader, $headerPatterns)]; - } } //end of load mapping } else { $js .= "swapOptions($formName, 'mapper[$i]', 0, 3, 'hs_mapper_0_');\n"; - if ($hasHeaders) { - // do array search first to see if has mapped key - $columnKey = array_search($columnHeader, $this->_mapperFields); - if (isset($this->_fieldUsed[$columnKey])) { - $defaults["mapper[$i]"] = $columnKey; - $this->_fieldUsed[$key] = TRUE; - } - else { - // Infer the default from the column names if we have them - $defaults["mapper[$i]"] = [ - $this->defaultFromHeader($columnHeader, $headerPatterns), - 0, - ]; - } - } if (!empty($mapperKeysValues) && ($mapperKeysValues[$i][0] ?? NULL) === 'soft_credit') { $softCreditField = $mapperKeysValues[$i][1]; $softCreditTypeID = $mapperKeysValues[$i][2]; @@ -240,7 +204,7 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { } $js .= "\n"; $this->assign('initHideBoxes', $js); - $this->setDefaults($defaults); + $this->setDefaults($this->getDefaults()); $this->addFormButtons(); } @@ -345,4 +309,37 @@ class CRM_Contribute_Import_Form_MapField extends CRM_Import_Form_MapField { return $this->parser; } + /** + * Get default values for the mapping. + * + * This looks up any saved mapping or derives them from the headers if possible. + * + * @return array + * + * @throws \CRM_Core_Exception + */ + protected function getDefaults(): array { + $defaults = []; + $fieldMappings = $this->getFieldMappings(); + foreach ($this->getColumnHeaders() as $i => $columnHeader) { + $defaults["mapper[$i]"] = []; + if ($this->getSubmittedValue('savedMapping')) { + $fieldMapping = $fieldMappings[$i] ?? NULL; + if ($fieldMapping) { + if ($fieldMapping['name'] !== ts('do_not_import')) { + // $mapping contact_type is not really a contact type - the data has been mangled + // into that field - see https://lab.civicrm.org/dev/core/-/issues/654 + // Since the soft credit type id is not stored we can't load it here. + $defaults["mapper[$i]"] = [$fieldMapping['name'], $fieldMapping['contact_type'] ?? '', '']; + } + } + } + elseif ($this->getSubmittedValue('skipColumnHeader')) { + $defaults["mapper[$i]"][0] = $this->guessMappingBasedOnColumns($columnHeader); + } + } + + return $defaults; + } + } diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 71d606647a..b938e7d080 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -501,4 +501,23 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { return array_values($categories); } + /** + * Get the 'best' mapping default from the column headers. + * + * @param string $columnHeader + * + * @return string + */ + 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); + if ($columnKey && empty($this->_fieldUsed[$columnKey])) { + $this->_fieldUsed[$columnKey] = TRUE; + return $columnKey; + } + // Infer the default from the column names if we have them + return $this->defaultFromHeader($columnHeader, $headerPatterns); + } + } -- 2.25.1