Merge pull request #24153 from agileware/CIVICRM-2025
[civicrm-core.git] / CRM / Event / Tokens.php
CommitLineData
46f5566c
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
46f5566c 6 | |
bc77d7c0
TO
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 |
46f5566c
TO
10 +--------------------------------------------------------------------+
11 */
12
873bfeb5 13use Civi\Api4\Event;
e80f2261 14use Civi\Token\TokenRow;
ce971869 15
46f5566c
TO
16/**
17 * Class CRM_Event_Tokens
18 *
19 * Generate "event.*" tokens.
20 *
21 * This TokenSubscriber was produced by refactoring the code from the
22 * scheduled-reminder system with the goal of making that system
23 * more flexible. The current implementation is still coupled to
24 * scheduled-reminders. It would be good to figure out a more generic
25 * implementation which is not tied to scheduled reminders, although
26 * that is outside the current scope.
27 */
ce971869 28class CRM_Event_Tokens extends CRM_Core_EntityTokens {
46f5566c 29
70599df6 30 /**
ce971869
EM
31 * Get the entity name for api v4 calls.
32 *
33 * @return string
34 */
35 protected function getApiEntityName(): string {
36 return 'Event';
37 }
38
39 /**
40 * Get all tokens.
41 *
42 * This function will be removed once the parent class can determine it.
70599df6 43 */
e9841a51
EM
44 protected function getBespokeTokens(): array {
45 return [
46 'location' => [
47 'title' => ts('Event Location'),
48 'name' => 'location',
49 'type' => 'calculated',
50 'options' => NULL,
51 'data_type' => 'String',
cc17ec91 52 'audience' => 'user',
be2fb01f 53 ],
e9841a51
EM
54 'info_url' => [
55 'title' => ts('Event Info URL'),
56 'name' => 'info_url',
57 'type' => 'calculated',
58 'options' => NULL,
59 'data_type' => 'String',
cc17ec91 60 'audience' => 'user',
e9841a51
EM
61 ],
62 'registration_url' => [
63 'title' => ts('Event Registration URL'),
64 'name' => 'registration_url',
65 'type' => 'calculated',
66 'options' => NULL,
67 'data_type' => 'String',
cc17ec91 68 'audience' => 'user',
e9841a51
EM
69 ],
70 'contact_email' => [
71 'title' => ts('Event Contact Email'),
72 'name' => 'contact_email',
73 'type' => 'calculated',
74 'options' => NULL,
75 'data_type' => 'String',
cc17ec91 76 'audience' => 'user',
e9841a51
EM
77 ],
78 'contact_phone' => [
79 'title' => ts('Event Contact Phone'),
80 'name' => 'contact_phone',
81 'type' => 'calculated',
82 'options' => NULL,
83 'data_type' => '',
cc17ec91 84 'audience' => 'user',
e9841a51
EM
85 ],
86 ];
46f5566c
TO
87 }
88
70599df6 89 /**
298795cd 90 * @inheritDoc
e80f2261 91 * @throws \API_Exception
70599df6 92 */
e80f2261
EM
93 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
94 $eventID = $this->getFieldValue($row, 'id');
873bfeb5
EM
95 if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
96 foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
cc17ec91 97 $row->format($format)->tokens($entity, $field, $value ?? '');
873bfeb5 98 }
873bfeb5
EM
99 }
100 }
101
102 /**
103 * Get the tokens available for the event.
104 *
105 * Cache by event as it's l
106 *
107 * @param int|null $eventID
108 *
109 * @return array
110 *
111 * @throws \API_Exception|\CRM_Core_Exception
112 *
113 * @internal
114 */
115 protected function getEventTokenValues(int $eventID = NULL): array {
116 $cacheKey = __CLASS__ . 'event_tokens' . $eventID . '_' . CRM_Core_I18n::getLocale();
889b0617
EM
117 if ($this->checkPermissions) {
118 $cacheKey .= '__' . CRM_Core_Session::getLoggedInContactID();
119 }
873bfeb5 120 if (!Civi::cache('metadata')->has($cacheKey)) {
889b0617 121 $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID)
e9841a51 122 ->setSelect(array_merge([
873bfeb5
EM
123 'loc_block_id.address_id.street_address',
124 'loc_block_id.address_id.city',
125 'loc_block_id.address_id.state_province_id:label',
126 'loc_block_id.address_id.postal_code',
127 'loc_block_id.email_id.email',
128 'loc_block_id.phone_id.phone',
129 'custom.*',
e9841a51 130 ], $this->getExposedFields()))
873bfeb5
EM
131 ->execute()->first();
132 $tokens['location']['text/plain'] = \CRM_Utils_Address::format([
133 'street_address' => $event['loc_block_id.address_id.street_address'],
134 'city' => $event['loc_block_id.address_id.city'],
135 'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
136 'postal_code' => $event['loc_block_id.address_id.postal_code'],
137
138 ]);
3c942359
EM
139 $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE);
140 $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE);
873bfeb5
EM
141 $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : '';
142 $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : '';
143 $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
e9841a51 144 $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
873bfeb5
EM
145 $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
146 $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
147
cc17ec91
EM
148 foreach ($this->getTokenMetadata() as $fieldName => $fieldSpec) {
149 if (!isset($tokens[$fieldName])) {
150 if ($fieldSpec['type'] === 'Custom') {
e80f2261 151 $this->prefetch[$eventID] = $event;
cc17ec91
EM
152 $value = $event[$fieldSpec['name']];
153 $tokens[$fieldName]['text/html'] = CRM_Core_BAO_CustomField::displayValue($value, $fieldSpec['custom_field_id']);
873bfeb5
EM
154 }
155 else {
cc17ec91 156 $tokens[$fieldName]['text/html'] = $event[$fieldName];
873bfeb5
EM
157 }
158 }
159 }
160 Civi::cache('metadata')->set($cacheKey, $tokens);
46f5566c 161 }
873bfeb5 162 return Civi::cache('metadata')->get($cacheKey);
46f5566c
TO
163 }
164
e9841a51
EM
165 /**
166 * Get entity fields that should be exposed as tokens.
167 *
168 * Event has traditionally exposed very few fields. This is probably because
169 * a) there are a tonne of weird fields so an opt out approach doesn't work and
170 * b) so people just added what they needed at the time...
171 *
172 * @return string[]
173 *
174 */
175 protected function getExposedFields(): array {
c16c5e08
EM
176 return [
177 'event_type_id',
e9841a51
EM
178 'title',
179 'id',
c16c5e08 180 'pay_later_receipt',
e9841a51
EM
181 'start_date',
182 'end_date',
183 'summary',
184 'description',
185 ];
186 }
187
46f5566c 188}