Participant tokens on scheduled reminders (wip- upgrade script needed at minimum)
[civicrm-core.git] / CRM / Event / Tokens.php
CommitLineData
46f5566c
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
bc77d7c0 5 | Copyright CiviCRM LLC. All rights reserved. |
46f5566c 6 | |
bc77d7c0
TO
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 |
46f5566c
TO
10 +--------------------------------------------------------------------+
11 */
12
873bfeb5 13use Civi\Api4\Event;
e80f2261 14use Civi\Token\TokenRow;
ce971869 15
46f5566c
TO
16/**
17 * Class CRM_Event_Tokens
18 *
19 * Generate "event.*" tokens.
20 *
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.
27 */
ce971869 28class CRM_Event_Tokens extends CRM_Core_EntityTokens {
46f5566c 29
70599df6 30 /**
ce971869
EM
31 * Get the entity name for api v4 calls.
32 *
33 * @return string
34 */
35 protected function getApiEntityName(): string {
36 return 'Event';
37 }
38
39 /**
40 * Get all tokens.
41 *
42 * This function will be removed once the parent class can determine it.
70599df6 43 */
ce971869
EM
44 public function getAllTokens(): array {
45 return array_merge(
be2fb01f 46 [
e80f2261 47 'event_type_id:label' => ts('Event Type'),
4e9b6a62 48 'title' => ts('Event Title'),
e80f2261 49 'id' => ts('Event ID'),
4e9b6a62 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'),
ce971869
EM
57 'contact_email' => ts('Event Contact Email'),
58 'contact_phone' => ts('Event Contact Phone'),
be2fb01f 59 ],
18c017c8 60 CRM_Utils_Token::getCustomFieldTokens('Event')
ce971869 61 );
46f5566c
TO
62 }
63
70599df6 64 /**
298795cd 65 * @inheritDoc
e80f2261 66 * @throws \API_Exception
70599df6 67 */
e80f2261
EM
68 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
69 $eventID = $this->getFieldValue($row, 'id');
70 if (!$eventID) {
71 $eventID = $row->context['actionSearchResult']->event_id;
f9ec2da6 72 }
873bfeb5
EM
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);
76 }
873bfeb5
EM
77 }
78 }
79
80 /**
81 * Get the tokens available for the event.
82 *
83 * Cache by event as it's l
84 *
85 * @param int|null $eventID
86 *
87 * @return array
88 *
89 * @throws \API_Exception|\CRM_Core_Exception
90 *
91 * @internal
92 */
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)
97 ->setSelect([
98 'event_type_id',
99 'title',
100 'id',
101 'start_date',
102 'end_date',
103 'summary',
104 'description',
105 'loc_block_id',
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',
112 'custom.*',
113 ])
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'],
120
121 ]);
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'];
129
130 foreach (array_keys($this->getAllTokens()) as $field) {
e80f2261 131 if (!isset($tokens[$field])) {
873bfeb5 132 if ($this->isCustomField($field)) {
e80f2261
EM
133 $this->prefetch[$eventID] = $event;
134 $tokens[$field]['text/html'] = $this->getCustomFieldValue($eventID, $field);
873bfeb5
EM
135 }
136 else {
137 $tokens[$field]['text/html'] = $event[$field];
138 }
139 }
140 }
141 Civi::cache('metadata')->set($cacheKey, $tokens);
46f5566c 142 }
873bfeb5 143 return Civi::cache('metadata')->get($cacheKey);
46f5566c
TO
144 }
145
146}