From b7d52f5eea596cdd83c4ebb7383de5f8fb06e560 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 20 May 2022 21:27:08 +1200 Subject: [PATCH] Switch now-tested birth & deceased date handling to be metadata based --- CRM/Contact/BAO/Contact.php | 6 ++-- CRM/Contact/Import/Parser/Contact.php | 48 +++------------------------ CRM/Import/Parser.php | 12 +++++++ 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 3a2e5f7716..c363b4a88c 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -147,9 +147,9 @@ class CRM_Contact_BAO_Contact extends CRM_Contact_DAO_Contact implements Civi\Co if ($contact->contact_type === 'Individual') { $allNull = FALSE; - // @todo allow the lines below to be overridden by input or hooks & add tests, - // as has been done for households and organizations. - // Format individual fields. + // @todo get rid of this - most of this formatting should + // be done by time we get here - maybe start with some + // deprecation notices. CRM_Contact_BAO_Individual::format($params, $contact); } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index 9ce95595a1..32cce1ed76 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -89,12 +89,15 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { * The end result is that all fields will be & this will go but for now it is * opt in. * - * @var array + * @var string[] */ protected $metadataHandledFields = [ - 'gender_id', 'contact_type', 'contact_sub_type', + 'gender_id', + 'birth_date', + 'deceased_date', + 'is_deceased', ]; /** @@ -931,17 +934,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { } } } - - if ($key == 'birth_date' && $val) { - CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); - } - elseif ($key == 'deceased_date' && $val) { - CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key); - $params['is_deceased'] = 1; - } - elseif ($key == 'is_deceased' && $val) { - $params[$key] = CRM_Utils_String::strtoboolstr($val); - } } //now format custom data. @@ -1209,38 +1201,8 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { $errors[] = $this->getFieldMetadata($key)['title']; } if ($value) { - $session = CRM_Core_Session::singleton(); - $dateType = $session->get("dateTypes"); switch ($key) { - case 'birth_date': - if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { - if (!CRM_Utils_Rule::date($params[$key])) { - $errors[] = ts('Birth Date'); - } - } - else { - $errors[] = ts('Birth-Date'); - } - break; - - case 'deceased_date': - if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { - if (!CRM_Utils_Rule::date($params[$key])) { - $errors[] = ts('Deceased Date'); - } - } - else { - $errors[] = ts('Deceased Date'); - } - break; - - case 'is_deceased': - if (CRM_Utils_String::strtoboolstr($value) === FALSE) { - $errors[] = ts('Deceased'); - } - break; - case 'preferred_communication_method': $preffComm = []; $preffComm = explode(',', $value); diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index f5045ff245..1a6374e5e8 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1201,6 +1201,18 @@ abstract class CRM_Import_Parser { if (empty($importedValue) || !in_array($fieldName, $this->metadataHandledFields, TRUE)) { return $importedValue; } + $fieldMetadata = $this->getFieldMetadata($fieldName); + if ($fieldMetadata['type'] === CRM_Utils_Type::T_BOOLEAN) { + $value = CRM_Utils_String::strtoboolstr($importedValue); + if ($value !== FALSE) { + return (bool) $value; + } + return 'invalid_import_value'; + } + if ($fieldMetadata['type'] === CRM_Utils_Type::T_DATE) { + $value = CRM_Utils_Date::formatDate($importedValue, $this->getSubmittedValue('dateFormats')); + return ($value) ?: 'invalid_import_value'; + } return $this->getFieldOptions($fieldName)[$importedValue] ?? 'invalid_import_value'; } -- 2.25.1