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 +--------------------------------------------------------------------+
13 use Civi\ActionSchedule\Event\MailingQueryEvent
;
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' => 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'),
62 CRM_Utils_Token
::getCustomFieldTokens('Event')
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);
77 * Alter action schedule query.
79 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
81 public function alterActionScheduleQuery(MailingQueryEvent
$e): void
{
82 if ($e->mapping
->getEntity() !== 'civicrm_participant') {
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
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);
113 if ($field === 'event_id') {
114 // @todo - migrate this to 'id'
115 $row->tokens($entity, $field, $eventID);
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']);
121 elseif ($field === 'balance') {
122 if ($actionSearchResult->entityTable
=== 'civicrm_contact') {
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);
130 $row->tokens($entity, $field, $balancePay);
132 elseif ($field === 'fee_amount') {
133 $row->tokens($entity, $field, \CRM_Utils_Money
::format($actionSearchResult->$field));
135 elseif ($cfID = CRM_Core_BAO_CustomField
::getKeyID($field)) {
136 $row->customToken($entity, $cfID, $actionSearchResult->event_id
);
139 parent
::evaluateToken($row, $entity, $field, $prefetch);
144 * Get the tokens available for the event.
146 * Cache by event as it's l
148 * @param int|null $eventID
152 * @throws \API_Exception|\CRM_Core_Exception
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)
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',
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'],
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'];
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));
199 $tokens[$field]['text/html'] = $event[$field];
203 Civi
::cache('metadata')->set($cacheKey, $tokens);
205 return Civi
::cache('metadata')->get($cacheKey);