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');
95 if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
96 foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
97 $row->format($format)->tokens($entity, $field, $value ??
'');
103 * Get the tokens available for the event.
105 * Cache by event as it's l
107 * @param int|null $eventID
111 * @throws \API_Exception|\CRM_Core_Exception
115 protected function getEventTokenValues(int $eventID = NULL): array {
116 $cacheKey = __CLASS__
. 'event_tokens' . $eventID . '_' . CRM_Core_I18n
::getLocale();
117 if ($this->checkPermissions
) {
118 $cacheKey .= '__' . CRM_Core_Session
::getLoggedInContactID();
120 if (!Civi
::cache('metadata')->has($cacheKey)) {
121 $event = Event
::get($this->checkPermissions
)->addWhere('id', '=', $eventID)
122 ->setSelect(array_merge([
123 'loc_block_id.address_id.street_address',
124 'loc_block_id.address_id.city',
125 'loc_block_id.address_id.state_province_id:label',
126 'loc_block_id.address_id.postal_code',
127 'loc_block_id.email_id.email',
128 'loc_block_id.phone_id.phone',
130 ], $this->getExposedFields()))
131 ->execute()->first();
132 $tokens['location']['text/plain'] = \CRM_Utils_Address
::format([
133 'street_address' => $event['loc_block_id.address_id.street_address'],
134 'city' => $event['loc_block_id.address_id.city'],
135 'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
136 'postal_code' => $event['loc_block_id.address_id.postal_code'],
139 $tokens['info_url']['text/html'] = \CRM_Utils_System
::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
140 $tokens['registration_url']['text/html'] = \CRM_Utils_System
::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
141 $tokens['start_date']['text/html'] = !empty($event['start_date']) ?
new DateTime($event['start_date']) : '';
142 $tokens['end_date']['text/html'] = !empty($event['end_date']) ?
new DateTime($event['end_date']) : '';
143 $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant
::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
144 $tokens['event_type_id:name']['text/html'] = CRM_Core_PseudoConstant
::getName('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
145 $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
146 $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
148 foreach ($this->getTokenMetadata() as $fieldName => $fieldSpec) {
149 if (!isset($tokens[$fieldName])) {
150 if ($fieldSpec['type'] === 'Custom') {
151 $this->prefetch
[$eventID] = $event;
152 $value = $event[$fieldSpec['name']];
153 $tokens[$fieldName]['text/html'] = CRM_Core_BAO_CustomField
::displayValue($value, $fieldSpec['custom_field_id']);
156 $tokens[$fieldName]['text/html'] = $event[$fieldName];
160 Civi
::cache('metadata')->set($cacheKey, $tokens);
162 return Civi
::cache('metadata')->get($cacheKey);
166 * Get entity fields that should be exposed as tokens.
168 * Event has traditionally exposed very few fields. This is probably because
169 * a) there are a tonne of weird fields so an opt out approach doesn't work and
170 * b) so people just added what they needed at the time...
175 protected function getExposedFields(): array {