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) »
  • Token to pull in Address of Current Employer with custom fields
Pages: [1]

Author Topic: Token to pull in Address of Current Employer with custom fields  (Read 494 times)

linkx

  • I post occasionally
  • **
  • Posts: 86
  • Karma: 7
Token to pull in Address of Current Employer with custom fields
October 01, 2014, 09:49:22 am
My use-case requires that printed mailing labels addressed to individuals should use the address of the individual's employer rather than the address attached to the individual contact record. If anyone knows of existing tokens to achieve this, stop reading now and reply with that info.

I wrote a Drupal module and used the token hooks to define a custom token. It shows up in the insert token GUI and I can get it to insert data when my PHP fills
Code: [Select]
$values[$cid]['employer.address'] with a static value (for testing). So all of that is working. However, when I try to get it to work dynamically as intended, using the following code, it does not insert values and the token does not work at all:

Code: [Select]
<?php
function mtx_crm_civicrm_tokens(&$tokens) {
  
$tokens['employer'] = array(
    
'employer.address' => 'Employer Address',
  );
}

function 
mtx_crm_civicrm_tokenValues(&$values, $cids, $job = null, $tokens = array(), $context = null) {
  
$contacts = implode(',', $cids);
     
$dao = &CRM_Core_DAO::executeQuery("
        SELECT civicrm_contact.id, street_address as employer_address
        FROM civicrm_contact 
        INNER JOIN civicrm_address ON civicrm_contact.employer_id=civicrm_address.contact_id 
        WHERE is_primary=1 AND civicrm_contact.id IN (
$contacts)
      "
);
     while (
$dao->fetch()) {
        
$cid = $dao->civicrm_contact.id;
        
$values[$cid]['employer.address'] = $dao->employer_address;       
      }

}


I ran the query from the MySQL command prompt and it works as intended. [Note: right now I am just pulling street_address to get going - I realize that later I will need to pull all address fields and format them before inserting into the token.]
Any ideas or tips are greatly appreciated. Thanks!
« Last Edit: October 02, 2014, 07:39:29 am by linkx »

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom token to pull in Address of Current Employer - help with PHP
October 01, 2014, 09:53:29 am
I think the "standard" way to do this in Civi would be to have employees share an address with employers using the "shared address" feature. Then the employee's "work address" would be available in standard tokens.... oh wait a minute, do tokens only support primary locations? In that case nevermind you do need custom code.
Try asking your question on the new CiviCRM help site.

linkx

  • I post occasionally
  • **
  • Posts: 86
  • Karma: 7
Re: Custom token to pull in Address of Current Employer - help with PHP
October 01, 2014, 10:20:46 am
Thanks Coleman!

I overlooked the shared address feature. This would work better than what I am trying to achieve with my hook.

Questions:

  • What is the best way to update 2,000 contacts so that their "Work" address is shared with their Current Employer?
  • Since the mailing label creation screen allows you to select from address types (Home, Work, Main, etc.) I assume that tokens work with this. I always used to select "Main" and it worked (all of my addresses are set to "Main"). However, after upgrading to 4.5 I can only use "Primary" - all others result in a blank PDF. Is this a bug?

Thanks for your tip, this is exactly why I said "stop reading now and post with that info"

Link

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: Custom token to pull in Address of Current Employer - help with PHP
October 01, 2014, 11:57:58 am
1) There's no easy way that I know of
2) Probably a bug

So back to your original code, which is probably a good way of doing things since you have some coding skills. I'm finding your query a little hard to read, possibly just because I'm tired right now. But have you tried firing that query off in phpMyAdmin just to make sure it works?
Try asking your question on the new CiviCRM help site.

linkx

  • I post occasionally
  • **
  • Posts: 86
  • Karma: 7
Re: Custom token to pull in Address of Current Employer - help with PHP
October 01, 2014, 01:04:33 pm
I did try the query at the MySQL prompt and it returns cid and the employer's street address, so it's good there.

Problem is somewhere in the process of getting data into $values[$cid]['employer.address']

linkx

  • I post occasionally
  • **
  • Posts: 86
  • Karma: 7
Re: Custom token to pull in Address of Current Employer - help with PHP
October 02, 2014, 07:39:01 am
Your suggestion to use shared addresses is working brilliantly for us and solving other issues as well, so thanks for that tip!

The last item on my quest is to show custom address fields on my mailing labels. It seems that when Shared Address is used, any custom address field tokens do not work. See my separate topic on that: http://forum.civicrm.org/index.php/topic,34367.msg145781.html


linkx

  • I post occasionally
  • **
  • Posts: 86
  • Karma: 7
Re: Token to pull in Address of Current Employer with custom fields
October 03, 2014, 09:32:26 am
@Coleman RE:

Quote
What is the best way to update 2,000 contacts so that their "Work" address is shared with their Current Employer?

Quote
There's no easy way that I know of

Found a solid method to mass update all contacts to share "work" address with Current Employer address:

http://forum.civicrm.org/index.php/topic,30353.0.html

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Token to pull in Address of Current Employer with custom fields

This forum was archived on 2017-11-26.