9863bf65667f0b584f89a76f0e6a08c162f30c9f
[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 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');
82 }
83 }
84
85 /**
86 * @inheritDoc
87 * @throws \CiviCRM_API3_Exception
88 */
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);
98 return;
99 }
100 parent::evaluateToken($row, $entity, $field, $prefetch);
101 }
102
103 /**
104 * Do not show event id in the UI as event.id will also be available.
105 *
106 * Discount id is probably a bit esoteric.
107 *
108 * @return string[]
109 */
110 protected function getHiddenTokens(): array {
111 return ['event_id', 'discount_id'];
112 }
113
114 /**
115 * Get entity fields that should not be exposed as tokens.
116 *
117 * @return string[]
118 */
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';
126 return $fields;
127 }
128
129 }