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 public function getAllTokens(): array {
47 'event_type_id:label' => ts('Event Type'),
48 'title' => ts('Event Title'),
49 'id' => ts('Event ID'),
50 'start_date' => ts('Event Start Date'),
51 'end_date' => ts('Event End Date'),
52 'summary' => ts('Event Summary'),
53 'description' => ts('Event Description'),
54 'location' => ts('Event Location'),
55 'info_url' => ts('Event Info URL'),
56 'registration_url' => ts('Event Registration URL'),
57 'contact_email' => ts('Event Contact Email'),
58 'contact_phone' => ts('Event Contact Phone'),
60 CRM_Utils_Token
::getCustomFieldTokens('Event')
66 * @throws \API_Exception
68 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL) {
69 $eventID = $this->getFieldValue($row, 'id');
71 $eventID = $row->context
['actionSearchResult']->event_id
;
73 if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
74 foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
75 $row->format($format)->tokens($entity, $field, $value);
81 * Get the tokens available for the event.
83 * Cache by event as it's l
85 * @param int|null $eventID
89 * @throws \API_Exception|\CRM_Core_Exception
93 protected function getEventTokenValues(int $eventID = NULL): array {
94 $cacheKey = __CLASS__
. 'event_tokens' . $eventID . '_' . CRM_Core_I18n
::getLocale();
95 if (!Civi
::cache('metadata')->has($cacheKey)) {
96 $event = Event
::get(FALSE)->addWhere('id', '=', $eventID)
106 'loc_block_id.address_id.street_address',
107 'loc_block_id.address_id.city',
108 'loc_block_id.address_id.state_province_id:label',
109 'loc_block_id.address_id.postal_code',
110 'loc_block_id.email_id.email',
111 'loc_block_id.phone_id.phone',
114 ->execute()->first();
115 $tokens['location']['text/plain'] = \CRM_Utils_Address
::format([
116 'street_address' => $event['loc_block_id.address_id.street_address'],
117 'city' => $event['loc_block_id.address_id.city'],
118 'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
119 'postal_code' => $event['loc_block_id.address_id.postal_code'],
122 $tokens['info_url']['text/html'] = \CRM_Utils_System
::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
123 $tokens['registration_url']['text/html'] = \CRM_Utils_System
::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
124 $tokens['start_date']['text/html'] = !empty($event['start_date']) ?
new DateTime($event['start_date']) : '';
125 $tokens['end_date']['text/html'] = !empty($event['end_date']) ?
new DateTime($event['end_date']) : '';
126 $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant
::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
127 $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
128 $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
130 foreach (array_keys($this->getAllTokens()) as $field) {
131 if (!isset($tokens[$field])) {
132 if ($this->isCustomField($field)) {
133 $this->prefetch
[$eventID] = $event;
134 $tokens[$field]['text/html'] = $this->getCustomFieldValue($eventID, $field);
137 $tokens[$field]['text/html'] = $event[$field];
141 Civi
::cache('metadata')->set($cacheKey, $tokens);
143 return Civi
::cache('metadata')->get($cacheKey);