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