Merge pull request #18279 from eileenmcnaughton/ordertest
[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 /**
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 */
25 class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber {
26
27 /**
28 * Class constructor.
29 */
30 public function __construct() {
31 parent::__construct('event', array_merge(
32 [
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'),
47 ],
48 CRM_Utils_Token::getCustomFieldTokens('Event')
49 ));
50 }
51
52 /**
53 * @inheritDoc
54 */
55 public function checkActive(\Civi\Token\TokenProcessor $processor) {
56 // Extracted from scheduled-reminders code. See the class description.
57 return !empty($processor->context['actionMapping'])
58 && $processor->context['actionMapping']->getEntity() === 'civicrm_participant';
59 }
60
61 /**
62 * Alter action schedule query.
63 *
64 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
65 */
66 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
67 if ($e->mapping->getEntity() !== 'civicrm_participant') {
68 return;
69 }
70
71 // FIXME: seems too broad.
72 $e->query->select('e.*');
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
78 LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
79 LEFT JOIN civicrm_address address ON address.id = lb.address_id
80 LEFT JOIN civicrm_email email ON email.id = lb.email_id
81 LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
82 ");
83 }
84
85 /**
86 * @inheritDoc
87 */
88 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
89 $actionSearchResult = $row->context['actionSearchResult'];
90
91 if ($field == 'location') {
92 $loc = [];
93 $stateProvince = \CRM_Core_PseudoConstant::stateProvince();
94 $loc['street_address'] = $actionSearchResult->street_address;
95 $loc['city'] = $actionSearchResult->city;
96 $loc['state_province'] = $stateProvince[$actionSearchResult->state_province_id] ?? NULL;
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 }
109 elseif (in_array($field, ['start_date', 'end_date'])) {
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');
118 $balancePay = $info['balance'] ?? NULL;
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 }
129 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
130 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
131 }
132 else {
133 $row->tokens($entity, $field, '');
134 }
135 }
136
137 }