Merge pull request #19166 from eileenmcnaughton/cont_type
[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
13/**
14 * Class CRM_Event_Tokens
15 *
16 * Generate "event.*" tokens.
17 *
18 * This TokenSubscriber was produced by refactoring the code from the
19 * scheduled-reminder system with the goal of making that system
20 * more flexible. The current implementation is still coupled to
21 * scheduled-reminders. It would be good to figure out a more generic
22 * implementation which is not tied to scheduled reminders, although
23 * that is outside the current scope.
24 */
25class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber {
26
70599df6 27 /**
28 * Class constructor.
29 */
46f5566c 30 public function __construct() {
4e9b6a62 31 parent::__construct('event', array_merge(
be2fb01f 32 [
4e9b6a62 33 'event_type' => ts('Event Type'),
34 'title' => ts('Event Title'),
35 'event_id' => ts('Event ID'),
36 'start_date' => ts('Event Start Date'),
37 'end_date' => ts('Event End Date'),
38 'summary' => ts('Event Summary'),
39 'description' => ts('Event Description'),
40 'location' => ts('Event Location'),
41 'info_url' => ts('Event Info URL'),
42 'registration_url' => ts('Event Registration URL'),
43 'fee_amount' => ts('Event Fee'),
44 'contact_email' => ts('Event Contact (Email)'),
45 'contact_phone' => ts('Event Contact (Phone)'),
46 'balance' => ts('Event Balance'),
be2fb01f 47 ],
18c017c8 48 CRM_Utils_Token::getCustomFieldTokens('Event')
46f5566c
TO
49 ));
50 }
51
70599df6 52 /**
298795cd 53 * @inheritDoc
70599df6 54 */
46f5566c
TO
55 public function checkActive(\Civi\Token\TokenProcessor $processor) {
56 // Extracted from scheduled-reminders code. See the class description.
90b461f1 57 return !empty($processor->context['actionMapping'])
46f5566c
TO
58 && $processor->context['actionMapping']->getEntity() === 'civicrm_participant';
59 }
60
8246bca4 61 /**
62 * Alter action schedule query.
63 *
64 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
65 */
f9ec2da6
TO
66 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
67 if ($e->mapping->getEntity() !== 'civicrm_participant') {
68 return;
69 }
70
90b461f1
SL
71 // FIXME: seems too broad.
72 $e->query->select('e.*');
f9ec2da6
TO
73 $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');
74 $e->query->join('participant_stuff', "
75!casMailingJoinType civicrm_event ev ON e.event_id = ev.id
76!casMailingJoinType civicrm_option_group og ON og.name = 'event_type'
77!casMailingJoinType civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id
78LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
79LEFT JOIN civicrm_address address ON address.id = lb.address_id
80LEFT JOIN civicrm_email email ON email.id = lb.email_id
81LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
82");
83 }
84
46f5566c 85 /**
298795cd 86 * @inheritDoc
46f5566c
TO
87 */
88 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
89 $actionSearchResult = $row->context['actionSearchResult'];
90
91 if ($field == 'location') {
be2fb01f 92 $loc = [];
46f5566c
TO
93 $stateProvince = \CRM_Core_PseudoConstant::stateProvince();
94 $loc['street_address'] = $actionSearchResult->street_address;
95 $loc['city'] = $actionSearchResult->city;
9e10fb6b 96 $loc['state_province'] = $stateProvince[$actionSearchResult->state_province_id] ?? NULL;
46f5566c
TO
97 $loc['postal_code'] = $actionSearchResult->postal_code;
98 //$entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Address::format($loc);
99 $row->tokens($entity, $field, \CRM_Utils_Address::format($loc));
100 }
101 elseif ($field == 'info_url') {
102 $row
103 ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE));
104 }
105 elseif ($field == 'registration_url') {
106 $row
107 ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE));
108 }
be2fb01f 109 elseif (in_array($field, ['start_date', 'end_date'])) {
46f5566c
TO
110 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
111 }
112 elseif ($field == 'balance') {
113 if ($actionSearchResult->entityTable == 'civicrm_contact') {
114 $balancePay = 'N/A';
115 }
116 elseif (!empty($actionSearchResult->entityID)) {
117 $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($actionSearchResult->entityID, 'event');
9e10fb6b 118 $balancePay = $info['balance'] ?? NULL;
46f5566c
TO
119 $balancePay = \CRM_Utils_Money::format($balancePay);
120 }
121 $row->tokens($entity, $field, $balancePay);
122 }
123 elseif ($field == 'fee_amount') {
124 $row->tokens($entity, $field, \CRM_Utils_Money::format($actionSearchResult->$field));
125 }
126 elseif (isset($actionSearchResult->$field)) {
127 $row->tokens($entity, $field, $actionSearchResult->$field);
128 }
8640061b 129 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
130 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
4e9b6a62 131 }
46f5566c
TO
132 else {
133 $row->tokens($entity, $field, '');
134 }
135 }
136
137}