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 CiviMail (Moderator: Piotr Szotkowski) »
  • CiviMail token hook trickory
Pages: [1]

Author Topic: CiviMail token hook trickory  (Read 3245 times)

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
CiviMail token hook trickory
May 18, 2010, 07:32:50 am
My goal is to pass in drupal node content as a token. I've already gotten the correct db query ready to go, and the correct formatting output ready to go, but I'm having a hint of trouble getting my test value of the token to work.

First, I'm creating a new token
Code: [Select]
//FUNCTION FOR THE TOKEN HOOK (hook_civicrm_tokens)
function civitoken_civicrm_tokens( &$tokens ){
$tokens['bulletins'] = array('bulletins.today');
}

Then, I'm setting the value of that new token. Since my value is not related to the contact in any way, it appears that I can ignore the $contactIDs values. I've stuck commented placeholders in this code to show you where things will eventually go.
Code: [Select]
//FUNCTION FOR THE TOKEN VALUE
function civitoken_civicrm_tokenValues( &$details, &$contactIDs ) {

//skipping the contact IDs array manip, is this my problem?

//db query
//execute query
//format results in plain html
//set details['bulletins.today'] = format results

//i'm testing setting a value of just a regular string
$details['bulletins.today'] = 'testing';

}

When I set $details['bulletins.today'] = 'testing'; and fire off a test email, the {bulletins.today} token doesn't display its 'testing' value. Am I setting its value incorrectly?
« Last Edit: May 18, 2010, 07:34:37 am by kurosevic »

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: CiviMail token hook trickory
May 18, 2010, 08:43:55 am

check the example in drupal/civitest.module.sample in your distribution

u basically need to do for each contactID

$details[$contactID]['bulletins.today'] = 'testing';

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

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Re: CiviMail token hook trickory
May 19, 2010, 12:07:20 am
Thanks for the suggestion lobo. I've made considerable headway. i've essentially got a drupal view outputting in an email using a token. Its pretty simple actually. And if i get any free time after this project is finished, I may take a stab at trying to make a module that automatically generates a civimail token for every drupal view you've saved.

It would have to work according to some steps that would perhaps go this way:
1. get all existing views in your drupal site, store them in an array
2. create a token name entry per views array item (using the token hook)
3. set each generated token value equal to the return result of the view's db query. (using the token hookValues)

anyway.. i'm still not done fixing up my own thing yet. i'm not quite sure which part of this query i actually need to make it work. It seems like the $dao->contact_id is the important part. I actually did go along to the example in the file you referred to... its just not very well commented, and its not clear what exactly is happening in the tokenvalues function in general.

Code: [Select]
...

$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['bulletins.today'] = $output;  //$output is referring to a separate query, which gathers node content
    }
« Last Edit: May 19, 2010, 12:09:27 am by kurosevic »

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: CiviMail token hook trickory
May 19, 2010, 09:48:55 am

hey kurosevic:

wanna get on IRC and we can chat realtime

i'm not sure i understand your message or where you are stuck. We might also be able to extend a few things to make things even easier for your use case:

CiviMail has 2 types of tokens: Global Tokens (domain.* are global tokens) and Contact ID tokens (all the other tokens). The current token hooks basically are the latter, we can also expose the former and that will allow folks to inject global content into an email

lets chat more on IRC

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

kurosevic

  • I’m new here
  • *
  • Posts: 25
  • Karma: 1
Re: CiviMail token hook trickory
May 19, 2010, 02:51:57 pm
oh my gosh. use of regular tokens would be primo in this case, because i need to minimize query bandwidth. i can imagine running 10,000 emails of some node content would get expensive really fast.

i'll get on IRC and wait for you for a bit.
« Last Edit: May 19, 2010, 03:16:27 pm by kurosevic »

wnpc

  • I’m new here
  • *
  • Posts: 19
  • Karma: 1
Re: CiviMail token hook trickory
August 18, 2010, 04:54:57 am
Hi,

just found this thread, and my email tokens which were working fine have suddenly stopped, but my real comment is that I'd love to know how to get the content a node into domain level tokens which I can use as the content in an email as it's exactly what I'm trying to achieve and am currently executing the query for each of the contacts that gets the email.

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: CiviMail token hook trickory
August 18, 2010, 12:49:40 pm

u'll need to add some debug code to figure out why your tokens just stopped working

why dont u store the contents of the node in a PHP static variable and then just reuse that value for every contact rather than repeating the same query?

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

wnpc

  • I’m new here
  • *
  • Posts: 19
  • Karma: 1
Re: CiviMail token hook trickory
August 18, 2010, 01:33:47 pm
I did some debugging and the query is being executed but the tokens don't seem to be being replaced. I'm not sure where to look next.

I'm happy to use variables instead it honestly just didn't occur to me! Excuse the ignorance but where do I define the variables?

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: CiviMail token hook trickory
August 18, 2010, 02:06:07 pm

1. can u cut-n-paste your hook code. both the token and tokenValues hook please

2. thats more a php question, check: http://www.php.net/manual/en/

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

wnpc

  • I’m new here
  • *
  • Posts: 19
  • Karma: 1
Re: CiviMail token hook trickory
August 18, 2010, 03:59:31 pm
Included the code now, and also realised that my last question was slightly less clear, I was wondering more where in the civicrm files to define variables rather than how to do it.

Cheers

function wnpc_civicrm_tokens( &$tokens ) {
    $tokens['mailing'] = array('mailing.cur_date', 'mailing.order_date');
    $tokens['member'] = array('member.start_date', 'member.end_date', 'member.new_start_date', 'member.new_end_date', 'member.member_type', 'member.fee');
    $tokens['fee'] = array('fee.fm1', 'fee.fm2', 'fee.csc1');
    $tokens['param'] = array('param.date', 'param.coordinator_name', 'param.coordinator_address', 'param.coordinator_line_address',
                             'param.coordinator_phone', 'param.coordinator_email', 'param.account_number',
                             'param.freepost', 'param.address',
                             'param.sales_name', 'param.sales_phone',
                             'param.kh_lib_name', 'param.ki_lib_name',
                             'param.hire_name', 'param.hire_phone',
                             'param.cbev_name', 'param.cbev_phone');
    $tokens['include'] = array('include.committee_contacts', 'include.email', 'include.path');
    $tokens['style'] = array('style.a_style', 'style.h2_style', 'style.h3_style');
}

function wnpc_civicrm_tokenValues( &$values, &$contactIDs ) {
    // Get the data for the equivalent of views_embed_view('brief_contacts')
    $view = get_contact_data();

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

    $queryNode = "
select n.nid, n.title, v.body
           from dr_node n
           inner join dr_term_node tn on tn.nid = n.nid
           inner join dr_node_revisions v on n.nid = v.nid and n.vid = v.vid
           where (n.title like date_format(curdate(), '%%%b%%%e%%%Y%%') or
       n.title like date_format(curdate(), '%%%e%%%b%%%Y%%') or
       n.title like date_format(date_add(curdate(), interval -1 day), '%%%b%%%e%%%Y%%') or
       n.title like date_format(date_add(curdate(), interval -1 day), '%%%e%%%b%%%Y%%'))
  and n.type = 'blog'
  and tn.tid = 1 ";

    $daoNode = CRM_Core_DAO::executeQuery( $queryNode );
    while ( $daoNode->fetch( ) ) {
      $include_path = 'node/' . $daoNode->nid;
      $include_email = str_replace(array('<h2', '<h3', '<a','href="/','src="/','<div>', '</div>'),
                                              array('<h2 ' . $value['style.h2_style'],
                                                    '<h3 ' . $value['style.h3_style'],
                                                    '<a ' . $value['style.a_style'],
                                                    'href="http://www.wnpc.org.nz/',
                                                    'src="http://www.wnpc.org.nz/'),
                                              $daoNode->body);
       
/*       
        print "contact tokens: <pre>";
        print_r( $value );
        print "</pre>";
*/       
    }
   
    $query = "
select m.contact_id,
       case t.duration_unit
         when 'year' then
           date_format(date_add(date_add(m.end_date, interval -1 year), interval 1 day), '%d %b %Y')
         else date_format(m.start_date, '%d %b %Y')
       end as start_date,
       date_format(date_add(m.end_date, interval 1 day), '%d %b %Y') as new_start_date,
       date_format(m.end_date, '%d %b %Y') as end_date,
       date_format(date_add(m.end_date, interval 1 year), '%d %b %Y') as new_end_date,
       t.name,
       t.minimum_fee,
       f.fm1,
       f.fm2,
       f.csc1,
       date_format(curdate(), '%d %b %Y') as dt,
       date_format(curdate(), '%Y%m%d') as dt_order,
       coord.email coord_email,
       coord.first_name coord_name,
       coord.address coord_address,
       coord.line_address coord_line_address,
       coord.phone coord_phone,
       ki_lib.first_name ki_lib_name,
       kh_lib.first_name kh_lib_name,
       sales.first_name sales_name,
       sales.phone sales_phone,
       hire.first_name hire_name,
       hire.phone hire_phone,
       cbev.first_name cbev_name,
       cbev.phone cbev_phone,
       co.source,
       co.legal_name,
              concat(coa.street_address, '<br/>',
                     ifnull(concat(coa.supplemental_address_1, '<br/>'),''),
                     ifnull(concat(coa.supplemental_address_2, '<br/>'), ''),
                     coa.city, ' ', ifnull(coa.postal_code,'')) co_address       
from civicrm_membership m
inner join civicrm_membership_type t on m.membership_type_id = t.id
inner join (select
              sum(case id when 1 then minimum_fee else 0 end) fm1,
              sum(case id when 6 then minimum_fee else 0 end) fm2,
              sum(case id when 4 then minimum_fee else 0 end) csc1
            from civicrm_membership_type) f
inner join civicrm_contact co on co.id = 18
inner join civicrm_address coa on co.id = coa.contact_id
inner join (select
              ce.email, cc.first_name, cp.phone,
              concat(ca.street_address, '<br/>',
                     ifnull(concat(ca.supplemental_address_1, '<br/>'),''),
                     ifnull(concat(ca.supplemental_address_2, '<br/>'), ''),
                     ca.city, ' ', ca.postal_code) address,
              concat(ca.street_address, ', ',
                     ifnull(concat(ca.supplemental_address_1, ', '),''),
                     ifnull(concat(ca.supplemental_address_2, ', '), ''),
                     ca.city, ' ', ca.postal_code) line_address
            from civicrm_contact cc
            inner join civicrm_address ca on cc.primary_contact_id = ca.contact_id
            inner join civicrm_email ce on ce.contact_id = 33
            inner join civicrm_phone cp on cp.contact_id = cc.primary_contact_id
            where ce.is_primary = 1
              and cp.is_primary = 1
              and cc.id = (select case when contact_id_a = 33 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 33 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) coord
inner join (select cc.first_name
            from civicrm_contact cc
            where cc.id = (select case when contact_id_a = 37 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 37 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) ki_lib
inner join (select cc.first_name
            from civicrm_contact cc
            where cc.id = (select case when contact_id_a = 38 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 38 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) kh_lib
inner join (select cc.first_name, cp.phone
            from civicrm_contact cc
            inner join civicrm_phone cp on cp.contact_id = cc.primary_contact_id
            where cp.is_primary = 1
              and cc.id = (select case when contact_id_a = 43 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 43 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) sales
inner join (select cc.first_name, cp.phone
            from civicrm_contact cc
            inner join civicrm_phone cp on cp.contact_id = cc.primary_contact_id
            where cp.is_primary = 1
              and cc.id = (select case when contact_id_a = 44 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 44 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) hire
inner join (select cc.first_name, cp.phone
            from civicrm_contact cc
            inner join civicrm_phone cp on cp.contact_id = cc.primary_contact_id
            where cp.is_primary = 1
              and cc.id = (select case when contact_id_a = 52 then contact_id_b else contact_id_a end contact_id
                           from civicrm_relationship
                           where 52 in (contact_id_a, contact_id_b)
                             and (end_date is null or end_date >= now())
                           limit 0,1)) cbev
where m.contact_id in ( $contactIDString )
  and is_test = 0";

    $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['member.start_date'] = $dao->start_date;
        $value['member.end_date'] = $dao->end_date;
        $value['member.new_start_date'] = $dao->new_start_date;
        $value['member.new_end_date'] = $dao->new_end_date;
        $value['member.member_type'] = $dao->name;
        $value['member.fee'] = '$' . $dao->minimum_fee;
        $value['fee.fm1'] = '$' . $dao->fm1;
        $value['fee.fm2'] = '$' . $dao->fm2;
        $value['fee.csc1'] = '$' . $dao->csc1;
        $value['param.date'] = $dao->dt;
        $value['param.coordinator_name'] = $dao->coord_name;
        $value['param.coordinator_address'] = $dao->coord_address;
        $value['param.coordinator_line_address'] = $dao->coord_line_address;
        $value['param.coordinator_phone'] = $dao->coord_phone;
        $value['param.coordinator_email'] = $dao->coord_email;
        $value['param.ki_lib_name'] = $dao->ki_lib_name;
        $value['param.kh_lib_name'] = $dao->kh_lib_name;
        $value['param.hire_name'] = $dao->hire_name;
        $value['param.hire_phone'] = $dao->hire_phone;
        $value['param.sales_name'] = $dao->sales_name;
        $value['param.sales_phone'] = $dao->sales_phone;
        $value['param.cbev_name'] = $dao->cbev_name;
        $value['param.cbev_phone'] = $dao->cbev_phone;
        $value['param.account_number'] = $dao->legal_name;
        $value['param.freepost'] = $dao->source;
        $value['param.address'] = $dao->co_address;
        $value['mailing.cur_date'] = $dao->dt;
        $value['mailing.order_date'] = $dao->dt_order;

        $value['style.h2_style'] = 'style="font-size:167%; color:#7c6fa7; font-weight:normal; margin-top:0; padding-top:0; padding-bottom:0; margin-bottom:20px;"';
        $value['style.h3_style'] = 'style="color:#cbad01; font-family:Tahoma; font-size:14px; font-weight:bold"';
        $value['style.a_style'] = 'style="color:#2a1b60"';
        $value['include.path'] = $include_path;
        $value['include.email'] = $include_email;
       
        $value['include.committee_contacts'] = str_replace('<h3>', '<h3 ' . $value['style.h3_style'] . '>',
                                                           get_contact_data());

    }


}

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: CiviMail token hook trickory
August 18, 2010, 08:36:35 pm

what version of civicrm were they working and what version did they stop working?

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

wnpc

  • I’m new here
  • *
  • Posts: 19
  • Karma: 1
Re: CiviMail token hook trickory
August 18, 2010, 09:13:38 pm
I've just upgraded to most recent version which is 3.2.1 and its still not working. they were both working and not on my previous version which was 3.1.3. Nothing overt changed on the site between when they were working and when they weren't
« Last Edit: August 18, 2010, 11:50:55 pm by wnpc »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviMail (Moderator: Piotr Szotkowski) »
  • CiviMail token hook trickory

This forum was archived on 2017-11-26.