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 (Moderator: Donald Lobo) »
  • activity tokens via civicrm_tokens()
Pages: [1]

Author Topic: activity tokens via civicrm_tokens()  (Read 1366 times)

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
activity tokens via civicrm_tokens()
April 11, 2011, 04:06:42 pm
Hi all,

I am working on a proof of concept for using civiCRM as our Family Medical Leave Act tracking tool. One important component is the ability to send letters to employees based on the type of leave they are taking. Using Smarty conditional statements will allow me to do that if I can access the appropriate tokens. I have been following the information about http://en.flossmanuals.net/civicrm/ch067_hooks hooks provided in the FLOSS manual.

However, the comment in the implementation of civicrm_tokenValues() states that you should use the API not the database to get the values of each token. I must be missing something regarding how the API accesses the array.

Code: [Select]
/**
 * @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( &$details, &$contactIDs ) {
  if ( is_array( $contactIDs ) ) {
         $contactIDString = implode( ',', array_values( $contactIDs ) );
         $single = false;
    } else {
        $contactIDString = "( $contactIDs )";
        $single = true;
    }

// set the token's value
  $value['activities.custom14'] = $single;
  $value['activities.custom15'] = $single;

}

The above code makes the tokens appear when creating letters but the values are blank. I hope I am not to far off base but I am confused about how to step through the array and assign the appropriate values to each token. Any guidance would be appreciated.

Thank you
« Last Edit: April 11, 2011, 04:34:54 pm by theMusician »

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: activity tokens via civicrm_tokens()
April 11, 2011, 07:11:36 pm

check civitest.module.sample in your distribution and check the implementation of:

function civitest_civicrm_tokens
function civitest_civicrm_tokenValues

there are two hooks u need to provide

can u specify where in the comments it states : "you should use the API not the database to get the values of each token"

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

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: activity tokens via civicrm_tokens()
April 12, 2011, 09:18:32 am
Hi Lobo,

Thank you for pointing me towards civitest.module.sample. I am a little closer now, though still not getting values. I am trying to pull them from a table created by civi based on a case. The example in civitest uses contact_id but the table that was created only contains entity_id. Eventually I will get it right and voila, token values will appear in the pdf letter.

Regarding the comments, in the FLOSS manual, http://en.flossmanuals.net/civicrm/ch067_hooks within the Custom Mail Merge Token section the second block of comments contains the statement,
Quote
#"now we'll set the value of our custom token;
# it's better in general to use the API rather than SQL queries to retrieve data,
# but in this case the MAX() function makes it very efficient to get the largest
# contribution, so let's make an exception

It appears we use queries though, so my original comment may be superfluous.


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: activity tokens via civicrm_tokens()
April 18, 2011, 04:19:38 pm
Okay. Just about there.

The following is the query that generates proper results whenever I substitute a numeric value for $contactIDString in a mysql console. An example would be in the query below replacing $contactIDString with the number 4.

Code: [Select]
SELECT leave_type_14, request_receieved_15, civicrm_contact.id, entity_id, civicrm_activity.id, civicrm_activity.source_contact_id as contact_id
FROM   civicrm_value_fmla_leave_occurence_2, civicrm_contact, civicrm_activity
WHERE entity_id = civicrm_activity.id
AND  civicrm_activity.source_contact_id  IN ($contactIDString)
AND civicrm_contact.id IN ($contactIDString)
GROUP BY contact_id

When I run this query inside of my module I never get a proper value returned. If I remove the WHERE clause I can retrieve the leave_type_14 values, though they are not tied to the proper person. I know the module is working but when I specify a contact_id the whole thing refuses to output a value. Is there something obvious that I am missing? It seems like the $contactIDString array does not actually contain numbers. I have output dao queries with the debug line define( 'CIVICRM_DAO_DEBUG', 1 ); and can only find one reference to contact.id.

Thank you.

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: activity tokens via civicrm_tokens()
April 19, 2011, 01:48:15 pm
Moving forward with the token hooks I have found that just one of the custom data fields I am calling is hanging up the processing of the hooks.

The code has now morphed into the following:

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['fmla'] = array(
      
'fmla.custom14' => 'Leave Type',
      
'fmla.custom15' => 'Request Received',
  'fmla.ContactID' => 'contact_id'
      
);
}

/**
 * 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 a.source_contact_id AS contact_id, fmla.leave_type_14 AS leave_type_14, fmla.request_receieved_15 AS request_receieved_15
FROM  civicrm_value_fmla_leave_occurence_2 AS fmla
INNER JOIN civicrm_activity AS a ON fmla.entity_id = a.id
WHERE  a.source_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['fmla.custom14'] = $dao->leave_type_14;
        
$value['fmla.custom15'] = $dao->request_receieved_15;
$value['fmla.ContactID'] = $dao->contact_id;
    }
}

It seems that
Code: [Select]
INNER JOIN civicrm_activity AS a ON fmla.entity_id = a.id somehow makes the leave_type_14 not output. When the leave_type_14 field does not write out it stops the pdf from being created properly. Simply removing the inner join and adding
Code: [Select]
civicrm_activity AS a to the FROM statement allows the pdf to be created properly. Without the inner join the values do not match the contact.

I am outputting the tokens like this in the Print PDF Letter box:

Contact ID: {fmla.ContactID}

Request Recieved: {fmla.custom15}

Leave Type: {fmla.custom14}

This text should appear.


This creates a PDF that prints the contact id, the request received number, but when {fmla.custom14} is encountered nothing appears.

Any idea why a comparison between the entity_id column and the activity id column would stop the output of fmla.custom14?

These are the relevant fields from my fmla_leave_occurence_2 table. Yes, I know that received is spelled wrong in the field name.

Name                        |    Type      |          Length

entity_id                         int                        10   
leave_type_14                 varchar                128   
request_receieved_15      tinyint                        4   

Thanks for any ideas.

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: activity tokens via civicrm_tokens()
April 28, 2011, 03:46:02 pm
This is a print_r dump of the values resulting from my custom token.

$dao:

 CRM_Core_DAO Object
(
    [_DB_DataObject_version] => 1.8.12
    [__table] =>
    [N] => 2
    [_database_dsn] =>
    [_database_dsn_md5] => c4186d02d3c74a6b9955e4e689bbc713
    [_database] => fa_civicrm
    [_query] =>
    [_DB_resultid] => 32
    [_resultFields] =>
    [_link_loaded] =>
    [_join] =>
    [_lastError] =>
    [contact_id] => 3
    [sort_name] => Bronsema, Max
    [case_type_label] => Health Condition For Employee SHCE
    [case_type_id] => 3
    [case_id] => 19
)

What are the the little square boxes surrounding my case_type_id? The value will not print in the pdf though it will send in an e-mail. The case type appears to be stored properly in the database.

Any ideas?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: activity tokens via civicrm_tokens()
April 28, 2011, 04:15:54 pm
Hi,

I *think* that had you used the api to get the tokens - ie.

require_once api/api.php;
civicrm_api('Activity','Get',array('version' => 3, 'contact_id' = x));

it might have stripped the separator for you - not 100% sure but you seem to have replicated the api in your code. (The reason the floss manual recommends using the api is that when the db changes the api should still work so it should be more future proof, also the api-team are more likely to jump in & help if you are using it)

Anyway, the character is something like cntrl-a or hex 001.

Something like

                $field = explode(CRM_Core_DAO::VALUE_SEPARATOR,
                                       substr($field,1,-1));

should work - with the right require once maybe
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

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: activity tokens via civicrm_tokens()
April 29, 2011, 08:56:40 am
 Thanks for the reply Eileen,

Using the API would be great in the future. I currently cannot find any api call to retrieve items within the civicrm table option_value. The token retrieval method I used was based upon the last example in the FLOSS manual which uses a SQL query.

Anyhow, I will work on using the API and your smart idea about removing the contents surrounding the value.

Thanks again for the tips.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: activity tokens via civicrm_tokens()
April 29, 2011, 05:30:36 pm
As long as you are using API v3 there is an API for optionvalues

http://svn.civicrm.org/civicrm/branches/v3.4/api/v3/examples/OptionValueGet.php

Constant Get in api v3 also returns a swag of things that might be of use - if you call

civicrm_api('Constant','getfields',array('version' => 3);

it will return an array of the things you can use it to GET
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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • activity tokens via civicrm_tokens()

This forum was archived on 2017-11-26.