From 2b2c59e8dd36048eb1f6ceafc8bb13b9c17dd3df Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 8 Jan 2024 16:29:10 +1300 Subject: [PATCH] dev/core#4781 Handle invalid deprecated fields with more grace --- CRM/Import/Form/MapField.php | 47 +++++++++++++++++++++++++++++ CRM/Member/Import/Form/MapField.php | 25 --------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/CRM/Import/Form/MapField.php b/CRM/Import/Form/MapField.php index 2c8785d424..2eda66a1ce 100644 --- a/CRM/Import/Form/MapField.php +++ b/CRM/Import/Form/MapField.php @@ -511,4 +511,51 @@ abstract class CRM_Import_Form_MapField extends CRM_Import_Forms { return $this->defaultFromHeader($columnHeader, $headerPatterns); } + /** + * Get default values for the mapping. + * + * @return array + * + * @throws \CRM_Core_Exception + */ + protected function getDefaults(): array { + $defaults = $mappingFailures = []; + $headerPatterns = $this->getHeaderPatterns(); + $fieldMappings = $this->getFieldMappings(); + foreach ($this->getColumnHeaders() as $i => $columnHeader) { + if ($this->getSubmittedValue('savedMapping')) { + $fieldMapping = $fieldMappings[$i] ?? NULL; + if (isset($fieldMappings[$i])) { + if (($fieldMapping['name'] === 'do_not_import')) { + $defaults["mapper[$i]"] = NULL; + } + elseif (array_key_exists($fieldMapping['name'], $this->getAvailableFields())) { + $defaults["mapper[$i]"] = $fieldMapping['name']; + } + else { + // The field from the saved mapping does not map to an available field. + // This could be because of an old, not-upgraded mapping or + // something we have failed to anticipate. + // In this case we should let the user know, but not + // set the default to the invalid field. + // See https://lab.civicrm.org/dev/core/-/issues/4781 + // Note that we have made attempts (e.g 5.51) to upgrade mappings and + // there is code to remove a mapping if a custom field is deleted + // (but perhaps not disabled or acl-restricted) but we should also + // handle it here rather than rely on our other efforts. + $mappingFailures[] = $columnHeader; + $defaults["mapper[$i]"] = NULL; + } + } + } + if (!isset($defaults["mapper[$i]"]) && $this->getSubmittedValue('skipColumnHeader')) { + $defaults["mapper[$i]"] = $this->defaultFromHeader($columnHeader, $headerPatterns); + } + } + if (!$this->isSubmitted() && $mappingFailures) { + CRM_Core_Session::setStatus(ts('Unable to load saved mapping. Please ensure all fields are correctly mapped')); + } + return $defaults; + } + } diff --git a/CRM/Member/Import/Form/MapField.php b/CRM/Member/Import/Form/MapField.php index 8d04b84bcf..ba9ff733c0 100644 --- a/CRM/Member/Import/Form/MapField.php +++ b/CRM/Member/Import/Form/MapField.php @@ -177,29 +177,4 @@ class CRM_Member_Import_Form_MapField extends CRM_Import_Form_MapField { return $highlightedFields; } - /** - * Get default values for the mapping. - * - * @return array - * - * @throws \CRM_Core_Exception - */ - protected function getDefaults(): array { - $defaults = []; - $headerPatterns = $this->getHeaderPatterns(); - $fieldMappings = $this->getFieldMappings(); - foreach ($this->getColumnHeaders() as $i => $columnHeader) { - if ($this->getSubmittedValue('savedMapping')) { - $fieldMapping = $fieldMappings[$i] ?? NULL; - if (isset($fieldMappings[$i])) { - $defaults["mapper[$i]"] = ($fieldMapping['name'] !== 'do_not_import') ? $fieldMapping['name'] : NULL; - } - } - if (!isset($defaults["mapper[$i]"]) && $this->getSubmittedValue('skipColumnHeader')) { - $defaults["mapper[$i]"] = $this->defaultFromHeader($columnHeader, $headerPatterns); - } - } - return $defaults; - } - } -- 2.25.1