Merge pull request #21767 from eileenmcnaughton/meta
[civicrm-core.git] / CRM / Event / Tokens.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\Api4\Event;
14 use Civi\Token\TokenRow;
15
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 */
28 class CRM_Event_Tokens extends CRM_Core_EntityTokens {
29
30 /**
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.
43 */
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',
52 'audience' => 'user',
53 ],
54 'info_url' => [
55 'title' => ts('Event Info URL'),
56 'name' => 'info_url',
57 'type' => 'calculated',
58 'options' => NULL,
59 'data_type' => 'String',
60 'audience' => 'user',
61 ],
62 'registration_url' => [
63 'title' => ts('Event Registration URL'),
64 'name' => 'registration_url',
65 'type' => 'calculated',
66 'options' => NULL,
67 'data_type' => 'String',
68 'audience' => 'user',
69 ],
70 'contact_email' => [
71 'title' => ts('Event Contact Email'),
72 'name' => 'contact_email',
73 'type' => 'calculated',
74 'options' => NULL,
75 'data_type' => 'String',
76 'audience' => 'user',
77 ],
78 'contact_phone' => [
79 'title' => ts('Event Contact Phone'),
80 'name' => 'contact_phone',
81 'type' => 'calculated',
82 'options' => NULL,
83 'data_type' => '',
84 'audience' => 'user',
85 ],
86 ];
87 }
88
89 /**
90 * @inheritDoc
91 * @throws \API_Exception
92 */
93 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
94 $eventID = $this->getFieldValue($row, 'id');
95 if (!$eventID) {
96 $eventID = $row->context['actionSearchResult']->event_id;
97 }
98 if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
99 foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
100 $row->format($format)->tokens($entity, $field, $value ?? '');
101 }
102 }
103 }
104
105 /**
106 * Get the tokens available for the event.
107 *
108 * Cache by event as it's l
109 *
110 * @param int|null $eventID
111 *
112 * @return array
113 *
114 * @throws \API_Exception|\CRM_Core_Exception
115 *
116 * @internal
117 */
118 protected function getEventTokenValues(int $eventID = NULL): array {
119 $cacheKey = __CLASS__ . 'event_tokens' . $eventID . '_' . CRM_Core_I18n::getLocale();
120 if ($this->checkPermissions) {
121 $cacheKey .= '__' . CRM_Core_Session::getLoggedInContactID();
122 }
123 if (!Civi::cache('metadata')->has($cacheKey)) {
124 $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID)
125 ->setSelect(array_merge([
126 'loc_block_id.address_id.street_address',
127 'loc_block_id.address_id.city',
128 'loc_block_id.address_id.state_province_id:label',
129 'loc_block_id.address_id.postal_code',
130 'loc_block_id.email_id.email',
131 'loc_block_id.phone_id.phone',
132 'custom.*',
133 ], $this->getExposedFields()))
134 ->execute()->first();
135 $tokens['location']['text/plain'] = \CRM_Utils_Address::format([
136 'street_address' => $event['loc_block_id.address_id.street_address'],
137 'city' => $event['loc_block_id.address_id.city'],
138 'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
139 'postal_code' => $event['loc_block_id.address_id.postal_code'],
140
141 ]);
142 $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
143 $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
144 $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : '';
145 $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : '';
146 $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
147 $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
148 $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
149 $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
150
151 foreach ($this->getTokenMetadata() as $fieldName => $fieldSpec) {
152 if (!isset($tokens[$fieldName])) {
153 if ($fieldSpec['type'] === 'Custom') {
154 $this->prefetch[$eventID] = $event;
155 $value = $event[$fieldSpec['name']];
156 $tokens[$fieldName]['text/html'] = CRM_Core_BAO_CustomField::displayValue($value, $fieldSpec['custom_field_id']);
157 }
158 else {
159 $tokens[$fieldName]['text/html'] = $event[$fieldName];
160 }
161 }
162 }
163 Civi::cache('metadata')->set($cacheKey, $tokens);
164 }
165 return Civi::cache('metadata')->get($cacheKey);
166 }
167
168 /**
169 * Get entity fields that should be exposed as tokens.
170 *
171 * Event has traditionally exposed very few fields. This is probably because
172 * a) there are a tonne of weird fields so an opt out approach doesn't work and
173 * b) so people just added what they needed at the time...
174 *
175 * @return string[]
176 *
177 */
178 protected function getExposedFields(): array {
179 return ['event_type_id',
180 'title',
181 'id',
182 'start_date',
183 'end_date',
184 'summary',
185 'description',
186 ];
187 }
188
189 }