Move engagement level validation into the validate section
[civicrm-core.git] / CRM / Activity / Import / Parser / Activity.php
index 96e2e7f9d85bfaf1fa88eb9ddbf4fd2bb189fb31..7699d2ad01af01f0e5767581bd6e37954acdfb8c 100644 (file)
@@ -132,10 +132,28 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
     $this->setActiveFieldValues($values);
 
     try {
+      // Check required fields if this is not an update.
+      if (!$this->getFieldValue($values, 'activity_id')) {
+        if (!$this->getFieldValue($values, 'activity_label')
+        && !$this->getFieldValue($values, 'activity_type_id')) {
+          throw new CRM_Core_Exception(ts('Missing required fields: Activity type label or Activity type ID'));
+        }
+        if (!$this->getFieldValue($values, 'activity_date_time')) {
+          throw new CRM_Core_Exception(ts('Missing required fields'));
+        }
+      }
+
       $this->validateActivityTypeIDAndLabel($values);
-      if (!$this->getFieldValue($values, 'activity_date_time')) {
-        throw new CRM_Core_Exception(ts('Missing required fields'));
+      if ($this->getFieldValue($values, 'activity_date_time')
+      && !$this->isValidDate($this->getFieldValue($values, 'activity_date_time'))) {
+        throw new CRM_Core_Exception(ts('Invalid Activity Date'));
+      }
+
+      if ($this->getFieldValue($values, 'activity_engagement_level')
+        && !CRM_Utils_Rule::positiveInteger($this->getFieldValue($values, 'activity_engagement_level'))) {
+        throw new CRM_Core_Exception(ts('Activity Engagement Index'));
       }
+
     }
     catch (CRM_Core_Exception $e) {
       return $this->addError($values, [$e->getMessage()]);
@@ -145,30 +163,6 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
 
     $errorMessage = NULL;
 
-    // For date-Formats
-    $session = CRM_Core_Session::singleton();
-    $dateType = $session->get('dateTypes');
-    if (!isset($params['source_contact_id'])) {
-      $params['source_contact_id'] = $session->get('userID');
-    }
-    foreach ($params as $key => $val) {
-      if ($key === 'activity_date_time') {
-        if ($val) {
-          $dateValue = CRM_Utils_Date::formatDate($val, $dateType);
-          if ($dateValue) {
-            $params[$key] = $dateValue;
-          }
-          else {
-            CRM_Contact_Import_Parser_Contact::addToErrorMsg('Activity date', $errorMessage);
-          }
-        }
-      }
-      elseif ($key == 'activity_engagement_level' && $val &&
-        !CRM_Utils_Rule::positiveInteger($val)
-      ) {
-        CRM_Contact_Import_Parser_Contact::addToErrorMsg('Activity Engagement Index', $errorMessage);
-      }
-    }
     // Date-Format part ends.
 
     // Checking error in custom data.
@@ -221,10 +215,7 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
 
     foreach ($params as $key => $val) {
       if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
-        if ($key === 'activity_date_time' && $val) {
-          $params[$key] = CRM_Utils_Date::formatDate($val, $dateType);
-        }
-        elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
+        if (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Date') {
           CRM_Contact_Import_Parser_Contact::formatCustomDate($params, $params, $dateType, $key);
         }
         elseif (!empty($customFields[$customFieldID]) && $customFields[$customFieldID]['data_type'] == 'Boolean') {
@@ -246,11 +237,6 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
       return CRM_Import_Parser::ERROR;
     }
 
-    $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
-      NULL,
-      'Activity'
-    );
-
     if ($this->_contactIdIndex < 0) {
 
       // Retrieve contact id using contact dedupe rule.
@@ -361,36 +347,12 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
     $fields = CRM_Activity_DAO_Activity::fields();
     _civicrm_api3_store_values($fields, $params, $values);
 
-    require_once 'CRM/Core/OptionGroup.php';
-    $customFields = CRM_Core_BAO_CustomField::getFields('Activity');
-
     foreach ($params as $key => $value) {
       // ignore empty values or empty arrays etc
       if (CRM_Utils_System::isNull($value)) {
         continue;
       }
 
-      //Handling Custom Data
-      if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
-        $values[$key] = $value;
-        $type = $customFields[$customFieldID]['html_type'];
-        if (CRM_Core_BAO_CustomField::isSerialized($customFields[$customFieldID])) {
-          $values[$key] = CRM_Import_Parser::unserializeCustomValue($customFieldID, $value, $type);
-        }
-        elseif ($type == 'Select' || $type == 'Radio') {
-          $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE);
-          foreach ($customOption as $customFldID => $customValue) {
-            $val = $customValue['value'] ?? NULL;
-            $label = $customValue['label'] ?? NULL;
-            $label = strtolower($label);
-            $value = strtolower(trim($value));
-            if (($value == $label) || ($value == strtolower($val))) {
-              $values[$key] = $val;
-            }
-          }
-        }
-      }
-
       if ($key == 'target_contact_id') {
         if (!CRM_Utils_Rule::integer($value)) {
           return civicrm_api3_create_error("contact_id not valid: $value");
@@ -460,9 +422,17 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Activity_Import_Parser {
       && $activityLabel !== CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $activityTypeID)) {
       throw new CRM_Core_Exception(ts('Activity type label and Activity type ID are in conflict'));
     }
-    if (!$activityLabel && !$activityTypeID) {
-      throw new CRM_Core_Exception(ts('Missing required fields: Activity type label or Activity type ID'));
-    }
+  }
+
+  /**
+   * Is the supplied date field valid based on selected date format.
+   *
+   * @param string $value
+   *
+   * @return bool
+   */
+  protected function isValidDate(string $value): bool {
+    return (bool) CRM_Utils_Date::formatDate($value, CRM_Core_Session::singleton()->get('dateTypes'));
   }
 
 }