Move validate field function to the parent class
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 24 May 2022 07:55:21 +0000 (19:55 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 24 May 2022 07:55:54 +0000 (19:55 +1200)
I am happy with the signature now so time for it to move to it's forever home

(Actually that's a bit of a euphemism - I'm kinda sending it to the farm
as I think it will get whittled away to nothing but that a conversation for another day

CRM/Contact/Import/Parser/Contact.php
CRM/Import/Parser.php

index 973c68b3c99a978e4034cb8aa8fa8c8d3a739356..c94880bc3965a7ab49aa68c47f1c2446504d9d7a 100644 (file)
@@ -137,91 +137,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser {
     $this->_mapperKeys = $mapperKeys;
   }
 
-  /**
-   * @param $customFieldID
-   * @param $value
-   * @param array $fieldMetaData
-   * @param $dateType
-   *
-   * @return ?string
-   */
-  private function validateCustomField($customFieldID, $value, array $fieldMetaData, $dateType): ?string {
-    // validate null values for required custom fields of type boolean
-    if (!empty($fieldMetaData['is_required']) && (empty($value) && !is_numeric($value)) && $fieldMetaData['data_type'] == 'Boolean') {
-      return $fieldMetaData['label'] . '::' . $fieldMetaData['groupTitle'];
-    }
-
-    /* validate the data against the CF type */
-
-    if ($value) {
-      $dataType = $fieldMetaData['data_type'];
-      $htmlType = $fieldMetaData['html_type'];
-      $isSerialized = CRM_Core_BAO_CustomField::isSerialized($fieldMetaData);
-      if ($dataType === 'Date') {
-        $params = ['date_field' => $value];
-        if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'date_field')) {
-          return NULL;
-        }
-        return $fieldMetaData['label'];
-      }
-      elseif ($dataType == 'Boolean') {
-        if (CRM_Utils_String::strtoboolstr($value) === FALSE) {
-          return $fieldMetaData['label'] . '::' . $fieldMetaData['groupTitle'];
-        }
-      }
-      // need not check for label filed import
-      $selectHtmlTypes = [
-        'CheckBox',
-        'Select',
-        'Radio',
-      ];
-      if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) || $dataType == 'Boolean' || $dataType == 'ContactReference') {
-        $valid = CRM_Core_BAO_CustomValue::typecheck($dataType, $value);
-        if (!$valid) {
-          return $fieldMetaData['label'];
-        }
-      }
-
-      // check for values for custom fields for checkboxes and multiselect
-      if ($isSerialized && $dataType != 'ContactReference') {
-        $value = trim($value);
-        $value = str_replace('|', ',', $value);
-        $mulValues = explode(',', $value);
-        $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
-        foreach ($mulValues as $v1) {
-          if (strlen($v1) == 0) {
-            continue;
-          }
-
-          $flag = FALSE;
-          foreach ($customOption as $v2) {
-            if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) || (strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
-              $flag = TRUE;
-            }
-          }
-
-          if (!$flag) {
-            return $fieldMetaData['label'];
-          }
-        }
-      }
-      elseif ($htmlType == 'Select' || ($htmlType == 'Radio' && $dataType != 'Boolean')) {
-        $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
-        $flag = FALSE;
-        foreach ($customOption as $v2) {
-          if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) || (strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
-            $flag = TRUE;
-          }
-        }
-        if (!$flag) {
-          return $fieldMetaData['label'];
-        }
-      }
-    }
-
-    return NULL;
-  }
-
   /**
    * The initializer code, called before processing.
    */
index 93e0a3cfafbcf71096227aa989c8fb0a5b675105..7242786a56d56f73e8560871d7d050b7e24b799f 100644 (file)
@@ -1246,4 +1246,89 @@ abstract class CRM_Import_Parser {
     return $fieldMetadata;
   }
 
+  /**
+   * @param $customFieldID
+   * @param $value
+   * @param array $fieldMetaData
+   * @param $dateType
+   *
+   * @return ?string
+   */
+  protected function validateCustomField($customFieldID, $value, array $fieldMetaData, $dateType): ?string {
+    // validate null values for required custom fields of type boolean
+    if (!empty($fieldMetaData['is_required']) && (empty($value) && !is_numeric($value)) && $fieldMetaData['data_type'] == 'Boolean') {
+      return $fieldMetaData['label'] . '::' . $fieldMetaData['groupTitle'];
+    }
+
+    /* validate the data against the CF type */
+
+    if ($value) {
+      $dataType = $fieldMetaData['data_type'];
+      $htmlType = $fieldMetaData['html_type'];
+      $isSerialized = CRM_Core_BAO_CustomField::isSerialized($fieldMetaData);
+      if ($dataType === 'Date') {
+        $params = ['date_field' => $value];
+        if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, 'date_field')) {
+          return NULL;
+        }
+        return $fieldMetaData['label'];
+      }
+      elseif ($dataType == 'Boolean') {
+        if (CRM_Utils_String::strtoboolstr($value) === FALSE) {
+          return $fieldMetaData['label'] . '::' . $fieldMetaData['groupTitle'];
+        }
+      }
+      // need not check for label filed import
+      $selectHtmlTypes = [
+        'CheckBox',
+        'Select',
+        'Radio',
+      ];
+      if ((!$isSerialized && !in_array($htmlType, $selectHtmlTypes)) || $dataType == 'Boolean' || $dataType == 'ContactReference') {
+        $valid = CRM_Core_BAO_CustomValue::typecheck($dataType, $value);
+        if (!$valid) {
+          return $fieldMetaData['label'];
+        }
+      }
+
+      // check for values for custom fields for checkboxes and multiselect
+      if ($isSerialized && $dataType != 'ContactReference') {
+        $value = trim($value);
+        $value = str_replace('|', ',', $value);
+        $mulValues = explode(',', $value);
+        $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
+        foreach ($mulValues as $v1) {
+          if (strlen($v1) == 0) {
+            continue;
+          }
+
+          $flag = FALSE;
+          foreach ($customOption as $v2) {
+            if ((strtolower(trim($v2['label'])) == strtolower(trim($v1))) || (strtolower(trim($v2['value'])) == strtolower(trim($v1)))) {
+              $flag = TRUE;
+            }
+          }
+
+          if (!$flag) {
+            return $fieldMetaData['label'];
+          }
+        }
+      }
+      elseif ($htmlType == 'Select' || ($htmlType == 'Radio' && $dataType != 'Boolean')) {
+        $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
+        $flag = FALSE;
+        foreach ($customOption as $v2) {
+          if ((strtolower(trim($v2['label'])) == strtolower(trim($value))) || (strtolower(trim($v2['value'])) == strtolower(trim($value)))) {
+            $flag = TRUE;
+          }
+        }
+        if (!$flag) {
+          return $fieldMetaData['label'];
+        }
+      }
+    }
+
+    return NULL;
+  }
+
 }