Merge pull request #19268 from seamuslee001/dev_core_2272
[civicrm-core.git] / CRM / Utils / DeprecatedUtils.php
index c9345e39adae1e7b19fd15eb4862b95d1d620667..386ba2054f8a044f3d958feaea09984944f687d6 100644 (file)
@@ -101,69 +101,6 @@ function _civicrm_api3_deprecated_check_contact_dedupe($params) {
   return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
 }
 
-/**
- * take the input parameter list as specified in the data model and
- * convert it into the same format that we use in QF and BAO object
- *
- * @param array $params
- *   Associative array of property name/value.
- *                             pairs to insert in new contact.
- * @param array $values
- *   The reformatted properties that we can use internally.
- *
- * @param array|bool $create Is the formatted Values array going to
- *                             be used for CRM_Activity_BAO_Activity::create()
- *
- * @return array|CRM_Error
- */
-function _civicrm_api3_deprecated_activity_formatted_param(&$params, &$values, $create = FALSE) {
-  // copy all the activity fields as is
-  $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");
-      }
-      $contactID = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value");
-      if (!$contactID) {
-        return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
-      }
-    }
-  }
-  return NULL;
-}
-
 /**
  * This function adds the contact variable in $values to the
  * parameter list $params.  For most cases, $values should have length 1.  If
@@ -551,80 +488,6 @@ function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
   return civicrm_api3_create_success(TRUE);
 }
 
-/**
- * Validate a formatted contact parameter list.
- *
- * @param array $params
- *   Structured parameter list (as in crm_format_params).
- *
- * @throw CRM_Core_Error
- */
-function _civicrm_api3_deprecated_validate_formatted_contact(&$params): void {
-  // Look for offending email addresses
-
-  if (array_key_exists('email', $params)) {
-    foreach ($params['email'] as $count => $values) {
-      if (!is_array($values)) {
-        continue;
-      }
-      if ($email = CRM_Utils_Array::value('email', $values)) {
-        // validate each email
-        if (!CRM_Utils_Rule::email($email)) {
-          throw new CRM_Core_Exception('No valid email address');
-        }
-
-        // check for loc type id.
-        if (empty($values['location_type_id'])) {
-          throw new CRM_Core_Exception('Location Type Id missing.');
-        }
-      }
-    }
-  }
-
-  // Validate custom data fields
-  if (array_key_exists('custom', $params) && is_array($params['custom'])) {
-    foreach ($params['custom'] as $key => $custom) {
-      if (is_array($custom)) {
-        foreach ($custom as $fieldId => $value) {
-          $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value),
-            CRM_Utils_Array::value('value', $value)
-          );
-          if (!$valid && $value['is_required']) {
-            throw new CRM_Core_Exception('Invalid value for custom field \'' .
-              $custom['name'] . '\''
-            );
-          }
-          if (CRM_Utils_Array::value('type', $custom) == 'Date') {
-            $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']);
-          }
-        }
-      }
-    }
-  }
-}
-
-/**
- * @deprecated - this is part of the import parser not the API & needs to be moved on out
- *
- * @param array $params
- * @param $onDuplicate
- *
- * @return array|bool
- *   <type>
- */
-function _civicrm_api3_deprecated_create_participant_formatted($params, $onDuplicate) {
-  require_once 'CRM/Event/Import/Parser.php';
-  if ($onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) {
-    CRM_Core_Error::reset();
-    $error = _civicrm_api3_deprecated_participant_check_params($params, TRUE);
-    if (civicrm_error($error)) {
-      return $error;
-    }
-  }
-  require_once "api/v3/Participant.php";
-  return civicrm_api3_participant_create($params);
-}
-
 /**
  *
  * @param array $params
@@ -683,118 +546,6 @@ function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplic
   return TRUE;
 }
 
-/**
- * @param array $params
- * @param bool $dupeCheck
- * @param null|int $dedupeRuleGroupID
- *
- * @throws \CRM_Core_Exception
- */
-function _civicrm_api3_deprecated_contact_check_params(
-  &$params,
-  $dupeCheck = TRUE,
-  $dedupeRuleGroupID = NULL) {
-
-  $requiredCheck = TRUE;
-
-  if (isset($params['id']) && is_numeric($params['id'])) {
-    $requiredCheck = FALSE;
-  }
-  if ($requiredCheck) {
-    if (isset($params['id'])) {
-      $required = ['Individual', 'Household', 'Organization'];
-    }
-    $required = [
-      'Individual' => [
-        ['first_name', 'last_name'],
-        'email',
-      ],
-      'Household' => [
-        'household_name',
-      ],
-      'Organization' => [
-        'organization_name',
-      ],
-    ];
-
-    // contact_type has a limited number of valid values
-    if (empty($params['contact_type'])) {
-      throw new CRM_Core_Exception("No Contact Type");
-    }
-    $fields = $required[$params['contact_type']] ?? NULL;
-    if ($fields == NULL) {
-      throw new CRM_Core_Exception("Invalid Contact Type: {$params['contact_type']}");
-    }
-
-    if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) {
-      if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) {
-        throw new CRM_Core_Exception("Invalid or Mismatched Contact Subtype: " . implode(', ', (array) $csType));
-      }
-    }
-
-    if (empty($params['contact_id']) && !empty($params['id'])) {
-      $valid = FALSE;
-      $error = '';
-      foreach ($fields as $field) {
-        if (is_array($field)) {
-          $valid = TRUE;
-          foreach ($field as $element) {
-            if (empty($params[$element])) {
-              $valid = FALSE;
-              $error .= $element;
-              break;
-            }
-          }
-        }
-        else {
-          if (!empty($params[$field])) {
-            $valid = TRUE;
-          }
-        }
-        if ($valid) {
-          break;
-        }
-      }
-
-      if (!$valid) {
-        throw new CRM_Core_Exception("Required fields not found for {$params['contact_type']} : $error");
-      }
-    }
-  }
-
-  if ($dupeCheck) {
-    // @todo switch to using api version
-    // $dupes = civicrm_api3('Contact', 'duplicatecheck', (array('match' => $params, 'dedupe_rule_id' => $dedupeRuleGroupID)));
-    // $ids = $dupes['count'] ? implode(',', array_keys($dupes['values'])) : NULL;
-    $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($params, $params['contact_type'], 'Unsupervised', [], CRM_Utils_Array::value('check_permissions', $params), $dedupeRuleGroupID);
-    if ($ids != NULL) {
-      $error = CRM_Core_Error::createError("Found matching contacts: " . implode(',', $ids),
-        CRM_Core_Error::DUPLICATE_CONTACT,
-        'Fatal', $ids
-      );
-      return civicrm_api3_create_error($error->pop());
-    }
-  }
-
-  // check for organisations with same name
-  if (!empty($params['current_employer'])) {
-    $organizationParams = ['organization_name' => $params['current_employer']];
-    $dupeIds = CRM_Contact_BAO_Contact::getDuplicateContacts($organizationParams, 'Organization', 'Supervised', [], FALSE);
-
-    // check for mismatch employer name and id
-    if (!empty($params['employer_id']) && !in_array($params['employer_id'], $dupeIds)
-    ) {
-      throw new CRM_Core_Exception('Employer name and Employer id Mismatch');
-    }
-
-    // show error if multiple organisation with same name exist
-    if (empty($params['employer_id']) && (count($dupeIds) > 1)
-    ) {
-      return civicrm_api3_create_error('Found more than one Organisation with same Name.');
-    }
-  }
-}
-
 /**
  * @param $result
  * @param int $activityTypeID