Merge pull request #9632 from yashodha/CRM-19795
[civicrm-core.git] / CRM / Event / Form / Registration / Register.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
0f03f337 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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/**
6a488035 29 * @package CRM
0f03f337 30 * @copyright CiviCRM LLC (c) 2004-2017
6a488035
TO
31 */
32
33/**
3bdf1f3a 34 * This class generates form components for processing Event.
6a488035
TO
35 */
36class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
37
38 /**
66f9e52b 39 * The fields involved in this page.
6a488035
TO
40 */
41 public $_fields;
42
6a488035
TO
43 /**
44 * The status message that user view.
6a488035
TO
45 */
46 protected $_waitlistMsg = NULL;
47 protected $_requireApprovalMsg = NULL;
48
3bdf1f3a 49 /**
50 * Deprecated parameter that we hope to remove.
51 *
52 * @var bool
53 */
6a488035
TO
54 public $_quickConfig = NULL;
55
56 /**
3bdf1f3a 57 * Skip duplicate check.
58 *
59 * This can be set using hook_civicrm_buildForm() to override the registration dupe check.
6a488035
TO
60 * CRM-7604
61 */
62 public $_skipDupeRegistrationCheck = FALSE;
63
cc789d46 64 public $_paymentProcessorID;
6a488035 65
16d1c8e2 66 /**
3bdf1f3a 67 * Show fee block or not.
68 *
16d1c8e2 69 * @var boolean determines if fee block should be shown or hidden
70 */
71 public $_noFees;
72
cc789d46 73 /**
3bdf1f3a 74 * Array of payment related fields to potentially display on this form (generally credit card or debit card fields).
75 *
76 * This is rendered via billingBlock.tpl.
77 *
cc789d46
EM
78 * @var array
79 */
80 public $_paymentFields = array();
81
6a488035 82 /**
66f9e52b 83 * Set variables up before form is built.
6a488035 84 */
00be9182 85 public function preProcess() {
6a488035 86 parent::preProcess();
7bf9cde2 87
6a488035
TO
88 //CRM-4320.
89 //here we can't use parent $this->_allowWaitlist as user might
cc789d46 90 //walk back and we might set this value in this postProcess.
6a488035 91 //(we set when spaces < group count and want to allow become part of waiting )
6a488035
TO
92 $eventFull = CRM_Event_BAO_Participant::eventFull($this->_eventId, FALSE, CRM_Utils_Array::value('has_waitlist', $this->_values['event']));
93
b6a469c5
CW
94 // Get payment processors if appropriate for this event
95 // We hide the payment fields if the event is full or requires approval,
96 // and the current user has not yet been approved CRM-12279
16d1c8e2 97 $this->_noFees = (($eventFull || $this->_requireApproval) && !$this->_allowConfirmation);
42e3a033
EM
98 $this->_paymentProcessors = $this->_noFees ? array() : $this->get('paymentProcessors');
99 $this->preProcessPaymentOptions();
b6a469c5 100
6a488035 101 $this->_allowWaitlist = FALSE;
8cc574cf 102 if ($eventFull && !$this->_allowConfirmation && !empty($this->_values['event']['has_waitlist'])) {
6a488035
TO
103 $this->_allowWaitlist = TRUE;
104 $this->_waitlistMsg = CRM_Utils_Array::value('waitlist_text', $this->_values['event']);
105 if (!$this->_waitlistMsg) {
106 $this->_waitlistMsg = ts('This event is currently full. However you can register now and get added to a waiting list. You will be notified if spaces become available.');
107 }
108 }
109 $this->set('allowWaitlist', $this->_allowWaitlist);
110
111 //To check if the user is already registered for the event(CRM-2426)
112 if (!$this->_skipDupeRegistrationCheck) {
113 self::checkRegistration(NULL, $this);
114 }
115
116 $this->assign('availableRegistrations', $this->_availableRegistrations);
117
118 // get the participant values from EventFees.php, CRM-4320
119 if ($this->_allowConfirmation) {
120 CRM_Event_Form_EventFees::preProcess($this);
121 }
6a488035
TO
122 }
123
124 /**
70d1766d 125 * Set default values for the form.
6a488035 126 */
00be9182 127 public function setDefaultValues() {
2ab5ff1d 128 $this->_defaults = array();
5ec4b965 129 if (!$this->_allowConfirmation && $this->_requireApproval) {
130 $this->_defaults['bypass_payment'] = 1;
131 }
5c280496 132 $contactID = $this->getContactID();
70d1766d 133 CRM_Core_Payment_Form::setDefaultValues($this, $contactID);
a28e436f 134
c7d52684 135 CRM_Event_BAO_Participant::formatFieldsAndSetProfileDefaults($contactID, $this);
3feb567a 136
13ac605f
DG
137 // Set default payment processor as default payment_processor radio button value
138 if (!empty($this->_paymentProcessors)) {
139 foreach ($this->_paymentProcessors as $pid => $value) {
a7488080 140 if (!empty($value['is_default'])) {
e02d7e96 141 $this->_defaults['payment_processor_id'] = $pid;
13ac605f
DG
142 }
143 }
144 }
145
6a488035
TO
146 //if event is monetary and pay later is enabled and payment
147 //processor is not available then freeze the pay later checkbox with
148 //default check
a7488080 149 if (!empty($this->_values['event']['is_pay_later']) &&
6a488035
TO
150 !is_array($this->_paymentProcessor)
151 ) {
152 $this->_defaults['is_pay_later'] = 1;
153 }
154
155 //set custom field defaults
156 if (!empty($this->_fields)) {
157 //load default campaign from page.
158 if (array_key_exists('participant_campaign_id', $this->_fields)) {
159 $this->_defaults['participant_campaign_id'] = CRM_Utils_Array::value('campaign_id',
160 $this->_values['event']
161 );
162 }
163
164 foreach ($this->_fields as $name => $field) {
165 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
166 // fix for CRM-1743
167 if (!isset($this->_defaults[$name])) {
168 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
169 NULL, CRM_Profile_Form::MODE_REGISTER
170 );
171 }
172 }
173 }
174 }
175
176 //fix for CRM-3088, default value for discount set.
177 $discountId = NULL;
178 if (!empty($this->_values['discount'])) {
179 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
180 if ($discountId) {
181 if (isset($this->_values['event']['default_discount_fee_id'])) {
182 $discountKey = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
183 $this->_values['event']['default_discount_fee_id'],
184 'weight', 'id'
185 );
186
187 $this->_defaults['amount'] = key(array_slice($this->_values['discount'][$discountId],
353ffa53
TO
188 $discountKey - 1, $discountKey, TRUE
189 ));
6a488035
TO
190 }
191 }
192 }
193
194 // add this event's default participant role to defaults array
195 // (for cases where participant_role field is included in form via profile)
196 if ($this->_values['event']['default_role_id']) {
608e6658 197 $this->_defaults['participant_role']
198 = $this->_defaults['participant_role_id'] = $this->_values['event']['default_role_id'];
6a488035
TO
199 }
200 if ($this->_priceSetId && !empty($this->_feeBlock)) {
201 foreach ($this->_feeBlock as $key => $val) {
0dc0b759 202 if (empty($val['options'])) {
203 continue;
204 }
205 $optionFullIds = CRM_Utils_Array::value('option_full_ids', $val, array());
6a488035 206 foreach ($val['options'] as $keys => $values) {
8cc574cf 207 if ($values['is_default'] && empty($values['is_full'])) {
6a488035
TO
208
209 if ($val['html_type'] == 'CheckBox') {
210 $this->_defaults["price_{$key}"][$keys] = 1;
211 }
212 else {
213 $this->_defaults["price_{$key}"] = $keys;
214 }
215 }
216 }
0dc0b759 217 $unsetSubmittedOptions[$val['id']] = $optionFullIds;
6a488035 218 }
0dc0b759 219 //reset values for all options those are full.
220 CRM_Event_Form_Registration::resetElementValue($unsetSubmittedOptions, $this);
6a488035
TO
221 }
222
223 //set default participant fields, CRM-4320.
224 $hasAdditionalParticipants = FALSE;
225 if ($this->_allowConfirmation) {
226 $this->_contactId = $contactID;
227 $this->_discountId = $discountId;
228 $forcePayLater = CRM_Utils_Array::value('is_pay_later', $this->_defaults, FALSE);
229 $this->_defaults = array_merge($this->_defaults, CRM_Event_Form_EventFees::setDefaultValues($this));
230 $this->_defaults['is_pay_later'] = $forcePayLater;
231
232 if ($this->_additionalParticipantIds) {
233 $hasAdditionalParticipants = TRUE;
234 $this->_defaults['additional_participants'] = count($this->_additionalParticipantIds);
235 }
236 }
237 $this->assign('hasAdditionalParticipants', $hasAdditionalParticipants);
238
239 // //hack to simplify credit card entry for testing
240 // $this->_defaults['credit_card_type'] = 'Visa';
241 // $this->_defaults['credit_card_number'] = '4807731747657838';
242 // $this->_defaults['cvv2'] = '000';
243 // $this->_defaults['credit_card_exp_date'] = array( 'Y' => '2010', 'M' => '05' );
244
245 // to process Custom data that are appended to URL
246 $getDefaults = CRM_Core_BAO_CustomGroup::extractGetParams($this, "'Contact', 'Individual', 'Contribution', 'Participant'");
247 if (!empty($getDefaults)) {
248 $this->_defaults = array_merge($this->_defaults, $getDefaults);
249 }
250
251 return $this->_defaults;
252 }
253
254 /**
66f9e52b 255 * Build the form object.
6a488035
TO
256 */
257 public function buildQuickForm() {
4839c695
KJ
258 // build profiles first so that we can determine address fields etc
259 // and then show copy address checkbox
260 $this->buildCustom($this->_values['custom_pre_id'], 'customPre');
261 $this->buildCustom($this->_values['custom_post_id'], 'customPost');
262
d38c288e 263 // CRM-18399: used by template to pass pre profile id as a url arg
264 $this->assign('custom_pre_id', $this->_values['custom_pre_id']);
4839c695 265
cc789d46 266 CRM_Core_Payment_ProcessorForm::buildQuickForm($this);
6a488035 267
5c280496 268 $contactID = $this->getContactID();
37326fa1
DG
269 if ($contactID) {
270 $this->assign('contact_id', $contactID);
f498a273 271 $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
37326fa1 272 }
6a488035 273
6a488035
TO
274 $this->add('hidden', 'scriptFee', NULL);
275 $this->add('hidden', 'scriptArray', NULL);
276
277 $bypassPayment = $allowGroupOnWaitlist = $isAdditionalParticipants = FALSE;
278 if ($this->_values['event']['is_multiple_registrations']) {
279 // don't allow to add additional during confirmation if not preregistered.
280 if (!$this->_allowConfirmation || $this->_additionalParticipantIds) {
f1bc01e0 281 // CRM-17745: Make maximum additional participants configurable
6a488035 282 // Label is value + 1, since the code sees this is ADDITIONAL participants (in addition to "self")
f1bc01e0
J
283 $additionalOptions = array();
284 $additionalOptions[''] = 1;
30ddbdcb 285 for ($i = 1; $i <= $this->_values['event']['max_additional_participants']; $i++) {
f1bc01e0
J
286 $additionalOptions[$i] = $i + 1;
287 }
5f39c1a6 288 $this->add('select', 'additional_participants',
6a488035
TO
289 ts('How many people are you registering?'),
290 $additionalOptions,
291 NULL,
292 array('onChange' => "allowParticipant()")
293 );
294 $isAdditionalParticipants = TRUE;
295 }
296 }
297
5ec4b965 298 if (!$this->_allowConfirmation) {
299 $bypassPayment = TRUE;
300 }
301
6a488035 302 //hack to allow group to register w/ waiting
8cc574cf 303 if ((!empty($this->_values['event']['is_multiple_registrations']) ||
6a488035
TO
304 $this->_priceSetId
305 ) &&
306 !$this->_allowConfirmation &&
353ffa53
TO
307 is_numeric($this->_availableRegistrations) && !empty($this->_values['event']['has_waitlist'])
308 ) {
6a488035
TO
309 $bypassPayment = TRUE;
310 //case might be group become as a part of waitlist.
311 //If not waitlist then they require admin approve.
312 $allowGroupOnWaitlist = TRUE;
313 $this->_waitlistMsg = ts("This event has only %1 space(s) left. If you continue and register more than %1 people (including yourself ), the whole group will be wait listed. Or, you can reduce the number of people you are registering to %1 to avoid being put on the waiting list.", array(1 => $this->_availableRegistrations));
314
315 if ($this->_requireApproval) {
316 $this->_requireApprovalMsg = CRM_Utils_Array::value('approval_req_text', $this->_values['event'],
317 ts('Registration for this event requires approval. Once your registration(s) have been reviewed, you will receive an email with a link to a web page where you can complete the registration process.')
318 );
319 }
320 }
321
322 //case where only approval needed - no waitlist.
323 if ($this->_requireApproval &&
324 !$this->_allowWaitlist && !$bypassPayment
325 ) {
326 $this->_requireApprovalMsg = CRM_Utils_Array::value('approval_req_text', $this->_values['event'],
327 ts('Registration for this event requires approval. Once your registration has been reviewed, you will receive an email with a link to a web page where you can complete the registration process.')
328 );
329 }
330
331 //lets display status to primary page only.
332 $this->assign('waitlistMsg', $this->_waitlistMsg);
333 $this->assign('requireApprovalMsg', $this->_requireApprovalMsg);
334 $this->assign('allowGroupOnWaitlist', $allowGroupOnWaitlist);
335 $this->assign('isAdditionalParticipants', $isAdditionalParticipants);
336
6a488035
TO
337 if ($this->_values['event']['is_monetary']) {
338 self::buildAmount($this);
339 }
340
596bff78 341 $pps = array();
342 //@todo this processor adding fn is another one duplicated on contribute - a shared
343 // common class would make this sort of thing extractable
cf6a124f 344 $onlinePaymentProcessorEnabled = FALSE;
6a488035 345 if (!empty($this->_paymentProcessors)) {
596bff78 346 foreach ($this->_paymentProcessors as $key => $name) {
22e263ad 347 if ($name['billing_mode'] == 1) {
596bff78 348 $onlinePaymentProcessorEnabled = TRUE;
349 }
6a488035
TO
350 $pps[$key] = $name['name'];
351 }
352 }
aa288d3f 353 if ($this->getContactID() === 0 && !$this->_values['event']['is_multiple_registrations']) {
e1ce628e 354 //@todo we are blocking for multiple registrations because we haven't tested
596bff78 355 $this->addCidZeroOptions($onlinePaymentProcessorEnabled);
356 }
a7488080 357 if (!empty($this->_values['event']['is_pay_later']) &&
6a488035
TO
358 ($this->_allowConfirmation || (!$this->_requireApproval && !$this->_allowWaitlist))
359 ) {
360 $pps[0] = $this->_values['event']['pay_later_text'];
361 }
362
363 if ($this->_values['event']['is_monetary']) {
364 if (count($pps) > 1) {
e02d7e96 365 $this->addRadio('payment_processor_id', ts('Payment Method'), $pps,
fdf1844b 366 NULL, "&nbsp;"
6a488035
TO
367 );
368 }
369 elseif (!empty($pps)) {
370 $ppKeys = array_keys($pps);
371 $currentPP = array_pop($ppKeys);
e02d7e96 372 $this->addElement('hidden', 'payment_processor_id', $currentPP);
6a488035
TO
373 }
374 }
375
5ec4b965 376 $this->addElement('hidden', 'bypass_payment', NULL, array('id' => 'bypass_payment'));
377
6a488035 378 $this->assign('bypassPayment', $bypassPayment);
6a488035 379
5c280496 380 $userID = $this->getContactID();
6a488035
TO
381
382 if (!$userID) {
383 $createCMSUser = FALSE;
384
385 if ($this->_values['custom_pre_id']) {
386 $profileID = $this->_values['custom_pre_id'];
387 $createCMSUser = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
388 }
389
390 if (!$createCMSUser &&
391 $this->_values['custom_post_id']
392 ) {
393 if (!is_array($this->_values['custom_post_id'])) {
394 $profileIDs = array($this->_values['custom_post_id']);
395 }
396 else {
397 $profileIDs = $this->_values['custom_post_id'];
398 }
399 foreach ($profileIDs as $pid) {
400 if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
401 $profileID = $pid;
402 $createCMSUser = TRUE;
403 break;
404 }
405 }
406 }
407
408 if ($createCMSUser) {
409 CRM_Core_BAO_CMSUser::buildForm($this, $profileID, TRUE);
410 }
411 }
412
413 //we have to load confirm contribution button in template
414 //when multiple payment processor as the user
415 //can toggle with payment processor selection
416 $billingModePaymentProcessors = 0;
417 if (!CRM_Utils_System::isNull($this->_paymentProcessors)) {
418 foreach ($this->_paymentProcessors as $key => $values) {
419 if ($values['billing_mode'] == CRM_Core_Payment::BILLING_MODE_BUTTON) {
420 $billingModePaymentProcessors++;
421 }
422 }
423 }
424
425 if ($billingModePaymentProcessors && count($this->_paymentProcessors) == $billingModePaymentProcessors) {
426 $allAreBillingModeProcessors = TRUE;
0db6c3e1
TO
427 }
428 else {
6a488035
TO
429 $allAreBillingModeProcessors = FALSE;
430 }
431
8cc574cf 432 if (!$allAreBillingModeProcessors || !empty($this->_values['event']['is_pay_later']) || $bypassPayment
6a488035
TO
433 ) {
434
435 //freeze button to avoid multiple calls.
436 $js = NULL;
437
a7488080 438 if (empty($this->_values['event']['is_monetary'])) {
6a488035
TO
439 $js = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');");
440 }
2a6da8d7 441
1909126f 442 // CRM-11182 - Optional confirmation screen
443 // Change button label depending on whether the next action is confirm or register
444 if (
445 !$this->_values['event']['is_multiple_registrations']
d6121d3e 446 && !$this->_values['event']['is_monetary']
1909126f 447 && !$this->_values['event']['is_confirm_enabled']
448 ) {
f212d37d 449 $buttonLabel = ts('Register');
0db6c3e1
TO
450 }
451 else {
f212d37d 452 $buttonLabel = ts('Continue');
1909126f 453 }
2a6da8d7 454
6a488035
TO
455 $this->addButtons(array(
456 array(
457 'type' => 'upload',
1909126f 458 'name' => $buttonLabel,
6a488035
TO
459 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
460 'isDefault' => TRUE,
461 'js' => $js,
462 ),
463 )
464 );
465 }
466
467 $this->addFormRule(array('CRM_Event_Form_Registration_Register', 'formRule'), $this);
86c0d461 468 $this->unsavedChangesWarn = TRUE;
6a488035
TO
469
470 // add pcp fields
471 if ($this->_pcpId) {
472 CRM_PCP_BAO_PCP::buildPcp($this->_pcpId, $this);
473 }
474 }
475
476 /**
100fef9d 477 * Build the radio/text form elements for the amount field
6a488035 478 *
d4dd1e85
TO
479 * @param CRM_Core_Form $form
480 * Form object.
481 * @param bool $required
482 * True if you want to add formRule.
483 * @param int $discountId
484 * Discount id for the event.
6a488035
TO
485 */
486 static public function buildAmount(&$form, $required = TRUE, $discountId = NULL) {
16d1c8e2 487 // build amount only when needed, skip incase of event full and waitlisting is enabled
488 // and few other conditions check preProcess()
a2a1e950 489 if (property_exists($form, '_noFees') && $form->_noFees) {
16d1c8e2 490 return;
491 }
492
6a488035 493 //if payment done, no need to build the fee block.
7bf9cde2 494 if (!empty($form->_paymentId)) {
b6e641a4 495 //fix to display line item in update mode.
6a488035
TO
496 $form->assign('priceSet', isset($form->_priceSet) ? $form->_priceSet : NULL);
497 return;
498 }
499
500 $feeFields = CRM_Utils_Array::value('fee', $form->_values);
501
502 if (is_array($feeFields)) {
503 $form->_feeBlock = &$form->_values['fee'];
504 }
505
506 //check for discount.
507 $discountedFee = CRM_Utils_Array::value('discount', $form->_values);
508 if (is_array($discountedFee) && !empty($discountedFee)) {
509 if (!$discountId) {
510 $form->_discountId = $discountId = CRM_Core_BAO_Discount::findSet($form->_eventId, 'civicrm_event');
511 }
512 if ($discountId) {
513 $form->_feeBlock = &$form->_values['discount'][$discountId];
514 }
515 }
516 if (!is_array($form->_feeBlock)) {
517 $form->_feeBlock = array();
518 }
519
520 //its time to call the hook.
521 CRM_Utils_Hook::buildAmount('event', $form, $form->_feeBlock);
522
523 //reset required if participant is skipped.
524 $button = substr($form->controller->getButtonName(), -4);
525 if ($required && $button == 'skip') {
526 $required = FALSE;
527 }
528
529 $className = CRM_Utils_System::getClassName($form);
530
531 //build the priceset fields.
532 if (isset($form->_priceSetId) && $form->_priceSetId) {
533
534 //format price set fields across option full.
535 self::formatFieldsForOptionFull($form);
536
a7488080 537 if (!empty($form->_priceSet['is_quick_config'])) {
6a488035
TO
538 $form->_quickConfig = $form->_priceSet['is_quick_config'];
539 }
540 $form->add('hidden', 'priceSetId', $form->_priceSetId);
541
c7b3d063 542 // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
ab8a593e 543 $adminFieldVisible = FALSE;
c7b3d063 544 if (CRM_Core_Permission::check('administer CiviCRM')) {
4eeb9a5b 545 $adminFieldVisible = TRUE;
c7b3d063
DG
546 }
547
6a488035 548 foreach ($form->_feeBlock as $field) {
d06f3157 549 // public AND admin visibility fields are included for back-office registration and back-office change selections
6a488035 550 if (CRM_Utils_Array::value('visibility', $field) == 'public' ||
4eeb9a5b 551 (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE) ||
d06f3157
DG
552 $className == 'CRM_Event_Form_Participant' ||
553 $className == 'CRM_Event_Form_ParticipantFeeSelection'
6a488035
TO
554 ) {
555 $fieldId = $field['id'];
556 $elementName = 'price_' . $fieldId;
557
558 $isRequire = CRM_Utils_Array::value('is_required', $field);
559 if ($button == 'skip') {
560 $isRequire = FALSE;
561 }
562
563 //user might modified w/ hook.
564 $options = CRM_Utils_Array::value('options', $field);
565 if (!is_array($options)) {
566 continue;
567 }
568
569 $optionFullIds = CRM_Utils_Array::value('option_full_ids', $field, array());
570
571 //soft suppress required rule when option is full.
572 if (!empty($optionFullIds) && (count($options) == count($optionFullIds))) {
573 $isRequire = FALSE;
574 }
575
576 //build the element.
9da8dc8c 577 CRM_Price_BAO_PriceField::addQuickFormElement($form,
6a488035
TO
578 $elementName,
579 $fieldId,
580 FALSE,
581 $isRequire,
582 NULL,
583 $options,
584 $optionFullIds
585 );
586 }
587 }
588 $form->assign('priceSet', $form->_priceSet);
589 }
590 else {
591 $eventFeeBlockValues = array();
592 foreach ($form->_feeBlock as $fee) {
593 if (is_array($fee)) {
594
595 //CRM-7632, CRM-6201
596 $totalAmountJs = NULL;
597 if ($className == 'CRM_Event_Form_Participant') {
598 $totalAmountJs = array('onClick' => "fillTotalAmount(" . $fee['value'] . ")");
599 }
600
601 $eventFeeBlockValues['amount_id_' . $fee['amount_id']] = $fee['value'];
602 $elements[] = &$form->createElement('radio', NULL, '',
603 CRM_Utils_Money::format($fee['value']) . ' ' .
604 $fee['label'],
605 $fee['amount_id'],
606 $totalAmountJs
607 );
608 }
609 }
610 $form->assign('eventFeeBlockValues', json_encode($eventFeeBlockValues));
611
612 $form->_defaults['amount'] = CRM_Utils_Array::value('default_fee_id', $form->_values['event']);
613 $element = &$form->addGroup($elements, 'amount', ts('Event Fee(s)'), '<br />');
614 if (isset($form->_online) && $form->_online) {
615 $element->freeze();
616 }
617 if ($required) {
618 $form->addRule('amount', ts('Fee Level is a required field.'), 'required');
619 }
620 }
621 }
622
0cf587a7 623 /**
c490a46a 624 * @param CRM_Core_Form $form
0cf587a7 625 */
6a488035
TO
626 public static function formatFieldsForOptionFull(&$form) {
627 $priceSet = $form->get('priceSet');
628 $priceSetId = $form->get('priceSetId');
e9bb507e 629 $defaultPricefieldIds = array();
630 if (!empty($form->_values['line_items'])) {
631 foreach ($form->_values['line_items'] as $lineItem) {
632 $defaultPricefieldIds[] = $lineItem['price_field_value_id'];
633 }
634 }
6a488035
TO
635 if (!$priceSetId ||
636 !is_array($priceSet) ||
353ffa53
TO
637 empty($priceSet) || empty($priceSet['optionsMaxValueTotal'])
638 ) {
6a488035
TO
639 return;
640 }
641
642 $skipParticipants = $formattedPriceSetDefaults = array();
e03317f1 643 if (!empty($form->_allowConfirmation) && (isset($form->_pId) || isset($form->_additionalParticipantId))) {
6a488035
TO
644 $participantId = isset($form->_pId) ? $form->_pId : $form->_additionalParticipantId;
645 $pricesetDefaults = CRM_Event_Form_EventFees::setDefaultPriceSet($participantId,
646 $form->_eventId
647 );
648 // modify options full to respect the selected fields
649 // options on confirmation.
217d80ab 650 $formattedPriceSetDefaults = self::formatPriceSetParams($form, $pricesetDefaults);
6a488035
TO
651
652 // to skip current registered participants fields option count on confirmation.
653 $skipParticipants[] = $form->_participantId;
654 if (!empty($form->_additionalParticipantIds)) {
655 $skipParticipants = array_merge($skipParticipants, $form->_additionalParticipantIds);
656 }
657 }
658
659 $className = CRM_Utils_System::getClassName($form);
660
661 //get the current price event price set options count.
662 $currentOptionsCount = self::getPriceSetOptionCount($form);
663 $recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, $skipParticipants);
e9bb507e 664 $optionFullTotalAmount = 0;
0dc0b759 665 $currentParticipantNo = (int) substr($form->_name, 12);
6a488035
TO
666 foreach ($form->_feeBlock as & $field) {
667 $optionFullIds = array();
668 $fieldId = $field['id'];
669 if (!is_array($field['options'])) {
670 continue;
671 }
672 foreach ($field['options'] as & $option) {
353ffa53
TO
673 $optId = $option['id'];
674 $count = CRM_Utils_Array::value('count', $option, 0);
675 $maxValue = CRM_Utils_Array::value('max_value', $option, 0);
676 $dbTotalCount = CRM_Utils_Array::value($optId, $recordedOptionsCount, 0);
6a488035
TO
677 $currentTotalCount = CRM_Utils_Array::value($optId, $currentOptionsCount, 0);
678
79b152ac 679 $totalCount = $currentTotalCount + $dbTotalCount;
6a488035
TO
680 $isFull = FALSE;
681 if ($maxValue &&
0dc0b759 682 (($totalCount >= $maxValue) &&
683 (empty($form->_lineItem[$currentParticipantNo][$optId]['price_field_id']) || $dbTotalCount >= $maxValue))
6a488035
TO
684 ) {
685 $isFull = TRUE;
686 $optionFullIds[$optId] = $optId;
e9bb507e 687 if ($field['html_type'] != 'Select') {
688 if (in_array($optId, $defaultPricefieldIds)) {
689 $optionFullTotalAmount += CRM_Utils_Array::value('amount', $option);
690 }
691 }
692 else {
693 if (!empty($defaultPricefieldIds) && in_array($optId, $defaultPricefieldIds)) {
694 unset($optionFullIds[$optId]);
695 }
696 }
6a488035 697 }
6a488035
TO
698 //here option is not full,
699 //but we don't want to allow participant to increase
700 //seats at the time of re-walking registration.
701 if ($count &&
8dfe9fe3 702 !empty($form->_allowConfirmation) &&
6a488035
TO
703 !empty($formattedPriceSetDefaults)
704 ) {
217d80ab 705 if (empty($formattedPriceSetDefaults["price_{$field}"]) || empty($formattedPriceSetDefaults["price_{$fieldId}"][$optId])) {
6a488035
TO
706 $optionFullIds[$optId] = $optId;
707 $isFull = TRUE;
708 }
709 }
710 $option['is_full'] = $isFull;
711 $option['db_total_count'] = $dbTotalCount;
712 $option['total_option_count'] = $dbTotalCount + $currentTotalCount;
713 }
714
715 //ignore option full for offline registration.
716 if ($className == 'CRM_Event_Form_Participant') {
717 $optionFullIds = array();
718 }
719
720 //finally get option ids in.
721 $field['option_full_ids'] = $optionFullIds;
722 }
e9bb507e 723 $form->assign('optionFullTotalAmount', $optionFullTotalAmount);
6a488035
TO
724 }
725
726 /**
66f9e52b 727 * Global form rule.
6a488035 728 *
d4dd1e85
TO
729 * @param array $fields
730 * The input form values.
731 * @param array $files
732 * The uploaded files if any.
2a6da8d7
EM
733 * @param $self
734 *
6a488035 735 *
72b3a70c
CW
736 * @return bool|array
737 * true if no errors, else array of errors
6a488035 738 */
00be9182 739 public static function formRule($fields, $files, $self) {
6a488035
TO
740 $errors = array();
741 //check that either an email or firstname+lastname is included in the form(CRM-9587)
742 self::checkProfileComplete($fields, $errors, $self->_eventId);
743 //To check if the user is already registered for the event(CRM-2426)
744 if (!$self->_skipDupeRegistrationCheck) {
168e792f 745 self::checkRegistration($fields, $self);
6a488035
TO
746 }
747 //check for availability of registrations.
8cc574cf 748 if (!$self->_allowConfirmation && empty($fields['bypass_payment']) &&
6a488035
TO
749 is_numeric($self->_availableRegistrations) &&
750 CRM_Utils_Array::value('additional_participants', $fields) >= $self->_availableRegistrations
751 ) {
752 $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", array(1 => $self->_availableRegistrations));
753 }
754
755 // during confirmation don't allow to increase additional participants, CRM-4320
8cc574cf 756 if ($self->_allowConfirmation && !empty($fields['additional_participants']) &&
6a488035
TO
757 is_array($self->_additionalParticipantIds) &&
758 $fields['additional_participants'] > count($self->_additionalParticipantIds)
759 ) {
760 $errors['additional_participants'] = ts("Oops. It looks like you are trying to increase the number of additional people you are registering for. You can confirm registration for a maximum of %1 additional people.", array(1 => count($self->_additionalParticipantIds)));
761 }
762
763 //don't allow to register w/ waiting if enough spaces available.
5ec4b965 764 if (!empty($fields['bypass_payment']) && $self->_allowConfirmation) {
6a488035 765 if (!is_numeric($self->_availableRegistrations) ||
8cc574cf 766 (empty($fields['priceSetId']) && CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations)
6a488035
TO
767 ) {
768 $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list.");
769 }
770 }
771
a7488080 772 if (!empty($fields['additional_participants']) &&
6a488035
TO
773 !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])
774 ) {
775 $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
776 }
777
778 // priceset validations
0dc0b759 779 if (!empty($fields['priceSetId']) &&
780 !$self->_requireApproval && !$self->_allowWaitlist
781 ) {
6a488035
TO
782 //format params.
783 $formatted = self::formatPriceSetParams($self, $fields);
784 $ppParams = array($formatted);
785 $priceSetErrors = self::validatePriceSet($self, $ppParams);
786 $primaryParticipantCount = self::getParticipantCount($self, $ppParams);
787
788 //get price set fields errors in.
789 $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, array()));
790
791 $totalParticipants = $primaryParticipantCount;
a7488080 792 if (!empty($fields['additional_participants'])) {
6a488035
TO
793 $totalParticipants += $fields['additional_participants'];
794 }
795
a7488080 796 if (empty($fields['bypass_payment']) &&
6a488035
TO
797 !$self->_allowConfirmation &&
798 is_numeric($self->_availableRegistrations) &&
799 $self->_availableRegistrations < $totalParticipants
800 ) {
801 $errors['_qf_default'] = ts("Only %1 Registrations available.", array(1 => $self->_availableRegistrations));
802 }
803
804 $lineItem = array();
9da8dc8c 805 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], $fields, $lineItem);
601c7a24 806
807 $minAmt = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount');
6a488035
TO
808 if ($fields['amount'] < 0) {
809 $errors['_qf_default'] = ts('Event Fee(s) can not be less than zero. Please select the options accordingly');
810 }
601c7a24 811 elseif (!empty($minAmt) && $fields['amount'] < $minAmt) {
812 $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Event Fee(s).', array(
813 1 => CRM_Utils_Money::format($minAmt),
814 ));
815 }
6a488035
TO
816 }
817
f48e6cf7 818 // @todo - can we remove the 'is_monetary' concept?
6a488035 819 if ($self->_values['event']['is_monetary']) {
e02d7e96
EM
820 if (empty($self->_requireApproval) && !empty($fields['amount']) && $fields['amount'] > 0 && !isset
821 ($fields['payment_processor_id'])) {
822 $errors['payment_processor_id'] = ts('Please select a Payment Method');
0d588131 823 }
6a488035 824
0d588131 825 $isZeroAmount = $skipPaymentValidation = FALSE;
a7488080 826 if (!empty($fields['priceSetId'])) {
6a488035
TO
827 if (CRM_Utils_Array::value('amount', $fields) == 0) {
828 $isZeroAmount = TRUE;
829 }
830 }
a7488080 831 elseif (!empty($fields['amount']) &&
6a488035
TO
832 (isset($self->_values['discount'][$fields['amount']])
833 && CRM_Utils_Array::value('value', $self->_values['discount'][$fields['amount']]) == 0
834 )
835 ) {
836 $isZeroAmount = TRUE;
837 }
a7488080 838 elseif (!empty($fields['amount']) &&
6a488035
TO
839 (isset($self->_values['fee'][$fields['amount']])
840 && CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) == 0
841 )
842 ) {
843 $isZeroAmount = TRUE;
844 }
845
8cc574cf 846 if ($isZeroAmount && !($self->_forcePayement && !empty($fields['additional_participants']))) {
0d588131 847 $skipPaymentValidation = TRUE;
6a488035
TO
848 }
849
f48e6cf7 850 // also return if zero fees for valid members
851 if (!empty($fields['bypass_payment']) ||
0d588131 852 $skipPaymentValidation ||
6a488035
TO
853 (!$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist))
854 ) {
855 return empty($errors) ? TRUE : $errors;
856 }
f48e6cf7 857 CRM_Core_Payment_Form::validatePaymentInstrument(
858 $fields['payment_processor_id'],
859 $fields,
860 $errors,
861 (!$self->_isBillingAddressRequiredForPayLater ? NULL : 'billing')
862 );
6a488035 863 }
6a488035 864
6a488035
TO
865 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
866 if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
867 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
868 if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) {
217d80ab 869 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
6a488035
TO
870 array(1 => ucwords(str_replace('_', ' ', $greeting)))
871 );
872 }
873 }
874 }
875 return empty($errors) ? TRUE : $errors;
876 }
877
878 /**
ec022878 879 * Check if profiles are complete when event registration occurs(CRM-9587).
880 *
881 * @param array $fields
882 * @param array $errors
883 * @param int $eventId
6a488035 884 */
00be9182 885 public static function checkProfileComplete($fields, &$errors, $eventId) {
6a488035
TO
886 $email = '';
887 foreach ($fields as $fieldname => $fieldvalue) {
888 if (substr($fieldname, 0, 6) == 'email-' && $fieldvalue) {
889 $email = $fieldvalue;
890 }
891 }
892
8cc574cf 893 if (!$email && !(!empty($fields['first_name']) && !empty($fields['last_name']))) {
6a488035
TO
894 $defaults = $params = array('id' => $eventId);
895 CRM_Event_BAO_Event::retrieve($params, $defaults);
896 $message = ts("Mandatory fields (first name and last name, OR email address) are missing from this form.");
897 $errors['_qf_default'] = $message;
898 }
899 }
900
901 /**
66f9e52b 902 * Process the form submission.
6a488035
TO
903 */
904 public function postProcess() {
905 // get the submitted form values.
906 $params = $this->controller->exportValues($this->_name);
907
908 //set as Primary participant
909 $params['is_primary'] = 1;
910
8ae4d0d3 911 if ($this->_values['event']['is_pay_later']
e02d7e96 912 && (!array_key_exists('hidden_processor', $params) || $params['payment_processor_id'] == 0)
353ffa53 913 ) {
6a488035
TO
914 $params['is_pay_later'] = 1;
915 }
916 else {
917 $params['is_pay_later'] = 0;
918 }
919
920 $this->set('is_pay_later', $params['is_pay_later']);
921
922 // assign pay later stuff
923 $this->_params['is_pay_later'] = CRM_Utils_Array::value('is_pay_later', $params, FALSE);
924 $this->assign('is_pay_later', $params['is_pay_later']);
925 if ($params['is_pay_later']) {
926 $this->assign('pay_later_text', $this->_values['event']['pay_later_text']);
927 $this->assign('pay_later_receipt', $this->_values['event']['pay_later_receipt']);
928 }
6a488035 929
168e792f
DL
930 if (!$this->_allowConfirmation) {
931 // check if the participant is already registered
932 if (!$this->_skipDupeRegistrationCheck) {
933 $params['contact_id'] = self::checkRegistration($params, $this, FALSE, TRUE, TRUE);
934 }
935 }
936
a7488080 937 if (!empty($params['image_URL'])) {
6a488035
TO
938 CRM_Contact_BAO_Contact::processImageParams($params);
939 }
940
941 //carry campaign to partcipants.
942 if (array_key_exists('participant_campaign_id', $params)) {
943 $params['campaign_id'] = $params['participant_campaign_id'];
944 }
945 else {
946 $params['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
947 }
948
949 //hack to allow group to register w/ waiting
950 $primaryParticipantCount = self::getParticipantCount($this, $params);
951
952 $totalParticipants = $primaryParticipantCount;
a7488080 953 if (!empty($params['additional_participants'])) {
6a488035
TO
954 $totalParticipants += $params['additional_participants'];
955 }
8cc574cf 956 if (!$this->_allowConfirmation && !empty($params['bypass_payment']) &&
6a488035
TO
957 is_numeric($this->_availableRegistrations) &&
958 $totalParticipants > $this->_availableRegistrations
959 ) {
960 $this->_allowWaitlist = TRUE;
961 $this->set('allowWaitlist', TRUE);
962 }
963
964 //carry participant id if pre-registered.
965 if ($this->_allowConfirmation && $this->_participantId) {
966 $params['participant_id'] = $this->_participantId;
967 }
968
969 $params['defaultRole'] = 1;
970 if (array_key_exists('participant_role', $params)) {
971 $params['participant_role_id'] = $params['participant_role'];
972 }
973
974 if (array_key_exists('participant_role_id', $params)) {
975 $params['defaultRole'] = 0;
976 }
a7488080 977 if (empty($params['participant_role_id']) &&
6a488035
TO
978 $this->_values['event']['default_role_id']
979 ) {
980 $params['participant_role_id'] = $this->_values['event']['default_role_id'];
981 }
982
983 $config = CRM_Core_Config::singleton();
984 $params['currencyID'] = $config->defaultCurrency;
985
986 if ($this->_values['event']['is_monetary']) {
987 // we first reset the confirm page so it accepts new values
988 $this->controller->resetPage('Confirm');
989
990 //added for discount
991 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
c039f658 992 $params['amount_level'] = $this->getAmountLevel($params, $discountId);
6a488035
TO
993 if (!empty($this->_values['discount'][$discountId])) {
994 $params['discount_id'] = $discountId;
6a488035
TO
995 $params['amount'] = $this->_values['discount'][$discountId][$params['amount']]['value'];
996 }
997 elseif (empty($params['priceSetId'])) {
16d1c8e2 998 if (!empty($params['amount'])) {
16d1c8e2 999 $params['amount'] = $this->_values['fee'][$params['amount']]['value'];
1000 }
1001 else {
c039f658 1002 $params['amount'] = '';
16d1c8e2 1003 }
6a488035
TO
1004 }
1005 else {
1006 $lineItem = array();
9da8dc8c 1007 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem);
d91b8b33 1008 if ($params['tax_amount']) {
1009 $this->set('tax_amount', $params['tax_amount']);
1010 }
9d8d8fd0 1011 $submittedLineItems = $this->get('lineItem');
1012 if (!empty($submittedLineItems) && is_array($submittedLineItems)) {
0dc0b759 1013 $submittedLineItems[0] = $lineItem;
1014 }
1015 else {
1016 $submittedLineItems = array($lineItem);
1017 }
1018 $this->set('lineItem', $submittedLineItems);
6a488035
TO
1019 $this->set('lineItemParticipantsCount', array($primaryParticipantCount));
1020 }
1021
1022 $this->set('amount', $params['amount']);
1023 $this->set('amount_level', $params['amount_level']);
1024
1025 // generate and set an invoiceID for this transaction
1026 $invoiceID = md5(uniqid(rand(), TRUE));
1027 $this->set('invoiceID', $invoiceID);
1028
b0c462ac 1029 if ($this->_paymentProcessor) {
077017db 1030 $payment = $this->_paymentProcessor['object'];
ec022878 1031 $payment->setBaseReturnUrl('civicrm/event/register');
6a488035 1032 }
0eb1f7ff 1033
1034 // ContributeMode is a deprecated concept. It is short-hand for a bunch of
1035 // assumptions we are working to remove.
6a488035
TO
1036 $this->set('contributeMode', 'direct');
1037
0eb1f7ff 1038 // This code is duplicated multiple places and should be consolidated.
6a488035
TO
1039 if (isset($params["state_province_id-{$this->_bltID}"]) &&
1040 $params["state_province_id-{$this->_bltID}"]
1041 ) {
1042 $params["state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params["state_province_id-{$this->_bltID}"]);
1043 }
1044
1045 if (isset($params["country_id-{$this->_bltID}"]) &&
1046 $params["country_id-{$this->_bltID}"]
1047 ) {
1048 $params["country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($params["country_id-{$this->_bltID}"]);
1049 }
1050 if (isset($params['credit_card_exp_date'])) {
1051 $params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($params);
1052 $params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($params);
1053 }
1054 if ($this->_values['event']['is_monetary']) {
1055 $params['ip_address'] = CRM_Utils_System::ipAddress();
1056 $params['currencyID'] = $config->defaultCurrency;
6a488035
TO
1057 $params['invoiceID'] = $invoiceID;
1058 }
d0ebccea 1059 $this->_params = $this->get('params');
ec022878 1060 // Set the button so we know what
1061 $params['button'] = $this->controller->getButtonName();
d0ebccea 1062 if (!empty($this->_params) && is_array($this->_params)) {
0dc0b759 1063 $this->_params[0] = $params;
1064 }
1065 else {
1066 $this->_params = array();
1067 $this->_params[] = $params;
1068 }
6a488035 1069 $this->set('params', $this->_params);
f92fc7eb 1070 if ($this->_paymentProcessor &&
223190c8 1071 // Actually we don't really need to check if it supports pre-approval - we could just call
1072 // it regardless as the function we call re-acts tot the rests of the preApproval call.
ec022878 1073 $this->_paymentProcessor['object']->supports('preApproval')
1074 && !$this->_allowWaitlist &&
1075 !$this->_requireApproval
f92fc7eb 1076 ) {
6a488035 1077
0f2b049e 1078 // The concept of contributeMode is deprecated - but still needs removal from the message templates.
ec022878 1079 $this->set('contributeMode', 'express');
6a488035 1080
ec022878 1081 // Send Event Name & Id in Params
1082 $params['eventName'] = $this->_values['event']['title'];
1083 $params['eventId'] = $this->_values['event']['id'];
6a488035 1084
ec022878 1085 $params['cancelURL'] = CRM_Utils_System::url('civicrm/event/register',
1086 "_qf_Register_display=1&qfKey={$this->controller->_key}",
1087 TRUE, NULL, FALSE
1088 );
1089 if (CRM_Utils_Array::value('additional_participants', $params, FALSE)) {
1090 $urlArgs = "_qf_Participant_1_display=1&rfp=1&qfKey={$this->controller->_key}";
1091 }
1092 else {
1093 $urlArgs = "_qf_Confirm_display=1&rfp=1&qfKey={$this->controller->_key}";
1094 }
1095 $params['returnURL'] = CRM_Utils_System::url('civicrm/event/register',
1096 $urlArgs,
1097 TRUE, NULL, FALSE
1098 );
1099 $params['invoiceID'] = $invoiceID;
6a488035 1100
ec022878 1101 $params['component'] = 'event';
223190c8 1102 $this->handlePreApproval($params);
6a488035 1103 }
f92fc7eb
CW
1104 elseif ($this->_paymentProcessor &&
1105 $this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_NOTIFY
1106 ) {
0f2b049e 1107 // The concept of contributeMode is deprecated - but still needs removal from the message templates.
6a488035
TO
1108 $this->set('contributeMode', 'notify');
1109 }
13f230bc 1110 }
1111 else {
1112 $params['description'] = ts('Online Event Registration') . ' ' . $this->_values['event']['title'];
6a488035 1113
13f230bc 1114 $this->_params = array();
1115 $this->_params[] = $params;
1116 $this->set('params', $this->_params);
6a488035 1117
13f230bc 1118 if (
1119 empty($params['additional_participants'])
1120 && !$this->_values['event']['is_confirm_enabled'] // CRM-11182 - Optional confirmation screen
1121 ) {
3033e657 1122 $this->processRegistration($this->_params);
6a488035
TO
1123 }
1124 }
1125
1126 // If registering > 1 participant, give status message
1127 if (CRM_Utils_Array::value('additional_participants', $params, FALSE)) {
1128 $statusMsg = ts('Registration information for participant 1 has been saved.');
1129 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
1130 }
1131 }
6a488035 1132
6a488035 1133 /**
66f9e52b 1134 * Method to check if the user is already registered for the event.
6a488035
TO
1135 * and if result found redirect to the event info page
1136 *
d4dd1e85
TO
1137 * @param array $fields
1138 * The input form values(anonymous user).
ec022878 1139 * @param CRM_Event_Form_Registration_Register $self
d4dd1e85
TO
1140 * Event data.
1141 * @param bool $isAdditional
1142 * Treat isAdditional participants a bit differently.
1143 * @param bool $returnContactId
1144 * Just find and return the contactID match to use.
1145 * @param bool $useDedupeRules
1146 * Force usage of dedupe rules.
6a488035 1147 *
54957108 1148 * @return int
6a488035 1149 */
00be9182 1150 public static function checkRegistration($fields, &$self, $isAdditional = FALSE, $returnContactId = FALSE, $useDedupeRules = FALSE) {
6a488035
TO
1151 // CRM-3907, skip check for preview registrations
1152 // CRM-4320 participant need to walk wizard
1153 if (!$returnContactId &&
1154 ($self->_mode == 'test' || $self->_allowConfirmation)
1155 ) {
1156 return FALSE;
1157 }
1158
1159 $contactID = NULL;
1160 $session = CRM_Core_Session::singleton();
1161 if (!$isAdditional) {
5c280496 1162 $contactID = $self->getContactID();
6a488035
TO
1163 }
1164
178073d6 1165 if (!$contactID && is_array($fields) && $fields) {
6a488035 1166
03390e26 1167 //CRM-14134 use Unsupervised rule for everyone
1168 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
6a488035 1169
03390e26 1170 // disable permission based on cache since event registration is public page/feature.
1171 $dedupeParams['check_permission'] = FALSE;
6a488035 1172
03390e26 1173 // find event dedupe rule
1174 if (CRM_Utils_Array::value('dedupe_rule_group_id', $self->_values['event'], 0) > 0) {
1175 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised', array(), $self->_values['event']['dedupe_rule_group_id']);
6a488035
TO
1176 }
1177 else {
03390e26 1178 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
6a488035 1179 }
03390e26 1180 $contactID = CRM_Utils_Array::value(0, $ids);
1181
6a488035
TO
1182 }
1183
1184 if ($returnContactId) {
1185 // CRM-7377
1186 // return contactID if contact already exists
1187 return $contactID;
1188 }
1189
1190 if ($contactID) {
1191 $participant = new CRM_Event_BAO_Participant();
1192 $participant->contact_id = $contactID;
1193 $participant->event_id = $self->_values['event']['id'];
1194 if (!empty($fields['participant_role']) && is_numeric($fields['participant_role'])) {
1195 $participant->role_id = $fields['participant_role'];
1196 }
1197 else {
1198 $participant->role_id = $self->_values['event']['default_role_id'];
1199 }
1200 $participant->is_test = 0;
1201 $participant->find();
1202 $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1');
1203 while ($participant->fetch()) {
1204 if (array_key_exists($participant->status_id, $statusTypes)) {
1205 if (!$isAdditional && !$self->_values['event']['allow_same_participant_emails']) {
1206 $registerUrl = CRM_Utils_System::url('civicrm/event/register',
1207 "reset=1&id={$self->_values['event']['id']}&cid=0"
1208 );
1209 if ($self->_pcpId) {
1210 $registerUrl .= '&pcpId=' . $self->_pcpId;
1211 }
1212
06141953 1213 $status = ts("It looks like you are already registered for this event. If you want to change your registration, or you feel that you've received this message in error, please contact the site administrator.") . ' ' . ts('You can also <a href="%1">register another participant</a>.', array(1 => $registerUrl));
6a488035
TO
1214 $session->setStatus($status, ts('Oops.'), 'alert');
1215 $url = CRM_Utils_System::url('civicrm/event/info',
1216 "reset=1&id={$self->_values['event']['id']}&noFullMsg=true"
1217 );
1218 if ($self->_action & CRM_Core_Action::PREVIEW) {
1219 $url .= '&action=preview';
1220 }
1221
1222 if ($self->_pcpId) {
1223 $url .= '&pcpId=' . $self->_pcpId;
1224 }
1225
1226 CRM_Utils_System::redirect($url);
1227 }
1228
1229 if ($isAdditional) {
06141953 1230 $status = ts("It looks like this participant is already registered for this event. If you want to change your registration, or you feel that you've received this message in error, please contact the site administrator.");
6a488035
TO
1231 $session->setStatus($status, ts('Oops.'), 'alert');
1232 return $participant->id;
1233 }
1234 }
1235 }
1236 }
1237 }
96025800 1238
6a488035 1239}