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