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',
77 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent
$e): void
{
78 // When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`.
79 parent
::alterActionScheduleQuery($e);
80 if ($e->mapping
->getEntity() === $this->getExtendableTableName()) {
81 $e->query
->select('e.event_id AS tokenContext_eventId');
87 * @throws \CiviCRM_API3_Exception
89 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
90 $this->prefetch
= (array) $prefetch;
91 if ($field === 'balance') {
92 // @todo - is this really a good idea to call this & potentially get the
93 // balance of the contribution attached to 'registered_by_id'
94 $info = \CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->getFieldValue($row, 'id'), 'event');
95 $balancePay = $info['balance'] ??
NULL;
96 $balancePay = \CRM_Utils_Money
::format($balancePay);
97 $row->tokens($entity, $field, $balancePay);
100 parent
::evaluateToken($row, $entity, $field, $prefetch);
104 * Do not show event id in the UI as event.id will also be available.
106 * Discount id is probably a bit esoteric.
110 protected function getHiddenTokens(): array {
111 return ['event_id', 'discount_id'];
115 * Get entity fields that should not be exposed as tokens.
119 protected function getSkippedFields(): array {
120 $fields = parent
::getSkippedFields();
121 // Never add these 2 fields - may not be a stable part of the schema.
122 // This field is on it's way out of core.
123 $fields[] = 'cart_id';
124 // this will probably get schema changed out of the table at some point.
125 $fields[] = 'is_pay_later';