Merge pull request #21691 from colemanw/sqlExprTitles
[civicrm-core.git] / CRM / Event / ParticipantTokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 use Civi\Token\TokenRow;
14
15 /**
16 * Class CRM_Event_ParticipantTokens
17 *
18 * Generate "participant.*" tokens.
19 */
20 class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
21
22 public static function getSubscribedEvents() {
23 $events = parent::getSubscribedEvents();
24 // Set the weight so it runs before event tokens.
25 $events['civi.token.eval'] = ['evaluateTokens', 400];
26 return $events;
27 }
28
29 /**
30 * Get the entity name for api v4 calls.
31 *
32 * @return string
33 */
34 protected function getApiEntityName(): string {
35 return 'Participant';
36 }
37
38 /**
39 * @return array
40 */
41 public function getCurrencyFieldName(): array {
42 return ['fee_currency'];
43 }
44
45 /**
46 * Get any tokens with custom calculation.
47 */
48 public function getBespokeTokens(): array {
49 return ['balance' => ts('Event Balance')];
50 }
51
52 /**
53 * @inheritDoc
54 * @throws \CiviCRM_API3_Exception
55 */
56 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
57 $this->prefetch = (array) $prefetch;
58 if (empty($row->context['eventId'])) {
59 $row->context['eventId'] = $this->getFieldValue($row, 'event_id');
60 }
61 if ($field === 'balance') {
62 // @todo - is this really a good idea to call this & potentially get the
63 // balance of the contribution attached to 'registered_by_id'
64 $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($this->getFieldValue($row, 'id'), 'event');
65 $balancePay = $info['balance'] ?? NULL;
66 $balancePay = \CRM_Utils_Money::format($balancePay);
67 $row->tokens($entity, $field, $balancePay);
68 return;
69 }
70 parent::evaluateToken($row, $entity, $field, $prefetch);
71 }
72
73 /**
74 * Do not show event id in the UI as event.id will also be available.
75 *
76 * Discount id is probably a bit esoteric.
77 *
78 * @return string[]
79 */
80 protected function getHiddenTokens(): array {
81 return ['event_id', 'discount_id'];
82 }
83
84 /**
85 * Get entity fields that should not be exposed as tokens.
86 *
87 * @return string[]
88 */
89 public function getSkippedFields(): array {
90 $fields = parent::getSkippedFields();
91 // Never add these 2 fields - may not be a stable part of the schema.
92 // This field is on it's way out of core.
93 $fields[] = 'cart_id';
94 // this will probably get schema changed out of the table at some point.
95 $fields[] = 'is_pay_later';
96 return $fields;
97 }
98
99 }