From 81882fd1147887f924e06f5f6cf8fe98073bc640 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 11 Jun 2022 11:57:49 +1200 Subject: [PATCH] dev/core#2706 fix activity import to update multi-select fields --- CRM/Activity/Import/Form/MapField.php | 77 +++++++++++++------------ CRM/Activity/Import/Parser/Activity.php | 5 +- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/CRM/Activity/Import/Form/MapField.php b/CRM/Activity/Import/Form/MapField.php index 4473033ed7..7034eb898b 100644 --- a/CRM/Activity/Import/Form/MapField.php +++ b/CRM/Activity/Import/Form/MapField.php @@ -25,6 +25,42 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { */ public $submitOnce = TRUE; + /** + * Validate that required fields are present. + * + * @param array $importKeys + * + * return string|null + */ + protected static function validateRequiredFields(array $importKeys): ?string { + $fieldMessage = NULL; + if (in_array('activity_id', $importKeys, TRUE)) { + return NULL; + } + $requiredFields = [ + 'target_contact_id' => ts('Contact ID'), + 'activity_date_time' => ts('Activity Date'), + 'activity_subject' => ts('Activity Subject'), + 'activity_type_id' => ts('Activity Type ID'), + ]; + + $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields('Individual', $importKeys); + foreach ($requiredFields as $field => $title) { + if (!in_array($field, $importKeys)) { + if ($field === 'target_contact_id') { + if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys)) { + continue; + } + $fieldMessage .= ts('Missing required contact matching fields.') + . $contactFieldsBelowWeightMessage + . '
'; + } + $fieldMessage .= ts('Missing required field: %1', [1 => $title]) . '
'; + } + } + return $fieldMessage; + } + /** * Build the form object. * @@ -121,49 +157,14 @@ class CRM_Activity_Import_Form_MapField extends CRM_Import_Form_MapField { // define so we avoid notices below $errors['_qf_default'] = ''; - $fieldMessage = NULL; if (!array_key_exists('savedMapping', $fields)) { $importKeys = []; foreach ($fields['mapper'] as $mapperPart) { $importKeys[] = $mapperPart[0]; } - // FIXME: should use the schema titles, not redeclare them - $requiredFields = [ - 'target_contact_id' => ts('Contact ID'), - 'activity_date_time' => ts('Activity Date'), - 'activity_subject' => ts('Activity Subject'), - 'activity_type_id' => ts('Activity Type ID'), - ]; - - $contactFieldsBelowWeightMessage = self::validateRequiredContactMatchFields('Individual', $importKeys); - foreach ($requiredFields as $field => $title) { - if (!in_array($field, $importKeys)) { - if ($field === 'target_contact_id') { - if (!$contactFieldsBelowWeightMessage || in_array('external_identifier', $importKeys)) { - continue; - } - else { - $errors['_qf_default'] .= ts('Missing required contact matching fields.') - . $contactFieldsBelowWeightMessage - . '
'; - } - } - elseif ($field === 'activity_type_id') { - if (in_array('activity_label', $importKeys)) { - continue; - } - else { - $errors['_qf_default'] .= ts('Missing required field: Provide %1 or %2', - [ - 1 => $title, - 2 => 'Activity Type Label', - ]) . '
'; - } - } - else { - $errors['_qf_default'] .= ts('Missing required field: %1', [1 => $title]) . '
'; - } - } + $missingFields = self::validateRequiredFields($importKeys); + if ($missingFields) { + $errors['_qf_default'] = $missingFields; } } diff --git a/CRM/Activity/Import/Parser/Activity.php b/CRM/Activity/Import/Parser/Activity.php index 502d678f9c..9a9892c58a 100644 --- a/CRM/Activity/Import/Parser/Activity.php +++ b/CRM/Activity/Import/Parser/Activity.php @@ -103,8 +103,9 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { $disp = $params['external_identifier']; } } - - throw new CRM_Core_Exception('No matching Contact found for (' . $disp . ')'); + if (empty($params['id'])) { + throw new CRM_Core_Exception('No matching Contact found for (' . $disp . ')'); + } } if (!empty($params['external_identifier'])) { $targetContactId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', -- 2.25.1