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

mail#46 - show label, not value, on contribution custom field tokens #14658

Merged
merged 4 commits into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 25 additions & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -5905,14 +5905,38 @@ public static function replaceContributionTokens(
}
$contributionDetails = [];
foreach ($contributionIds as $id) {
$result = civicrm_api3('Contribution', 'get', ['id' => $id]);
$result = self::getContributionTokenValues($id, $messageToken);
$contributionDetails[$result['values'][$result['id']]['contact_id']]['subject'] = CRM_Utils_Token::replaceContributionTokens($subject, $result, FALSE, $subjectToken, FALSE, $escapeSmarty);
$contributionDetails[$result['values'][$result['id']]['contact_id']]['text'] = CRM_Utils_Token::replaceContributionTokens($text, $result, FALSE, $messageToken, FALSE, $escapeSmarty);
$contributionDetails[$result['values'][$result['id']]['contact_id']]['html'] = CRM_Utils_Token::replaceContributionTokens($html, $result, FALSE, $messageToken, FALSE, $escapeSmarty);
}
return $contributionDetails;
}

/**
* Get the contribution fields for $id and display labels where
* appropriate (if the token is present).
*
* @param int $id
* @param array $messageToken
* @return array
*/
public static function getContributionTokenValues($id, $messageToken) {
if (empty($id)) {
return [];
}
$result = civicrm_api3('Contribution', 'get', ['id' => $id]);
MegaphoneJon marked this conversation as resolved.
Show resolved Hide resolved
// lab.c.o mail#46 - show labels, not values, for custom fields with option values.
if (!empty($messageToken)) {
foreach ($result['values'][$id] as $fieldName => $fieldValue) {
if (strpos($fieldName, 'custom_') === 0 && array_search($fieldName, $messageToken['contribution']) !== FALSE) {
$result['values'][$id][$fieldName] = CRM_Core_BAO_CustomField::displayValue($result['values'][$id][$fieldName], $fieldName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be also written as

$result['values'][$id][$fieldName] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $fieldName);

}
}
}
return $result;
}

/**
* Get invoice_number for contribution.
*
Expand Down
11 changes: 1 addition & 10 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,7 @@ private static function resolveTokens($html_message, $contact, $contribution, $m
public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
$contributions = $contacts = [];
foreach ($contributionIDs as $item => $contributionId) {
// Basic return attributes available to the template.
$returnValues = ['contact_id', 'total_amount', 'financial_type', 'receive_date', 'contribution_campaign_title'];
if (!empty($messageToken['contribution'])) {
$returnValues = array_merge($messageToken['contribution'], $returnValues);
}
// retrieve contribution tokens listed in $returnProperties using Contribution.Get API
$contribution = civicrm_api3('Contribution', 'getsingle', [
'id' => $contributionId,
'return' => $returnValues,
]);
$contribution = CRM_Contribute_BAO_Contribution::getContributionTokenValues($contributionId, $messageToken)['values'][$contributionId];
$contribution['campaign'] = CRM_Utils_Array::value('contribution_campaign_title', $contribution);
$contributions[$contributionId] = $contribution;

Expand Down
10 changes: 8 additions & 2 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,8 @@ public function testProcessOnBehalfOrganization() {
* This function tests whether the contribution tokens are replaced with values from contribution.
*/
public function testReplaceContributionTokens() {
$customGroup = $this->customGroupCreate(['extends' => 'Contribution', 'title' => 'contribution stuff']);
$customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField');
$contactId1 = $this->individualCreate();
$params = array(
'contact_id' => $contactId1,
Expand All @@ -1476,6 +1478,7 @@ public function testReplaceContributionTokens() {
'invoice_id' => 67890,
'source' => 'SSF',
'contribution_status_id' => 2,
"custom_{$customField['id']}" => 'value1',
);
$contribution1 = $this->contributionCreate($params);
$contactId2 = $this->individualCreate();
Expand All @@ -1488,6 +1491,7 @@ public function testReplaceContributionTokens() {
'invoice_id' => 12345,
'source' => 'ABC',
'contribution_status_id' => 1,
"custom_{$customField['id']}" => 'value2',
);
$contribution2 = $this->contributionCreate($params);
$ids = array($contribution1, $contribution2);
Expand All @@ -1496,7 +1500,8 @@ public function testReplaceContributionTokens() {
$text = "Contribution Amount: {contribution.total_amount}";
$html = "<p>Contribution Source: {contribution.contribution_source}</p></br>
<p>Contribution Invoice ID: {contribution.invoice_id}</p></br>
<p>Contribution Receive Date: {contribution.receive_date}</p></br>";
<p>Contribution Receive Date: {contribution.receive_date}</p></br>
<p>Contribution Custom Field 1: {contribution.custom_1}</p></br>";

$subjectToken = CRM_Utils_Token::getTokens($subject);
$messageToken = CRM_Utils_Token::getTokens($text);
Expand All @@ -1515,7 +1520,8 @@ public function testReplaceContributionTokens() {
$this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match");
$this->assertEquals("<p>Contribution Source: ABC</p></br>
<p>Contribution Invoice ID: 12345</p></br>
<p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
<p>Contribution Receive Date: May 11th, 2015</p></br>
<p>Contribution Custom Field 1: Label2</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,7 @@ public function customFieldOptionValueCreate($customGroup, $name, $extraParams =
'option_value' => array('value1', 'value2'),
'option_name' => array($name . '_1', $name . '_2'),
'option_weight' => array(1, 2),
'option_status' => 1,
'option_status' => array(1, 1),
);

$params = array_merge($fieldParams, $optionGroup, $optionValue, $extraParams);
Expand Down