Merge pull request #21620 from colemanw/purifier2
[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\Event\TokenValueEvent;
14 use Civi\Token\TokenRow;
15
16 /**
17 * Class CRM_Event_ParticipantTokens
18 *
19 * Generate "participant.*" tokens.
20 */
21 class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens {
22
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];
27 return $events;
28 }
29
30 /**
31 * Get the entity name for api v4 calls.
32 *
33 * @return string
34 */
35 protected function getApiEntityName(): string {
36 return 'Participant';
37 }
38
39 /**
40 * @return array
41 */
42 public function getCurrencyFieldName(): array {
43 return ['fee_currency'];
44 }
45
46 /**
47 * To handle variable tokens, override this function and return the active tokens.
48 *
49 * @param \Civi\Token\Event\TokenValueEvent $e
50 *
51 * @return mixed
52 */
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;
57 }
58 return parent::getActiveTokens($e);
59 }
60
61 /**
62 * Get any tokens with custom calculation.
63 */
64 protected function getBespokeTokens(): array {
65 return [
66 'balance' => [
67 'title' => ts('Event Balance'),
68 'name' => 'balance',
69 'type' => 'calculated',
70 'options' => NULL,
71 'data_type' => 'Money',
72 'audience' => 'user',
73 ],
74 ];
75 }
76
77 /**
78 * @inheritDoc
79 * @throws \CiviCRM_API3_Exception
80 */
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');
85 }
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);
93 return;
94 }
95 parent::evaluateToken($row, $entity, $field, $prefetch);
96 }
97
98 /**
99 * Do not show event id in the UI as event.id will also be available.
100 *
101 * Discount id is probably a bit esoteric.
102 *
103 * @return string[]
104 */
105 protected function getHiddenTokens(): array {
106 return ['event_id', 'discount_id'];
107 }
108
109 /**
110 * Get entity fields that should not be exposed as tokens.
111 *
112 * @return string[]
113 */
114 protected 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';
121 return $fields;
122 }
123
124 }