Commit | Line | Data |
---|---|---|
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 | ||
ce971869 EM |
13 | use Civi\ActionSchedule\Event\MailingQueryEvent; |
14 | ||
46f5566c TO |
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 | */ | |
ce971869 | 27 | class CRM_Event_Tokens extends CRM_Core_EntityTokens { |
46f5566c | 28 | |
70599df6 | 29 | /** |
ce971869 EM |
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. | |
70599df6 | 42 | */ |
ce971869 EM |
43 | public function getAllTokens(): array { |
44 | return array_merge( | |
be2fb01f | 45 | [ |
4e9b6a62 | 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'), | |
ce971869 EM |
57 | 'contact_email' => ts('Event Contact Email'), |
58 | 'contact_phone' => ts('Event Contact Phone'), | |
4e9b6a62 | 59 | 'balance' => ts('Event Balance'), |
be2fb01f | 60 | ], |
18c017c8 | 61 | CRM_Utils_Token::getCustomFieldTokens('Event') |
ce971869 | 62 | ); |
46f5566c TO |
63 | } |
64 | ||
70599df6 | 65 | /** |
298795cd | 66 | * @inheritDoc |
70599df6 | 67 | */ |
46f5566c TO |
68 | public function checkActive(\Civi\Token\TokenProcessor $processor) { |
69 | // Extracted from scheduled-reminders code. See the class description. | |
ce971869 EM |
70 | return ((!empty($processor->context['actionMapping']) |
71 | && $processor->context['actionMapping']->getEntity() === 'civicrm_participant')) | |
72 | || in_array($this->getEntityIDField(), $processor->context['schema'], TRUE); | |
46f5566c TO |
73 | } |
74 | ||
8246bca4 | 75 | /** |
76 | * Alter action schedule query. | |
77 | * | |
78 | * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e | |
79 | */ | |
ce971869 | 80 | public function alterActionScheduleQuery(MailingQueryEvent $e): void { |
f9ec2da6 TO |
81 | if ($e->mapping->getEntity() !== 'civicrm_participant') { |
82 | return; | |
83 | } | |
84 | ||
90b461f1 SL |
85 | // FIXME: seems too broad. |
86 | $e->query->select('e.*'); | |
f9ec2da6 TO |
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 | ||
46f5566c | 99 | /** |
298795cd | 100 | * @inheritDoc |
46f5566c TO |
101 | */ |
102 | public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { | |
103 | $actionSearchResult = $row->context['actionSearchResult']; | |
104 | ||
105 | if ($field == 'location') { | |
be2fb01f | 106 | $loc = []; |
46f5566c TO |
107 | $stateProvince = \CRM_Core_PseudoConstant::stateProvince(); |
108 | $loc['street_address'] = $actionSearchResult->street_address; | |
109 | $loc['city'] = $actionSearchResult->city; | |
9e10fb6b | 110 | $loc['state_province'] = $stateProvince[$actionSearchResult->state_province_id] ?? NULL; |
46f5566c TO |
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 | } | |
be2fb01f | 123 | elseif (in_array($field, ['start_date', 'end_date'])) { |
46f5566c TO |
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'); | |
9e10fb6b | 132 | $balancePay = $info['balance'] ?? NULL; |
46f5566c TO |
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 | } | |
8640061b | 143 | elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) { |
4bd9197e | 144 | $row->customToken($entity, $cfID, $actionSearchResult->event_id); |
4e9b6a62 | 145 | } |
46f5566c TO |
146 | else { |
147 | $row->tokens($entity, $field, ''); | |
148 | } | |
149 | } | |
150 | ||
151 | } |