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