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 +--------------------------------------------------------------------+
14 use Civi\Token\TokenRow
;
17 * Class CRM_Event_Tokens
19 * Generate "event.*" tokens.
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.
28 class CRM_Event_Tokens
extends CRM_Core_EntityTokens
{
31 * Get the entity name for api v4 calls.
35 protected function getApiEntityName(): string {
42 * This function will be removed once the parent class can determine it.
44 protected function getBespokeTokens(): array {
47 'title' => ts('Event Location'),
49 'type' => 'calculated',
51 'data_type' => 'String',
55 'title' => ts('Event Info URL'),
57 'type' => 'calculated',
59 'data_type' => 'String',
62 'registration_url' => [
63 'title' => ts('Event Registration URL'),
64 'name' => 'registration_url',
65 'type' => 'calculated',
67 'data_type' => 'String',
71 'title' => ts('Event Contact Email'),
72 'name' => 'contact_email',
73 'type' => 'calculated',
75 'data_type' => 'String',
79 'title' => ts('Event Contact Phone'),
80 'name' => 'contact_phone',
81 'type' => 'calculated',
91 * @throws \API_Exception
93 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
94 $eventID = $this->getFieldValue($row, 'id');
96 $eventID = $row->context
['actionSearchResult']->event_id
;
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 ??
'');
106 * Get the tokens available for the event.
108 * Cache by event as it's l
110 * @param int|null $eventID
114 * @throws \API_Exception|\CRM_Core_Exception
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();
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',
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'],
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'];
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']);
159 $tokens[$fieldName]['text/html'] = $event[$fieldName];
163 Civi
::cache('metadata')->set($cacheKey, $tokens);
165 return Civi
::cache('metadata')->get($cacheKey);
169 * Get entity fields that should be exposed as tokens.
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...
178 protected function getExposedFields(): array {
179 return ['event_type_id',