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',
54 'title' => ts('Event Info URL'),
56 'type' => 'calculated',
58 'data_type' => 'String',
60 'registration_url' => [
61 'title' => ts('Event Registration URL'),
62 'name' => 'registration_url',
63 'type' => 'calculated',
65 'data_type' => 'String',
68 'title' => ts('Event Contact Email'),
69 'name' => 'contact_email',
70 'type' => 'calculated',
72 'data_type' => 'String',
75 'title' => ts('Event Contact Phone'),
76 'name' => 'contact_phone',
77 'type' => 'calculated',
86 * @throws \API_Exception
88 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
89 $eventID = $this->getFieldValue($row, 'id');
91 $eventID = $row->context
['actionSearchResult']->event_id
;
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);
101 * Get the tokens available for the event.
103 * Cache by event as it's l
105 * @param int|null $eventID
109 * @throws \API_Exception|\CRM_Core_Exception
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();
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',
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'],
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'];
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);
153 $tokens[$field]['text/html'] = $event[$field];
157 Civi
::cache('metadata')->set($cacheKey, $tokens);
159 return Civi
::cache('metadata')->get($cacheKey);
163 * Get entity fields that should be exposed as tokens.
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...
172 protected function getExposedFields(): array {
173 return ['event_type_id',