Skip to content

Commit

Permalink
dev/core#1285 import support for campaign_id (& other fields where it…
Browse files Browse the repository at this point in the history
… could be an id, a name or a label.

After digging into https://lab.civicrm.org/dev/core/issues/1285 it feels a little arguable what we should support but issues in
gitlab indicate there is at least some demand for id & label so it now will
- accept id if input matches an id
- accept name if input matches a name
- accept label if input matches a label.

This means for payment instrument it would accept '1', 'Check' or 'Cheque' (in that order if name were renamed).

This should apply to a few fields - payment instrument, constribution status & per the issue campaign
(on contribution). There are 'some' on membership too I think
  • Loading branch information
eileenmcnaughton committed Oct 8, 2019
1 parent d375fc0 commit 9ae10cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
19 changes: 13 additions & 6 deletions CRM/Import/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,9 @@ protected function checkContactDuplicate(&$formatValues) {
/**
* Parse a field which could be represented by a label or name value rather than the DB value.
*
* We will try to match name first but if not available then see if we have a label that can be converted to a name.
* We will try to match name first or (per https://lab.civicrm.org/dev/core/issues/1285 if we have an id.
*
* but if not available then see if we have a label that can be converted to a name.
*
* @param string|int|null $submittedValue
* @param array $fieldSpec
Expand All @@ -567,11 +569,16 @@ protected function parsePseudoConstantField($submittedValue, $fieldSpec) {
$bao = $fieldSpec['bao'];
// For historical reasons use validate as context - ie disabled name matches ARE permitted.
$nameOptions = $bao::buildOptions($fieldSpec['name'], 'validate');
if (!isset($nameOptions[$submittedValue])) {
$labelOptions = array_flip($bao::buildOptions($fieldSpec['name'], 'match'));
if (isset($labelOptions[$submittedValue])) {
return array_search($labelOptions[$submittedValue], $nameOptions, TRUE);
}
if (isset($nameOptions[$submittedValue])) {
return $submittedValue;
}
if (in_array($submittedValue, $nameOptions)) {
return array_search($submittedValue, $nameOptions, TRUE);
}

$labelOptions = array_flip($bao::buildOptions($fieldSpec['name'], 'match'));
if (isset($labelOptions[$submittedValue])) {
return array_search($labelOptions[$submittedValue], $nameOptions, TRUE);
}
return '';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ public function testContributionStatusLabel() {
$values['contribution_status_id'] = 'just say no';
$this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::ERROR);
$this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2);

// Per https://lab.civicrm.org/dev/core/issues/1285 it's a bit arguable but Ok we can support id...
$values['contribution_status_id'] = 3;
$this->runImport($values, CRM_Import_Parser::DUPLICATE_UPDATE, NULL);
$this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID, 'contribution_status_id' => 3], 1);

}

/**
Expand Down

0 comments on commit 9ae10cd

Please sign in to comment.