4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 use Civi\Token\Event\TokenValueEvent
;
14 use Civi\Token\TokenRow
;
17 * Class CRM_Event_ParticipantTokens
19 * Generate "participant.*" tokens.
21 class CRM_Event_ParticipantTokens
extends CRM_Core_EntityTokens
{
23 public static function getSubscribedEvents() {
24 $events = parent
::getSubscribedEvents();
25 // Set the weight so it runs before event tokens.
26 $events['civi.token.eval'] = ['evaluateTokens', 400];
31 * Get the entity name for api v4 calls.
35 protected function getApiEntityName(): string {
42 public function getCurrencyFieldName(): array {
43 return ['fee_currency'];
47 * To handle variable tokens, override this function and return the active tokens.
49 * @param \Civi\Token\Event\TokenValueEvent $e
53 public function getActiveTokens(TokenValueEvent
$e) {
54 $messageTokens = $e->getTokenProcessor()->getMessageTokens();
55 if (!isset($messageTokens[$this->entity
])) {
56 return isset($messageTokens['event']) ?
['event_id'] : FALSE;
58 return parent
::getActiveTokens($e);
62 * Get any tokens with custom calculation.
64 protected function getBespokeTokens(): array {
67 'title' => ts('Event Balance'),
69 'type' => 'calculated',
71 'data_type' => 'Money',
79 * @throws \CiviCRM_API3_Exception
81 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
82 $this->prefetch
= (array) $prefetch;
83 if (empty($row->context
['eventId'])) {
84 $row->context
['eventId'] = $this->getFieldValue($row, 'event_id');
86 if ($field === 'balance') {
87 // @todo - is this really a good idea to call this & potentially get the
88 // balance of the contribution attached to 'registered_by_id'
89 $info = \CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->getFieldValue($row, 'id'), 'event');
90 $balancePay = $info['balance'] ??
NULL;
91 $balancePay = \CRM_Utils_Money
::format($balancePay);
92 $row->tokens($entity, $field, $balancePay);
95 parent
::evaluateToken($row, $entity, $field, $prefetch);
99 * Do not show event id in the UI as event.id will also be available.
101 * Discount id is probably a bit esoteric.
105 protected function getHiddenTokens(): array {
106 return ['event_id', 'discount_id'];
110 * Get entity fields that should not be exposed as tokens.
114 public function getSkippedFields(): array {
115 $fields = parent
::getSkippedFields();
116 // Never add these 2 fields - may not be a stable part of the schema.
117 // This field is on it's way out of core.
118 $fields[] = 'cart_id';
119 // this will probably get schema changed out of the table at some point.
120 $fields[] = 'is_pay_later';