forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParticipantTokens.php
103 lines (92 loc) · 2.98 KB
/
ParticipantTokens.php
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
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
use Civi\Token\TokenRow;
/**
* Class CRM_Event_ParticipantTokens
*
* Generate "participant.*" tokens.
*/
class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
/**
* Get the entity name for api v4 calls.
*
* @return string
*/
protected function getApiEntityName(): string {
return 'Participant';
}
/**
* @return array
*/
public function getCurrencyFieldName(): array {
return ['fee_currency'];
}
/**
* Get any tokens with custom calculation.
*/
protected function getBespokeTokens(): array {
return [
'balance' => [
'title' => ts('Event Balance'),
'name' => 'balance',
'type' => 'calculated',
'options' => NULL,
'data_type' => 'Money',
'audience' => 'user',
],
];
}
public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void {
// When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`.
parent::alterActionScheduleQuery($e);
if ($e->mapping->getEntityTable($e->actionSchedule) === $this->getExtendableTableName()) {
$e->query->select('e.event_id AS tokenContext_eventId');
}
}
/**
* @inheritDoc
* @throws \CRM_Core_Exception
*/
public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
$this->prefetch = (array) $prefetch;
if ($field === 'balance') {
// @todo - is this really a good idea to call this & potentially get the
// balance of the contribution attached to 'registered_by_id'
$info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($this->getFieldValue($row, 'id'), 'event');
$balancePay = $info['balance'] ?? NULL;
$balancePay = \CRM_Utils_Money::format($balancePay);
$row->tokens($entity, $field, $balancePay);
return;
}
parent::evaluateToken($row, $entity, $field, $prefetch);
}
/**
* Do not show event id in the UI as event.id will also be available.
*
* Discount id is probably a bit esoteric.
*
* @return string[]
*/
protected function getHiddenTokens(): array {
return ['event_id', 'discount_id'];
}
/**
* Get entity fields that should not be exposed as tokens.
*
* @return string[]
*/
protected function getSkippedFields(): array {
$fields = parent::getSkippedFields();
// this will probably get schema changed out of the table at some point.
$fields[] = 'is_pay_later';
return $fields;
}
}