Merge pull request #7970 from otetard/CRM-18235
[civicrm-core.git] / CRM / Event / Page / EventInfo.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2016
32 */
33
34 /**
35 * Event Info Page - Summmary about the event
36 */
37 class 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
47 */
48 public function run() {
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,
54 $this->_id, 'view event info'
55 )
56 ) {
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);
62 $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'register');
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 );
72
73 //retrieve event information
74 $params = array('id' => $this->_id);
75 CRM_Event_BAO_Event::retrieve($params, $values['event']);
76
77 if (!$values['event']['is_active']) {
78 // form is inactive, die a fatal death
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();
81 }
82
83 if (!empty($values['event']['is_template'])) {
84 // form is an Event Template
85 CRM_Core_Error::fatal(ts('The page you requested is currently unavailable.'));
86 }
87
88 // Add Event Type to $values in case folks want to display it
89 $values['event']['event_type'] = CRM_Utils_Array::value($values['event']['event_type_id'], CRM_Event_PseudoConstant::eventType());
90
91 $this->assign('isShowLocation', CRM_Utils_Array::value('is_show_location', $values['event']));
92
93 // show event fees.
94 if ($this->_id && !empty($values['event']['is_monetary'])) {
95 //CRM-6907
96 $config = CRM_Core_Config::singleton();
97 $config->defaultCurrency = CRM_Utils_Array::value('currency',
98 $values['event'],
99 $config->defaultCurrency
100 );
101
102 //CRM-10434
103 $discountId = CRM_Core_BAO_Discount::findSet($this->_id, 'civicrm_event');
104 if ($discountId) {
105 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Discount', $discountId, 'price_set_id');
106 }
107 else {
108 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $this->_id);
109 }
110
111 // get price set options, - CRM-5209
112 if ($priceSetId) {
113 $setDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, TRUE);
114
115 $priceSetFields = $setDetails[$priceSetId]['fields'];
116 if (is_array($priceSetFields)) {
117 $fieldCnt = 1;
118 $visibility = CRM_Core_PseudoConstant::visibility('name');
119
120 // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
121 $adminFieldVisible = FALSE;
122 if (CRM_Core_Permission::check('administer CiviCRM')) {
123 $adminFieldVisible = TRUE;
124 }
125
126 foreach ($priceSetFields as $fid => $fieldValues) {
127 if (!is_array($fieldValues['options']) ||
128 empty($fieldValues['options']) ||
129 (CRM_Utils_Array::value('visibility_id', $fieldValues) != array_search('public', $visibility) && $adminFieldVisible == FALSE)
130 ) {
131 continue;
132 }
133
134 if (count($fieldValues['options']) > 1) {
135 $values['feeBlock']['value'][$fieldCnt] = '';
136 $values['feeBlock']['label'][$fieldCnt] = $fieldValues['label'];
137 $values['feeBlock']['lClass'][$fieldCnt] = 'price_set_option_group-label';
138 $values['feeBlock']['isDisplayAmount'][$fieldCnt] = CRM_Utils_Array::value('is_display_amounts', $fieldValues);
139 $fieldCnt++;
140 $labelClass = 'price_set_option-label';
141 }
142 else {
143 $labelClass = 'price_set_field-label';
144 }
145 // show tax rate with amount
146 $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
147 $taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
148 $displayOpt = CRM_Utils_Array::value('tax_display_settings', $invoiceSettings);
149 $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
150 foreach ($fieldValues['options'] as $optionId => $optionVal) {
151 $values['feeBlock']['isDisplayAmount'][$fieldCnt] = CRM_Utils_Array::value('is_display_amounts', $fieldValues);
152 if ($invoicing && isset($optionVal['tax_amount'])) {
153 $values['feeBlock']['value'][$fieldCnt] = CRM_Price_BAO_PriceField::getTaxLabel($optionVal, 'amount', $displayOpt, $taxTerm);
154 $values['feeBlock']['tax_amount'][$fieldCnt] = $optionVal['tax_amount'];
155 }
156 else {
157 $values['feeBlock']['value'][$fieldCnt] = $optionVal['amount'];
158 }
159 $values['feeBlock']['label'][$fieldCnt] = $optionVal['label'];
160 $values['feeBlock']['lClass'][$fieldCnt] = $labelClass;
161 $fieldCnt++;
162 }
163 }
164 }
165 // Tell tpl we have price set fee data and whether it's a quick_config price set
166 $this->assign('isPriceSet', 1);
167 $this->assign('isQuickConfig', $setDetails[$priceSetId]['is_quick_config']);
168 }
169 }
170
171 $params = array('entity_id' => $this->_id, 'entity_table' => 'civicrm_event');
172 $values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
173
174 // fix phone type labels
175 if (!empty($values['location']['phone'])) {
176 $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
177 foreach ($values['location']['phone'] as &$val) {
178 if (!empty($val['phone_type_id'])) {
179 $val['phone_type_display'] = $phoneTypes[$val['phone_type_id']];
180 }
181 }
182 }
183
184 //retrieve custom field information
185 $groupTree = CRM_Core_BAO_CustomGroup::getTree('Event', $this, $this->_id, 0, $values['event']['event_type_id']);
186 CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->_id);
187 $this->assign('action', CRM_Core_Action::VIEW);
188 //To show the event location on maps directly on event info page
189 $locations = CRM_Event_BAO_Event::getMapInfo($this->_id);
190 if (!empty($locations) && !empty($values['event']['is_map'])) {
191 $this->assign('locations', $locations);
192 $this->assign('mapProvider', $config->mapProvider);
193 $this->assign('mapKey', $config->mapAPIKey);
194 $sumLat = $sumLng = 0;
195 $maxLat = $maxLng = -400;
196 $minLat = $minLng = 400;
197 foreach ($locations as $location) {
198 $sumLat += $location['lat'];
199 $sumLng += $location['lng'];
200
201 if ($location['lat'] > $maxLat) {
202 $maxLat = $location['lat'];
203 }
204 if ($location['lat'] < $minLat) {
205 $minLat = $location['lat'];
206 }
207
208 if ($location['lng'] > $maxLng) {
209 $maxLng = $location['lng'];
210 }
211 if ($location['lng'] < $minLng) {
212 $minLng = $location['lng'];
213 }
214 }
215
216 $center = array(
217 'lat' => (float ) $sumLat / count($locations),
218 'lng' => (float ) $sumLng / count($locations),
219 );
220 $span = array(
221 'lat' => (float ) ($maxLat - $minLat),
222 'lng' => (float ) ($maxLng - $minLng),
223 );
224 $this->assign_by_ref('center', $center);
225 $this->assign_by_ref('span', $span);
226 if ($action == CRM_Core_Action::PREVIEW) {
227 $mapURL = CRM_Utils_System::url('civicrm/contact/map/event',
228 "eid={$this->_id}&reset=1&action=preview",
229 TRUE, NULL, TRUE,
230 TRUE
231 );
232 }
233 else {
234 $mapURL = CRM_Utils_System::url('civicrm/contact/map/event',
235 "eid={$this->_id}&reset=1",
236 TRUE, NULL, TRUE,
237 TRUE
238 );
239 }
240
241 $this->assign('skipLocationType', TRUE);
242 $this->assign('mapURL', $mapURL);
243 }
244
245 if (CRM_Core_Permission::check('view event participants') &&
246 CRM_Core_Permission::check('view all contacts')
247 ) {
248 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
249 $statusTypesPending = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 0', 'label');
250 $findParticipants['statusCounted'] = implode(', ', array_values($statusTypes));
251 $findParticipants['statusNotCounted'] = implode(', ', array_values($statusTypesPending));
252 $this->assign('findParticipants', $findParticipants);
253 }
254
255 $participantListingID = CRM_Utils_Array::value('participant_listing_id', $values['event']);
256 if ($participantListingID) {
257 $participantListingURL = CRM_Utils_System::url('civicrm/event/participant',
258 "reset=1&id={$this->_id}",
259 TRUE, NULL, TRUE, TRUE
260 );
261 $this->assign('participantListingURL', $participantListingURL);
262 }
263
264 $hasWaitingList = CRM_Utils_Array::value('has_waitlist', $values['event']);
265 $eventFullMessage = CRM_Event_BAO_Participant::eventFull($this->_id,
266 FALSE,
267 $hasWaitingList
268 );
269
270 $allowRegistration = FALSE;
271 if (!empty($values['event']['is_online_registration'])) {
272 if (CRM_Event_BAO_Event::validRegistrationRequest($values['event'], $this->_id)) {
273 // we always generate urls for the front end in joomla
274 $action_query = $action === CRM_Core_Action::PREVIEW ? "&action=$action" : '';
275 $url = CRM_Utils_System::url('civicrm/event/register',
276 "id={$this->_id}&reset=1{$action_query}",
277 TRUE, NULL, TRUE,
278 TRUE
279 );
280 if (!$eventFullMessage || $hasWaitingList) {
281 $registerText = ts('Register Now');
282 if (!empty($values['event']['registration_link_text'])) {
283 $registerText = $values['event']['registration_link_text'];
284 }
285
286 // check if we're in shopping cart mode for events
287 $enable_cart = Civi::settings()->get('enable_cart');
288 if ($enable_cart) {
289 $link = CRM_Event_Cart_BAO_EventInCart::get_registration_link($this->_id);
290 $registerText = $link['label'];
291
292 $url = CRM_Utils_System::url($link['path'], $link['query'] . $action_query, TRUE, NULL, TRUE, TRUE);
293 }
294
295 //Fixed for CRM-4855
296 $allowRegistration = CRM_Event_BAO_Event::showHideRegistrationLink($values);
297
298 $this->assign('registerText', $registerText);
299 $this->assign('registerURL', $url);
300 $this->assign('eventCartEnabled', $enable_cart);
301 }
302 }
303 elseif (CRM_Core_Permission::check('register for events')) {
304 $this->assign('registerClosed', TRUE);
305 }
306 }
307
308 $this->assign('allowRegistration', $allowRegistration);
309
310 $session = CRM_Core_Session::singleton();
311 $params = array(
312 'contact_id' => $session->get('userID'),
313 'event_id' => CRM_Utils_Array::value('id', $values['event']),
314 'role_id' => CRM_Utils_Array::value('default_role_id', $values['event']),
315 );
316
317 if ($eventFullMessage && ($noFullMsg == 'false') || CRM_Event_BAO_Event::checkRegistration($params)) {
318 $statusMessage = $eventFullMessage;
319 if (CRM_Event_BAO_Event::checkRegistration($params)) {
320 if ($noFullMsg == 'false') {
321 if ($values['event']['allow_same_participant_emails']) {
322 $statusMessage = ts('It looks like you are already registered for this event. You may proceed if you want to create an additional registration.');
323 }
324 else {
325 $registerUrl = CRM_Utils_System::url('civicrm/event/register',
326 "reset=1&id={$values['event']['id']}&cid=0"
327 );
328 $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>.', array(1 => $registerUrl));
329 }
330 }
331 }
332 elseif ($hasWaitingList) {
333 $statusMessage = CRM_Utils_Array::value('waitlist_text', $values['event']);
334 if (!$statusMessage) {
335 $statusMessage = ts('Event is currently full, but you can register and be a part of waiting list.');
336 }
337 }
338
339 CRM_Core_Session::setStatus($statusMessage);
340 }
341 // we do not want to display recently viewed items, so turn off
342 $this->assign('displayRecent', FALSE);
343
344 // set page title = event title
345 CRM_Utils_System::setTitle($values['event']['title']);
346
347 $this->assign('event', $values['event']);
348 if (isset($values['feeBlock'])) {
349 $this->assign('feeBlock', $values['feeBlock']);
350 }
351 $this->assign('location', $values['location']);
352
353 if (CRM_Core_Permission::check('access CiviEvent')) {
354 $enableCart = Civi::settings()->get('enable_cart');
355 $this->assign('manageEventLinks', CRM_Event_Page_ManageEvent::tabs($enableCart));
356 }
357
358 return parent::run();
359 }
360
361 /**
362 * @return string
363 */
364 public function getTemplateFileName() {
365 if ($this->_id) {
366 $templateFile = "CRM/Event/Page/{$this->_id}/EventInfo.tpl";
367 $template = CRM_Core_Page::getTemplate();
368
369 if ($template->template_exists($templateFile)) {
370 return $templateFile;
371 }
372 }
373 return parent::getTemplateFileName();
374 }
375
376 }