Merge pull request #3257 from pratik-joshi/views-upgrade-error
[civicrm-core.git] / CRM / Event / Form / Registration / AdditionalParticipant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37/**
38 * This class generates form components for processing Event
39 *
40 */
41class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_Registration {
42
43 /**
44 * The defaults involved in this page
45 *
46 */
47 public $_defaults = array();
48
49 /**
50 * pre-registered additional participant id.
51 *
52 */
53 public $additionalParticipantId = NULL;
54
55 /**
56 * Function to set variables up before form is built
57 *
58 * @return void
59 * @access public
60 */
61 function preProcess() {
62 parent::preProcess();
63
64 $participantNo = substr($this->_name, 12);
65
66 //lets process in-queue participants.
67 if ($this->_participantId && $this->_additionalParticipantIds) {
68 $this->_additionalParticipantId = CRM_Utils_Array::value($participantNo, $this->_additionalParticipantIds);
69 }
70
71 $participantCnt = $participantNo + 1;
72 $this->assign('formId', $participantNo);
73 $this->_params = array();
74 $this->_params = $this->get('params');
75
76 $participantTot = $this->_params[0]['additional_participants'] + 1;
77 $skipCount = count(array_keys($this->_params, "skip"));
78 if ($skipCount) {
79 $this->assign('skipCount', $skipCount);
80 }
81 CRM_Utils_System::setTitle(ts('Register Participant %1 of %2', array(1 => $participantCnt, 2 => $participantTot)));
82
83 //CRM-4320, hack to check last participant.
84 $this->_lastParticipant = FALSE;
85 if ($participantTot == $participantCnt) {
86 $this->_lastParticipant = TRUE;
87 }
88 $this->assign('lastParticipant', $this->_lastParticipant);
89 }
90
91 /**
92 * This function sets the default values for the form. For edit/view mode
93 * the default values are retrieved from the database
94 *
95 * @access public
96 *
355ba699 97 * @return void
6a488035
TO
98 */
99 function setDefaultValues() {
100 $defaults = $unsetSubmittedOptions = array();
101 $discountId = NULL;
102 //fix for CRM-3088, default value for discount set.
103 if (!empty($this->_values['discount'])) {
104 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
8cc574cf 105 if ($discountId && !empty($this->_values['event']['default_discount_fee_id'])) {
6a488035
TO
106 $discountKey = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionValue", $this->_values['event']['default_discount_fee_id']
107 , 'weight', 'id'
108 );
109 $defaults['amount'] = key(array_slice($this->_values['discount'][$discountId], $discountKey - 1, $discountKey, TRUE));
110 }
111 }
112 if ($this->_priceSetId) {
113 foreach ($this->_feeBlock as $key => $val) {
a7488080 114 if (empty($val['options'])) {
6a488035
TO
115 continue;
116 }
117
118 $optionsFull = CRM_Utils_Array::value('option_full_ids', $val, array());
119 foreach ($val['options'] as $keys => $values) {
120 if ($values['is_default'] && !in_array($keys, $optionsFull)) {
121 if ($val['html_type'] == 'CheckBox') {
122 $defaults["price_{$key}"][$keys] = 1;
123 }
124 else {
125 $defaults["price_{$key}"] = $keys;
126 }
127 }
128 }
129 if (!empty($optionsFull)) {
130 $unsetSubmittedOptions[$val['id']] = $optionsFull;
131 }
132 }
133 }
134
135 //CRM-4320, setdefault additional participant values.
136 if ($this->_allowConfirmation && $this->_additionalParticipantId) {
137 //hack to get set default from eventFees.php
138 $this->_discountId = $discountId;
139 $this->_pId = $this->_additionalParticipantId;
140 $this->_contactId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_additionalParticipantId, 'contact_id');
141 $participantDefaults = CRM_Event_Form_EventFees::setDefaultValues($this);
142 $participantDefaults = array_merge($this->_defaults, $participantDefaults);
143 // use primary email address if billing email address is empty
144 if (empty($this->_defaults["email-{$this->_bltID}"]) &&
145 !empty($this->_defaults["email-Primary"])
146 ) {
147 $participantDefaults["email-{$this->_bltID}"] = $this->_defaults["email-Primary"];
148 }
149 $defaults = array_merge($defaults, $participantDefaults);
150 }
151
152 $defaults = array_merge($this->_defaults, $defaults);
153
154 //reset values for all options those are full.
155 if (!empty($unsetSubmittedOptions) && empty($_POST)) {
156 $this->resetElementValue($unsetSubmittedOptions);
157 }
158
159 //load default campaign from page.
160 if (array_key_exists('participant_campaign_id', $this->_fields)) {
161 $defaults['participant_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
162 }
163 CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults);
164
165 return $defaults;
166 }
167
168 /**
169 * Function to build the form
170 *
355ba699 171 * @return void
6a488035
TO
172 * @access public
173 */
174 public function buildQuickForm() {
175 $config = CRM_Core_Config::singleton();
176 $button = substr($this->controller->getButtonName(), -4);
177
178 $this->add('hidden', 'scriptFee', NULL);
179 $this->add('hidden', 'scriptArray', NULL);
180
181 if ($this->_values['event']['is_monetary']) {
182 CRM_Event_Form_Registration_Register::buildAmount($this);
183 }
184 $first_name = $last_name = NULL;
185 $pre = $post = array();
186 foreach (array(
187 'pre', 'post') as $keys) {
188 if (isset($this->_values['additional_custom_' . $keys . '_id'])) {
189 $this->buildCustom($this->_values['additional_custom_' . $keys . '_id'], 'additionalCustom' . ucfirst($keys), TRUE);
190 $$keys = CRM_Core_BAO_UFGroup::getFields($this->_values['additional_custom_' . $keys . '_id']);
191 }
192 foreach (array(
193 'first_name', 'last_name') as $name) {
a7488080 194 if (!empty($$keys[$name]) &&
6a488035
TO
195 CRM_Utils_Array::value('is_required', CRM_Utils_Array::value($name, $$keys))
196 ) {
197 $$name = 1;
198 }
199 }
200 }
201
202 $required = ($button == 'skip' ||
203 $this->_values['event']['allow_same_participant_emails'] == 1 &&
204 ($first_name && $last_name)
205 ) ? FALSE : TRUE;
206
207 //add buttons
208 $js = NULL;
8cc574cf 209 if ($this->isLastParticipant(TRUE) && empty($this->_values['event']['is_monetary'])) {
6a488035
TO
210 $js = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');");
211 }
212
213 //handle case where user might sart with waiting by group
214 //registration and skip some people and now group fit to
215 //become registered so need to take payment from user.
216 //this case only occurs at dynamic waiting status, CRM-4320
217 $statusMessage = NULL;
218 $allowToProceed = TRUE;
219 $includeSkipButton = TRUE;
220 $this->_resetAllowWaitlist = FALSE;
221
9da8dc8c 222 $pricesetFieldsCount = CRM_Price_BAO_PriceSet::getPricesetCount($this->_priceSetId);
6a488035
TO
223
224 if ($this->_lastParticipant || $pricesetFieldsCount) {
225 //get the participant total.
226 $processedCnt = self::getParticipantCount($this, $this->_params, TRUE);
227 }
228
8cc574cf 229 if (!$this->_allowConfirmation && !empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
230 $this->_lastParticipant
231 ) {
232
233 //get the event spaces.
234 $spaces = $this->_availableRegistrations;
235
236 $currentPageMaxCount = 1;
237 if ($pricesetFieldsCount) {
238 $currentPageMaxCount = $pricesetFieldsCount;
239 }
240
241 //we might did reset allow waiting in case of dynamic calculation
a7488080 242 if (!empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
243 is_numeric($spaces) &&
244 $processedCnt > $spaces
245 ) {
246 $this->_allowWaitlist = TRUE;
247 $this->set('allowWaitlist', TRUE);
248 }
249
250 //lets allow to become a part of runtime waiting list, if primary selected pay later.
251 $realPayLater = FALSE;
8cc574cf 252 if (!empty($this->_values['event']['is_monetary']) && !empty($this->_values['event']['is_pay_later'])) {
6a488035
TO
253 $realPayLater = CRM_Utils_Array::value('is_pay_later', $this->_params[0]);
254 }
255
256 //truly spaces are greater than required.
257 if (is_numeric($spaces) && $spaces >= ($processedCnt + $currentPageMaxCount)) {
258 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0 || $this->_requireApproval) {
259 $this->_allowWaitlist = FALSE;
260 $this->set('allowWaitlist', $this->_allowWaitlist);
261 if ($this->_requireApproval) {
262 $statusMessage = ts("It looks like you are now registering a group of %1 participants. The event has %2 available spaces (you will not be wait listed). Registration for this event requires approval. You will receive an email once your registration has been reviewed.", array(1 => ++$processedCnt, 2 => $spaces));
263 }
264 else {
265 $statusMessage = ts("It looks like you are now registering a group of %1 participants. The event has %2 available spaces (you will not be wait listed).", array(1 => ++$processedCnt, 2 => $spaces));
266 }
267 }
268 else {
269 $statusMessage = ts("It looks like you are now registering a group of %1 participants. The event has %2 available spaces (you will not be wait listed). Please go back to the main registration page and reduce the number of additional people. You will also need to complete payment information.", array(1 => ++$processedCnt, 2 => $spaces));
270 $allowToProceed = FALSE;
271 }
272 CRM_Core_Session::setStatus($statusMessage, ts('Registration Error'), 'error');
273 }
274 elseif ($processedCnt == $spaces) {
275 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0
276 || $realPayLater || $this->_requireApproval
277 ) {
278 $this->_resetAllowWaitlist = TRUE;
279 if ($this->_requireApproval) {
280 $statusMessage = ts("If you skip this participant there will be enough spaces in the event for your group (you will not be wait listed). Registration for this event requires approval. You will receive an email once your registration has been reviewed.");
281 }
282 else {
283 $statusMessage = ts("If you skip this participant there will be enough spaces in the event for your group (you will not be wait listed).");
284 }
285 }
286 else {
287 //hey there is enough space and we require payment.
288 $statusMessage = ts("If you skip this participant there will be enough spaces in the event for your group (you will not be wait listed). Please go back to the main registration page and reduce the number of additional people. You will also need to complete payment information.");
289 $includeSkipButton = FALSE;
290 }
291 }
292 }
293
294 // for priceset with count
8cc574cf 295 if ($pricesetFieldsCount && !empty($this->_values['event']['has_waitlist']) &&
6a488035
TO
296 !$this->_allowConfirmation
297 ) {
298
299 if ($this->_isEventFull) {
300 $statusMessage = ts('This event is currently full. You are registering for the waiting list. You will be notified if spaces become available.');
301 }
302 elseif ($this->_allowWaitlist ||
303 (!$this->_allowWaitlist && ($processedCnt + $pricesetFieldsCount) > $this->_availableRegistrations)
304 ) {
305
306 $waitingMsg = ts('It looks like you are registering more participants then there are spaces available. All participants will be added to the waiting list. You will be notified if spaces become available.');
307 $confirmedMsg = ts('It looks like there are enough spaces in this event for your group (you will not be wait listed).');
308 if ($this->_requireApproval) {
309 $waitingMsg = ts('It looks like you are now registering a group of %1 participants. The event has %2 available spaces (you will not be wait listed). Registration for this event requires approval. You will receive an email once your registration has been reviewed.');
310 $confirmedMsg = ts('It looks there are enough spaces in this event for your group (you will not be wait listed). Registration for this event requires approval. You will receive an email once your registration has been reviewed.');
311 }
312
313 $this->assign('waitingMsg', $waitingMsg);
314 $this->assign('confirmedMsg', $confirmedMsg);
315
316 $this->assign('availableRegistrations', $this->_availableRegistrations);
317 $this->assign('currentParticipantCount', $processedCnt);
318 $this->assign('allowGroupOnWaitlist', TRUE);
319
320 $paymentBypassed = NULL;
a7488080 321 if (!empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
322 !$this->_allowWaitlist &&
323 !$realPayLater &&
324 !$this->_requireApproval &&
325 !(CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0)
326 ) {
327 $paymentBypassed = ts('Please go back to the main registration page, to complete payment information.');
328 }
329 $this->assign('paymentBypassed', $paymentBypassed);
330 }
331 }
332
333 $this->assign('statusMessage', $statusMessage);
334
335 $buttons = array(
336 array('type' => 'back',
337 'name' => ts('<< Go Back'),
338 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
339 ),
340 );
341
342 //CRM-4320
343 if ($allowToProceed) {
344 $buttons = array_merge($buttons, array(
345 array('type' => 'next',
346 'name' => ts('Continue >>'),
347 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
348 'isDefault' => TRUE,
349 'js' => $js,
350 ),
351 )
352 );
353 if ($includeSkipButton) {
354 $buttons = array_merge($buttons, array(
355 array('type' => 'next',
356 'name' => ts('Skip Participant >>|'),
357 'subName' => 'skip',
358 ),
359 )
360 );
361 }
362 }
363 $this->addButtons($buttons);
364 $this->addFormRule(array('CRM_Event_Form_Registration_AdditionalParticipant', 'formRule'), $this);
365 }
366
367 /**
368 * global form rule
369 *
77b97be7
EM
370 * @param array $fields the input form values
371 * @param array $files the uploaded files if any
372 * @param $self
373 *
374 * @internal param array $options additional user data
6a488035
TO
375 *
376 * @return true if no errors, else array of errors
377 * @access public
378 * @static
379 */
380 static function formRule($fields, $files, $self) {
381 $errors = array();
382 //get the button name.
383 $button = substr($self->controller->getButtonName(), -4);
384
385 $realPayLater = FALSE;
8cc574cf 386 if (!empty($self->_values['event']['is_monetary']) && !empty($self->_values['event']['is_pay_later'])) {
6a488035
TO
387 $realPayLater = CRM_Utils_Array::value('is_pay_later', $self->_params[0]);
388 }
389
390 if ($button != 'skip') {
391 //Check that either an email or firstname+lastname is included in the form(CRM-9587)
392 CRM_Event_Form_Registration_Register::checkProfileComplete($fields, $errors, $self->_eventId);
393
394 //Additional Participant can also register for an event only once
395 $isRegistered = CRM_Event_Form_Registration_Register::checkRegistration($fields, $self, TRUE);
396 if ($isRegistered) {
397 if ($self->_values['event']['allow_same_participant_emails']) {
398 $errors['_qf_default'] = ts('A person is already registered for this event.');
399 }
400 else {
401 $errors["email-{$self->_bltID}"] = ts('A person with this email address is already registered for this event.');
402 }
403 }
404
405 //get the complete params.
406 $params = $self->get('params');
407
408 //take the participant instance.
409 $addParticipantNum = substr($self->_name, 12);
410
411 if (is_array($params)) {
412 foreach ($params as $key => $value) {
413 if ($key != $addParticipantNum) {
414 if (!$self->_values['event']['allow_same_participant_emails']) {
415 //collect all email fields
416 $existingEmails = array();
417 $additionalParticipantEmails = array();
418 if (is_array($value)) {
419 foreach ($value as $key => $val) {
420 if (substr($key, 0, 6) == 'email-' && $val) {
421 $existingEmails[] = $val;
422 }
423 }
424 }
425 foreach ($fields as $key => $val) {
426 if (substr($key, 0, 6) == 'email-' && $val) {
427 $additionalParticipantEmails[] = $val;
428 $mailKey = $key;
429 }
430 }
431 //check if any emails are common to both arrays
432 if (count(array_intersect($existingEmails, $additionalParticipantEmails))) {
433 $errors[$mailKey] = ts('The email address must be unique for each participant.');
434 break;
435 }
436 }
437 else {
438 // check with first_name and last_name for additional participants
33ab1d69 439 if (!empty($value['first_name']) && ($value['first_name'] == CRM_Utils_Array::value('first_name', $fields)) &&
3896b90a 440 (CRM_Utils_Array::value('last_name',$value) == CRM_Utils_Array::value('last_name', $fields))
6a488035
TO
441 ) {
442 $errors['first_name'] = ts('The first name and last name must be unique for each participant.');
443 break;
444 }
445 }
446 }
447 }
448 }
449
450 //check for atleast one pricefields should be selected
a7488080 451 if (!empty($fields['priceSetId'])) {
6a488035
TO
452 $allParticipantParams = $params;
453
454 //format current participant params.
455 $allParticipantParams[$addParticipantNum] = self::formatPriceSetParams($self, $fields);
456 $totalParticipants = self::getParticipantCount($self, $allParticipantParams);
457
458 //validate price field params.
459 $priceSetErrors = self::validatePriceSet($self, $allParticipantParams);
460 $errors = array_merge($errors, CRM_Utils_Array::value($addParticipantNum, $priceSetErrors, array()));
461
462 if (!$self->_allowConfirmation &&
463 is_numeric($self->_availableRegistrations)
464 ) {
a7488080 465 if (!empty($self->_params[0]['bypass_payment']) &&
6a488035
TO
466 !$self->_allowWaitlist &&
467 !$realPayLater &&
468 !$self->_requireApproval &&
469 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0) &&
470 $totalParticipants < $self->_availableRegistrations
471 ) {
472 $errors['_qf_default'] = ts("Your event registration will be confirmed. Please go back to the main registration page, to complete payment information.");
473 }
474 //check for availability of registrations.
8cc574cf 475 if (!$self->_allowConfirmation && empty($self->_values['event']['has_waitlist']) &&
6a488035
TO
476 $totalParticipants > $self->_availableRegistrations
477 ) {
478 $errors['_qf_default'] = ts('It looks like event has only %2 seats available and you are trying to register %1 participants, so could you please select price options accordingly.', array(1 => $totalParticipants, 2 => $self->_availableRegistrations));
479 }
480 }
481 }
482 }
483
8cc574cf 484 if ($button == 'skip' && $self->_lastParticipant && !empty($fields['priceSetId'])) {
9da8dc8c 485 $pricesetFieldsCount = CRM_Price_BAO_PriceSet::getPricesetCount($fields['priceSetId']);
6a488035
TO
486 if (($pricesetFieldsCount < 1) || $self->_allowConfirmation) {
487 return $errors;
488 }
489
8cc574cf 490 if (!empty($self->_values['event']['has_waitlist']) && !empty($self->_params[0]['bypass_payment']) &&
6a488035
TO
491 !$self->_allowWaitlist &&
492 !$realPayLater &&
493 !$self->_requireApproval &&
494 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0)
495 ) {
496 $errors['_qf_default'] = ts("You are going to skip the last participant, your event registration will be confirmed. Please go back to the main registration page, to complete payment information.");
497 }
498 }
499
500
501 if ($button != 'skip' &&
502 $self->_values['event']['is_monetary'] &&
503 !isset($errors['_qf_default']) &&
504 !$self->validatePaymentValues($self, $fields)
505 ) {
506 $errors['_qf_default'] = ts("Your payment information looks incomplete. Please go back to the main registration page, to complete payment information.");
507 $self->set('forcePayement', TRUE);
508 }
509 elseif ($button == 'skip') {
510 $self->set('forcePayement', TRUE);
511 }
512
513 return $errors;
514 }
515
516 function validatePaymentValues($self, $fields) {
517
a7488080 518 if (!empty($self->_params[0]['bypass_payment']) ||
6a488035
TO
519 $self->_allowWaitlist ||
520 empty($self->_fields) ||
521 CRM_Utils_Array::value('amount', $self->_params[0]) > 0
522 ) {
523 return TRUE;
524 }
525
526 $validatePayement = FALSE;
a7488080 527 if (!empty($fields['priceSetId'])) {
6a488035 528 $lineItem = array();
9da8dc8c 529 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], $fields, $lineItem);
6a488035
TO
530 if ($fields['amount'] > 0) {
531 $validatePayement = TRUE;
532 // $self->_forcePayement = true;
533 // return false;
534 }
535 }
a7488080 536 elseif (!empty($fields['amount']) &&
6a488035
TO
537 (CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) > 0)
538 ) {
539 $validatePayement = TRUE;
540 }
541
542 if (!$validatePayement) {
6a488035 543 return TRUE;
6a488035
TO
544 }
545
7cb3d4f0
CW
546 $errors = array();
547
548 CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
6a488035
TO
549
550 // make sure that credit card number and cvv are valid
7cb3d4f0 551 CRM_Core_Payment_Form::validateCreditCard($self->_params[0], $errors);
6a488035 552
7cb3d4f0
CW
553 if ($errors) {
554 return FALSE;
6a488035 555 }
7cb3d4f0 556
6a488035
TO
557 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
558 if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) {
559 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
560 if ($customizedValue == $greetingType && empty($self->_params[0][$greeting . '_custom'])) {
561 return FALSE;
562 }
563 }
564 }
565 }
566
567 /**
568 * Function to process the form
569 *
570 * @access public
571 *
355ba699 572 * @return void
6a488035
TO
573 */
574 public function postProcess() {
575 //get the button name.
576 $button = substr($this->controller->getButtonName(), -4);
577
578 //take the participant instance.
579 $addParticipantNum = substr($this->_name, 12);
580
581 //user submitted params.
582 $params = $this->controller->exportValues($this->_name);
583
584 if (!$this->_allowConfirmation) {
585 // check if the participant is already registered
586 $params['contact_id'] = CRM_Event_Form_Registration_Register::checkRegistration($params, $this, TRUE, TRUE);
587 }
588
589 //carry campaign to partcipants.
590 if (array_key_exists('participant_campaign_id', $params)) {
591 $params['campaign_id'] = $params['participant_campaign_id'];
592 }
593 else {
594 $params['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
595 }
596
597 // if waiting is enabled
598 if (!$this->_allowConfirmation &&
599 is_numeric($this->_availableRegistrations)
600 ) {
601 $this->_allowWaitlist = FALSE;
602 //get the current page count.
603 $currentCount = self::getParticipantCount($this, $params);
604 if ($button == 'skip') {
605 $currentCount = 'skip';
606 }
607
608 //get the total count.
609 $previousCount = self::getParticipantCount($this, $this->_params, TRUE);
610 $totalParticipants = $previousCount;
611 if (is_numeric($currentCount)) {
612 $totalParticipants += $currentCount;
613 }
a7488080 614 if (!empty($this->_values['event']['has_waitlist']) &&
6a488035
TO
615 $totalParticipants > $this->_availableRegistrations
616 ) {
617 $this->_allowWaitlist = TRUE;
618 }
619 $this->set('allowWaitlist', $this->_allowWaitlist);
620 $this->_lineItemParticipantsCount[$addParticipantNum] = $currentCount;
621 }
622
623 if ($button == 'skip') {
624 //hack for free/zero amount event.
625 if ($this->_resetAllowWaitlist) {
626 $this->_allowWaitlist = FALSE;
627 $this->set('allowWaitlist', FALSE);
628 if ($this->_requireApproval) {
629 $status = ts("You have skipped last participant and which result into event having enough spaces, but your registration require 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.");
630 }
631 else {
632 $status = ts("You have skipped last participant and which result into event having enough spaces, hence your group become as register participants though you selected on wait list.");
633 }
634 CRM_Core_Session::setStatus($status);
635 }
636
637 $this->_params[$addParticipantNum] = 'skip';
638 if (isset($this->_lineItem)) {
639 $this->_lineItem[$addParticipantNum] = 'skip';
640 $this->_lineItemParticipantsCount[$addParticipantNum] = 'skip';
641 }
642 }
643 else {
644
645 $config = CRM_Core_Config::singleton();
646 $params['currencyID'] = $config->defaultCurrency;
647
648 if ($this->_values['event']['is_monetary']) {
649
650 //added for discount
651 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
652
653 if (!empty($this->_values['discount'][$discountId])) {
654 $params['discount_id'] = $discountId;
655 $params['amount_level'] = $this->_values['discount'][$discountId][$params['amount']]['label'];
656 $params['amount'] = $this->_values['discount'][$discountId][$params['amount']]['value'];
657 }
658 elseif (empty($params['priceSetId'])) {
659 $params['amount_level'] = $this->_values['fee'][$params['amount']]['label'];
660 $params['amount'] = $this->_values['fee'][$params['amount']]['value'];
661 }
662 else {
663 $lineItem = array();
9da8dc8c 664 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem);
6a488035
TO
665
666 //build the line item..
667 if (array_key_exists($addParticipantNum, $this->_lineItem)) {
668 $this->_lineItem[$addParticipantNum] = $lineItem;
669 }
670 else {
671 $this->_lineItem[] = $lineItem;
672 }
673 }
674 }
675
676 if (array_key_exists('participant_role', $params)) {
677 $params['participant_role_id'] = $params['participant_role'];
678 }
679
a7488080 680 if (empty($params['participant_role_id']) && $this->_values['event']['default_role_id']) {
6a488035
TO
681 $params['participant_role_id'] = $this->_values['event']['default_role_id'];
682 }
683
a7488080 684 if (!empty($this->_params[0]['is_pay_later'])) {
6a488035
TO
685 $params['is_pay_later'] = 1;
686 }
687
688 //carry additional participant id, contact id if pre-registered.
689 if ($this->_allowConfirmation && $this->_additionalParticipantId) {
690 $params['contact_id'] = $this->_contactId;
691 $params['participant_id'] = $this->_additionalParticipantId;
692 }
693
694 //build the params array.
695 if (array_key_exists($addParticipantNum, $this->_params)) {
696 $this->_params[$addParticipantNum] = $params;
697 }
698 else {
699 $this->_params[] = $params;
700 }
701 }
702
703 //finally set the params.
704 $this->set('params', $this->_params);
705 //set the line item.
706 if ($this->_lineItem) {
707 $this->set('lineItem', $this->_lineItem);
708 $this->set('lineItemParticipantsCount', $this->_lineItemParticipantsCount);
709 }
710
711 $participantNo = count($this->_params);
712 if ($button != 'skip') {
713 $statusMsg = ts('Registration information for participant %1 has been saved.', array(1 => $participantNo));
714 CRM_Core_Session::setStatus($statusMsg, ts('Registration Saved'), 'success');
715 }
716
1909126f 717 // Check whether to process the registration now, calling processRegistration()
718 if (
719 !$this->_values['event']['is_confirm_enabled'] // CRM-11182 - Optional confirmation screen
d6121d3e 720 && !$this->_values['event']['is_monetary']
1909126f 721 && CRM_Utils_Array::value('additional_participants', $this->_params[0])
722 && $this->isLastParticipant()
6a488035
TO
723 ) {
724 CRM_Event_Form_Registration_Register::processRegistration($this->_params, NULL);
725 }
726 }
727
5dd94759 728 public static function &getPages($additionalParticipant) {
6a488035
TO
729 $details = array();
730 for ($i = 1; $i <= $additionalParticipant; $i++) {
731 $details["Participant_{$i}"] = array(
732 'className' => 'CRM_Event_Form_Registration_AdditionalParticipant',
733 'title' => "Register Additional Participant {$i}",
734 );
735 }
736 return $details;
737 }
738
739 /**
740 * check whether call current participant is last one
741 *
2a6da8d7
EM
742 * @param bool $isButtonJs
743 *
6a488035
TO
744 * @return boolean ture on success.
745 * @access public
746 */
747 function isLastParticipant($isButtonJs = FALSE) {
748 $participant = $isButtonJs ? $this->_params[0]['additional_participants'] : $this->_params[0]['additional_participants'] + 1;
749 if (count($this->_params) == $participant) {
750 return TRUE;
751 }
752 return FALSE;
753 }
754
755 /**
756 * Reset values for all options those are full.
757 *
758 **/
759 function resetElementValue($optionFullIds = array(
760 )) {
761 if (!is_array($optionFullIds) ||
762 empty($optionFullIds) ||
763 !$this->isSubmitted()
764 ) {
765 return;
766 }
767
768 foreach ($optionFullIds as $fldId => $optIds) {
769 $name = "price_$fldId";
770 if (!$this->elementExists($name)) {
771 continue;
772 }
773
774 $element = $this->getElement($name);
775 $eleType = $element->getType();
776
777 $resetSubmitted = FALSE;
778 switch ($eleType) {
779 case 'text':
780 if ($element->isFrozen()) {
781 $element->setValue('');
782 $resetSubmitted = TRUE;
783 }
784 break;
785
786 case 'group':
787 if (is_array($element->_elements)) {
788 foreach ($element->_elements as $child) {
789 $childType = $child->getType();
790 $methodName = 'getName';
791 if ($childType) {
792 $methodName = 'getValue';
793 }
794 if (in_array($child->{$methodName}(), $optIds) && $child->isFrozen()) {
795 $resetSubmitted = TRUE;
796 $child->updateAttributes(array('checked' => NULL));
797 }
798 }
799 }
800 break;
801
802 case 'select':
803 $resetSubmitted = TRUE;
804 $element->_values = array();
805 break;
806 }
807
808 //finally unset values from submitted.
809 if ($resetSubmitted) {
810 $this->resetSubmittedValue($name, $optIds);
811 }
812 }
813 }
814
815 function resetSubmittedValue($elementName, $optionIds = array(
816 )) {
817 if (empty($elementName) ||
818 !$this->elementExists($elementName) ||
819 !$this->getSubmitValue($elementName)
820 ) {
821 return;
822 }
823 foreach (array(
824 'constantValues', 'submitValues', 'defaultValues') as $val) {
825 $values = &$this->{"_$val"};
826 if (!is_array($values) || empty($values)) {
827 continue;
828 }
829 $eleVal = CRM_Utils_Array::value($elementName, $values);
830 if (empty($eleVal)) {
831 continue;
832 }
833 if (is_array($eleVal)) {
834 $found = FALSE;
835 foreach ($eleVal as $keyId => $ignore) {
836 if (in_array($keyId, $optionIds)) {
837 $found = TRUE;
838 unset($values[$elementName][$keyId]);
839 }
840 }
841 if ($found && empty($values[$elementName][$keyId])) {
842 $values[$elementName][$keyId] = NULL;
843 }
844 }
845 else {
846 $values[$elementName][$keyId] = NULL;
847 }
848 }
849 }
850}
851