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) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions »
  • Usability Improvements (Moderator: Dave Greenberg) »
  • Smarty Templates for PDF Letters
Pages: 1 [2]

Author Topic: Smarty Templates for PDF Letters  (Read 8459 times)

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Smarty Templates for PDF Letters
February 21, 2011, 12:13:33 pm
I haven't try but as you go through smarty, you might be able to use {crmAPI} and fetch the membership informations. Check the wiki for more info on using the api from smarty (or the developer chapter in our book)

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

resga

  • I post occasionally
  • **
  • Posts: 38
  • Karma: 3
  • CiviCRM version: 3.4.2
  • CMS version: Drupal
  • MySQL version: 5.1.41
  • PHP version: 5.3
Re: Smarty Templates for PDF Letters
February 21, 2011, 01:17:33 pm
I have now made a module, which is detected and the {membership.membership_type_id} and {membership.start_date} tokens are present in the "Smarty Debug Console". But they are not printed in the actual PDF...

This is just to prove that I can actually inject tokens. I probably have to do some SQL joins later to get the actual name of membership type and price, yeah?

I would much prefer to define these tokens in civicrm.settings.php, it this is in any way possible with smarty or otherwise, in stead of building a module  ;)

Code: [Select]
<?php

function civicrm_extratokens_civicrm_tokens( &$tokens ) {
    
$tokens['membership'] = array( 'membership.membership_type_id', 'membership.start_date' );
}

function 
civicrm_extratokens_civicrm_tokenValues( &$values, &$contactIDs ) {
    if ( 
is_array( $contactIDs ) ) {
        
$contactIDString = implode( ',', array_values( $contactIDs ) );
        
$single = false;
    } else {
        
$contactIDString = "( $contactIDs )";
        
$single = true;
    }

    
$query = "
SELECT membership_type_id,
       start_date
FROM   civicrm_membership 
WHERE  contact_id IN ( 
$contactIDString )
AND    is_test = 0
GROUP BY contact_id
"
;

    
$dao = CRM_Core_DAO::executeQuery( $query );
    while ( 
$dao->fetch( ) ) {
        if ( 
$single ) {
            
$value =& $values;
        } else {
            if ( ! 
array_key_exists( $dao->contact_id, $values ) ) {
                
$values[$dao->contact_id] = array( );
            }
            
$value =& $values[$dao->contact_id];
        }

        
$value['membership.membership_type_id'] = $dao->membership_type_id;
        
$value['membership.start_date'  ] = $dao->start_date;
    }
}
« Last Edit: February 21, 2011, 03:56:55 pm by resga »

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Smarty Templates for PDF Letters
February 21, 2011, 05:08:11 pm
Pretty sure you need to do this in a separate module (extending via hooks). Not sure why you're new token values aren't showing up in output - you might want to jump on IRC to discuss further.
Protect your investment in CiviCRM by  becoming a Member!

resga

  • I post occasionally
  • **
  • Posts: 38
  • Karma: 3
  • CiviCRM version: 3.4.2
  • CMS version: Drupal
  • MySQL version: 5.1.41
  • PHP version: 5.3
Re: Smarty Templates for PDF Letters
February 27, 2011, 03:16:14 am
I also can't see why it doesn't work... but I have found another and perhaps simpler approach here, without database look ups: http://w2.forum.civicrm.org/blogs/michael-mcandrew/creating-address-block-token

The module:
Code: [Select]
<?php
function addressblocktoken_civicrm_tokens( &$tokens ) {
    
$tokens['contact'] = array( 
        
'contact.address_block' => 'Address block' );
}

function 
addressblocktoken_civicrm_tokenValues( &$values, &$contactIDs ) {
require_once 'api/v2/Contact.php';
require_once 'CRM/Utils/Address.php';
foreach($contactIDs as $id){
$params=array('contact_id'=>$id);
$contact=civicrm_contact_get($params);
$values[$id]['contact.address_block']=nl2br(CRM_Utils_Address::format($contact[$id]));
}
}
?>

I have verified that the contact.address_block token does work, and is in fact displayed in the PDF letter.

Perhaps it's possible to do the same for membership information, like retrieving Membership Name, Membership Fee, etc.? I tried to hack the module by including the Membership equivalent of Address.php, but couldn't make it work...
« Last Edit: February 27, 2011, 03:19:41 am by resga »

lathos

  • I’m new here
  • *
  • Posts: 8
  • Karma: 0
Re: Smarty Templates for PDF Letters
March 02, 2011, 02:26:18 am
I'm using Smarty Templates for PDF letters but am finding a problem with the HTML editor.

If my template contains e.g. {$smarty.now|date_format:"%d %B %Y"}, every time I use CKE, it helpfully rewrites this to {$smarty.now|date_format:&quot;%d %B %Y&quot;} and then Smarty chokes on it. Is there a way to stop this from happening?

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Smarty Templates for PDF Letters
March 02, 2011, 01:06:31 pm
I don't know how to prevent that. You might want to research this on the CK editor site / forums. If you get an answer, please post back here.
Protect your investment in CiviCRM by  becoming a Member!

lathos

  • I’m new here
  • *
  • Posts: 8
  • Karma: 0
Re: Smarty Templates for PDF Letters
March 03, 2011, 01:43:28 am
Thanks Dave. It looks like this has been recently addressed in CK changeset 6285. You would still have to be careful with (i.e. you can't have) < and > in templates though, but at least this is something.

I guess this will be in the next release of CK and so will be automatically picked up by Civi in its next release? I can't quite tell from the patch how to configure the non-escaping behaviour, but I think it should be turned off by default in Civi because of this issue.

Dave Greenberg

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 5760
  • Karma: 226
    • My CiviCRM Blog
Re: Smarty Templates for PDF Letters
March 03, 2011, 03:46:03 pm
We periodically pick up new releases of included libraries / projects - so sounds like this will be resolved at some point.
Protect your investment in CiviCRM by  becoming a Member!

Pages: 1 [2]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions »
  • Usability Improvements (Moderator: Dave Greenberg) »
  • Smarty Templates for PDF Letters

This forum was archived on 2017-11-26.