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