| 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 | /** |
| 23 | * Get the entity name for api v4 calls. |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | protected function getApiEntityName(): string { |
| 28 | return 'Participant'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @return array |
| 33 | */ |
| 34 | public function getCurrencyFieldName(): array { |
| 35 | return ['fee_currency']; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get any tokens with custom calculation. |
| 40 | */ |
| 41 | protected function getBespokeTokens(): array { |
| 42 | return [ |
| 43 | 'balance' => [ |
| 44 | 'title' => ts('Event Balance'), |
| 45 | 'name' => 'balance', |
| 46 | 'type' => 'calculated', |
| 47 | 'options' => NULL, |
| 48 | 'data_type' => 'Money', |
| 49 | 'audience' => 'user', |
| 50 | ], |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void { |
| 55 | // When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`. |
| 56 | parent::alterActionScheduleQuery($e); |
| 57 | if ($e->mapping->getEntity() === $this->getExtendableTableName()) { |
| 58 | $e->query->select('e.event_id AS tokenContext_eventId'); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @inheritDoc |
| 64 | * @throws \CiviCRM_API3_Exception |
| 65 | */ |
| 66 | public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { |
| 67 | $this->prefetch = (array) $prefetch; |
| 68 | if ($field === 'balance') { |
| 69 | // @todo - is this really a good idea to call this & potentially get the |
| 70 | // balance of the contribution attached to 'registered_by_id' |
| 71 | $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($this->getFieldValue($row, 'id'), 'event'); |
| 72 | $balancePay = $info['balance'] ?? NULL; |
| 73 | $balancePay = \CRM_Utils_Money::format($balancePay); |
| 74 | $row->tokens($entity, $field, $balancePay); |
| 75 | return; |
| 76 | } |
| 77 | parent::evaluateToken($row, $entity, $field, $prefetch); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Do not show event id in the UI as event.id will also be available. |
| 82 | * |
| 83 | * Discount id is probably a bit esoteric. |
| 84 | * |
| 85 | * @return string[] |
| 86 | */ |
| 87 | protected function getHiddenTokens(): array { |
| 88 | return ['event_id', 'discount_id']; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get entity fields that should not be exposed as tokens. |
| 93 | * |
| 94 | * @return string[] |
| 95 | */ |
| 96 | protected function getSkippedFields(): array { |
| 97 | $fields = parent::getSkippedFields(); |
| 98 | // Never add these 2 fields - may not be a stable part of the schema. |
| 99 | // This field is on it's way out of core. |
| 100 | $fields[] = 'cart_id'; |
| 101 | // this will probably get schema changed out of the table at some point. |
| 102 | $fields[] = 'is_pay_later'; |
| 103 | return $fields; |
| 104 | } |
| 105 | |
| 106 | } |