From 611426b5d890d4206034c73e4080b0112f26fd69 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 11 Aug 2022 09:53:50 +1200 Subject: [PATCH] Deprecated BAO_Contribution::importableFields --- CRM/Contribute/BAO/Contribution.php | 3 +- CRM/Contribute/Import/Parser/Contribution.php | 69 ++++++++++++++++++- CRM/Upgrade/Incremental/php/FiveFiftyOne.php | 64 ++++++++++++++++- .../Import/Parser/ContributionTest.php | 3 +- 4 files changed, 134 insertions(+), 5 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 2f393e5120..fc2e1b2a84 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -693,11 +693,12 @@ class CRM_Contribute_BAO_Contribution extends CRM_Contribute_DAO_Contribution im * * @param string $contactType * @param bool $status - * + * @deprecated * @return array * array of importable Fields */ public static function &importableFields($contactType = 'Individual', $status = TRUE) { + CRM_Core_Error::deprecatedFunctionWarning('api'); if (!self::$_importableFields) { if (!self::$_importableFields) { self::$_importableFields = []; diff --git a/CRM/Contribute/Import/Parser/Contribution.php b/CRM/Contribute/Import/Parser/Contribution.php index ca4fdb8aef..3dc5d108aa 100644 --- a/CRM/Contribute/Import/Parser/Contribution.php +++ b/CRM/Contribute/Import/Parser/Contribution.php @@ -234,7 +234,7 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { */ protected function setFieldMetadata() { if (empty($this->importableFieldsMetadata)) { - $fields = CRM_Contribute_BAO_Contribution::importableFields($this->getContactType(), FALSE); + $fields = $this->importableFields($this->getContactType(), FALSE); $fields = array_merge($fields, [ @@ -274,6 +274,73 @@ class CRM_Contribute_Import_Parser_Contribution extends CRM_Import_Parser { } } + /** + * Combine all the importable fields from the lower levels object. + * + * The ordering is important, since currently we do not have a weight + * scheme. Adding weight is super important and should be done in the + * next week or so, before this can be called complete. + * + * @param string $contactType + * @param bool $status + * + * @return array + * array of importable Fields + */ + private function importableFields($contactType = 'Individual', $status = TRUE) { + + if (!$status) { + $fields = ['' => ['title' => ts('- do not import -')]]; + } + else { + $fields = ['' => ['title' => ts('- Contribution Fields -')]]; + } + + $note = CRM_Core_DAO_Note::import(); + $tmpFields = CRM_Contribute_DAO_Contribution::import(); + unset($tmpFields['option_value']); + $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); + + // Using new Dedupe rule. + $ruleParams = [ + 'contact_type' => $contactType, + 'used' => 'Unsupervised', + ]; + $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams); + $tmpContactField = []; + if (is_array($fieldsArray)) { + foreach ($fieldsArray as $value) { + //skip if there is no dupe rule + if ($value === 'none') { + continue; + } + $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', + $value, + 'id', + 'column_name' + ); + $value = $customFieldId ? 'custom_' . $customFieldId : $value; + $tmpContactField[trim($value)] = $contactFields[trim($value)]; + if (!$status) { + $title = $tmpContactField[trim($value)]['title'] . ' ' . ts('(match to contact)'); + } + else { + $title = $tmpContactField[trim($value)]['title']; + } + $tmpContactField[trim($value)]['title'] = $title; + } + } + + $tmpContactField['external_identifier'] = $contactFields['external_identifier']; + $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)'); + $tmpFields['contribution_contact_id']['title'] = $tmpFields['contribution_contact_id']['html']['label'] = $tmpFields['contribution_contact_id']['title'] . ' ' . ts('(match to contact)'); + $fields = array_merge($fields, $tmpContactField); + $fields = array_merge($fields, $tmpFields); + $fields = array_merge($fields, $note); + $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); + return $fields; + } + /** * Handle the values in import mode. * diff --git a/CRM/Upgrade/Incremental/php/FiveFiftyOne.php b/CRM/Upgrade/Incremental/php/FiveFiftyOne.php index 305167456e..94639ab0ef 100644 --- a/CRM/Upgrade/Incremental/php/FiveFiftyOne.php +++ b/CRM/Upgrade/Incremental/php/FiveFiftyOne.php @@ -94,7 +94,7 @@ class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_B ->setSelect(['id', 'name']) ->addWhere('mapping_id.mapping_type_id:name', '=', 'Import Contribution') ->execute(); - $fields = CRM_Contribute_BAO_Contribution::importableFields('All', FALSE); + $fields = self::importableContributionFields('All', FALSE); $fieldMap = []; foreach ($fields as $fieldName => $field) { $fieldMap[$field['title']] = $fieldName; @@ -272,4 +272,66 @@ class CRM_Upgrade_Incremental_php_FiveFiftyOne extends CRM_Upgrade_Incremental_B return TRUE; } + /** + * Historical copy of Contribution importable fields function. + * + * @param string $contactType + * @param bool $status + * + * @return array + * array of importable Fields + */ + private static function importableContributionFields($contactType = 'Individual', $status = TRUE) { + if (!$status) { + $fields = ['' => ['title' => ts('- do not import -')]]; + } + else { + $fields = ['' => ['title' => ts('- Contribution Fields -')]]; + } + + $note = CRM_Core_DAO_Note::import(); + $tmpFields = CRM_Contribute_DAO_Contribution::import(); + unset($tmpFields['option_value']); + $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); + + // Using new Dedupe rule. + $ruleParams = [ + 'contact_type' => $contactType, + 'used' => 'Unsupervised', + ]; + $fieldsArray = CRM_Dedupe_BAO_DedupeRule::dedupeRuleFields($ruleParams); + $tmpContactField = []; + if (is_array($fieldsArray)) { + foreach ($fieldsArray as $value) { + //skip if there is no dupe rule + if ($value === 'none') { + continue; + } + $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', + $value, + 'id', + 'column_name' + ); + $value = $customFieldId ? 'custom_' . $customFieldId : $value; + $tmpContactField[trim($value)] = $contactFields[trim($value)]; + if (!$status) { + $title = $tmpContactField[trim($value)]['title'] . ' ' . ts('(match to contact)'); + } + else { + $title = $tmpContactField[trim($value)]['title']; + } + $tmpContactField[trim($value)]['title'] = $title; + } + } + + $tmpContactField['external_identifier'] = $contactFields['external_identifier']; + $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . ' ' . ts('(match to contact)'); + $tmpFields['contribution_contact_id']['title'] = $tmpFields['contribution_contact_id']['html']['label'] = $tmpFields['contribution_contact_id']['title'] . ' ' . ts('(match to contact)'); + $fields = array_merge($fields, $tmpContactField); + $fields = array_merge($fields, $tmpFields); + $fields = array_merge($fields, $note); + $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Contribution')); + return $fields; + } + } diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 7375c6b1d7..167bb0eca2 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -226,9 +226,8 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { 'rule_weight' => 10, 'rule_field' => 'phone_numeric', ]); - $userJobID = UserJob::create()->setValues(['job_type:name' => 'Import Contributions', 'status_id:name' => 'Draft', 'metadata' => ['contactType' => 'Individual']])->execute()->first()['id']; $parser = new CRM_Contribute_Import_Parser_Contribution(); - $parser ->setUserJobID($userJobID); + $parser ->setUserJobID($this->getUserJobID()); $fields = $parser->getAvailableFields(); $this->assertArrayHasKey('phone', $fields); $this->callApiSuccess('RuleGroup', 'create', [ -- 2.25.1