From e95f7d125e8f523d520e4e27293cc4cb18f99d39 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 2 Jun 2022 18:50:52 +1200 Subject: [PATCH] Move isErrorInCustomData to shared class --- CRM/Activity/Import/Parser/Activity.php | 2 +- CRM/Contact/Import/Parser/Contact.php | 74 ------------------ CRM/Contribute/Import/Parser/Contribution.php | 2 +- CRM/Custom/Import/Parser/Api.php | 2 +- CRM/Event/Import/Parser/Participant.php | 2 +- CRM/Import/Parser.php | 75 +++++++++++++++++++ CRM/Member/Import/Parser/Membership.php | 2 +- .../CRM/Contact/Import/Parser/ContactTest.php | 3 +- 8 files changed, 82 insertions(+), 80 deletions(-) diff --git a/CRM/Activity/Import/Parser/Activity.php b/CRM/Activity/Import/Parser/Activity.php index e626abd022..b09c38683e 100644 --- a/CRM/Activity/Import/Parser/Activity.php +++ b/CRM/Activity/Import/Parser/Activity.php @@ -358,7 +358,7 @@ class CRM_Activity_Import_Parser_Activity extends CRM_Import_Parser { $errorMessage = NULL; // Checking error in custom data. $params['contact_type'] = 'Activity'; - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); + $this->isErrorInCustomData($params, $errorMessage); if ($errorMessage) { throw new CRM_Core_Exception('Invalid value for field(s) : ' . $errorMessage); } diff --git a/CRM/Contact/Import/Parser/Contact.php b/CRM/Contact/Import/Parser/Contact.php index fcc2e8aefc..59d5957e0f 100644 --- a/CRM/Contact/Import/Parser/Contact.php +++ b/CRM/Contact/Import/Parser/Contact.php @@ -652,62 +652,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { return $this->_newRelatedContacts; } - /** - * Check if an error in custom data. - * - * @param array $params - * @param string $errorMessage - * A string containing all the error-fields. - * - * @param null $csType - */ - public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL) { - $dateType = CRM_Core_Session::singleton()->get("dateTypes"); - $errors = []; - - if (!empty($params['contact_sub_type'])) { - $csType = $params['contact_sub_type'] ?? NULL; - } - - if (empty($params['contact_type'])) { - $params['contact_type'] = 'Individual'; - } - - // get array of subtypes - CRM-18708 - if (in_array($csType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { - $csType = self::getSubtypes($params['contact_type']); - } - - if (is_array($csType)) { - // fetch custom fields for every subtype and add it to $customFields array - // CRM-18708 - $customFields = []; - foreach ($csType as $cType) { - $customFields += CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $cType); - } - } - else { - $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType); - } - - $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); - $parser = new CRM_Contact_Import_Parser_Contact(); - foreach ($params as $key => $value) { - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - //For address custom fields, we do get actual custom field value as an inner array of - //values so need to modify - if (!array_key_exists($customFieldID, $customFields)) { - return ts('field ID'); - } - /* check if it's a valid custom field id */ - $errors[] = $parser->validateCustomField($customFieldID, $value, $customFields[$customFieldID], $dateType); - } - } - if ($errors) { - $errorMessage .= ($errorMessage ? '; ' : '') . implode('; ', array_filter($errors)); - } - } - /** * Ckeck a value present or not in a array. * @@ -1333,24 +1277,6 @@ class CRM_Contact_Import_Parser_Contact extends CRM_Import_Parser { return ''; } - /** - * get subtypes given the contact type - * - * @param string $contactType - * @return array $subTypes - */ - public static function getSubtypes($contactType) { - $subTypes = []; - $types = CRM_Contact_BAO_ContactType::subTypeInfo($contactType); - - if (count($types) > 0) { - foreach ($types as $type) { - $subTypes[] = $type['name']; - } - } - return $subTypes; - } - /** * Get the possible contact matches. * diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index e2f831b64c..c818b09475 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -701,7 +701,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { $params['contact_type'] = 'Contribution'; //checking error in custom data - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); + $this->isErrorInCustomData($params, $errorMessage); if ($errorMessage) { $tempMsg = "Invalid value for field(s) : $errorMessage"; diff --git a/CRM/Custom/Import/Parser/Api.php b/CRM/Custom/Import/Parser/Api.php index 80485e7111..2532614ca0 100644 --- a/CRM/Custom/Import/Parser/Api.php +++ b/CRM/Custom/Import/Parser/Api.php @@ -135,7 +135,7 @@ class CRM_Custom_Import_Parser_Api extends CRM_Import_Parser { $errorMessage = NULL; $contactType = $this->_contactType ? $this->_contactType : 'Organization'; - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($this->_params + ['contact_type' => $contactType], $errorMessage, $this->_contactSubType, NULL); + $this->isErrorInCustomData($this->_params + ['contact_type' => $contactType], $errorMessage, $this->_contactSubType, NULL); // pseudoconstants if ($errorMessage) { diff --git a/CRM/Event/Import/Parser/Participant.php b/CRM/Event/Import/Parser/Participant.php index 5b767d27bf..391d6a49c9 100644 --- a/CRM/Event/Import/Parser/Participant.php +++ b/CRM/Event/Import/Parser/Participant.php @@ -248,7 +248,7 @@ class CRM_Event_Import_Parser_Participant extends CRM_Import_Parser { $params['contact_type'] = 'Participant'; //checking error in custom data - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); + $this->isErrorInCustomData($params, $errorMessage); if ($errorMessage) { $tempMsg = "Invalid value for field(s) : $errorMessage"; diff --git a/CRM/Import/Parser.php b/CRM/Import/Parser.php index 0f36753f79..3b39ce69dc 100644 --- a/CRM/Import/Parser.php +++ b/CRM/Import/Parser.php @@ -1554,4 +1554,79 @@ abstract class CRM_Import_Parser { return $mappedFields; } + + /** + * Check if an error in custom data. + * + * @deprecated all of this is duplicated if getTransformedValue is used. + * + * @param array $params + * @param string $errorMessage + * A string containing all the error-fields. + * + * @param null $csType + */ + public function isErrorInCustomData($params, &$errorMessage, $csType = NULL) { + $dateType = CRM_Core_Session::singleton()->get("dateTypes"); + $errors = []; + + if (!empty($params['contact_sub_type'])) { + $csType = $params['contact_sub_type'] ?? NULL; + } + + if (empty($params['contact_type'])) { + $params['contact_type'] = 'Individual'; + } + + // get array of subtypes - CRM-18708 + if (in_array($csType, CRM_Contact_BAO_ContactType::basicTypes(TRUE), TRUE)) { + $csType = $this->getSubtypes($params['contact_type']); + } + + if (is_array($csType)) { + // fetch custom fields for every subtype and add it to $customFields array + // CRM-18708 + $customFields = []; + foreach ($csType as $cType) { + $customFields += CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $cType); + } + } + else { + $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType); + } + + foreach ($params as $key => $value) { + if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { + //For address custom fields, we do get actual custom field value as an inner array of + //values so need to modify + if (!array_key_exists($customFieldID, $customFields)) { + return ts('field ID'); + } + /* check if it's a valid custom field id */ + $errors[] = $this->validateCustomField($customFieldID, $value, $customFields[$customFieldID], $dateType); + } + } + if ($errors) { + $errorMessage .= ($errorMessage ? '; ' : '') . implode('; ', array_filter($errors)); + } + } + + /** + * get subtypes given the contact type + * + * @param string $contactType + * @return array $subTypes + */ + protected function getSubtypes($contactType) { + $subTypes = []; + $types = CRM_Contact_BAO_ContactType::subTypeInfo($contactType); + + if (count($types) > 0) { + foreach ($types as $type) { + $subTypes[] = $type['name']; + } + } + return $subTypes; + } + } diff --git a/CRM/Member/Import/Parser/Membership.php b/CRM/Member/Import/Parser/Membership.php index b484a964f8..915385c653 100644 --- a/CRM/Member/Import/Parser/Membership.php +++ b/CRM/Member/Import/Parser/Membership.php @@ -586,7 +586,7 @@ class CRM_Member_Import_Parser_Membership extends CRM_Import_Parser { $params['contact_type'] = 'Membership'; //checking error in custom data - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); + $this->isErrorInCustomData($params, $errorMessage); if ($errorMessage) { $tempMsg = "Invalid value for field(s) : $errorMessage"; diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index e95682dd0e..02bdc43963 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -946,7 +946,8 @@ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { $params = [ 'custom_' . $customField['id'] => 'Label1|Label2', ]; - CRM_Contact_Import_Parser_Contact::isErrorInCustomData($params, $errorMessage); + $parser = new CRM_Contact_Import_Parser_Contact(); + $parser->isErrorInCustomData($params, $errorMessage); $this->assertEquals(NULL, $errorMessage); } -- 2.25.1