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) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • CiviMember details and Tokens (PDF templates)
Pages: [1]

Author Topic: CiviMember details and Tokens (PDF templates)  (Read 1795 times)

gibsonoliver

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 2
    • Northbridge Digital
CiviMember details and Tokens (PDF templates)
April 07, 2011, 07:21:55 am
Hi

I'm working with an organisation who want to use PDF letter templates to create Membership certificates.

I know you can use custom fields as Tokens - is there anyway to include Membership details (i.e. start date etc)

Oliver
Oliver Gibson, Northbridge Digital

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviMember details and Tokens (PDF templates)
April 07, 2011, 09:51:18 am

hey oliver:

u'll need to do this via the token hook

the main issue will be, what happens when a contact has multiple memberships

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

gibsonoliver

  • I post occasionally
  • **
  • Posts: 65
  • Karma: 2
    • Northbridge Digital
Re: CiviMember details and Tokens (PDF templates)
April 08, 2011, 12:12:08 am
Thanks Lobo

I can see the issue, in this case though the membership to contact relationship will be singular

Olly
Oliver Gibson, Northbridge Digital

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: CiviMember details and Tokens (PDF templates)
April 15, 2011, 12:11:00 am
I have been trying to do this with a custom module using token hook, without luck.

I also only need the membership to contact relationship to be singular, so please do post your solution here, if you manage to make Membership details available as Tokens!

theMusician

  • I post occasionally
  • **
  • Posts: 48
  • Karma: 3
  • CiviCRM version: 4.0 and 4.0.1
  • CMS version: Drupal 7.0
  • MySQL version: MySQL 5.x
  • PHP version: 5.2
Re: CiviMember details and Tokens (PDF templates)
April 15, 2011, 04:25:53 pm
Hi resga,

I have been trying to do the same with a PDF. Running the query directly against the mysql database returns the results I need but when using the query in the custom module I get nothing output. I am trying to retrieve information from a few custom data fields that have created for a CiviCase.

Here is my module code if it helps.

Code: [Select]
<?php 

/**
 * @file
 * Module using civicrm hooks to provide the user with civicrm custom data.
 *
 * This module will implement the civicrm_token hook to add custom data fields
 * to the list of tokens displayed to CiviMail users.
 */

/**
 * Implements hook_civicrm_tokens()
 */
function fmla_hooks_civicrm_tokens( &$tokens ) {
    
$tokens['activities'] = array(
      
'activities.custom14' => 'Leave Type',
      
'activities.custom15' => 'Request Received',
      );
}

/**
 * Implements hook_civicrm_tokenValues()
 */
function fmla_hooks_civicrm_tokenValues( &$values, &$contactIDs ) {
  if ( 
is_array( $contactIDs ) ) {
         
$contactIDString = implode( ',', array_values( $contactIDs ) );
         
$single = false;
    } else {
        
$contactIDString = "( $contactIDs )";
        
$single = true;
    }

    
$query = "
SELECT civicrm_activity.source_contact_id, civicrm_value_fmla_leave_occurence_2.leave_type_14, civicrm_value_fmla_leave_occurence_2.request_receieved_15, civicrm_value_fmla_leave_occurence_2.entity_id, civicrm_activity.id AS activitId, civicrm_contact.id as contactID
FROM   civicrm_value_fmla_leave_occurence_2, civicrm_contact, civicrm_case, civicrm_case_contact, civicrm_activity
WHERE civicrm_value_fmla_leave_occurence_2.entity_id = civicrm_activity.id
AND
civicrm_activity.source_contact_id IN (
$contactIDString)
GROUP BY civicrm_activity.source_contact_id
"
;

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

        
$value['activities.custom14'] = $dao->civicrm_value_fmla_leave_occurence_2.leave_type_14;
        
$value['activities.custom15'  ] = $dao->civicrm_value_fmla_leave_occurence_2.request_receieved_15;
    }
}

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: CiviMember details and Tokens (PDF templates)
April 16, 2011, 01:43:08 am
Yeah, it's frustrating! I have also tried to write my own SQL calls in a module, but the information don't get passed to the PDF print process.

I have a dream that somebody have at some point made a solution like the one pasted below, using the API of the system to retrieve membership data, or in your case activities data. Specifically I need the membership type and fee of a member, where members will only have a single membership type. I have tried, but just can't find the magical Membership.php file, AND figure out how to write the code which will extract membership name and fee...

This module extracts the address information for a member using the Contact.php and Address.php files of the system:
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]));
}
}
?>
http://w2.forum.civicrm.org/blogs/michael-mcandrew/creating-address-block-token

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviMember details and Tokens (PDF templates)
April 16, 2011, 06:44:41 am

theMusician:

check:

http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification#CiviCRMhookspecification-GoalsandBackground

so:

1. have u enabled your module within drupal

2. whats your module name? fmla? or fmla_hooks?

resga:

If you are more specific with what/where/how u got stuck that will allow us to help you. While we do encourage people to dream, its not likely to push that issue a lot further.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviMember details and Tokens (PDF templates)
April 16, 2011, 07:01:06 am

resga:

i tried the following on 3.4 and it worked. I just added it to civicrm.module since i was "hacking" on my local sandbox. u'll need to put this in  a seperate module:

Code: [Select]
function civicrm_civicrm_tokens( &$tokens ) {
    $tokens['membership'] = array( 'membership.membership_type_id', 'membership.start_date' );
}

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

    $query = "                                                                                                                             
SELECT contact_id,                                                                                                                         
       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;
    }
}

I used the "Source" mode of the html editor to enter the following letter

Code: [Select]
Dear {contact.first_name}, {contact.last_name}
<p>
Your membership started on {membership.start_date} and is of type {membership.membership_type_id}
<p>
Thanx
<p>
Your friends @ CiviCRM

Hopefully this will get you going. If you find CiviCRM useful, please do contribute to one of the many MIH's for 4.1 or ot the general project:

http://civicrm.org/mih
http://civicrm.org/civicrm/contribute/transact&reset=1&id=19

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

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: CiviMember details and Tokens (PDF templates)
April 16, 2011, 08:24:26 am
Thank you so much lobo! The module I had built looked at lot like your example, but some part of it was obviously faulty... But not anymore! With your example it was pretty easy to grab the membership name and minimum_fee with a left join.

I have inserted the module below for other users, looking for similar solutions.

Code: [Select]
<?php
function civicrm_extratokens_civicrm_tokens( &$tokens ) {
    
$tokens['membership'] = array( 'membership.membership_type_id', 'membership.start_date', 'membership.name', 'membership.minimum_fee' );
}

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 contact_id,
       membership_type_id,
       start_date,
       name,
       minimum_fee
FROM   civicrm_membership             
LEFT JOIN civicrm_membership_type ON civicrm_membership.membership_type_id=civicrm_membership_type.id
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;
        
$value['membership.name'] = $dao->name;
        
$value['membership.minimum_fee'] = $dao->minimum_fee;
    }
}
« Last Edit: April 17, 2011, 11:35:54 am by resga »

theMusician

  • I post occasionally
  • **
  • Posts: 48
  • Karma: 3
  • CiviCRM version: 4.0 and 4.0.1
  • CMS version: Drupal 7.0
  • MySQL version: MySQL 5.x
  • PHP version: 5.2
Re: CiviMember details and Tokens (PDF templates)
April 18, 2011, 09:43:01 am
I do not mean to hijack this thread and I am very happy to see that resga has developed a solution. It appears that using the hook is not as straightforward when a table does not have contact_id as a field.

The structure of the fmlaLeaveOccurrence table that was created by civi for use in a Case lacks the contact_id field. It does have an entity_id field. From examining the schema, the entity_id is a foreign key related to the activity table id field. The source_contact_id foreign key within the activity table relates to the civicrm_contact.id field.

This means that I cannot simply pull the fields from my fmlaLeaveOccurence table and reference them with the id field from that table, instead I must tie the entity id field back to a contact id.

The dao seems to balk at the idea of using .field parameters so I do not know how to make it play nice with the id fields. I have tried several variations all with no luck.

My module fmla_hooks works properly, but obtaining the correct values for the array values does not.

Is there a proper way to include fields referenced inside of the dao object that use the . parameter? Such as civicrm_activity.source_contact_id

I am new to the dao design pattern and I apologize if this is a silly question.

For testing purposes this is the query that I would like to run with the dao object. It works properly within the mysql console when I replace $contactIDString with a value.

Code: [Select]
SELECT leave_type_14, request_receieved_15, civicrm_value_fmla_leave_occurence_2.id, civicrm_contact.id as ContactID, civicrm_case.id as Case_ID, display_name, civicrm_case_contact.case_id, civicrm_value_fmla_leave_occurence_2.entity_id, civicrm_activity.id as Activity_ID, civicrm_activity.source_contact_id as ActivitySource
FROM   civicrm_value_fmla_leave_occurence_2, civicrm_contact, civicrm_case, civicrm_case_contact, civicrm_activity
WHERE civicrm_value_fmla_leave_occurence_2.id = civicrm_contact.id
AND
civicrm_value_fmla_leave_occurence_2.entity_id = civicrm_activity.id
AND
civicrm_contact.id IN ($contactIDString)
GROUP BY civicrm_contact.id



Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: CiviMember details and Tokens (PDF templates)
April 18, 2011, 09:55:14 am

not sure i understand your question. might be easiest to jump on IRC and discuss there. will be a lot faster

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMember (Moderator: Deepak Srivastava) »
  • CiviMember details and Tokens (PDF templates)

This forum was archived on 2017-11-26.