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) »
  • placing api calls in civicrm_tokenValues - is this dangerous?
Pages: [1]

Author Topic: placing api calls in civicrm_tokenValues - is this dangerous?  (Read 1089 times)

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
placing api calls in civicrm_tokenValues - is this dangerous?
April 20, 2012, 08:48:04 am
Hi,

I'm using the tokenValues hook customize our use of nick_name. Since not every contact has a nick_name , we want to use first_name if nick_name is missing; and if there's no first_name either, then use a greeting such as 'Friend'. Below is the code to do this. I had to look up the contact['first_name'] for each contact using the api because, if the token {contact.first_name} was missing from the email or label, then the $value['first_name'] token is empty.

What I am concerned is- is this a bad idea? will the thousands of api calls when doing a mass mailing become a problem? I have used this only once so far, to generate a few dozen mailing labels, which is a very small sample.

If it does seem safe and not likely to overload a server, I can add it to the bottom of this page: http://civicrm.org/node/637

Code: [Select]
function custom_civicrm_tokenValues( &$values, &$contactIDs ) {
    if ( is_array( $contactIDs ) ) {
        $single = false;
    } else {
        $contactIDs = array( $contactIDs );
        $single = true;
    }
        foreach ( $contactIDs as $cid ) {
        if ( $single ) {
            $value =& $values;
        } else {
            $value =& $values[$cid];
        }
            if ( !isset( $value['nick_name'] ) || empty( $value['nick_name']) || ($value['nick_name'] == '') || ( $value['nick_name'] == 'NULL' ) ) {
                   // $value['nick_name'] = $value['first_name'];
                 $params = array( 'id' => $cid , 'sequential' => 1 , 'version' =>'3' , 'return' => 'first_name' ); 
                 $results = civicrm_api('Contact', 'getsingle', $params);
                 $value['nick_name'] = $results['first_name'];
                 //var_dump($results['values'[0]['first_name']);
            }
            if ( ($value['nick_name']  == '?') || ($value['nick_name']  == '_') || ($value['nick_name']  == '') ) {
                $value['nick_name']  = "Friend";
            }
    }
}
« Last Edit: April 23, 2012, 10:29:33 am by jimurl »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: placing api calls in civicrm_tokenValues - is this dangerous?
April 20, 2012, 09:08:28 am
Hi,

1) There is now a new param on the hook showing what params are actually requested. this avoid generating a token when not used.
2) always specify the return fields with only the fields you want (first,nick) to avoid fetching email/address for nothing

Otherwise, it's running one query per contact, this obviously isn't super good but should be ok. The API doesn't handle yet a list of contact ids as a param, but the BAO under it does, so it shouldn't be super complicated to modify. Would be great if you could help on that.

Finally on the code:
you don't have to add the require api/api.php
99% sure you don't get to civicrm_initialize in this case (plenty initialised before already when generating a token)
you could have used the getsingle method instead of the get, so that's $result['nick_name]



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

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Re: placing api calls in civicrm_tokenValues - is this dangerous?
April 23, 2012, 10:36:48 am
Hi Xavier,

I updated my code above to reflect your suggestions. Yes, all of those should cut down on processing. I can look at the API and see if I have the skills to allow it to accept a list of $cids. Thanks.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: placing api calls in civicrm_tokenValues - is this dangerous?
April 23, 2012, 10:58:16 am
Hi,

Feel free to join us on IRC if you want to discuss implementation or need some help

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

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: placing api calls in civicrm_tokenValues - is this dangerous?
April 24, 2012, 11:55:16 pm
Great if you can contribute!!!
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

jimurl

  • I post occasionally
  • **
  • Posts: 70
  • Karma: 0
  • CiviCRM version: 3.4.6
  • CMS version: drupal 6.22
  • MySQL version: 5+
  • PHP version: 5+
Re: placing api calls in civicrm_tokenValues - is this dangerous?
June 25, 2012, 09:25:54 am
For those thinking to try my suggestion above, I recommend a better 'recipe', described by Coleman Watts in a blog post called "Create Your Own Tokens for Fun and Profit":
http://civicrm.org/blogs/colemanw/create-your-own-tokens-fun-and-profit

It describes a bit better of a way, using the DAO, rather than the api. The benefits of the other method are summarized in a statement 1/3 of the way through:

Quote
You'll also notice that we were able to process all the contacts in bulk with a single query - much faster than calling the api once per contact!
.


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: placing api calls in civicrm_tokenValues - is this dangerous?
June 25, 2012, 09:58:33 am
@jimurl,

Agree, better. The best would be that the api is able to handle a list of ids beside a single id. Would avoid that your token brakes if the underlying DAO does.

Do you think you could make it happen?

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

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • placing api calls in civicrm_tokenValues - is this dangerous?

This forum was archived on 2017-11-26.