4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 use Civi\ActionSchedule\Event\MailingQueryEvent
;
16 * Class CRM_Event_Tokens
18 * Generate "event.*" tokens.
20 * This TokenSubscriber was produced by refactoring the code from the
21 * scheduled-reminder system with the goal of making that system
22 * more flexible. The current implementation is still coupled to
23 * scheduled-reminders. It would be good to figure out a more generic
24 * implementation which is not tied to scheduled reminders, although
25 * that is outside the current scope.
27 class CRM_Event_Tokens
extends CRM_Core_EntityTokens
{
30 * Get the entity name for api v4 calls.
34 protected function getApiEntityName(): string {
41 * This function will be removed once the parent class can determine it.
43 public function getAllTokens(): array {
46 'event_type' => ts('Event Type'),
47 'title' => ts('Event Title'),
48 'event_id' => ts('Event ID'),
49 'start_date' => ts('Event Start Date'),
50 'end_date' => ts('Event End Date'),
51 'summary' => ts('Event Summary'),
52 'description' => ts('Event Description'),
53 'location' => ts('Event Location'),
54 'info_url' => ts('Event Info URL'),
55 'registration_url' => ts('Event Registration URL'),
56 'fee_amount' => ts('Event Fee'),
57 'contact_email' => ts('Event Contact Email'),
58 'contact_phone' => ts('Event Contact Phone'),
59 'balance' => ts('Event Balance'),
61 CRM_Utils_Token
::getCustomFieldTokens('Event')
68 public function checkActive(\Civi\Token\TokenProcessor
$processor) {
69 // Extracted from scheduled-reminders code. See the class description.
70 return ((!empty($processor->context
['actionMapping'])
71 && $processor->context
['actionMapping']->getEntity() === 'civicrm_participant'))
72 ||
in_array($this->getEntityIDField(), $processor->context
['schema'], TRUE);
76 * Alter action schedule query.
78 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
80 public function alterActionScheduleQuery(MailingQueryEvent
$e): void
{
81 if ($e->mapping
->getEntity() !== 'civicrm_participant') {
85 // FIXME: seems too broad.
86 $e->query
->select('e.*');
87 $e->query
->select('ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone');
88 $e->query
->join('participant_stuff', "
89 !casMailingJoinType civicrm_event ev ON e.event_id = ev.id
90 !casMailingJoinType civicrm_option_group og ON og.name = 'event_type'
91 !casMailingJoinType civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id
92 LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
93 LEFT JOIN civicrm_address address ON address.id = lb.address_id
94 LEFT JOIN civicrm_email email ON email.id = lb.email_id
95 LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
102 public function evaluateToken(\Civi\Token\TokenRow
$row, $entity, $field, $prefetch = NULL) {
103 $actionSearchResult = $row->context
['actionSearchResult'];
105 if ($field == 'location') {
107 $stateProvince = \CRM_Core_PseudoConstant
::stateProvince();
108 $loc['street_address'] = $actionSearchResult->street_address
;
109 $loc['city'] = $actionSearchResult->city
;
110 $loc['state_province'] = $stateProvince[$actionSearchResult->state_province_id
] ??
NULL;
111 $loc['postal_code'] = $actionSearchResult->postal_code
;
112 //$entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Address::format($loc);
113 $row->tokens($entity, $field, \CRM_Utils_Address
::format($loc));
115 elseif ($field == 'info_url') {
117 ->tokens($entity, $field, \CRM_Utils_System
::url('civicrm/event/info', 'reset=1&id=' . $actionSearchResult->event_id
, TRUE, NULL, FALSE));
119 elseif ($field == 'registration_url') {
121 ->tokens($entity, $field, \CRM_Utils_System
::url('civicrm/event/register', 'reset=1&id=' . $actionSearchResult->event_id
, TRUE, NULL, FALSE));
123 elseif (in_array($field, ['start_date', 'end_date'])) {
124 $row->tokens($entity, $field, \CRM_Utils_Date
::customFormat($actionSearchResult->$field));
126 elseif ($field == 'balance') {
127 if ($actionSearchResult->entityTable
== 'civicrm_contact') {
130 elseif (!empty($actionSearchResult->entityID
)) {
131 $info = \CRM_Contribute_BAO_Contribution
::getPaymentInfo($actionSearchResult->entityID
, 'event');
132 $balancePay = $info['balance'] ??
NULL;
133 $balancePay = \CRM_Utils_Money
::format($balancePay);
135 $row->tokens($entity, $field, $balancePay);
137 elseif ($field == 'fee_amount') {
138 $row->tokens($entity, $field, \CRM_Utils_Money
::format($actionSearchResult->$field));
140 elseif (isset($actionSearchResult->$field)) {
141 $row->tokens($entity, $field, $actionSearchResult->$field);
143 elseif ($cfID = \CRM_Core_BAO_CustomField
::getKeyID($field)) {
144 $row->customToken($entity, $cfID, $actionSearchResult->event_id
);
147 $row->tokens($entity, $field, '');