CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • accessing custom contribution data in a mail template token (smarty? hook?)
Pages: [1]

Author Topic: accessing custom contribution data in a mail template token (smarty? hook?)  (Read 1216 times)

fen

  • I post frequently
  • ***
  • Posts: 216
  • Karma: 13
    • CivicActions
  • CiviCRM version: 3.3-4.3
  • CMS version: Drupal 6/7
  • MySQL version: 5.1/5.5
  • PHP version: 5.3/5.4
accessing custom contribution data in a mail template token (smarty? hook?)
May 01, 2012, 07:45:02 pm
I have a (contribution) custom data group "Additional Information" in table civicrm_value_check_data_7 that has a datetime column check_date_13.  Check_date is the date the donation check was written - as opposed to receive_date, which is the date the check was received.  This is particularly important in January as end-of-the-year tax-deductible gifts may not be received until January.  Everything is fine except that the thank you notes (which may be used for tax purposes) are currently saying "Thank you for your donation on {$received_date}..." when we'd rather they use the check_date field (if it has been set).

It would be nice if {contribution.custom_13} worked but it appears these custom_* values only work for contact data (am I wrong?).

Considered Smarty in mail templates (http://wiki.civicrm.org/confluence/display/CRMDOC41/Smarty+in+mail+templates) and tried this:
Code: [Select]
{assign var=addinfo  value="Additional Information"}
{assign var=chkfield value="Check Date"}
{assign var=chkdate  value=$customGroup.$addinfo.$chkdate}
...
  "Thank you for your donation on {$chkdate}"
But this didn't work either.

Speaking of Smarty, how does one know what additional Smarty variables are available to message templates?  It appears that (contribution) $amount and $receive_date are available, though these do not appear in any page I can bring up with smartyDebug=1.  And where does $customGroup come from?  (update: I see some are defined on CRM/Contribute/Form/ContributionBase.php - is there any way to get a listing of these from e.g. smartyDebug?)

Anyway, then I tried using hooks (see code below):

Minor question first: if hook_civicrm_tokens() always defines $tokens['donation'], then won't a test for (empty($tokens['donation'])) in hook_civicrm_tokenValues() always fail?

Major question: if a single contact has made two donations, one with check_date set, the code below will set the {donation.check_date} token on *both* donations.  It appears to me that the contribution ID would need to be included in the passed parameters to solve this - but it is not available.  Is there some other way to set the check_date token only for the contribution thank you note (receipt) for which it was set?

Code: [Select]
function hook_civicrm_tokens( &$tokens ) {
  $tokens['donation'] = array( 'donation.check_date' );
}

function hook_civicrm_tokenValues(&$values, &$cids, $job = null, $tokens = array(), $context = null ) {
  if (empty($tokens['donation'])) {
    return;
  }
  $contactIDString = implode( ',', array_values( $cids ) );
  $query = "
SELECT contribution.contact_id as contact_id,
       contribution.total_amount as total_amount,
       check_data.check_date_13 as check_date
FROM   civicrm_contribution contribution
JOIN   civicrm_value_check_data_7 check_data on contribution.id = check_data.entity_id
WHERE  contribution.contact_id IN ( $contactIDString )
AND    contribution.is_test = 0;
";

  $dao = CRM_Core_DAO::executeQuery( $query );
  while ( $dao->fetch( ) ) {
    $cid = $dao->contact_id;
    $check_date = $dao->check_date;
    if ($check_date && in_array($cid, $cids)) {
      $values[$cid]['donation.check_date'] = $check_date;
    }
  }
}

Thanks!
« Last Edit: May 02, 2012, 06:22:46 am by fen »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • accessing custom contribution data in a mail template token (smarty? hook?)

This forum was archived on 2017-11-26.