Skip to content

Commit

Permalink
Merge pull request #8222 from eileenmcnaughton/CRM-18465
Browse files Browse the repository at this point in the history
CRM-18465: fully resolve contributions before resolving tokens
  • Loading branch information
monishdeb committed May 19, 2016
2 parents e95182e + e9379b5 commit 3f699c2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 19 deletions.
47 changes: 28 additions & 19 deletions CRM/Contribute/Form/Task/PDFLetterCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public static function postProcess(&$form) {
// skip some contacts ?
$skipOnHold = isset($form->skipOnHold) ? $form->skipOnHold : FALSE;
$skipDeceased = isset($form->skipDeceased) ? $form->skipDeceased : TRUE;

list($contributions, $contacts) = self::buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator);
$contributionIDs = $form->getVar('_contributionIds');
if ($form->_includesSoftCredits) {
//@todo - comment on what is stored there
$contributionIDs = $form->getVar('_contributionContactIds');
}
list($contributions, $contacts) = self::buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $form->_includesSoftCredits);
$html = array();
foreach ($contributions as $contributionId => $contribution) {
$contact = &$contacts[$contribution['contact_id']];
Expand Down Expand Up @@ -204,23 +208,19 @@ private static function resolveTokens($html_message, $contact, $contribution, $m
* around contact_id of contribution_recur_id
*
* @param string $groupBy
* @param CRM_Contribute_Form_Task $form
* @param array $contributionIDs
* @param array $returnProperties
* @param bool $skipOnHold
* @param bool $skipDeceased
* @param array $messageToken
* @param string $task
* @param string $separator
* @param bool $isIncludeSoftCredits
*
* @return array
*/
public static function buildContributionArray($groupBy, $form, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator) {
public static function buildContributionArray($groupBy, $contributionIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $task, $separator, $isIncludeSoftCredits) {
$contributions = $contacts = $notSent = array();
$contributionIDs = $form->getVar('_contributionIds');
if ($form->_includesSoftCredits) {
//@todo - comment on what is stored there
$contributionIDs = $form->getVar('_contributionContactIds');
}
foreach ($contributionIDs as $item => $contributionId) {
// get contribution information
$contribution = CRM_Utils_Token::getContributionTokenDetails(array('contribution_id' => $contributionId),
Expand All @@ -230,7 +230,8 @@ public static function buildContributionArray($groupBy, $form, $returnProperties
$task
);
$contribution = $contributions[$contributionId] = $contribution[$contributionId];
if ($form->_includesSoftCredits) {

if ($isIncludeSoftCredits) {
//@todo find out why this happens & add comments
list($contactID) = explode('-', $item);
$contactID = (int) $contactID;
Expand All @@ -239,15 +240,7 @@ public static function buildContributionArray($groupBy, $form, $returnProperties
$contactID = $contribution['contact_id'];
}
if (!isset($contacts[$contactID])) {
list($contact) = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID),
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
$task
);
$contacts[$contactID] = $contact[$contactID];
$contacts[$contactID] = array();
$contacts[$contactID]['contact_aggregate'] = 0;
$contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
}
Expand All @@ -265,6 +258,22 @@ public static function buildContributionArray($groupBy, $form, $returnProperties
$contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
}
}
// Assign the available contributions before calling tokens so hooks parsing smarty can access it.
// Note that in core code you can only use smarty here if enable if for the whole site, incl
// CiviMail, with a big performance impact.
// Hooks allow more nuanced smarty usage here.
CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
foreach ($contacts as $contactID => $contact) {
$tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID),
$returnProperties,
$skipOnHold,
$skipDeceased,
NULL,
$messageToken,
$task
);
$contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
}
return array($contributions, $contacts);
}

Expand Down
85 changes: 85 additions & 0 deletions tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2016 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Test APIv3 civicrm_contribute_* functions
*
* @package CiviCRM_APIv3
* @subpackage API_Contribution
* @group headless
*/
class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase {

/**
* Assume empty database with just civicrm_data.
*/
protected $_individualId;

/**
* Clean up after each test.
*/
public function tearDown() {
$this->quickCleanUpFinancialEntities();
CRM_Utils_Hook::singleton()->reset();
}

/**
* Test the buildContributionArray function.
*/
public function testBuildContributionArray() {
$this->_individualId = $this->individualCreate();
$params = array('contact_id' => $this->_individualId, 'total_amount' => 6, 'financial_type_id' => 'Donation');
$contributionIDs = $returnProperties = $messageToken = array();
$result = $this->callAPISuccess('Contribution', 'create', $params);
$contributionIDs[] = $result['id'];
$result = $this->callAPISuccess('Contribution', 'create', $params);
$contributionIDs[] = $result['id'];
$this->hookClass->setHook('civicrm_tokenValues', array($this, 'hookTokenValues'));

list($contributions, $contacts) = CRM_Contribute_Form_Task_PDFLetterCommon::buildContributionArray('contact_id', $contributionIDs, $returnProperties, TRUE, TRUE, $messageToken, 'test', '**', FALSE);

$this->assertEquals('Anthony', $contacts[$this->_individualId]['first_name']);
$this->assertEquals('emo', $contacts[$this->_individualId]['favourite_emoticon']);
$this->assertEquals('Donation', $contributions[$result['id']]['financial_type']);
}

/**
* Implement token values hook.
*
* @param array $details
* @param array $contactIDs
* @param int $jobID
* @param array $tokens
* @param string $className
*/
public function hookTokenValues(&$details, $contactIDs, $jobID, $tokens, $className) {
foreach ($details as $index => $detail) {
$details[$index]['favourite_emoticon'] = 'emo';
}
}

}

0 comments on commit 3f699c2

Please sign in to comment.