X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;ds=sidebyside;f=CRM%2FEvent%2FParticipantTokens.php;h=b29050fcf143fd24f98f5fdfba01af4557788751;hb=07c1cf5696136f8f2298f37d8d20e5affa5a179f;hp=717b11756f410323e3664d600b134f9140c07e54;hpb=a7116eec1bcfa05e8f200f89200f4b45eb5f396b;p=civicrm-core.git diff --git a/CRM/Event/ParticipantTokens.php b/CRM/Event/ParticipantTokens.php index 717b11756f..b29050fcf1 100644 --- a/CRM/Event/ParticipantTokens.php +++ b/CRM/Event/ParticipantTokens.php @@ -10,6 +10,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Token\TokenRow; + /** * Class CRM_Event_ParticipantTokens * @@ -33,4 +35,72 @@ class CRM_Event_ParticipantTokens extends CRM_Core_EntityTokens { return ['fee_currency']; } + /** + * Get any tokens with custom calculation. + */ + protected function getBespokeTokens(): array { + return [ + 'balance' => [ + 'title' => ts('Event Balance'), + 'name' => 'balance', + 'type' => 'calculated', + 'options' => NULL, + 'data_type' => 'Money', + 'audience' => 'user', + ], + ]; + } + + public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e): void { + // When targeting `civicrm_participant` records, we enable both `{participant.*}` (per usual) and the related `{event.*}`. + parent::alterActionScheduleQuery($e); + if ($e->mapping->getEntity() === $this->getExtendableTableName()) { + $e->query->select('e.event_id AS tokenContext_eventId'); + } + } + + /** + * @inheritDoc + * @throws \CiviCRM_API3_Exception + */ + public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) { + $this->prefetch = (array) $prefetch; + if ($field === 'balance') { + // @todo - is this really a good idea to call this & potentially get the + // balance of the contribution attached to 'registered_by_id' + $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($this->getFieldValue($row, 'id'), 'event'); + $balancePay = $info['balance'] ?? NULL; + $balancePay = \CRM_Utils_Money::format($balancePay); + $row->tokens($entity, $field, $balancePay); + return; + } + parent::evaluateToken($row, $entity, $field, $prefetch); + } + + /** + * Do not show event id in the UI as event.id will also be available. + * + * Discount id is probably a bit esoteric. + * + * @return string[] + */ + protected function getHiddenTokens(): array { + return ['event_id', 'discount_id']; + } + + /** + * Get entity fields that should not be exposed as tokens. + * + * @return string[] + */ + protected function getSkippedFields(): array { + $fields = parent::getSkippedFields(); + // Never add these 2 fields - may not be a stable part of the schema. + // This field is on it's way out of core. + $fields[] = 'cart_id'; + // this will probably get schema changed out of the table at some point. + $fields[] = 'is_pay_later'; + return $fields; + } + }