Commit | Line | Data |
---|---|---|
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 | 13 | use Civi\Api4\Event; |
e80f2261 | 14 | use 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 | 28 | class 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', | |
be2fb01f | 52 | ], |
e9841a51 EM |
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 | ]; | |
46f5566c TO |
82 | } |
83 | ||
70599df6 | 84 | /** |
298795cd | 85 | * @inheritDoc |
e80f2261 | 86 | * @throws \API_Exception |
70599df6 | 87 | */ |
e80f2261 EM |
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; | |
f9ec2da6 | 92 | } |
873bfeb5 EM |
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 | } | |
873bfeb5 EM |
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 (!Civi::cache('metadata')->has($cacheKey)) { | |
116 | $event = Event::get(FALSE)->addWhere('id', '=', $eventID) | |
e9841a51 | 117 | ->setSelect(array_merge([ |
873bfeb5 EM |
118 | 'loc_block_id.address_id.street_address', |
119 | 'loc_block_id.address_id.city', | |
120 | 'loc_block_id.address_id.state_province_id:label', | |
121 | 'loc_block_id.address_id.postal_code', | |
122 | 'loc_block_id.email_id.email', | |
123 | 'loc_block_id.phone_id.phone', | |
124 | 'custom.*', | |
e9841a51 | 125 | ], $this->getExposedFields())) |
873bfeb5 EM |
126 | ->execute()->first(); |
127 | $tokens['location']['text/plain'] = \CRM_Utils_Address::format([ | |
128 | 'street_address' => $event['loc_block_id.address_id.street_address'], | |
129 | 'city' => $event['loc_block_id.address_id.city'], | |
130 | 'state_province' => $event['loc_block_id.address_id.state_province_id:label'], | |
131 | 'postal_code' => $event['loc_block_id.address_id.postal_code'], | |
132 | ||
133 | ]); | |
134 | $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE); | |
135 | $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE); | |
136 | $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : ''; | |
137 | $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : ''; | |
138 | $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']); | |
e9841a51 | 139 | $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']); |
873bfeb5 EM |
140 | $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone']; |
141 | $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email']; | |
142 | ||
143 | foreach (array_keys($this->getAllTokens()) as $field) { | |
e80f2261 | 144 | if (!isset($tokens[$field])) { |
873bfeb5 | 145 | if ($this->isCustomField($field)) { |
e80f2261 EM |
146 | $this->prefetch[$eventID] = $event; |
147 | $tokens[$field]['text/html'] = $this->getCustomFieldValue($eventID, $field); | |
873bfeb5 EM |
148 | } |
149 | else { | |
150 | $tokens[$field]['text/html'] = $event[$field]; | |
151 | } | |
152 | } | |
153 | } | |
154 | Civi::cache('metadata')->set($cacheKey, $tokens); | |
46f5566c | 155 | } |
873bfeb5 | 156 | return Civi::cache('metadata')->get($cacheKey); |
46f5566c TO |
157 | } |
158 | ||
e9841a51 EM |
159 | /** |
160 | * Get entity fields that should be exposed as tokens. | |
161 | * | |
162 | * Event has traditionally exposed very few fields. This is probably because | |
163 | * a) there are a tonne of weird fields so an opt out approach doesn't work and | |
164 | * b) so people just added what they needed at the time... | |
165 | * | |
166 | * @return string[] | |
167 | * | |
168 | */ | |
169 | protected function getExposedFields(): array { | |
170 | return ['event_type_id', | |
171 | 'title', | |
172 | 'id', | |
173 | 'start_date', | |
174 | 'end_date', | |
175 | 'summary', | |
176 | 'description', | |
177 | ]; | |
178 | } | |
179 | ||
46f5566c | 180 | } |