Author Topic: Contribution Tokens in Mailings?  (Read 889 times)

Offline markandphil

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 3.3.5
  • CMS version: 6.20
  • MySQL version: 4
  • PHP version: 5
Contribution Tokens in Mailings?
« on: March 09, 2011, 03:18:38 am »
Hi everyone,
I've been banging my head against a wall for a few days now trying to figure out how to get tokens from a contribution into a Message Template so the user can create a PDF of all their contributions on one nice thank you letter. I've read the documentation at en.flossmanuals.net/CiviCRM/Mail and it seems misleading because it suggests it is possible to simply add the tokens from the token list, but alas, when I look at the list there are no tokens for CiviContribute fields.

Am I just dense and missing it completely? Any light you could shed on this would be must appreciated.

Thanks so much!

Offline Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 3235
  • Karma: 155
    • Fuzion
Re: Contribution Tokens in Mailings?
« Reply #1 on: March 09, 2011, 03:23:18 am »
You can only add contribution specific fields to templates coming from the contribution search or a contribution action, or via a hook

Code here is an eg. of adding contribution tokens via hook

Code: [Select]
<?php

function civitoken_civicrm_tokens( &$tokens ) {
    
$tokens['civitoken'] = array( 'civitoken.contributionTotal',  'civitoken.contributionLast','civitoken.contributionList' );
}

function 
civitoken_civicrm_tokenValues( &$values, &$contactIDs$jobID ) {

    
// we could be given an array of contact IDs or a string
    
require_once 'api/v2/Contact.php';
    require_once 
'api/v2/Contribute.php';
    if (isset(
$contactIDs[0])){
    
$params['contact_id'] = $contactIDs[0];
    }else{
        
$params['contact_id'] = $contactIDs['contact_id'];
    }


    if ( 
is_array$contactIDs ) ) {
        foreach (
$contactIDs as $contactID){
          
$value =& $values[$contactID];
          
$value get_contribution_details($contactID,$value);
          
$value get_contribution_totals($contactID,$value);


        }

    } else {
          
$value =& $values;
          
$value get_contribution_details($contactIDs,$value);
    }





    
//  $value['civitoken.contributionTotal'] = 'total amount from above functions' ;
    
}

function 
get_contribution_totals($cid, &$value){
        global 
$db_url;
        if (
$db_url['civicrm']){
          
db_set_active('civicrm');
        }
     
$query "
SELECT sum( total_amount ) as total_amount,
       contact_id,
       max( receive_date ) as receive_date
FROM   civicrm_contribution
WHERE  contact_id = ( 
$cid )
AND    is_test = 0
GROUP BY contact_id
"
;
    
$dao CRM_Core_DAO::executeQuery$query );
        
//Switch back to the default connection when finished.
        
db_set_active('default');



    while ( 
$dao->fetch( ) ) {
        
$value['civitoken.contributionTotal'] = $dao->total_amount;
        
$value['civitoken.contributionLast'  ] =  CRM_Utils_Date::customFormat($dao->receive_datenull,array('M','d','Y')) ;
    }
    return 
$value;

}



function 
get_contribution_details($cid, &$value){

    require_once 
'api/v2/Contribute.php';
    
$params['sort']       = 'receive_date DESC';
    
$params['contact_id'] = $cid;
    
$params['limit'] = 5;
    
$contributions civicrm_contribution_search($params);
    
//get field labels

    
$i 0;
    
$value['civitoken.contributionList'] = "<table><tr><th>Amount</th><th>Date</th><th>Status</th><th>Type</th><th>Worker</th><th>Promo</th><th>Dept</th><th>Staff member</th></tr>";

    foreach(
$contributions as $contribution){
    if (
$i<5){
      
$value['civitoken.contributionList'] .= "<tr><td>$" $contribution['total_amount'] . "</td>";
      
$value['civitoken.contributionList'] .= "<td>" CRM_Utils_Date::customFormat($contribution['receive_date'], null,array('M','d','Y')) . "</td>";
      
$value['civitoken.contributionList'] .= "<td>" .$contribution['contribution_status_id']. "</td>";
      
$value['civitoken.contributionList'] .= "<td>" .$contribution['contribution_type']. "</td>";
      
$value['civitoken.contributionList'] .= "<td>" .$contribution['custom_50']. "</td>";
      
$value['civitoken.contributionList'] .= "<td>" .$contribution['custom_20']. "</td>";
      
$value['civitoken.contributionList'] .= "<td>" .$contribution['custom_21']. "</td></tr>";

    }
    
$i++;
    }
        
$value['civitoken.contributionList'] .= "</table>";

    return 
$value;

}

(http://civicrm.org/sites/civicrm.org/files/bugsmithing-2013-03.png)

Offline xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 3732
  • Karma: 134
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contribution Tokens in Mailings?
« Reply #2 on: March 11, 2011, 03:08:41 am »
beware that this code will be run for every mail sent, even when you don't have this token.

Offline markandphil

  • I’m new here
  • *
  • Posts: 3
  • Karma: 0
  • CiviCRM version: 3.3.5
  • CMS version: 6.20
  • MySQL version: 4
  • PHP version: 5
Re: Contribution Tokens in Mailings?
« Reply #3 on: March 14, 2011, 11:51:37 am »
Thanks so much for this code! I'll try it today! One question, where did you get this from? I had looked in the documentation relentlessly and couldn't find it anywhere. 

Offline Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 3235
  • Karma: 155
    • Fuzion
Re: Contribution Tokens in Mailings?
« Reply #4 on: March 14, 2011, 11:59:30 am »
The code is from a custom module I wrote - there is documentation on the hook specification on the wiki & on the api. There is are also some example hooks in the drupal module in your install (it's called something like sample.something).

However, I would encourage you to put the code in a place where you would have found when you were looking so others may find it
(http://civicrm.org/sites/civicrm.org/files/bugsmithing-2013-03.png)

Offline prinetti

  • I’m new here
  • *
  • Posts: 4
  • Karma: 0
  • CiviCRM version: 3.3.6
  • CMS version: Joomla
  • MySQL version: 5.1.48
  • PHP version: 5.2.16
Re: Contribution Tokens in Mailings?
« Reply #5 on: June 07, 2011, 07:00:06 am »
Hi,

I used th hooks as described in the code and it works fine.

the only things I'm not able to do is to select only one year of contribution

I need to print for each contributor the total contribution in one specific year (e.g. 2011) and the list of contribution in this list.

Is there a way to limit the functionality to a specific year?

Please let me know
Giovanni