From e077bdea96375518e3746680f6e8c4ded8d20ce2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 11 Jun 2022 10:27:34 +1200 Subject: [PATCH] dev/core#3511 Improve handling of invalid saved mapping fields --- CRM/Import/ImportProcessor.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/CRM/Import/ImportProcessor.php b/CRM/Import/ImportProcessor.php index fcdb58255b..bc72dd2c9e 100644 --- a/CRM/Import/ImportProcessor.php +++ b/CRM/Import/ImportProcessor.php @@ -439,13 +439,23 @@ class CRM_Import_ImportProcessor { 'mapping_id' => $this->getMappingID(), 'options' => ['limit' => 0], ])['values']; + $skipped = []; foreach ($fields as $index => $field) { + if (!$this->isValidField($field['name'])) { + // This scenario could occur if the name of a saved mapping field + // changed or became unavailable https://lab.civicrm.org/dev/core/-/issues/3511. + $skipped[] = $field['name']; + $fields[$index]['name'] = $field['name'] = 'do_not_import'; + } $fieldSpec = $this->getFieldMetadata($field['name']); $fields[$index]['label'] = $fieldSpec['title']; if (empty($field['location_type_id']) && !empty($fieldSpec['hasLocationType'])) { $fields[$index]['location_type_id'] = 'Primary'; } } + if (!empty($skipped)) { + CRM_Core_Session::setStatus(ts('Invalid saved mappings were skipped') . ':' . implode(', ', $skipped)); + } $this->mappingFields = $this->rekeyBySortedColumnNumbers($fields); } @@ -460,6 +470,19 @@ class CRM_Import_ImportProcessor { return $this->getMetadata()[$fieldName] ?? CRM_Contact_BAO_Contact::importableFields('All')[$fieldName]; } + /** + * Is the field valid for this import. + * + * If not defined in metadata is is not valid. + * + * @param string $fieldName + * + * @return bool + */ + public function isValidField(string $fieldName): bool { + return isset($this->getMetadata()[$fieldName]) || isset(CRM_Contact_BAO_Contact::importableFields('All')[$fieldName]); + } + /** * Load the mapping from the database into the pre-5.50 format. * -- 2.25.1