-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcivicrm_token.module
171 lines (147 loc) · 4.72 KB
/
civicrm_token.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
// $Id: $
/**
* @file
* The CiviCRM Token module.
*
* The CiviCRM Token module provides tokens for CiviCRM Contact object.
*
* @ingroup civicrm
*/
/**
* Implementation of hook_token_list().
*/
function civicrm_token_token_list($type = 'all') {
$tokens = array();
if ($type == 'contact' || $type == 'all') {
$civicrm_root = drupal_get_path('module', 'civicrm') . '/..';
require_once $civicrm_root . '/civicrm.config.php';
require_once $civicrm_root . '/CRM/Core/Config.php';
require_once $civicrm_root . '/api/v2/Contact.php';
// Contact tokens
$contactFields =& CRM_Contact_DAO_Contact::fields( );
foreach ($contactFields as $field) {
if ($field['export']) {
$tokens['contact'][$field['name']] = $field['title'];
}
}
// Address tokens
$addressFields =& CRM_Core_DAO_Address::fields( );
foreach ($addressFields as $field) {
if ($field['export']) {
$tokens['contact']['address_'.$field['name']] = $field['title'];
}
}
// Email tokens
$emailFields =& CRM_Core_DAO_Email::fields( );
foreach ($emailFields as $field) {
if ($field['export']) {
$tokens['contact']['email_'.$field['name']] = $field['title'];
}
}
// Phone tokens
$phoneFields =& CRM_Core_DAO_Phone::fields( );
foreach ($phoneFields as $field) {
if ($field['export']) {
$tokens['contact']['phone_'.$field['name']] = $field['title'];
}
}
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function civicrm_token_token_values($type, $object = NULL) {
$values = array();
switch ($type) {
case 'contact':
$contact = (object)$object;
// TODO remove
// dpm($contact);
if ( $contact->id ) {
// Contact tokens.
// see CRM/Contact/DAO/Contact.PHP for field definitions
$contactFields =& CRM_Contact_DAO_Contact::fields( );
foreach ($contactFields as $field) {
if ($field['export']) {
$values[$field['name']] = $contact->$field['name'];
}
}
// Address tokens
// See CRM/Core/DAO/Address.PHP for field definitions
// Only first or primary address is returned
if (isset($contact->address)) {
foreach ($contact->address as $caddr) {
if ($caddr->is_primary)
{
$address = $caddr;
break;
}
}
if (isset($address)) {
// figure out which fields to return
$addressFields =& CRM_Core_DAO_Address::fields( );
foreach ($addressFields as $field) {
if ($field['export']) {
$values['address_'. $field['name']] = $address->$field['name'];
}
}
}
}
// Phone tokens
// See CRM/Core/DAO/Phone.PHP for field definitions
// find the first or primary phone
if (isset($contact->phone)) {
foreach ($contact->phone as $cphone) {
if ($cphone->is_primary) {
$phone = $cphone;
break;
}
}
// set return value
if (isset($phone)) {
// figure out which fields to return
$phoneFields =& CRM_Core_DAO_Phone::fields( );
foreach ($phoneFields as $field) {
if ($field['export']) {
$values['phone_'. $field['name']] = $phone->$field['name'];
}
}
}
}
// Email tokens
// See CRM/Core/DAO/Email.PHP for field definitions
// find first or primary email
if (isset($contact->email)) {
foreach ($contact->email as $cemail) {
if ($cemail->is_primary)
{
$email = $cemail;
break;
}
}
// set return value
if (isset($email)) {
// figure out which fields to return
$emailFields =& CRM_Core_DAO_Email::fields( );
foreach ($emailFields as $field) {
if ($field['export']) {
$values['email_'. $field['name']] = $email->$field['name'];
}
}
}
}
}
// TODO add more...
break;
}
// TODO remove all this code when and if CiviCRM fixes bugs where NULL values are set to "null"
// see http://issues.civicrm.org/jira/browse/CRM-7944
foreach (array_keys($values) as $key){
if ($values[$key] == "null"){
$values[$key] = NULL;
}
}
return $values;
}