-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcivicrm_token.module
More file actions
86 lines (80 loc) · 3.16 KB
/
civicrm_token.module
File metadata and controls
86 lines (80 loc) · 3.16 KB
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
<?php
/**
* @file
* CiviCRM Token main module file.
*/
/**
* Implements hook_token_info().
*/
function civicrm_token_token_info() {
$info = [];
$info['types']['civi'] = array(
'name' => t('CiviCRM'),
'description' => t('Tokens related to CiviCRM.'),
);
$info['tokens']['civi']['contact-ID'] = array(
'name' => t('Contact ID'),
'description' => t('CiviCRM contact ID of current user.'),
);
$info['tokens']['civi']['contact-dashboard'] = array(
'name' => t('Contact Dashboard'),
'description' => t('CiviCRM contact dashborad of current user.'),
);
$info['tokens']['civi']['contact-first-name'] = array(
'name' => t('Contact First Name'),
'description' => t('CiviCRM contact first name of current user.'),
);
$info['tokens']['civi']['contact-last-name'] = array(
'name' => t('Contact Last Name'),
'description' => t('CiviCRM contact last name of current user.'),
);
$info['tokens']['civi']['contact-email'] = array(
'name' => t('Contact Email'),
'description' => t('CiviCRM contact email of current user.'),
);
$info['tokens']['civi']['contact-telephone'] = array(
'name' => t('Contact Phone Number'),
'description' => t('CiviCRM contact telephone number of current user.'),
);
$info['tokens']['civi']['num-of-records-via-gid'] = array(
'name' => t('Number of records submitted'),
'description' => t('Number of records submitted for given group ID by the current user.'),
'dynamic' => TRUE,
);
return $info;
}
/**
* Implements hook_tokens().
*
*/
function civicrm_token_tokens($type, $tokens, array $data = [], array $options = []) {
$replacements = array();
$currentUser = \Drupal::currentUser();
$sanitize = !empty($options['sanitize']);
\Drupal::service('civicrm')->initialize();
$currContactId = \CRM_Core_BAO_UFMatch::getContactId($currentUser->id());
if ($type == 'civi' && !is_null($currContactId)) {
$result = civicrm_api3('Contact', 'get', array('id' => $currContactId));
if ($result['count'] !== 0 || $result['is_error'] == 0) {
$params = array_pop($result['values']);
$replacements['[civi:contact-ID]'] = $currContactId;
$replacements['[civi:contact-dashboard]'] = "civicrm/user?reset=1&id=" . $currContactId;
$replacements['[civi:contact-first_name]'] = $params['first_name'];
$replacements['[civi:contact-last_name]'] = $params['last_name'];
$replacements['[civi:contact-email]'] = $params['email'];
$replacements['[civi:contact-telephone]'] = $params['phone'];
// Process dynamic tokens.
$tokenValues = \Drupal::token()->findWithPrefix($tokens, 'num-of-records-via-gid');
foreach ($tokenValues as $groupId => $tokenName) {
$groupRes = civicrm_api3('CustomGroup', 'get', ['sequential' => 1, 'id' => $groupId]);
if ($groupRes['count'] !== 0 || $groupRes['is_error'] == 0) {
$groupTableName = $groupRes['values'][0]['table_name'];
$dao = \CRM_Core_DAO::executeQuery("SELECT COUNT(id) as res FROM `${groupTableName}` WHERE entity_id = '${currContactId}'");
$dao->fetch();
$replacements[$tokenName] = (int)$dao->res;
}
}
}
}
return $replacements;
}