Merge pull request #21521 from eileenmcnaughton/member_tokens
[civicrm-core.git] / CRM / Event / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 use Civi\ActionSchedule\Event\MailingQueryEvent;
14 use Civi\Api4\Event;
15
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 */
28 class CRM_Event_Tokens extends CRM_Core_EntityTokens {
29
30 /**
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.
43 */
44 public function getAllTokens(): array {
45 return array_merge(
46 [
47 'event_type' => ts('Event Type'),
48 'title' => ts('Event Title'),
49 'event_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 'fee_amount' => ts('Event Fee'),
58 'contact_email' => ts('Event Contact Email'),
59 'contact_phone' => ts('Event Contact Phone'),
60 'balance' => ts('Event Balance'),
61 ],
62 CRM_Utils_Token::getCustomFieldTokens('Event')
63 );
64 }
65
66 /**
67 * @inheritDoc
68 */
69 public function checkActive(\Civi\Token\TokenProcessor $processor) {
70 // Extracted from scheduled-reminders code. See the class description.
71 return ((!empty($processor->context['actionMapping'])
72 && $processor->context['actionMapping']->getEntity() === 'civicrm_participant'))
73 || in_array($this->getEntityIDField(), $processor->context['schema'], TRUE);
74 }
75
76 /**
77 * Alter action schedule query.
78 *
79 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
80 */
81 public function alterActionScheduleQuery(MailingQueryEvent $e): void {
82 if ($e->mapping->getEntity() !== 'civicrm_participant') {
83 return;
84 }
85
86 // FIXME: seems too broad.
87 $e->query->select('e.*');
88 $e->query->select('ov.label as event_type, ev.title, ev.id as event_id, ev.start_date, ev.end_date, ev.summary, ev.description, address.street_address, address.city, address.state_province_id, address.postal_code, email.email as contact_email, phone.phone as contact_phone');
89 $e->query->join('participant_stuff', "
90 !casMailingJoinType civicrm_event ev ON e.event_id = ev.id
91 !casMailingJoinType civicrm_option_group og ON og.name = 'event_type'
92 !casMailingJoinType civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id
93 LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
94 LEFT JOIN civicrm_address address ON address.id = lb.address_id
95 LEFT JOIN civicrm_email email ON email.id = lb.email_id
96 LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
97 ");
98 }
99
100 /**
101 * @inheritDoc
102 */
103 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
104 $actionSearchResult = $row->context['actionSearchResult'];
105 $eventID = $row->context['eventId'] ?? $actionSearchResult->event_id;
106 if (array_key_exists($field, $this->getEventTokenValues($eventID))) {
107 foreach ($this->getEventTokenValues($eventID)[$field] as $format => $value) {
108 $row->format($format)->tokens($entity, $field, $value);
109 }
110 return;
111 }
112
113 if ($field === 'event_id') {
114 // @todo - migrate this to 'id'
115 $row->tokens($entity, $field, $eventID);
116 }
117 elseif ($field === 'event_type') {
118 // temporary - @todo db update to event_type_id:label
119 $row->tokens($entity, $field, $this->getEventTokenValues($eventID)['event_type_id:label']['text/html']);
120 }
121 elseif ($field === 'balance') {
122 if ($actionSearchResult->entityTable === 'civicrm_contact') {
123 $balancePay = 'N/A';
124 }
125 elseif (!empty($actionSearchResult->entityID)) {
126 $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($actionSearchResult->entityID, 'event');
127 $balancePay = $info['balance'] ?? NULL;
128 $balancePay = \CRM_Utils_Money::format($balancePay);
129 }
130 $row->tokens($entity, $field, $balancePay);
131 }
132 elseif ($field === 'fee_amount') {
133 $row->tokens($entity, $field, \CRM_Utils_Money::format($actionSearchResult->$field));
134 }
135 elseif ($cfID = CRM_Core_BAO_CustomField::getKeyID($field)) {
136 $row->customToken($entity, $cfID, $actionSearchResult->event_id);
137 }
138 else {
139 parent::evaluateToken($row, $entity, $field, $prefetch);
140 }
141 }
142
143 /**
144 * Get the tokens available for the event.
145 *
146 * Cache by event as it's l
147 *
148 * @param int|null $eventID
149 *
150 * @return array
151 *
152 * @throws \API_Exception|\CRM_Core_Exception
153 *
154 * @internal
155 */
156 protected function getEventTokenValues(int $eventID = NULL): array {
157 $cacheKey = __CLASS__ . 'event_tokens' . $eventID . '_' . CRM_Core_I18n::getLocale();
158 if (!Civi::cache('metadata')->has($cacheKey)) {
159 $event = Event::get(FALSE)->addWhere('id', '=', $eventID)
160 ->setSelect([
161 'event_type_id',
162 'title',
163 'id',
164 'start_date',
165 'end_date',
166 'summary',
167 'description',
168 'loc_block_id',
169 'loc_block_id.address_id.street_address',
170 'loc_block_id.address_id.city',
171 'loc_block_id.address_id.state_province_id:label',
172 'loc_block_id.address_id.postal_code',
173 'loc_block_id.email_id.email',
174 'loc_block_id.phone_id.phone',
175 'custom.*',
176 ])
177 ->execute()->first();
178 $tokens['location']['text/plain'] = \CRM_Utils_Address::format([
179 'street_address' => $event['loc_block_id.address_id.street_address'],
180 'city' => $event['loc_block_id.address_id.city'],
181 'state_province' => $event['loc_block_id.address_id.state_province_id:label'],
182 'postal_code' => $event['loc_block_id.address_id.postal_code'],
183
184 ]);
185 $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
186 $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE);
187 $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : '';
188 $tokens['end_date']['text/html'] = !empty($event['end_date']) ? new DateTime($event['end_date']) : '';
189 $tokens['event_type_id:label']['text/html'] = CRM_Core_PseudoConstant::getLabel('CRM_Event_BAO_Event', 'event_type_id', $event['event_type_id']);
190 $tokens['contact_phone']['text/html'] = $event['loc_block_id.phone_id.phone'];
191 $tokens['contact_email']['text/html'] = $event['loc_block_id.email_id.email'];
192
193 foreach (array_keys($this->getAllTokens()) as $field) {
194 if (!isset($tokens[$field]) && isset($event[$field])) {
195 if ($this->isCustomField($field)) {
196 $tokens[$field]['text/html'] = CRM_Core_BAO_CustomField::displayValue($event[$field], str_replace('custom_', '', $field));
197 }
198 else {
199 $tokens[$field]['text/html'] = $event[$field];
200 }
201 }
202 }
203 Civi::cache('metadata')->set($cacheKey, $tokens);
204 }
205 return Civi::cache('metadata')->get($cacheKey);
206 }
207
208 }