// 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;
}
}
}
+ /**
+ * @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.');
+ }
+ }
+ }
+
}
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
+++ /dev/null
-<?php
-
-require_once 'CRM/Utils/DeprecatedUtils.php';
-
-/**
- * Class CRM_Utils_DeprecatedUtilsTest
- * @group headless
- */
-class CRM_Utils_DeprecatedUtilsTest extends CiviUnitTestCase {
-
- public function setUp() {
- parent::setUp();
- }
-
- public function tearDown() {
- // truncate a few tables
- $tablesToTruncate = [
- 'civicrm_contact',
- 'civicrm_email',
- 'civicrm_contribution',
- 'civicrm_website',
- ];
-
- $this->quickCleanup($tablesToTruncate);
- }
-
- /**
- * Test civicrm_contact_check_params with no contact type.
- */
- public function testCheckParamsWithNoContactType(): void {
- $params = ['foo' => 'bar'];
- try {
- _civicrm_api3_deprecated_contact_check_params($params, FALSE);
- }
- catch (CRM_Core_Exception $e) {
- return;
- }
- $this->fail('An exception should have been thrown');
- }
-
- /**
- * Test civicrm_contact_check_params with a duplicate.
- *
- * @throws \CiviCRM_API3_Exception
- */
- public function testCheckParamsWithDuplicateContact2(): void {
- $this->individualCreate(['first_name' => 'Test', 'last_name' => 'Contact', 'email' => 'TestContact@example.com']);
-
- $params = [
- 'first_name' => 'Test',
- 'last_name' => 'Contact',
- 'email' => 'TestContact@example.com',
- 'contact_type' => 'Individual',
- ];
- try {
- $error = _civicrm_api3_deprecated_contact_check_params($params, TRUE);
- $this->assertEquals(1, $error['is_error']);
- }
- catch (CRM_Core_Exception $e) {
- $this->assertRegexp("/matching contacts.*1/s",
- $e->getMessage()
- );
- return;
- }
- // $this->fail('Exception was not optional');
- }
-
- /**
- * Test civicrm_contact_check_params with check for required params.
- */
- public function testCheckParamsWithNoParams(): void {
- $params = [];
- try {
- _civicrm_api3_deprecated_contact_check_params($params, FALSE);
- }
- catch (CRM_Core_Exception $e) {
- return;
- }
- $this->fail('Exception required');
- }
-
-}