Merge pull request #22798 from civicrm/5.47
[civicrm-core.git] / CRM / Event / Page / EventInfo.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Event Info Page - Summary about the event
20 */
21 class CRM_Event_Page_EventInfo extends CRM_Core_Page {
22
23 /**
24 * Run the page.
25 *
26 * This method is called after the page is created. It checks for the
27 * type of action and executes that action.
28 * Finally it calls the parent's run method.
29 *
30 * @return void
31 */
32 public function run() {
33 //get the event id.
34 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
35 $config = CRM_Core_Config::singleton();
36 // ensure that the user has permission to see this page
37 if (!CRM_Core_Permission::event(CRM_Core_Permission::VIEW,
38 $this->_id, 'view event info'
39 )
40 ) {
41 CRM_Utils_System::setUFMessage(ts('You do not have permission to view this event'));
42 return CRM_Utils_System::permissionDenied();
43 }
44
45 $action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
46 $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, 'register');
47 $this->assign('context', $context);
48
49 $this->assign('iCal', CRM_Event_BAO_Event::getICalLinks($this->_id));
50
51 // Sometimes we want to suppress the Event Full msg
52 $noFullMsg = CRM_Utils_Request::retrieve('noFullMsg', 'String', $this, FALSE, 'false');
53
54 // set breadcrumb to append to 2nd layer pages
55 $breadCrumbPath = CRM_Utils_System::url('civicrm/event/info',
56 "id={$this->_id}&reset=1"
57 );
58
59 //retrieve event information
60 $params = ['id' => $this->_id];
61 CRM_Event_BAO_Event::retrieve($params, $values['event']);
62
63 if (!$values['event']['is_active']) {
64 CRM_Utils_System::setUFMessage(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
65 return CRM_Utils_System::permissionDenied();
66 }
67
68 if (!$values['event']['is_public']) {
69 CRM_Utils_System::addHTMLHead('<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">');
70 }
71
72 if (!empty($values['event']['is_template'])) {
73 // form is an Event Template
74 CRM_Core_Error::statusBounce(ts('The page you requested is currently unavailable.'));
75 }
76
77 // Add Event Type to $values in case folks want to display it
78 $values['event']['event_type'] = CRM_Utils_Array::value($values['event']['event_type_id'], CRM_Event_PseudoConstant::eventType());
79
80 $this->assign('isShowLocation', CRM_Utils_Array::value('is_show_location', $values['event']));
81
82 // Reset event time zone info
83 CRM_Event_BAO_Event::setOutputTimeZone($values['event'], $values['event']['event_tz']);
84
85 $values['event']['event_tz'] = CRM_Core_SelectValues::timezone()[$values['event']['event_tz']];
86
87 // show event fees.
88 if ($this->_id && !empty($values['event']['is_monetary'])) {
89 CRM_Contribute_BAO_Contribution_Utils::overrideDefaultCurrency($values['event']);
90
91 //CRM-10434
92 $discountId = CRM_Core_BAO_Discount::findSet($this->_id, 'civicrm_event');
93 if ($discountId) {
94 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $discountId, 'price_set_id');
95 }
96 else {
97 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_id);
98 }
99
100 // get price set options, - CRM-5209
101 if ($priceSetId) {
102 $setDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, TRUE);
103
104 $priceSetFields = $setDetails[$priceSetId]['fields'];
105 if (is_array($priceSetFields)) {
106 $fieldCnt = 1;
107 $visibility = CRM_Core_PseudoConstant::visibility('name');
108
109 // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
110 $adminFieldVisible = FALSE;
111 if (CRM_Core_Permission::check('administer CiviCRM')) {
112 $adminFieldVisible = TRUE;
113 }
114
115 foreach ($priceSetFields as $fid => $fieldValues) {
116 if (!is_array($fieldValues['options']) ||
117 empty($fieldValues['options']) ||
118 (CRM_Utils_Array::value('visibility_id', $fieldValues) != array_search('public', $visibility) && $adminFieldVisible == FALSE)
119 ) {
120 continue;
121 }
122
123 if (count($fieldValues['options']) > 1) {
124 $values['feeBlock']['value'][$fieldCnt] = '';
125 $values['feeBlock']['label'][$fieldCnt] = $fieldValues['label'];
126 $values['feeBlock']['lClass'][$fieldCnt] = 'price_set_option_group-label';
127 $values['feeBlock']['isDisplayAmount'][$fieldCnt] = $fieldValues['is_display_amounts'] ?? NULL;
128 $fieldCnt++;
129 $labelClass = 'price_set_option-label';
130 }
131 else {
132 $labelClass = 'price_set_field-label';
133 }
134 // show tax rate with amount
135 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
136 $taxTerm = Civi::settings()->get('tax_term');
137 $displayOpt = $invoiceSettings['tax_display_settings'] ?? NULL;
138
139 foreach ($fieldValues['options'] as $optionId => $optionVal) {
140 if (CRM_Utils_Array::value('visibility_id', $optionVal) != array_search('public', $visibility) &&
141 $adminFieldVisible == FALSE
142 ) {
143 continue;
144 }
145
146 $values['feeBlock']['isDisplayAmount'][$fieldCnt] = $fieldValues['is_display_amounts'] ?? NULL;
147 if (Civi::settings()->get('invoicing') && isset($optionVal['tax_amount'])) {
148 $values['feeBlock']['value'][$fieldCnt] = CRM_Price_BAO_PriceField::getTaxLabel($optionVal, 'amount', $displayOpt, $taxTerm);
149 $values['feeBlock']['tax_amount'][$fieldCnt] = $optionVal['tax_amount'];
150 }
151 else {
152 $values['feeBlock']['value'][$fieldCnt] = $optionVal['amount'];
153 $values['feeBlock']['tax_amount'][$fieldCnt] = 0;
154 }
155 $values['feeBlock']['label'][$fieldCnt] = $optionVal['label'];
156 $values['feeBlock']['lClass'][$fieldCnt] = $labelClass;
157 $fieldCnt++;
158 }
159 }
160 }
161 // Tell tpl we have price set fee data and whether it's a quick_config price set
162 $this->assign('isPriceSet', 1);
163 $this->assign('isQuickConfig', $setDetails[$priceSetId]['is_quick_config']);
164 }
165 }
166
167 $params = ['entity_id' => $this->_id, 'entity_table' => 'civicrm_event'];
168 $values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
169
170 // fix phone type labels
171 if (!empty($values['location']['phone'])) {
172 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
173 foreach ($values['location']['phone'] as &$val) {
174 if (!empty($val['phone_type_id'])) {
175 $val['phone_type_display'] = $phoneTypes[$val['phone_type_id']];
176 }
177 }
178 }
179
180 //retrieve custom field information
181 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Event', NULL, $this->_id, 0, $values['event']['event_type_id'], NULL,
182 TRUE, NULL, FALSE, CRM_Core_Permission::VIEW, NULL, TRUE);
183 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->_id);
184 $this->assign('action', CRM_Core_Action::VIEW);
185 //To show the event location on maps directly on event info page
186 $locations = CRM_Event_BAO_Event::getMapInfo($this->_id);
187 if (!empty($locations) && !empty($values['event']['is_map'])) {
188 $this->assign('locations', $locations);
189 $this->assign('mapProvider', $config->mapProvider);
190 $this->assign('mapKey', $config->mapAPIKey);
191 $sumLat = $sumLng = 0;
192 $maxLat = $maxLng = -400;
193 $minLat = $minLng = 400;
194 foreach ($locations as $location) {
195 $sumLat += $location['lat'];
196 $sumLng += $location['lng'];
197
198 if ($location['lat'] > $maxLat) {
199 $maxLat = $location['lat'];
200 }
201 if ($location['lat'] < $minLat) {
202 $minLat = $location['lat'];
203 }
204
205 if ($location['lng'] > $maxLng) {
206 $maxLng = $location['lng'];
207 }
208 if ($location['lng'] < $minLng) {
209 $minLng = $location['lng'];
210 }
211 }
212
213 $center = [
214 'lat' => (float ) $sumLat / count($locations),
215 'lng' => (float ) $sumLng / count($locations),
216 ];
217 $span = [
218 'lat' => (float ) ($maxLat - $minLat),
219 'lng' => (float ) ($maxLng - $minLng),
220 ];
221 $this->assign_by_ref('center', $center);
222 $this->assign_by_ref('span', $span);
223 if ($action == CRM_Core_Action::PREVIEW) {
224 $mapURL = CRM_Utils_System::url('civicrm/contact/map/event',
225 "eid={$this->_id}&reset=1&action=preview",
226 FALSE, NULL, TRUE,
227 TRUE
228 );
229 }
230 else {
231 $mapURL = CRM_Utils_System::url('civicrm/contact/map/event',
232 "eid={$this->_id}&reset=1",
233 FALSE, NULL, TRUE,
234 TRUE
235 );
236 }
237
238 $this->assign('skipLocationType', TRUE);
239 $this->assign('mapURL', $mapURL);
240 }
241
242 if (CRM_Core_Permission::check('view event participants')) {
243 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
244 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
245 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
246 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
247 $this->assign('findParticipants', $findParticipants);
248 }
249
250 $participantListingID = $values['event']['participant_listing_id'] ?? NULL;
251 if ($participantListingID) {
252 $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
253 "reset=1&id={$this->_id}",
254 FALSE, NULL, TRUE, TRUE
255 );
256 $this->assign('participantListingURL', $participantListingURL);
257 }
258
259 $hasWaitingList = $values['event']['has_waitlist'] ?? NULL;
260 $eventFullMessage = CRM_Event_BAO_Participant::eventFull($this->_id,
261 FALSE,
262 $hasWaitingList
263 );
264
265 $allowRegistration = FALSE;
266 $isEventOpenForRegistration = CRM_Event_BAO_Event::validRegistrationRequest($values['event'], $this->_id);
267 if (!empty($values['event']['is_online_registration'])) {
268 if ($isEventOpenForRegistration == 1) {
269 // we always generate urls for the front end in joomla
270 $action_query = $action === CRM_Core_Action::PREVIEW ? "&action=$action" : '';
271 $url = CRM_Utils_System::url('civicrm/event/register',
272 "id={$this->_id}&reset=1{$action_query}",
273 FALSE, NULL, TRUE,
274 TRUE
275 );
276 if (!$eventFullMessage || $hasWaitingList) {
277 $registerText = ts('Register Now');
278 if (!empty($values['event']['registration_link_text'])) {
279 $registerText = $values['event']['registration_link_text'];
280 }
281
282 //Fixed for CRM-4855
283 $allowRegistration = CRM_Event_BAO_Event::showHideRegistrationLink($values);
284
285 $this->assign('registerText', $registerText);
286 $this->assign('registerURL', $url);
287 }
288 }
289 elseif (CRM_Core_Permission::check('register for events')) {
290 $this->assign('registerClosed', TRUE);
291 }
292 }
293
294 $this->assign('allowRegistration', $allowRegistration);
295
296 $session = CRM_Core_Session::singleton();
297 $params = [
298 'contact_id' => $session->get('userID'),
299 'event_id' => $values['event']['id'] ?? NULL,
300 'role_id' => $values['event']['default_role_id'] ?? NULL,
301 ];
302
303 if ($eventFullMessage && ($noFullMsg == 'false') || CRM_Event_BAO_Event::checkRegistration($params)) {
304 $statusMessage = $eventFullMessage;
305 if (CRM_Event_BAO_Event::checkRegistration($params)) {
306 if ($noFullMsg == 'false') {
307 if ($values['event']['allow_same_participant_emails']) {
308 $statusMessage = ts('It looks like you are already registered for this event. You may proceed if you want to create an additional registration.');
309 }
310 else {
311 $registerUrl = CRM_Utils_System::url('civicrm/event/register',
312 "reset=1&id={$values['event']['id']}&cid=0"
313 );
314 $statusMessage = ts("It looks like you are already registered for this event. If you want to change your registration, or you feel that you've gotten this message in error, please contact the site administrator.") . ' ' . ts('You can also <a href="%1">register another participant</a>.', [1 => $registerUrl]);
315 }
316 }
317 }
318 elseif ($hasWaitingList) {
319 $statusMessage = $values['event']['waitlist_text'] ?? NULL;
320 if (!$statusMessage) {
321 $statusMessage = ts('Event is currently full, but you can register and be a part of waiting list.');
322 }
323 }
324 if ($isEventOpenForRegistration == 1) {
325 CRM_Core_Session::setStatus($statusMessage);
326 }
327 }
328 // we do not want to display recently viewed items, so turn off
329 $this->assign('displayRecent', FALSE);
330
331 // set page title = event title
332 CRM_Utils_System::setTitle($values['event']['title']);
333
334 $this->assign('event', $values['event']);
335 if (isset($values['feeBlock'])) {
336 $this->assign('feeBlock', $values['feeBlock']);
337 }
338 $this->assign('location', $values['location']);
339
340 if (CRM_Core_Permission::check(['access CiviEvent', 'edit all events'])) {
341 $this->assign('manageEventLinks', CRM_Event_Page_ManageEvent::tabs());
342 }
343
344 return parent::run();
345 }
346
347 /**
348 * @return string
349 */
350 public function getTemplateFileName() {
351 if ($this->_id) {
352 $templateFile = "CRM/Event/Page/{$this->_id}/EventInfo.tpl";
353 $template = CRM_Core_Page::getTemplate();
354
355 if ($template->template_exists($templateFile)) {
356 return $templateFile;
357 }
358 }
359 return parent::getTemplateFileName();
360 }
361
362 }