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