Short array syntax - auto-format CRM directory
[civicrm-core.git] / CRM / Event / Tokens.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Class CRM_Event_Tokens
31 *
32 * Generate "event.*" tokens.
33 *
34 * This TokenSubscriber was produced by refactoring the code from the
35 * scheduled-reminder system with the goal of making that system
36 * more flexible. The current implementation is still coupled to
37 * scheduled-reminders. It would be good to figure out a more generic
38 * implementation which is not tied to scheduled reminders, although
39 * that is outside the current scope.
40 */
41 class CRM_Event_Tokens extends \Civi\Token\AbstractTokenSubscriber {
42
43 /**
44 * Class constructor.
45 */
46 public function __construct() {
47 parent::__construct('event', array_merge(
48 [
49 'event_type' => ts('Event Type'),
50 'title' => ts('Event Title'),
51 'event_id' => ts('Event ID'),
52 'start_date' => ts('Event Start Date'),
53 'end_date' => ts('Event End Date'),
54 'summary' => ts('Event Summary'),
55 'description' => ts('Event Description'),
56 'location' => ts('Event Location'),
57 'info_url' => ts('Event Info URL'),
58 'registration_url' => ts('Event Registration URL'),
59 'fee_amount' => ts('Event Fee'),
60 'contact_email' => ts('Event Contact (Email)'),
61 'contact_phone' => ts('Event Contact (Phone)'),
62 'balance' => ts('Event Balance'),
63 ],
64 CRM_Utils_Token::getCustomFieldTokens('Event')
65 ));
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public function checkActive(\Civi\Token\TokenProcessor $processor) {
72 // Extracted from scheduled-reminders code. See the class description.
73 return
74 !empty($processor->context['actionMapping'])
75 && $processor->context['actionMapping']->getEntity() === 'civicrm_participant';
76 }
77
78 /**
79 * Alter action schedule query.
80 *
81 * @param \Civi\ActionSchedule\Event\MailingQueryEvent $e
82 */
83 public function alterActionScheduleQuery(\Civi\ActionSchedule\Event\MailingQueryEvent $e) {
84 if ($e->mapping->getEntity() !== 'civicrm_participant') {
85 return;
86 }
87
88 $e->query->select('e.*'); // FIXME: seems too broad.
89 $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');
90 $e->query->join('participant_stuff', "
91 !casMailingJoinType civicrm_event ev ON e.event_id = ev.id
92 !casMailingJoinType civicrm_option_group og ON og.name = 'event_type'
93 !casMailingJoinType civicrm_option_value ov ON ev.event_type_id = ov.value AND ov.option_group_id = og.id
94 LEFT JOIN civicrm_loc_block lb ON lb.id = ev.loc_block_id
95 LEFT JOIN civicrm_address address ON address.id = lb.address_id
96 LEFT JOIN civicrm_email email ON email.id = lb.email_id
97 LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id
98 ");
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
105 $actionSearchResult = $row->context['actionSearchResult'];
106
107 if ($field == 'location') {
108 $loc = [];
109 $stateProvince = \CRM_Core_PseudoConstant::stateProvince();
110 $loc['street_address'] = $actionSearchResult->street_address;
111 $loc['city'] = $actionSearchResult->city;
112 $loc['state_province'] = \CRM_Utils_Array::value($actionSearchResult->state_province_id, $stateProvince);
113 $loc['postal_code'] = $actionSearchResult->postal_code;
114 //$entityTokenParams[$tokenEntity][$field] = \CRM_Utils_Address::format($loc);
115 $row->tokens($entity, $field, \CRM_Utils_Address::format($loc));
116 }
117 elseif ($field == 'info_url') {
118 $row
119 ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE));
120 }
121 elseif ($field == 'registration_url') {
122 $row
123 ->tokens($entity, $field, \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $actionSearchResult->event_id, TRUE, NULL, FALSE));
124 }
125 elseif (in_array($field, ['start_date', 'end_date'])) {
126 $row->tokens($entity, $field, \CRM_Utils_Date::customFormat($actionSearchResult->$field));
127 }
128 elseif ($field == 'balance') {
129 if ($actionSearchResult->entityTable == 'civicrm_contact') {
130 $balancePay = 'N/A';
131 }
132 elseif (!empty($actionSearchResult->entityID)) {
133 $info = \CRM_Contribute_BAO_Contribution::getPaymentInfo($actionSearchResult->entityID, 'event');
134 $balancePay = \CRM_Utils_Array::value('balance', $info);
135 $balancePay = \CRM_Utils_Money::format($balancePay);
136 }
137 $row->tokens($entity, $field, $balancePay);
138 }
139 elseif ($field == 'fee_amount') {
140 $row->tokens($entity, $field, \CRM_Utils_Money::format($actionSearchResult->$field));
141 }
142 elseif (isset($actionSearchResult->$field)) {
143 $row->tokens($entity, $field, $actionSearchResult->$field);
144 }
145 elseif ($cfID = \CRM_Core_BAO_CustomField::getKeyID($field)) {
146 $row->customToken($entity, $cfID, $actionSearchResult->entity_id);
147 }
148 else {
149 $row->tokens($entity, $field, '');
150 }
151 }
152
153 }