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) »
  • Token hook for most recent contribution PLEASE HELP!
Pages: [1]

Author Topic: Token hook for most recent contribution PLEASE HELP!  (Read 1359 times)

Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 03:02:51 am
Hi Everyone

I have been searching all over for example code to pull the most recent contribution into a PDF letter. I see everyone asking all over about this functionality as there is no way of making PDF thank you letters, but no one seems to share their solutions. I am a bit of a rookie when it comes to coding in PHP and the way that the Hooks documentation explains it is really not helpful for someone with a low level of knowledge.

I know how to set up the hooks and i have tested some code that I found thanks to Michel McAndrew but i cant seem to get that to work ether.

Is anyone able to help me with something because I'm really at a loss as to how to do this and i cant find any helpful information anywhere.

If this needs to be a make it happen then is anyone able to estimate a cost?

Thanks Everyone!

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 03:16:42 am
It's old code but I think that's one of the hooks I implemented in this module

https://svn.fuzion.co.nz/repos/mprc/drupal/custom/civitoken/

You'd need to hack out a good portion of it though - it's got all sorts of rubbish in there
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 03:28:42 am
Thanks for the help. Unfortunately its asking me for a username and password? or am i doing something wrong?


Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 03:33:16 am
I found another code that you put in but i cant seem to make that work ether.

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_date, null,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;

}


Is there something i need to do into .info file for the module. I am using the http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification guide to tell me how to set up the hooks. is there something else that i need to do.

I have created a folder and then named the module "hook_civicrm_tokens.module" and then set up the .info file as it was on the guide. Do i need to change anything like the version?

Thanks Eileen

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 03:40:14 am
So, when you see the word 'hook' you should replace that with your module name - there are also examples in the Core code base - try

http://svn.civicrm.org/civicrm/branches/v3.4/drupal/civitest.module.sample
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 04:21:32 am
it doesn't seem to matter what i do the token hooks just aren't working.

Is there something i'm missing? do I have to change any settings or anything? should the token just appear when the hook has been activated?

Sorry about this. I would use the on-line guides if any of them actually gave you a step by step instruction to use the hooks, the documentation is so confusing and only seems to give you half a story if your lucky.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 05:08:30 am
Did you read that chapter of our book ?

http://en.flossmanuals.net/civicrm/ch067_hooks/

Fully agree that the documentation could be improved (it always can ;). Feel free to clarify on the wiki once you found your way and got a hook working.

Yes, the token should appear once you have enabled the module containing the hook.
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 05:26:01 am
Thanks for the reply, i just looked at the link you sent and its a bit more clear than the Wiki but i tried one of the examples in there and it changed all my font sizes and threw an error at the top of the page. I would be more than happy to do documentation write up when i know how it works but I just don't know what i need to put in to these examples to make it tell me the most recent donation. and there seems to be no documentation to tell you how. Everything assumes an advanced level of knowledge.

Do you have any sample code that shows a single donation as a token? or would you be able to give me a hand with it?


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Token hook for most recent contribution PLEASE HELP!
June 21, 2011, 05:48:48 am
Hi,

They are various examples of tokens, but not to my knowledge something that does exactly what you want.

You will need to be able to install and create a module, drupal as more detailed explanations if this is that part that is missing, and the book as a complete example that is cut'n paste working (I did test it when we wrote the book).

If this is a one single need, you might save you time and money by hiring someone that already jumped through the hoops of doing a token/module?

Good luck,

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Chelcone

  • I post occasionally
  • **
  • Posts: 82
  • Karma: 1
  • CiviCRM version: 3.4.0
  • CMS version: Drupal 6.*
  • MySQL version: MySQL 5.*
Re: Token hook for most recent contribution PLEASE HELP!
June 23, 2011, 08:01:54 am
Cant get this to work and I have no budget to hire somone to help so it seems im going to have to come up with some other method for producing the thank you letters. Do companies not send thank you letters in America is that why this function is not in the system? I see so many people asking about this function does anyone know if there is ever going to be higher letter functions in the system?

for a solution Im thinking ODBC connection and pulling data manually into word. Any thoughts?

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Token hook for most recent contribution PLEASE HELP!

This forum was archived on 2017-11-26.