Merge branch 'master' into findById
[civicrm-core.git] / CRM / Event / Form / Registration.php
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 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36
37 /**
38 * This class generates form components for processing Event
39 *
40 */
41 class CRM_Event_Form_Registration extends CRM_Core_Form {
42
43 /**
44 * How many locationBlocks should we display?
45 *
46 * @var int
47 * @const
48 */
49 CONST LOCATION_BLOCKS = 1;
50
51 /**
52 * The id of the event we are proceessing
53 *
54 * @var int
55 * @protected
56 */
57 public $_eventId;
58
59 /**
60 * The array of ids of all the participant we are proceessing
61 *
62 * @var int
63 * @protected
64 */
65 protected $_participantIDS = NULL;
66
67 /**
68 * The id of the participant we are proceessing
69 *
70 * @var int
71 * @protected
72 */
73 protected $_participantId;
74
75 /**
76 * Is participant able to walk registration wizard.
77 *
78 * @var Boolean
79 * @protected
80 */
81 public $_allowConfirmation;
82
83 /**
84 * Is participant requires approval
85 *
86 * @var Boolean
87 * @public
88 */
89 public $_requireApproval;
90
91 /**
92 * Is event configured for waitlist.
93 *
94 * @var Boolean
95 * @public
96 */
97 public $_allowWaitlist;
98
99 /**
100 * Store additional participant ids
101 * when there are pre-registered.
102 *
103 * @var array
104 * @public
105 */
106 public $_additionalParticipantIds;
107
108 /**
109 * The mode that we are in
110 *
111 * @var string
112 * @protect
113 */
114 public $_mode;
115
116 /**
117 * The values for the contribution db object
118 *
119 * @var array
120 * @protected
121 */
122 public $_values;
123
124 /**
125 * The paymentProcessor attributes for this page
126 *
127 * @var array
128 * @protected
129 */
130 public $_paymentProcessor;
131
132 /**
133 * The params submitted by the form and computed by the app
134 *
135 * @var array
136 * @protected
137 */
138 protected $_params;
139
140 /**
141 * The fields involved in this contribution page
142 *
143 * @var array
144 * @protected
145 */
146 public $_fields;
147
148 /**
149 * The billing location id for this contribiution page
150 *
151 * @var int
152 * @protected
153 */
154 public $_bltID;
155
156 /**
157 * Price Set ID, if the new price set method is used
158 *
159 * @var int
160 * @protected
161 */
162 public $_priceSetId = NULL;
163
164 /**
165 * Array of fields for the price set
166 *
167 * @var array
168 * @protected
169 */
170 public $_priceSet;
171
172 public $_action;
173
174 public $_pcpId;
175
176 /* Is event already full.
177 *
178 * @var boolean
179 * @protected
180 */
181
182 public $_isEventFull;
183
184 public $_lineItem;
185 public $_lineItemParticipantsCount;
186 public $_availableRegistrations;
187
188 public $_forcePayement;
189
190 public $_isBillingAddressRequiredForPayLater;
191
192 /**
193 * Set variables up before form is built
194 *
195 * @return void
196 * @access public
197 */
198 function preProcess() {
199 $this->_eventId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
200 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
201
202 //CRM-4320
203 $this->_participantId = CRM_Utils_Request::retrieve('participantId', 'Positive', $this);
204
205 // current mode
206 $this->_mode = ($this->_action == 1024) ? 'test' : 'live';
207
208 $this->_values = $this->get('values');
209 $this->_fields = $this->get('fields');
210 $this->_bltID = $this->get('bltID');
211 $this->_paymentProcessor = $this->get('paymentProcessor');
212 $this->_priceSetId = $this->get('priceSetId');
213 $this->_priceSet = $this->get('priceSet');
214 $this->_lineItem = $this->get('lineItem');
215 $this->_isEventFull = $this->get('isEventFull');
216 $this->_lineItemParticipantsCount = $this->get('lineItemParticipants');
217 if (!is_array($this->_lineItem)) {
218 $this->_lineItem = array();
219 }
220 if (!is_array($this->_lineItemParticipantsCount)) {
221 $this->_lineItemParticipantsCount = array();
222 }
223 $this->_availableRegistrations = $this->get('availableRegistrations');
224 $this->_participantIDS = $this->get('participantIDs');
225
226 //check if participant allow to walk registration wizard.
227 $this->_allowConfirmation = $this->get('allowConfirmation');
228
229 // check for Approval
230 $this->_requireApproval = $this->get('requireApproval');
231
232 // check for waitlisting.
233 $this->_allowWaitlist = $this->get('allowWaitlist');
234
235 $this->_forcePayement = $this->get('forcePayement');
236
237 //get the additional participant ids.
238 $this->_additionalParticipantIds = $this->get('additionalParticipantIds');
239 $config = CRM_Core_Config::singleton();
240
241 if (!$this->_values) {
242 // create redirect URL to send folks back to event info page is registration not available
243 $infoUrl = CRM_Utils_System::url('civicrm/event/info', "reset=1&id={$this->_eventId}",
244 FALSE, NULL, FALSE, TRUE
245 );
246
247 // this is the first time we are hitting this, so check for permissions here
248 if (!CRM_Core_Permission::event(CRM_Core_Permission::EDIT, $this->_eventId, 'register for events')) {
249 CRM_Core_Error::statusBounce(ts('You do not have permission to register for this event'), $infoUrl);
250 }
251
252 // get all the values from the dao object
253 $this->_values = $this->_fields = array();
254 $this->_forcePayement = FALSE;
255
256 //retrieve event information
257 $params = array('id' => $this->_eventId);
258 CRM_Event_BAO_Event::retrieve($params, $this->_values['event']);
259
260 $this->checkValidEvent($infoUrl);
261 // get the participant values, CRM-4320
262 $this->_allowConfirmation = FALSE;
263 if ($this->_participantId) {
264 $this->processFirstParticipant($this->_participantId);
265 }
266 //check for additional participants.
267 if ($this->_allowConfirmation && $this->_values['event']['is_multiple_registrations']) {
268 $additionalParticipantIds = CRM_Event_BAO_Participant::getAdditionalParticipantIds($this->_participantId);
269 $cnt = 1;
270 foreach ($additionalParticipantIds as $additionalParticipantId) {
271 $this->_additionalParticipantIds[$cnt] = $additionalParticipantId;
272 $cnt++;
273 }
274 $this->set('additionalParticipantIds', $this->_additionalParticipantIds);
275 }
276
277 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE,
278 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
279 );
280
281 $this->_allowWaitlist = $this->_isEventFull = FALSE;
282 if ($eventFull && !$this->_allowConfirmation) {
283 $this->_isEventFull = TRUE;
284 //lets redirecting to info only when to waiting list.
285 $this->_allowWaitlist = CRM_Utils_Array::value('has_waitlist', $this->_values['event']);
286 if (!$this->_allowWaitlist) {
287 CRM_Utils_System::redirect($infoUrl);
288 }
289 }
290 $this->set('isEventFull', $this->_isEventFull);
291 $this->set('allowWaitlist', $this->_allowWaitlist);
292
293 //check for require requires approval.
294 $this->_requireApproval = FALSE;
295 if (!empty($this->_values['event']['requires_approval']) && !$this->_allowConfirmation) {
296 $this->_requireApproval = TRUE;
297 }
298 $this->set('requireApproval', $this->_requireApproval);
299
300 if (isset($this->_values['event']['default_role_id'])) {
301 $participant_role = CRM_Core_OptionGroup::values('participant_role');
302 $this->_values['event']['participant_role'] = $participant_role["{$this->_values['event']['default_role_id']}"];
303 }
304 // check for is_monetary status
305 $isMonetary = CRM_Utils_Array::value('is_monetary', $this->_values['event']);
306 $isPayLater = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'is_pay_later');
307 //check for variour combination for paylater, payment
308 //process with paid event.
309 if ($isMonetary && (!$isPayLater || !empty($this->_values['event']['payment_processor']))) {
310 $ppID = CRM_Utils_Array::value('payment_processor',
311 $this->_values['event']
312 );
313 if (!$ppID) {
314 CRM_Core_Error::statusBounce(ts('A payment processor must be selected for this event registration page, or the event must be configured to give users the option to pay later (contact the site administrator for assistance).'), $infoUrl);
315 }
316 $ppIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $ppID);
317 $this->_paymentProcessors = CRM_Financial_BAO_PaymentProcessor::getPayments($ppIds,
318 $this->_mode
319 );
320 $this->set('paymentProcessors', $this->_paymentProcessors);
321
322 //set default payment processor
323 if (!empty($this->_paymentProcessors) && empty($this->_paymentProcessor)) {
324 foreach ($this->_paymentProcessors as $ppId => $values) {
325 if ($values['is_default'] == 1 || (count($this->_paymentProcessors) == 1)) {
326 $defaultProcessorId = $ppId;
327 break;
328 }
329 }
330 }
331
332 if (isset($defaultProcessorId)) {
333 $this->_paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($defaultProcessorId, $this->_mode);
334 }
335 // make sure we have a valid payment class, else abort
336 if ($this->_values['event']['is_monetary']) {
337 if (!CRM_Utils_System::isNull($this->_paymentProcessors)) {
338 foreach ($this->_paymentProcessors as $eachPaymentProcessor) {
339 // check selected payment processor is active
340 if (!$eachPaymentProcessor) {
341 CRM_Core_Error::fatal(ts('The site administrator must set a Payment Processor for this event in order to use online registration.'));
342 }
343 // ensure that processor has a valid config
344 $payment = CRM_Core_Payment::singleton($this->_mode, $eachPaymentProcessor, $this);
345 $error = $payment->checkConfig();
346 if (!empty($error)) {
347 CRM_Core_Error::fatal($error);
348 }
349 }
350 }
351 }
352 }
353 //init event fee.
354 self::initEventFee($this, $this->_eventId);
355
356 // get the profile ids
357 $ufJoinParams = array(
358 'entity_table' => 'civicrm_event',
359 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
360 'module' => 'CiviEvent',
361 'entity_id' => $this->_eventId,
362 );
363 list($this->_values['custom_pre_id'],
364 $this->_values['custom_post_id']
365 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
366
367 // set profiles for additional participants
368 if ($this->_values['event']['is_multiple_registrations']) {
369 // CRM-4377: CiviEvent for the main participant, CiviEvent_Additional for additional participants
370 $ufJoinParams['module'] = 'CiviEvent_Additional';
371
372 list($this->_values['additional_custom_pre_id'],
373 $this->_values['additional_custom_post_id'], $preActive, $postActive
374 ) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
375
376 // CRM-4377: we need to maintain backward compatibility, hence if there is profile for main contact
377 // set same profile for additional contacts.
378 if ($this->_values['custom_pre_id'] && !$this->_values['additional_custom_pre_id']) {
379 $this->_values['additional_custom_pre_id'] = $this->_values['custom_pre_id'];
380 }
381
382 if ($this->_values['custom_post_id'] && !$this->_values['additional_custom_post_id']) {
383 $this->_values['additional_custom_post_id'] = $this->_values['custom_post_id'];
384 }
385 // now check for no profile condition, in that case is_active = 0
386 if (isset($preActive) && !$preActive) {
387 unset($this->_values['additional_custom_pre_id']);
388 }
389 if (isset($postActive) && !$postActive) {
390 unset($this->_values['additional_custom_post_id']);
391 }
392 }
393 // get the billing location type
394 $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id', array(), 'validate');
395 // CRM-8108 remove ts from Billing as the location type can not be translated in CiviCRM!
396 //$this->_bltID = array_search( ts('Billing'), $locationTypes );
397 $this->_bltID = array_search('Billing', $locationTypes);
398 if (!$this->_bltID) {
399 CRM_Core_Error::fatal(ts('Please set a location type of %1', array(1 => 'Billing')));
400 }
401 $this->set('bltID', $this->_bltID);
402
403 if ($this->_values['event']['is_monetary']) {
404 CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor);
405 }
406 $params = array('entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event');
407 $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
408
409 $this->set('values', $this->_values);
410 $this->set('fields', $this->_fields);
411
412 $this->_availableRegistrations =
413 CRM_Event_BAO_Participant::eventFull(
414 $this->_values['event']['id'], TRUE,
415 CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
416 );
417 $this->set('availableRegistrations', $this->_availableRegistrations);
418 }
419 $this->assign_by_ref('paymentProcessor', $this->_paymentProcessor);
420
421 // check if this is a paypal auto return and redirect accordingly
422 if (CRM_Core_Payment::paypalRedirect($this->_paymentProcessor)) {
423 $url = CRM_Utils_System::url('civicrm/event/register',
424 "_qf_ThankYou_display=1&qfKey={$this->controller->_key}"
425 );
426 CRM_Utils_System::redirect($url);
427 }
428 $this->_contributeMode = $this->get('contributeMode');
429 $this->assign('contributeMode', $this->_contributeMode);
430
431 // setting CMS page title
432 CRM_Utils_System::setTitle($this->_values['event']['title']);
433 $this->assign('title', $this->_values['event']['title']);
434
435 $this->assign('paidEvent', $this->_values['event']['is_monetary']);
436
437 // we do not want to display recently viewed items on Registration pages
438 $this->assign('displayRecent', FALSE);
439 // Registration page values are cleared from session, so can't use normal Printer Friendly view.
440 // Use Browser Print instead.
441 $this->assign('browserPrint', TRUE);
442
443 $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']);
444 $this->assign('isShowLocation', $isShowLocation);
445 // Handle PCP
446 $pcpId = CRM_Utils_Request::retrieve('pcpId', 'Positive', $this);
447 if ($pcpId) {
448 $pcp = CRM_PCP_BAO_PCP::handlePcp($pcpId, 'event', $this->_values['event']);
449 $this->_pcpId = $pcp['pcpId'];
450 $this->_values['event']['intro_text'] = CRM_Utils_Array::value('intro_text', $pcp['pcpInfo']);
451 }
452
453 // assign all event properties so wizard templates can display event info.
454 $this->assign('event', $this->_values['event']);
455 $this->assign('location', $this->_values['location']);
456 $this->assign('bltID', $this->_bltID);
457 $isShowLocation = CRM_Utils_Array::value('is_show_location', $this->_values['event']);
458 $this->assign('isShowLocation', $isShowLocation);
459 //CRM-6907
460 $config->defaultCurrency = CRM_Utils_Array::value('currency', $this->_values['event'],
461 $config->defaultCurrency
462 );
463
464 //lets allow user to override campaign.
465 $campID = CRM_Utils_Request::retrieve('campID', 'Positive', $this);
466 if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
467 $this->_values['event']['campaign_id'] = $campID;
468 }
469
470 // check if billing block is required for pay later
471 if (CRM_Utils_Array::value('is_pay_later', $this->_values['event'])) {
472 $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values['event']);
473 $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
474 }
475 }
476
477 /**
478 * Assign the minimal set of variables to the template
479 *
480 * @return void
481 * @access public
482 */
483 function assignToTemplate() {
484 //process only primary participant params
485 $this->_params = $this->get('params');
486 if (isset($this->_params[0])) {
487 $params = $this->_params[0];
488 }
489 $name = '';
490 if (!empty($params['billing_first_name'])) {
491 $name = $params['billing_first_name'];
492 }
493
494 if (!empty($params['billing_middle_name'])) {
495 $name .= " {$params['billing_middle_name']}";
496 }
497
498 if (!empty($params['billing_last_name'])) {
499 $name .= " {$params['billing_last_name']}";
500 }
501 $this->assign('billingName', $name);
502 $this->set('name', $name);
503
504 $vars = array(
505 'amount', 'currencyID', 'credit_card_type',
506 'trxn_id', 'amount_level', 'receive_date',
507 );
508
509 foreach ($vars as $v) {
510 if (!empty($params[$v])) {
511 if ($v == 'receive_date') {
512 $this->assign($v, CRM_Utils_Date::mysqlToIso($params[$v]));
513 }
514 else {
515 $this->assign($v, $params[$v]);
516 }
517 }
518 elseif (CRM_Utils_Array::value('amount', $params) == 0) {
519 $this->assign($v, CRM_Utils_Array::value($v, $params));
520 }
521 }
522
523 // assign the address formatted up for display
524 $addressParts = array(
525 "street_address-{$this->_bltID}",
526 "city-{$this->_bltID}",
527 "postal_code-{$this->_bltID}",
528 "state_province-{$this->_bltID}",
529 "country-{$this->_bltID}",
530 );
531 $addressFields = array();
532 foreach ($addressParts as $part) {
533 list($n, $id) = explode('-', $part);
534 if (isset($params['billing_' . $part])) {
535 $addressFields[$n] = CRM_Utils_Array::value('billing_' . $part, $params);
536 }
537 }
538
539 $this->assign('address', CRM_Utils_Address::format($addressFields));
540
541 if ($this->_contributeMode == 'direct' && empty($params['is_pay_later'])) {
542 $date = CRM_Utils_Date::format(CRM_Utils_Array::value('credit_card_exp_date', $params));
543 $date = CRM_Utils_Date::mysqlToIso($date);
544 $this->assign('credit_card_exp_date', $date);
545 $this->assign('credit_card_number',
546 CRM_Utils_System::mungeCreditCard(CRM_Utils_Array::value('credit_card_number', $params))
547 );
548 }
549
550 // get the email that the confirmation would have been sent to
551 $session = CRM_Core_Session::singleton();
552
553 // assign is_email_confirm to templates
554 if (isset($this->_values['event']['is_email_confirm'])) {
555 $this->assign('is_email_confirm', $this->_values['event']['is_email_confirm']);
556 }
557
558 // assign pay later stuff
559 $params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
560 $this->assign('is_pay_later', $params['is_pay_later']);
561 if ($params['is_pay_later']) {
562 $this->assign('pay_later_text', $this->_values['event']['pay_later_text']);
563 $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']);
564 }
565
566 // also assign all participantIDs to the template
567 // useful in generating confirmation numbers if needed
568 $this->assign('participantIDs',
569 $this->_participantIDS
570 );
571 }
572
573 /**
574 * Add the custom fields
575 *
576 * @param int $id
577 * @param string $name
578 * @param bool $viewOnly
579 *
580 * @return void
581 * @access public
582 */
583 function buildCustom($id, $name, $viewOnly = FALSE) {
584 if ($id) {
585 $button = substr($this->controller->getButtonName(), -4);
586 $cid = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
587 $session = CRM_Core_Session::singleton();
588 $contactID = $session->get('userID');
589
590 // we don't allow conflicting fields to be
591 // configured via profile
592 $fieldsToIgnore = array(
593 'participant_fee_amount' => 1,
594 'participant_fee_level' => 1,
595 );
596 if ($contactID) {
597 //FIX CRM-9653
598 if (is_array($id)) {
599 $fields = array();
600 foreach ($id as $profileID) {
601 $field = CRM_Core_BAO_UFGroup::getFields($profileID, FALSE, CRM_Core_Action::ADD,
602 NULL, NULL, FALSE, NULL,
603 FALSE, NULL, CRM_Core_Permission::CREATE,
604 'field_name', TRUE
605 );
606 $fields = array_merge($fields, $field);
607 }
608 }
609 else {
610 if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $contactID)) {
611 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
612 NULL, NULL, FALSE, NULL,
613 FALSE, NULL, CRM_Core_Permission::CREATE,
614 'field_name', TRUE
615 );
616 }
617 }
618 }
619 else {
620 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD,
621 NULL, NULL, FALSE, NULL,
622 FALSE, NULL, CRM_Core_Permission::CREATE,
623 'field_name', TRUE
624 );
625 }
626
627 if (array_intersect_key($fields, $fieldsToIgnore)) {
628 $fields = array_diff_key($fields, $fieldsToIgnore);
629 CRM_Core_Session::setStatus(ts('Some of the profile fields cannot be configured for this page.'));
630 }
631 $addCaptcha = FALSE;
632
633 if (!empty($this->_fields)) {
634 $fields = @array_diff_assoc($fields, $this->_fields);
635 }
636
637 if (empty($this->_params[0]['additional_participants']) &&
638 is_null($cid)
639 ) {
640 CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
641 }
642 $this->assign($name, $fields);
643 if (is_array($fields)) {
644 foreach ($fields as $key => $field) {
645 if ($viewOnly &&
646 isset($field['data_type']) &&
647 $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
648 ) {
649 // ignore file upload fields
650 continue;
651 }
652 //make the field optional if primary participant
653 //have been skip the additional participant.
654 if ($button == 'skip') {
655 $field['is_required'] = FALSE;
656 }
657 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
658 elseif ($field['add_captcha'] && !$contactID) {
659 // only add captcha for first page
660 $addCaptcha = TRUE;
661 }
662 list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
663 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE, $contactID, TRUE);
664
665 $this->_fields[$key] = $field;
666 }
667 }
668
669 if ($addCaptcha && !$viewOnly) {
670 $captcha = CRM_Utils_ReCAPTCHA::singleton();
671 $captcha->add($this);
672 $this->assign('isCaptcha', TRUE);
673 }
674 }
675 }
676
677 /**
678 * @param CRM_Core_Form $form
679 * @param int $eventID
680 *
681 * @throws Exception
682 */
683 static function initEventFee(&$form, $eventID) {
684 // get price info
685
686 // retrive all active price set fields.
687 $discountId = CRM_Core_BAO_Discount::findSet($eventID, 'civicrm_event');
688 if (property_exists($form, '_discountId') && $form->_discountId) {
689 $discountId = $form->_discountId;
690 }
691 if ($discountId) {
692 $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Core_BAO_Discount', $discountId, 'price_set_id');
693 $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE, $priceSetId);
694 }
695 else {
696 $price = CRM_Price_BAO_PriceSet::initSet($form, $eventID, 'civicrm_event', TRUE);
697 }
698
699 if (property_exists($form, '_context') && ($form->_context == 'standalone'
700 || $form->_context == 'participant')) {
701 $discountedEvent = CRM_Core_BAO_Discount::getOptionGroup($eventID, 'civicrm_event');
702 if (is_array( $discountedEvent)) {
703 foreach ($discountedEvent as $key => $priceSetId) {
704 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId);
705 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
706 $form->_values['discount'][$key] = CRM_Utils_Array::value('fields', $priceSet);
707 $fieldID = key($form->_values['discount'][$key]);
708 $form->_values['discount'][$key][$fieldID]['name'] = CRM_Core_DAO::getFieldValue(
709 'CRM_Price_DAO_PriceSet',
710 $priceSetId,
711 'title'
712 );
713 }
714 }
715 }
716 $eventFee = CRM_Utils_Array::value('fee', $form->_values);
717 if (!is_array($eventFee) || empty($eventFee)) {
718 $form->_values['fee'] = array();
719 }
720
721 //fix for non-upgraded price sets.CRM-4256.
722 if (isset($form->_isPaidEvent)) {
723 $isPaidEvent = $form->_isPaidEvent;
724 }
725 else {
726 $isPaidEvent = CRM_Utils_Array::value('is_monetary', $form->_values['event']);
727 }
728 if ($isPaidEvent && empty($form->_values['fee'])) {
729 if (CRM_Utils_System::getClassName($form) != 'CRM_Event_Form_Participant') {
730 CRM_Core_Error::fatal(ts('No Fee Level(s) or Price Set is configured for this event.<br />Click <a href=\'%1\'>CiviEvent >> Manage Event >> Configure >> Event Fees</a> to configure the Fee Level(s) or Price Set for this event.', array(1 => CRM_Utils_System::url('civicrm/event/manage/fee', 'reset=1&action=update&id=' . $form->_eventId))));
731 }
732 }
733 }
734
735 /**
736 * Handle process after the confirmation of payment by User
737 *
738 * @param int $contactID
739 * @param null $contribution
740 * @param null $payment
741 *
742 * @return void
743 * @access public
744 */
745 function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
746 // add/update contact information
747 $fields = array();
748 unset($this->_params['note']);
749
750 //to avoid conflict overwrite $this->_params
751 $this->_params = $this->get('value');
752
753 //get the amount of primary participant
754 if (!empty($this->_params['is_primary'])) {
755 $this->_params['fee_amount'] = $this->get('primaryParticipantAmount');
756 }
757
758 // add participant record
759 $participant = CRM_Event_Form_Registration::addParticipant($this, $contactID);
760 $this->_participantIDS[] = $participant->id;
761
762 //setting register_by_id field and primaryContactId
763 if (!empty($this->_params['is_primary'])) {
764 $this->set('registerByID', $participant->id);
765 $this->set('primaryContactId', $contactID);
766
767 // CRM-10032
768 $this->processFirstParticipant($participant->id);
769 }
770
771 CRM_Core_BAO_CustomValueTable::postProcess($this->_params,
772 CRM_Core_DAO::$_nullArray,
773 'civicrm_participant',
774 $participant->id,
775 'Participant'
776 );
777
778 $createPayment = (CRM_Utils_Array::value('amount', $this->_params, 0) != 0) ? TRUE : FALSE;
779
780 // force to create zero amount payment, CRM-5095
781 // we know the amout is zero since createPayment is false
782 if (!$createPayment &&
783 (isset($contribution) && $contribution->id) &&
784 $this->_priceSetId &&
785 $this->_lineItem
786 ) {
787 $createPayment = TRUE;
788 }
789
790 if ($createPayment && $this->_values['event']['is_monetary'] && !empty($this->_params['contributionID'])) {
791 $paymentParams = array(
792 'participant_id' => $participant->id,
793 'contribution_id' => $contribution->id,
794 );
795 $ids = array();
796 $paymentPartcipant = CRM_Event_BAO_ParticipantPayment::create($paymentParams, $ids);
797 }
798
799 //set only primary participant's params for transfer checkout.
800 if (($this->_contributeMode == 'checkout' || $this->_contributeMode == 'notify') && !empty($this->_params['is_primary'])) {
801 $this->_params['participantID'] = $participant->id;
802 $this->set('primaryParticipant', $this->_params);
803 }
804
805 $this->assign('action', $this->_action);
806
807 // create CMS user
808 if (!empty($this->_params['cms_create_account'])) {
809 $this->_params['contactID'] = $contactID;
810
811 if (array_key_exists('email-5', $this->_params)) {
812 $mail = 'email-5';
813 } else {
814 foreach ($this->_params as $name => $dontCare) {
815 if (substr($name, 0, 5) == 'email') {
816 $mail = $name;
817 break;
818 }
819 }
820 }
821
822 // we should use primary email for
823 // 1. free event registration.
824 // 2. pay later participant.
825 // 3. waiting list participant.
826 // 4. require approval participant.
827 if (!empty($this->_params['is_pay_later']) ||
828 $this->_allowWaitlist || $this->_requireApproval || empty($this->_values['event']['is_monetary'])) {
829 $mail = 'email-Primary';
830 }
831
832 if (!CRM_Core_BAO_CMSUser::create($this->_params, $mail)) {
833 CRM_Core_Error::statusBounce(ts('Your profile is not saved and Account is not created.'));
834 }
835 }
836 }
837
838 /**
839 * Process the participant
840 *
841 * @param array $params
842 * @param int $contactID
843 *
844 * @return void
845 * @access public
846 */
847 public static function addParticipant(&$form, $contactID) {
848 if (empty($form->_params)) {
849 return;
850 }
851 $params = $form->_params;
852 $transaction = new CRM_Core_Transaction();
853
854 $groupName = 'participant_role';
855 $query = "
856 SELECT v.label as label ,v.value as value
857 FROM civicrm_option_value v,
858 civicrm_option_group g
859 WHERE v.option_group_id = g.id
860 AND g.name = %1
861 AND v.is_active = 1
862 AND g.is_active = 1
863 ";
864 $p = array(1 => array($groupName, 'String'));
865
866 $dao = CRM_Core_DAO::executeQuery($query, $p);
867 if ($dao->fetch()) {
868 $roleID = $dao->value;
869 }
870
871 // handle register date CRM-4320
872 $registerDate = NULL;
873 if (!empty($form->_allowConfirmation) && $form->_participantId) {
874 $registerDate = $params['participant_register_date'];
875 }
876 elseif (!empty($params['participant_register_date']) &&
877 is_array($params['participant_register_date']) &&
878 !empty($params['participant_register_date'])
879 ) {
880 $registerDate = CRM_Utils_Date::format($params['participant_register_date']);
881 }
882
883 $participantFields = CRM_Event_DAO_Participant::fields();
884 $participantParams = array('id' => CRM_Utils_Array::value('participant_id', $params),
885 'contact_id' => $contactID,
886 'event_id' => $form->_eventId ? $form->_eventId : $params['event_id'],
887 'status_id' => CRM_Utils_Array::value('participant_status',
888 $params, 1
889 ),
890 'role_id' => CRM_Utils_Array::value('participant_role_id',
891 $params, $roleID
892 ),
893 'register_date' => ($registerDate) ? $registerDate : date('YmdHis'),
894 'source' => CRM_Utils_String::ellipsify(
895 isset($params['participant_source']) ?
896 CRM_Utils_Array::value('participant_source', $params) : CRM_Utils_Array::value('description', $params),
897 $participantFields['participant_source']['maxlength']
898 ),
899 'fee_level' => CRM_Utils_Array::value('amount_level', $params),
900 'is_pay_later' => CRM_Utils_Array::value('is_pay_later', $params, 0),
901 'fee_amount' => CRM_Utils_Array::value('fee_amount', $params),
902 'registered_by_id' => CRM_Utils_Array::value('registered_by_id', $params),
903 'discount_id' => CRM_Utils_Array::value('discount_id', $params),
904 'fee_currency' => CRM_Utils_Array::value('currencyID', $params),
905 'campaign_id' => CRM_Utils_Array::value('campaign_id', $params),
906 );
907
908 if ($form->_action & CRM_Core_Action::PREVIEW || CRM_Utils_Array::value('mode', $params) == 'test') {
909 $participantParams['is_test'] = 1;
910 }
911 else {
912 $participantParams['is_test'] = 0;
913 }
914
915 if (!empty($form->_params['note'])) {
916 $participantParams['note'] = $form->_params['note'];
917 }
918 elseif (!empty($form->_params['participant_note'])) {
919 $participantParams['note'] = $form->_params['participant_note'];
920 }
921
922 // reuse id if one already exists for this one (can happen
923 // with back button being hit etc)
924 if (!$participantParams['id'] && !empty($params['contributionID'])) {
925 $pID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment',
926 $params['contributionID'],
927 'participant_id',
928 'contribution_id'
929 );
930 $participantParams['id'] = $pID;
931 }
932 $participantParams['discount_id'] = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
933
934 if (!$participantParams['discount_id']) {
935 $participantParams['discount_id'] = "null";
936 }
937
938 $participant = CRM_Event_BAO_Participant::create($participantParams);
939
940 $transaction->commit();
941
942 return $participant;
943 }
944
945 /* Calculate the total participant count as per params.
946 *
947 * @param array $params user params.
948 *
949 * @return $totalCount total participant count.
950 * @access public
951 */
952 /**
953 * @param CRM_Core_Form $form
954 * @param array $params
955 * @param bool $skipCurrent
956 *
957 * @return int|string
958 */
959 public static function getParticipantCount(&$form, $params, $skipCurrent = FALSE) {
960 $totalCount = 0;
961 if (!is_array($params) || empty($params)) {
962 return $totalCount;
963 }
964
965 $priceSetId = $form->get('priceSetId');
966 $addParticipantNum = substr($form->_name, 12);
967 $priceSetFields = $priceSetDetails = array();
968 $hasPriceFieldsCount = FALSE;
969 if ($priceSetId) {
970 $priceSetDetails = $form->get('priceSet');
971 if (isset($priceSetDetails['optionsCountTotal'])
972 && $priceSetDetails['optionsCountTotal']
973 ) {
974 $hasPriceFieldsCount = TRUE;
975 $priceSetFields = $priceSetDetails['optionsCountDetails']['fields'];
976 }
977 }
978
979 $singleFormParams = FALSE;
980 foreach ($params as $key => $val) {
981 if (!is_numeric($key)) {
982 $singleFormParams = TRUE;
983 break;
984 }
985 }
986
987 //first format the params.
988 if ($singleFormParams) {
989 $params = self::formatPriceSetParams($form, $params);
990 $params = array($params);
991 }
992
993 foreach ($params as $key => $values) {
994 if (!is_numeric($key) ||
995 $values == 'skip' ||
996 ($skipCurrent && ($addParticipantNum == $key))
997 ) {
998 continue;
999 }
1000 $count = 1;
1001
1002 $usedCache = FALSE;
1003 $cacheCount = CRM_Utils_Array::value($key, $form->_lineItemParticipantsCount);
1004 if ($cacheCount && is_numeric($cacheCount)) {
1005 $count = $cacheCount;
1006 $usedCache = TRUE;
1007 }
1008
1009 if (!$usedCache && $hasPriceFieldsCount) {
1010 $count = 0;
1011 foreach ($values as $valKey => $value) {
1012 if (strpos($valKey, 'price_') === FALSE) {
1013 continue;
1014 }
1015 $priceFieldId = substr($valKey, 6);
1016 if (!$priceFieldId ||
1017 !is_array($value) ||
1018 !array_key_exists($priceFieldId, $priceSetFields)
1019 ) {
1020 continue;
1021 }
1022 foreach ($value as $optId => $optVal) {
1023 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1024 if ($currentCount) {
1025 $count += $currentCount;
1026 }
1027 }
1028 }
1029 if (!$count) {
1030 $count = 1;
1031 }
1032 }
1033 $totalCount += $count;
1034 }
1035 if (!$totalCount) {
1036 $totalCount = 1;
1037 }
1038
1039 return $totalCount;
1040 }
1041
1042 /* Format user submitted price set params.
1043 * Convert price set each param as an array.
1044 *
1045 * @param $params an array of user submitted params.
1046 *
1047 *
1048 * @return array $formatted, formatted price set params.
1049 * @access public
1050 */
1051 /**
1052 * @param CRM_Core_Form $form
1053 * @param array $params
1054 *
1055 * @return mixed
1056 */
1057 public static function formatPriceSetParams(&$form, $params) {
1058 if (!is_array($params) || empty($params)) {
1059 return $params;
1060 }
1061
1062 $priceSetId = $form->get('priceSetId');
1063 if (!$priceSetId) {
1064 return $params;
1065 }
1066 $priceSetDetails = $form->get('priceSet');
1067
1068 foreach ($params as $key => & $value) {
1069 $vals = array();
1070 if (strpos($key, 'price_') !== FALSE) {
1071 $fieldId = substr($key, 6);
1072 if (!array_key_exists($fieldId, $priceSetDetails['fields']) ||
1073 is_array($value) ||
1074 !$value
1075 ) {
1076 continue;
1077 }
1078 $field = $priceSetDetails['fields'][$fieldId];
1079 if ($field['html_type'] == 'Text') {
1080 $fieldOption = current($field['options']);
1081 $value = array($fieldOption['id'] => $value);
1082 }
1083 else {
1084 $value = array($value => TRUE);
1085 }
1086 }
1087 }
1088
1089 return $params;
1090 }
1091
1092 /* Calculate total count for each price set options.
1093 * those are currently selected by user.
1094 *
1095 * @param $form form object.
1096 *
1097 *
1098 * @return array $optionsCount, array of each option w/ count total.
1099 * @access public
1100 */
1101 /**
1102 * @param $form
1103 *
1104 * @return array
1105 */
1106 public static function getPriceSetOptionCount(&$form) {
1107 $params = $form->get('params');
1108 $priceSet = $form->get('priceSet');
1109 $priceSetId = $form->get('priceSetId');
1110
1111 $optionsCount = array();
1112 if (!$priceSetId ||
1113 !is_array($priceSet) ||
1114 empty($priceSet) ||
1115 !is_array($params) ||
1116 empty($params)
1117 ) {
1118 return $optionsCount;
1119 }
1120
1121 $priceSetFields = $priceMaxFieldDetails = array();
1122 if (!empty($priceSet['optionsCountTotal'])) {
1123 $priceSetFields = $priceSet['optionsCountDetails']['fields'];
1124 }
1125
1126 if (!empty($priceSet['optionsMaxValueTotal'])) {
1127 $priceMaxFieldDetails = $priceSet['optionsMaxValueDetails']['fields'];
1128 }
1129
1130 $addParticipantNum = substr($form->_name, 12);
1131 foreach ($params as $pCnt => $values) {
1132 if ($values == 'skip' ||
1133 $pCnt == $addParticipantNum
1134 ) {
1135 continue;
1136 }
1137
1138 foreach ($values as $valKey => $value) {
1139 if (strpos($valKey, 'price_') === FALSE) {
1140 continue;
1141 }
1142
1143 $priceFieldId = substr($valKey, 6);
1144 if (!$priceFieldId ||
1145 !is_array($value) ||
1146 !(array_key_exists($priceFieldId, $priceSetFields) || array_key_exists($priceFieldId, $priceMaxFieldDetails))
1147 ) {
1148 continue;
1149 }
1150
1151 foreach ($value as $optId => $optVal) {
1152 if (CRM_Utils_Array::value('html_type', $priceSet['fields'][$priceFieldId]) == 'Text') {
1153 $currentCount = $optVal;
1154 }
1155 else {
1156 $currentCount = 1;
1157 }
1158
1159 if (isset($priceSetFields[$priceFieldId]) && isset($priceSetFields[$priceFieldId]['options'][$optId])) {
1160 $currentCount = $priceSetFields[$priceFieldId]['options'][$optId] * $optVal;
1161 }
1162
1163 $optionsCount[$optId] = $currentCount + CRM_Utils_Array::value($optId, $optionsCount, 0);
1164 }
1165 }
1166 }
1167
1168 return $optionsCount;
1169 }
1170
1171 /**
1172 * @param string $suffix
1173 *
1174 * @return null|string
1175 */
1176 function checkTemplateFileExists($suffix = '') {
1177 if ($this->_eventId) {
1178 $templateName = $this->_name;
1179 if (substr($templateName, 0, 12) == 'Participant_') {
1180 $templateName = 'AdditionalParticipant';
1181 }
1182
1183 $templateFile = "CRM/Event/Form/Registration/{$this->_eventId}/{$templateName}.{$suffix}tpl";
1184 $template = CRM_Core_Form::getTemplate();
1185 if ($template->template_exists($templateFile)) {
1186 return $templateFile;
1187 }
1188 }
1189 return NULL;
1190 }
1191
1192 /**
1193 * @return null|string
1194 */
1195 function getTemplateFileName() {
1196 $fileName = $this->checkTemplateFileExists();
1197 return $fileName ? $fileName : parent::getTemplateFileName();
1198 }
1199
1200 /**
1201 * @return null|string
1202 */
1203 function overrideExtraTemplateFileName() {
1204 $fileName = $this->checkTemplateFileExists('extra.');
1205 return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
1206 }
1207
1208 /**
1209 * Validate price set submitted params for price option limit,
1210 * as well as user should select at least one price field option.
1211 * @param unknown_type $form
1212 * @param unknown_type $params
1213 * @return multitype:|Ambigous <multitype:, string, string>
1214 */
1215 static function validatePriceSet(&$form, $params) {
1216 $errors = array();
1217 $hasOptMaxValue = FALSE;
1218 if (!is_array($params) || empty($params)) {
1219 return $errors;
1220 }
1221
1222 $currentParticipantNum = substr($form->_name, 12);
1223 if (!$currentParticipantNum) {
1224 $currentParticipantNum = 0;
1225 }
1226
1227 $priceSetId = $form->get('priceSetId');
1228 $priceSetDetails = $form->get('priceSet');
1229 if (
1230 !$priceSetId ||
1231 !is_array($priceSetDetails) ||
1232 empty($priceSetDetails)
1233 ) {
1234 return $errors;
1235 }
1236
1237 $optionsCountDetails = $optionsMaxValueDetails = array();
1238 if (
1239 isset($priceSetDetails['optionsMaxValueTotal'])
1240 && $priceSetDetails['optionsMaxValueTotal']
1241 ) {
1242 $hasOptMaxValue = TRUE;
1243 $optionsMaxValueDetails = $priceSetDetails['optionsMaxValueDetails']['fields'];
1244 }
1245 if (
1246 isset($priceSetDetails['optionsCountTotal'])
1247 && $priceSetDetails['optionsCountTotal']
1248 ) {
1249 $hasOptCount = TRUE;
1250 $optionsCountDetails = $priceSetDetails['optionsCountDetails']['fields'];
1251 }
1252 $feeBlock = $form->_feeBlock;
1253
1254 if (empty($feeBlock)) {
1255 $feeBlock = $priceSetDetails['fields'];
1256 }
1257
1258 $optionMaxValues = $fieldSelected = array();
1259 foreach ($params as $pNum => $values) {
1260 if (!is_array($values) || $values == 'skip') {
1261 continue;
1262 }
1263
1264 foreach ($values as $valKey => $value) {
1265 if (strpos($valKey, 'price_') === FALSE) {
1266 continue;
1267 }
1268 $priceFieldId = substr($valKey, 6);
1269 $noneOptionValueSelected = FALSE;
1270 if (!$feeBlock[$priceFieldId]['is_required'] && $value == 0) {
1271 $noneOptionValueSelected = TRUE;
1272 }
1273
1274 if (
1275 !$priceFieldId ||
1276 (!$noneOptionValueSelected && !is_array($value))
1277 ) {
1278 continue;
1279 }
1280
1281 $fieldSelected[$pNum] = TRUE;
1282
1283 if (!$hasOptMaxValue || !is_array($value)) {
1284 continue;
1285 }
1286
1287 foreach ($value as $optId => $optVal) {
1288 if (CRM_Utils_Array::value('html_type', $feeBlock[$priceFieldId]) == 'Text') {
1289 $currentMaxValue = $optVal;
1290 }
1291 else {
1292 $currentMaxValue = 1;
1293 }
1294
1295 if (isset($optionsCountDetails[$priceFieldId]) && isset($optionsCountDetails[$priceFieldId]['options'][$optId])) {
1296 $currentMaxValue = $optionsCountDetails[$priceFieldId]['options'][$optId] * $optVal;
1297 }
1298 if (empty($optionMaxValues)) {
1299 $optionMaxValues[$priceFieldId][$optId] = $currentMaxValue;
1300 }
1301 else {
1302 $optionMaxValues[$priceFieldId][$optId] =
1303 $currentMaxValue + CRM_Utils_Array::value($optId, CRM_Utils_Array::value($priceFieldId, $optionMaxValues), 0);
1304 }
1305
1306 }
1307 }
1308 }
1309
1310 //validate for option max value.
1311 foreach ($optionMaxValues as $fieldId => $values) {
1312 $options = CRM_Utils_Array::value('options', $feeBlock[$fieldId], array());
1313 foreach ($values as $optId => $total) {
1314 $optMax = $optionsMaxValueDetails[$fieldId]['options'][$optId];
1315 $opDbCount = CRM_Utils_Array::value('db_total_count', $options[$optId], 0);
1316 $total += $opDbCount;
1317 if ($optMax && $total > $optMax) {
1318 if ($opDbCount && ($opDbCount >= $optMax)) {
1319 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1320 ts('Sorry, this option is currently sold out.');
1321 }
1322 elseif (($optMax - $opDbCount) == 1) {
1323 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1324 ts('Sorry, currently only a single seat is available for this option.', array(1 => ($optMax - $opDbCount)));
1325 }
1326 else {
1327 $errors[$currentParticipantNum]["price_{$fieldId}"] =
1328 ts('Sorry, currently only %1 seats are available for this option.', array(1 => ($optMax - $opDbCount)));
1329 }
1330 }
1331 }
1332 }
1333
1334 //validate for price field selection.
1335 foreach ($params as $pNum => $values) {
1336 if (!is_array($values) || $values == 'skip') {
1337 continue;
1338 }
1339 if (empty($fieldSelected[$pNum])) {
1340 $errors[$pNum]['_qf_default'] = ts('Select at least one option from Event Fee(s).');
1341 }
1342 }
1343
1344 return $errors;
1345 }
1346
1347 // set the first participant ID if not set, CRM-10032
1348 /**
1349 * @param int $participantID
1350 */
1351 function processFirstParticipant($participantID) {
1352 $this->_participantId = $participantID;
1353 $this->set('participantId', $this->_participantId);
1354
1355 $ids = $participantValues = array();
1356 $participantParams = array('id' => $this->_participantId);
1357 CRM_Event_BAO_Participant::getValues($participantParams, $participantValues, $ids);
1358 $this->_values['participant'] = $participantValues[$this->_participantId];
1359 $this->set('values', $this->_values);
1360
1361 // also set the allow confirmation stuff
1362 if (array_key_exists(
1363 $this->_values['participant']['status_id'],
1364 CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Pending'")
1365 )) {
1366 $this->_allowConfirmation = TRUE;
1367 $this->set('allowConfirmation', TRUE);
1368 }
1369 }
1370
1371 /**
1372 * @todo - combine this with CRM_Event_BAO_Event::validRegistrationRequest
1373 * (probably extract relevant values here & call that with them & handle bounces & redirects here -as
1374 * those belong in the form layer)
1375 *
1376 * @param string $redirect
1377 */
1378 function checkValidEvent($redirect = NULL) {
1379 // is the event active (enabled)?
1380 if (!$this->_values['event']['is_active']) {
1381 // form is inactive, die a fatal death
1382 CRM_Core_Error::statusBounce(ts('The event you requested is currently unavailable (contact the site administrator for assistance).'));
1383 }
1384
1385 // is online registration is enabled?
1386 if (!$this->_values['event']['is_online_registration']) {
1387 CRM_Core_Error::statusBounce(ts('Online registration is not currently available for this event (contact the site administrator for assistance).'), $redirect);
1388 }
1389
1390 // is this an event template ?
1391 if (!empty($this->_values['event']['is_template'])) {
1392 CRM_Core_Error::statusBounce(ts('Event templates are not meant to be registered.'), $redirect);
1393 }
1394
1395 $now = date('YmdHis');
1396 $startDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_start_date',
1397 $this->_values['event']
1398 ));
1399
1400 if (
1401 $startDate &&
1402 $startDate >= $now
1403 ) {
1404 CRM_Core_Error::statusBounce(ts('Registration for this event begins on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_start_date', $this->_values['event'])))), $redirect);
1405 }
1406
1407 $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date',
1408 $this->_values['event']
1409 ));
1410 $eventEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('event_end_date', $this->_values['event']));
1411 if (
1412 $endDate &&
1413 $endDate < $now
1414 ) {
1415 CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event'])))), $redirect);
1416 }
1417 if (!empty($eventEndDate) && $eventEndDate < $now) {
1418 CRM_Core_Error::statusBounce(ts('Event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('event_end_date', $this->_values['event'])))), $redirect);
1419 }
1420 }
1421 }
1422