Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Relocate another deprecated utils function to the only class that calls it. #19248

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 113 additions & 1 deletion CRM/Contact/Import/Parser/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ public function createContact(&$formatted, &$contactFields, $onDuplicate, $conta
// setting required check to false, CRM-2839
// plus we do our own required check in import
try {
$error = _civicrm_api3_deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
$error = $this->deprecated_contact_check_params($formatted, $dupeCheck, $dedupeRuleGroupID);
if ($error) {
return $error;
}
Expand Down Expand Up @@ -2122,4 +2122,116 @@ public function deprecated_validate_formatted_contact(&$params): void {
}
}

/**
* @param array $params
* @param bool $dupeCheck
* @param null|int $dedupeRuleGroupID
*
* @throws \CRM_Core_Exception
*/
public function 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.');
}
}
}

}
112 changes: 0 additions & 112 deletions CRM/Utils/DeprecatedUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,118 +568,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
Expand Down
82 changes: 0 additions & 82 deletions tests/phpunit/CRM/Utils/DeprecatedUtilsTest.php

This file was deleted.