Override the DrupalBase getUserObject function with a Drupal8/9 compatible version
[civicrm-core.git] / CRM / Utils / DeprecatedUtils.php
index 1fe35cc8da1ed6fa345212f9e1670dbc1723cb9f..d7e4fcbce7bee36d5f1fc442ab424df94b185b62 100644 (file)
 
 require_once 'api/v3/utils.php';
 
-/**
- * Check duplicate contacts based on de-dupe parameters.
- *
- * @param array $params
- *
- * @return array
- */
-function _civicrm_api3_deprecated_check_contact_dedupe($params) {
-  static $cIndieFields = NULL;
-  static $defaultLocationId = NULL;
-
-  $contactType = $params['contact_type'];
-  if ($cIndieFields == NULL) {
-    require_once 'CRM/Contact/BAO/Contact.php';
-    $cTempIndieFields = CRM_Contact_BAO_Contact::importableFields($contactType);
-    $cIndieFields = $cTempIndieFields;
-
-    require_once "CRM/Core/BAO/LocationType.php";
-    $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
-
-    // set the value to default location id else set to 1
-    if (!$defaultLocationId = (int) $defaultLocation->id) {
-      $defaultLocationId = 1;
-    }
-  }
-
-  require_once 'CRM/Contact/BAO/Query.php';
-  $locationFields = CRM_Contact_BAO_Query::$_locationSpecificFields;
-
-  $contactFormatted = [];
-  foreach ($params as $key => $field) {
-    if ($field == NULL || $field === '') {
-      continue;
-    }
-    // CRM-17040, Considering only primary contact when importing contributions. So contribution inserts into primary contact
-    // instead of soft credit contact.
-    if (is_array($field) && $key != "soft_credit") {
-      foreach ($field as $value) {
-        $break = FALSE;
-        if (is_array($value)) {
-          foreach ($value as $name => $testForEmpty) {
-            if ($name !== 'phone_type' &&
-              ($testForEmpty === '' || $testForEmpty == NULL)
-            ) {
-              $break = TRUE;
-              break;
-            }
-          }
-        }
-        else {
-          $break = TRUE;
-        }
-        if (!$break) {
-          _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
-        }
-      }
-      continue;
-    }
-
-    $value = [$key => $field];
-
-    // check if location related field, then we need to add primary location type
-    if (in_array($key, $locationFields)) {
-      $value['location_type_id'] = $defaultLocationId;
-    }
-    elseif (array_key_exists($key, $cIndieFields)) {
-      $value['contact_type'] = $contactType;
-    }
-
-    _civicrm_api3_deprecated_add_formatted_param($value, $contactFormatted);
-  }
-
-  $contactFormatted['contact_type'] = $contactType;
-
-  return _civicrm_api3_deprecated_duplicate_formatted_contact($contactFormatted);
-}
-
 /**
  * This function adds the contact variable in $values to the
  * parameter list $params.  For most cases, $values should have length 1.  If
@@ -487,241 +410,3 @@ function _civicrm_api3_deprecated_duplicate_formatted_contact($params) {
   }
   return civicrm_api3_create_success(TRUE);
 }
-
-/**
- * @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
- *
- * @param bool $checkDuplicate
- *
- * @return array|bool
- *   <type>
- */
-function _civicrm_api3_deprecated_participant_check_params($params, $checkDuplicate = FALSE) {
-
-  // check if participant id is valid or not
-  if (!empty($params['id'])) {
-    $participant = new CRM_Event_BAO_Participant();
-    $participant->id = $params['id'];
-    if (!$participant->find(TRUE)) {
-      return civicrm_api3_create_error(ts('Participant  id is not valid'));
-    }
-  }
-  require_once 'CRM/Contact/BAO/Contact.php';
-  // check if contact id is valid or not
-  if (!empty($params['contact_id'])) {
-    $contact = new CRM_Contact_BAO_Contact();
-    $contact->id = $params['contact_id'];
-    if (!$contact->find(TRUE)) {
-      return civicrm_api3_create_error(ts('Contact id is not valid'));
-    }
-  }
-
-  // check that event id is not an template
-  if (!empty($params['event_id'])) {
-    $isTemplate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $params['event_id'], 'is_template');
-    if (!empty($isTemplate)) {
-      return civicrm_api3_create_error(ts('Event templates are not meant to be registered.'));
-    }
-  }
-
-  $result = [];
-  if ($checkDuplicate) {
-    if (CRM_Event_BAO_Participant::checkDuplicate($params, $result)) {
-      $participantID = array_pop($result);
-
-      $error = CRM_Core_Error::createError("Found matching participant record.",
-        CRM_Core_Error::DUPLICATE_PARTICIPANT,
-        'Fatal', $participantID
-      );
-
-      return civicrm_api3_create_error($error->pop(),
-        [
-          'contactID' => $params['contact_id'],
-          'participantID' => $participantID,
-        ]
-      );
-    }
-  }
-  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
- *
- * @return array
- *   <type> $params
- */
-function _civicrm_api3_deprecated_activity_buildmailparams($result, $activityTypeID) {
-  // get ready for collecting data about activity to be created
-  $params = [];
-
-  $params['activity_type_id'] = $activityTypeID;
-
-  $params['status_id'] = 'Completed';
-  if (!empty($result['from']['id'])) {
-    $params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
-  }
-  $params['target_contact_id'] = [];
-  $keys = ['to', 'cc', 'bcc'];
-  foreach ($keys as $key) {
-    if (is_array($result[$key])) {
-      foreach ($result[$key] as $key => $keyValue) {
-        if (!empty($keyValue['id'])) {
-          $params['target_contact_id'][] = $keyValue['id'];
-        }
-      }
-    }
-  }
-  $params['subject'] = $result['subject'];
-  $params['activity_date_time'] = $result['date'];
-  $params['details'] = $result['body'];
-
-  $numAttachments = Civi::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND;
-  for ($i = 1; $i <= $numAttachments; $i++) {
-    if (isset($result["attachFile_$i"])) {
-      $params["attachFile_$i"] = $result["attachFile_$i"];
-    }
-    else {
-      // No point looping 100 times if there's only one attachment
-      break;
-    }
-  }
-
-  return $params;
-}