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 (!Civi
::cache('metadata')->has($cacheKey)) {
116 $event = Event
::get(FALSE)->addWhere('id', '=', $eventID)
117 ->setSelect(array_merge([
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',
125 ], $this->getExposedFields()))
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'],
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']);
139 $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant
::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
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'];
143 foreach (array_keys($this->getAllTokens()) as $field) {
144 if (!isset($tokens[$field])) {
145 if ($this->isCustomField($field)) {
146 $this->prefetch
[$eventID] = $event;
147 $tokens[$field]['text/html'] = $this->getCustomFieldValue($eventID, $field);
150 $tokens[$field]['text/html'] = $event[$field];
154 Civi
::cache('metadata')->set($cacheKey, $tokens);
156 return Civi
::cache('metadata')->get($cacheKey);
160 * Get entity fields that should be exposed as tokens.
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...
169 protected function getExposedFields(): array {
170 return ['event_type_id',