Switch now-tested birth & deceased date handling to be metadata based
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 20 May 2022 09:27:08 +0000 (21:27 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Sat, 21 May 2022 00:07:36 +0000 (12:07 +1200)
CRM/Contact/BAO/Contact.php
CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php

index 3a2e5f7716ad8afa3e026a6759a9c245b6af4581..c363b4a88c5028c9720f53ed8c26bb567e3e20ac 100644 (file)
@@ -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);
     }
 
index 9ce95595a14a580048274293c61de0070a34624e..32cce1ed763501e2cfbe85c740fa4362aafbfa61 100644 (file)
@@ -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);
index f5045ff24582869270bab5e161572f56f6c9531e..1a6374e5e82e3deb8aa864cf4390a69ec44f0800 100644 (file)
@@ -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';
   }