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) »
  • Custom token ok in civimail but not in event registration confirmation mail
Pages: [1]

Author Topic: Custom token ok in civimail but not in event registration confirmation mail  (Read 403 times)

erwindeclerck

  • I’m new here
  • *
  • Posts: 12
  • Karma: 0
  • CiviCRM version: 4.4.9 + 4.5.2 + 4.5.4 + 4.5.5
  • CMS version: 7
  • MySQL version: 5.5.40
  • PHP version: 5.3.10-1ubuntu3.15
Custom token ok in civimail but not in event registration confirmation mail
January 28, 2015, 06:07:35 am
Hi

I wrote a little Drupal7 module to add a structured code to help tracing payments.
I use a mod97 checksum 11 digit number that I calculate from userid.
like 123/4567/890XX where xx is mod97 of 1234567890

The token works fine when I use it in a civimail.
It is however not recognized in the confirmation mail send to a subscriber of an event.
I use no context-limitation (for as much as I understand...)
My token is: {contact.structcode}

What am I doing wrong? Or is this a bug?

If someone has an idea or a better solution, all help is more than welcome;
Is setting a custom field by PHP code perhaps a better way?

Kind regards,
ERwin

Code: [Select]
/*
  /sites/all/modules/mycivitokens/mycivitokens.module
*/
<?php

function mycivitokens_civicrm_tokens(&$tokens) {
    
$tokens['contact']['contact.structcode'] = 'Gestructureerde code';
}

function 
mycivitokens_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
    foreach (
$cids as $cid) {
        
$values[$cid]['contact.structcode'] = structcode(''.$cid);
    }
}


function 
structcode($string){
    
$code  = fixlength($string);
    
$code .= mod97($string);
    
$structcode = substr($code,0,3)."-".substr($code,3,4)."-".substr($code,7,5);
    return 
$structcode;
}

function 
mod97($nr){
    
$temp = "";
    
$deler = 97;
    
$i = 0;

    while (
$i < strlen($nr))
    {
        do
        {
            
$temp.=$nr{$i++};
            
$erg = (int)$temp % $deler;
        } while ( 
$erg == (int)$temp && $i < strlen($nr) );

        
$temp = $erg;
    }
    return 
fixlength($temp,2);
}

function 
fixlength($nr,$target=10){

    
$length = strlen($nr);
    
$i = 0;
    while(
$i<$target-$length){
        
$nr = "0".$nr;
        
$i++;
    }
    return 
$nr;
}


/*
/sites/all/modules/mycivitokens/mycivitokens.info
*/

name = My civitokens module
description 
= Create extra tokens for civicrm.
dependencies[] = civicrm
package 
= CiviCRM
core 
= 7.x

version 
= "7.x-0.1"
« Last Edit: January 28, 2015, 06:32:34 am by erwindeclerck »

erwindeclerck

  • I’m new here
  • *
  • Posts: 12
  • Karma: 0
  • CiviCRM version: 4.4.9 + 4.5.2 + 4.5.4 + 4.5.5
  • CMS version: 7
  • MySQL version: 5.5.40
  • PHP version: 5.3.10-1ubuntu3.15
Re: Custom token ok in civimail but not in event registration confirmation mail
January 28, 2015, 12:50:28 pm
Okay, I tried the other approach and I was more successfull.

I used hook_civicrm_custom in stead of tokens. Custom fields are available as tokens anyway.
I created a custom field (read-only) and when I update a contact-record this field nicely gets updated. But when I create a new contact by subscribing with a new name/emailaddress, this hook isn't called.

As far as I understand the creation of a new contact is done in the public static function updateContactFields in /sites/all/modules/civicrm/CRM/Event/Form/Registration/Confirm.php line 996 or
482: $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields, $this);

How can I force civi to pass via my hook?

Thanks for any help!

Kind regards,
Erwin



Code: [Select]
<?php

function mycivitokens_civicrm_custom( $op, $groupID, $entityID, &$params )
{    

//  we are either creating or editing a row
    
if ($op == 'create' || $op == 'edit' ) {
        if (
$groupID == 1) {    //  "Contact extra" In my case: the name of the (first) group of custom fields 
            
$tableName = "civicrm_value_contact_extra_1";
            
$mystructcode = structcode('' . $entityID);
            
$sql = "update $tableName set gestructureerde_code_6 = '$mystructcode' WHERE entity_id = $entityID";
            
$dao = CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
            if (!
$dao->fetch()) {  //  just for debugging
                
$stuff = array(
                    
'op' => $op,
                    
'groupID' => $groupID,
                    
'entityID' => $entityID,
                    
'params' => &$params,
                    
'tableName' => $tableName,
                    
'sql' => $sql,
                    
'dao' => $dao,
                    
'struct' => $mystructcode
                
);
                
drupal_set_message(t('<pre>data:.').print_r($stuff,true).'</pre>', 'error'); //  just for debugging
                
return;
            }
        }
    }
}

jaapjansma

  • I post frequently
  • ***
  • Posts: 247
  • Karma: 9
    • CiviCoop
  • CiviCRM version: 4.4.2
  • CMS version: Drupal 7
  • MySQL version: 5
  • PHP version: 5.4
Re: Custom token ok in civimail but not in event registration confirmation mail
January 29, 2015, 01:04:27 am
Hey Erwin,

There is something strange with the token hook. It is the parameters $tokens. Sometimes the token your are looking for is as a key and othertimes it as a value of the array.
E.g. my check if a token is used consist of the following code, Pay attention to the if and the check key_exist and isset:
Code: [Select]
if (in_array('afdeling_naam', $tokens['sp']) || array_key_exists('afdeling_naam', $tokens['sp'])) {
  $this->afdeling_naam($values, $cids, $job, $tokens, $context);
}
Developer at Edeveloper / CiviCoop

erwindeclerck

  • I’m new here
  • *
  • Posts: 12
  • Karma: 0
  • CiviCRM version: 4.4.9 + 4.5.2 + 4.5.4 + 4.5.5
  • CMS version: 7
  • MySQL version: 5.5.40
  • PHP version: 5.3.10-1ubuntu3.15
Re: Custom token ok in civimail but not in event registration confirmation mail
February 06, 2015, 12:47:36 am
Hey Jaap,

A bit late, but thx!

I find the combination of template-files, nested with template-chunks injected via forms, tokens and php-code very hard to understand. Especially when working in different contexts...
Civi is a great solution, but as with many 'organic' products, not completely spaghetti-free...

Erwin

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Custom token ok in civimail but not in event registration confirmation mail

This forum was archived on 2017-11-26.