Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Event / Form / Registration / AdditionalParticipant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 *
97 * @return None
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');
105 if ($discountId && CRM_Utils_Array::value('default_discount_fee_id', $this->_values['event'])) {
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) {
114 if (!CRM_Utils_Array::value('options', $val)) {
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 *
171 * @return None
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) {
194 if (CRM_Utils_Array::value($name, $$keys) &&
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;
209 if ($this->isLastParticipant(TRUE) && !CRM_Utils_Array::value('is_monetary', $this->_values['event'])) {
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
222 $pricesetFieldsCount = CRM_Price_BAO_Set::getPricesetCount($this->_priceSetId);
223
224 if ($this->_lastParticipant || $pricesetFieldsCount) {
225 //get the participant total.
226 $processedCnt = self::getParticipantCount($this, $this->_params, TRUE);
227 }
228
229 if (!$this->_allowConfirmation &&
230 CRM_Utils_Array::value('bypass_payment', $this->_params[0]) &&
231 $this->_lastParticipant
232 ) {
233
234 //get the event spaces.
235 $spaces = $this->_availableRegistrations;
236
237 $currentPageMaxCount = 1;
238 if ($pricesetFieldsCount) {
239 $currentPageMaxCount = $pricesetFieldsCount;
240 }
241
242 //we might did reset allow waiting in case of dynamic calculation
243 if (CRM_Utils_Array::value('bypass_payment', $this->_params[0]) &&
244 is_numeric($spaces) &&
245 $processedCnt > $spaces
246 ) {
247 $this->_allowWaitlist = TRUE;
248 $this->set('allowWaitlist', TRUE);
249 }
250
251 //lets allow to become a part of runtime waiting list, if primary selected pay later.
252 $realPayLater = FALSE;
253 if (CRM_Utils_Array::value('is_monetary', $this->_values['event']) &&
254 CRM_Utils_Array::value('is_pay_later', $this->_values['event'])
255 ) {
256 $realPayLater = CRM_Utils_Array::value('is_pay_later', $this->_params[0]);
257 }
258
259 //truly spaces are greater than required.
260 if (is_numeric($spaces) && $spaces >= ($processedCnt + $currentPageMaxCount)) {
261 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0 || $this->_requireApproval) {
262 $this->_allowWaitlist = FALSE;
263 $this->set('allowWaitlist', $this->_allowWaitlist);
264 if ($this->_requireApproval) {
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). Registration for this event requires approval. You will receive an email once your registration has been reviewed.", array(1 => ++$processedCnt, 2 => $spaces));
266 }
267 else {
268 $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));
269 }
270 }
271 else {
272 $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));
273 $allowToProceed = FALSE;
274 }
275 CRM_Core_Session::setStatus($statusMessage, ts('Registration Error'), 'error');
276 }
277 elseif ($processedCnt == $spaces) {
278 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0
279 || $realPayLater || $this->_requireApproval
280 ) {
281 $this->_resetAllowWaitlist = TRUE;
282 if ($this->_requireApproval) {
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). Registration for this event requires approval. You will receive an email once your registration has been reviewed.");
284 }
285 else {
286 $statusMessage = ts("If you skip this participant there will be enough spaces in the event for your group (you will not be wait listed).");
287 }
288 }
289 else {
290 //hey there is enough space and we require payment.
291 $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.");
292 $includeSkipButton = FALSE;
293 }
294 }
295 }
296
297 // for priceset with count
298 if ($pricesetFieldsCount &&
299 CRM_Utils_Array::value('has_waitlist', $this->_values['event']) &&
300 !$this->_allowConfirmation
301 ) {
302
303 if ($this->_isEventFull) {
304 $statusMessage = ts('This event is currently full. You are registering for the waiting list. You will be notified if spaces become available.');
305 }
306 elseif ($this->_allowWaitlist ||
307 (!$this->_allowWaitlist && ($processedCnt + $pricesetFieldsCount) > $this->_availableRegistrations)
308 ) {
309
310 $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.');
311 $confirmedMsg = ts('It looks like there are enough spaces in this event for your group (you will not be wait listed).');
312 if ($this->_requireApproval) {
313 $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.');
314 $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.');
315 }
316
317 $this->assign('waitingMsg', $waitingMsg);
318 $this->assign('confirmedMsg', $confirmedMsg);
319
320 $this->assign('availableRegistrations', $this->_availableRegistrations);
321 $this->assign('currentParticipantCount', $processedCnt);
322 $this->assign('allowGroupOnWaitlist', TRUE);
323
324 $paymentBypassed = NULL;
325 if (CRM_Utils_Array::value('bypass_payment', $this->_params[0]) &&
326 !$this->_allowWaitlist &&
327 !$realPayLater &&
328 !$this->_requireApproval &&
329 !(CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0)
330 ) {
331 $paymentBypassed = ts('Please go back to the main registration page, to complete payment information.');
332 }
333 $this->assign('paymentBypassed', $paymentBypassed);
334 }
335 }
336
337 $this->assign('statusMessage', $statusMessage);
338
339 $buttons = array(
340 array('type' => 'back',
341 'name' => ts('<< Go Back'),
342 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
343 ),
344 );
345
346 //CRM-4320
347 if ($allowToProceed) {
348 $buttons = array_merge($buttons, array(
349 array('type' => 'next',
350 'name' => ts('Continue >>'),
351 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
352 'isDefault' => TRUE,
353 'js' => $js,
354 ),
355 )
356 );
357 if ($includeSkipButton) {
358 $buttons = array_merge($buttons, array(
359 array('type' => 'next',
360 'name' => ts('Skip Participant >>|'),
361 'subName' => 'skip',
362 ),
363 )
364 );
365 }
366 }
367 $this->addButtons($buttons);
368 $this->addFormRule(array('CRM_Event_Form_Registration_AdditionalParticipant', 'formRule'), $this);
369 }
370
371 /**
372 * global form rule
373 *
374 * @param array $fields the input form values
375 * @param array $files the uploaded files if any
376 * @param array $options additional user data
377 *
378 * @return true if no errors, else array of errors
379 * @access public
380 * @static
381 */
382 static function formRule($fields, $files, $self) {
383 $errors = array();
384 //get the button name.
385 $button = substr($self->controller->getButtonName(), -4);
386
387 $realPayLater = FALSE;
388 if (CRM_Utils_Array::value('is_monetary', $self->_values['event']) &&
389 CRM_Utils_Array::value('is_pay_later', $self->_values['event'])
390 ) {
391 $realPayLater = CRM_Utils_Array::value('is_pay_later', $self->_params[0]);
392 }
393
394 if ($button != 'skip') {
395 //Check that either an email or firstname+lastname is included in the form(CRM-9587)
396 CRM_Event_Form_Registration_Register::checkProfileComplete($fields, $errors, $self->_eventId);
397
398 //Additional Participant can also register for an event only once
399 $isRegistered = CRM_Event_Form_Registration_Register::checkRegistration($fields, $self, TRUE);
400 if ($isRegistered) {
401 if ($self->_values['event']['allow_same_participant_emails']) {
402 $errors['_qf_default'] = ts('A person is already registered for this event.');
403 }
404 else {
405 $errors["email-{$self->_bltID}"] = ts('A person with this email address is already registered for this event.');
406 }
407 }
408
409 //get the complete params.
410 $params = $self->get('params');
411
412 //take the participant instance.
413 $addParticipantNum = substr($self->_name, 12);
414
415 if (is_array($params)) {
416 foreach ($params as $key => $value) {
417 if ($key != $addParticipantNum) {
418 if (!$self->_values['event']['allow_same_participant_emails']) {
419 //collect all email fields
420 $existingEmails = array();
421 $additionalParticipantEmails = array();
422 if (is_array($value)) {
423 foreach ($value as $key => $val) {
424 if (substr($key, 0, 6) == 'email-' && $val) {
425 $existingEmails[] = $val;
426 }
427 }
428 }
429 foreach ($fields as $key => $val) {
430 if (substr($key, 0, 6) == 'email-' && $val) {
431 $additionalParticipantEmails[] = $val;
432 $mailKey = $key;
433 }
434 }
435 //check if any emails are common to both arrays
436 if (count(array_intersect($existingEmails, $additionalParticipantEmails))) {
437 $errors[$mailKey] = ts('The email address must be unique for each participant.');
438 break;
439 }
440 }
441 else {
442 // check with first_name and last_name for additional participants
443 if ((CRM_Utils_Array::value('first_name',$value) == $fields['first_name']) &&
444 (CRM_Utils_Array::value('last_name',$value) == $fields['last_name'])
445 ) {
446 $errors['first_name'] = ts('The first name and last name must be unique for each participant.');
447 break;
448 }
449 }
450 }
451 }
452 }
453
454 //check for atleast one pricefields should be selected
455 if (CRM_Utils_Array::value('priceSetId', $fields)) {
456 $allParticipantParams = $params;
457
458 //format current participant params.
459 $allParticipantParams[$addParticipantNum] = self::formatPriceSetParams($self, $fields);
460 $totalParticipants = self::getParticipantCount($self, $allParticipantParams);
461
462 //validate price field params.
463 $priceSetErrors = self::validatePriceSet($self, $allParticipantParams);
464 $errors = array_merge($errors, CRM_Utils_Array::value($addParticipantNum, $priceSetErrors, array()));
465
466 if (!$self->_allowConfirmation &&
467 is_numeric($self->_availableRegistrations)
468 ) {
469 if (CRM_Utils_Array::value('bypass_payment', $self->_params[0]) &&
470 !$self->_allowWaitlist &&
471 !$realPayLater &&
472 !$self->_requireApproval &&
473 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0) &&
474 $totalParticipants < $self->_availableRegistrations
475 ) {
476 $errors['_qf_default'] = ts("Your event registration will be confirmed. Please go back to the main registration page, to complete payment information.");
477 }
478 //check for availability of registrations.
479 if (!$self->_allowConfirmation &&
480 !CRM_Utils_Array::value('has_waitlist', $self->_values['event']) &&
481 $totalParticipants > $self->_availableRegistrations
482 ) {
483 $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));
484 }
485 }
486 }
487 }
488
489 if ($button == 'skip' && $self->_lastParticipant && CRM_Utils_Array::value('priceSetId', $fields)) {
490 $pricesetFieldsCount = CRM_Price_BAO_Set::getPricesetCount($fields['priceSetId']);
491 if (($pricesetFieldsCount < 1) || $self->_allowConfirmation) {
492 return $errors;
493 }
494
495 if (CRM_Utils_Array::value('has_waitlist', $self->_values['event']) &&
496 CRM_Utils_Array::value('bypass_payment', $self->_params[0]) &&
497 !$self->_allowWaitlist &&
498 !$realPayLater &&
499 !$self->_requireApproval &&
500 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0)
501 ) {
502 $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.");
503 }
504 }
505
506
507 if ($button != 'skip' &&
508 $self->_values['event']['is_monetary'] &&
509 !isset($errors['_qf_default']) &&
510 !$self->validatePaymentValues($self, $fields)
511 ) {
512 $errors['_qf_default'] = ts("Your payment information looks incomplete. Please go back to the main registration page, to complete payment information.");
513 $self->set('forcePayement', TRUE);
514 }
515 elseif ($button == 'skip') {
516 $self->set('forcePayement', TRUE);
517 }
518
519 return $errors;
520 }
521
522 function validatePaymentValues($self, $fields) {
523
524 if (CRM_Utils_Array::value('bypass_payment', $self->_params[0]) ||
525 $self->_allowWaitlist ||
526 empty($self->_fields) ||
527 CRM_Utils_Array::value('amount', $self->_params[0]) > 0
528 ) {
529 return TRUE;
530 }
531
532 $validatePayement = FALSE;
533 if (CRM_Utils_Array::value('priceSetId', $fields)) {
534 $lineItem = array();
535 CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
536 if ($fields['amount'] > 0) {
537 $validatePayement = TRUE;
538 // $self->_forcePayement = true;
539 // return false;
540 }
541 }
542 elseif (CRM_Utils_Array::value('amount', $fields) &&
543 (CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) > 0)
544 ) {
545 $validatePayement = TRUE;
546 }
547
548 if (!$validatePayement) {
549
550 return TRUE;
551
552 }
553
554 foreach ($self->_fields as $name => $fld) {
555 if ($fld['is_required'] &&
556 CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))
557 ) {
558 return FALSE;
559 }
560 }
561
562 // make sure that credit card number and cvv are valid
563 if (CRM_Utils_Array::value('credit_card_type', $self->_params[0])) {
564 if (CRM_Utils_Array::value('credit_card_number', $self->_params[0]) &&
565 !CRM_Utils_Rule::creditCardNumber($self->_params[0]['credit_card_number'], $self->_params[0]['credit_card_type'])
566 ) {
567 return FALSE;
568 }
569
570 if (CRM_Utils_Array::value('cvv2', $self->_params[0]) &&
571 !CRM_Utils_Rule::cvv($self->_params[0]['cvv2'], $self->_params[0]['credit_card_type'])
572 ) {
573 return FALSE;
574 }
575 }
576 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
577 if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) {
578 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
579 if ($customizedValue == $greetingType && empty($self->_params[0][$greeting . '_custom'])) {
580 return FALSE;
581 }
582 }
583 }
584 }
585
586 /**
587 * Function to process the form
588 *
589 * @access public
590 *
591 * @return None
592 */
593 public function postProcess() {
594 //get the button name.
595 $button = substr($this->controller->getButtonName(), -4);
596
597 //take the participant instance.
598 $addParticipantNum = substr($this->_name, 12);
599
600 //user submitted params.
601 $params = $this->controller->exportValues($this->_name);
602
603 if (!$this->_allowConfirmation) {
604 // check if the participant is already registered
605 $params['contact_id'] = CRM_Event_Form_Registration_Register::checkRegistration($params, $this, TRUE, TRUE);
606 }
607
608 //carry campaign to partcipants.
609 if (array_key_exists('participant_campaign_id', $params)) {
610 $params['campaign_id'] = $params['participant_campaign_id'];
611 }
612 else {
613 $params['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
614 }
615
616 // if waiting is enabled
617 if (!$this->_allowConfirmation &&
618 is_numeric($this->_availableRegistrations)
619 ) {
620 $this->_allowWaitlist = FALSE;
621 //get the current page count.
622 $currentCount = self::getParticipantCount($this, $params);
623 if ($button == 'skip') {
624 $currentCount = 'skip';
625 }
626
627 //get the total count.
628 $previousCount = self::getParticipantCount($this, $this->_params, TRUE);
629 $totalParticipants = $previousCount;
630 if (is_numeric($currentCount)) {
631 $totalParticipants += $currentCount;
632 }
633 if (CRM_Utils_Array::value('has_waitlist', $this->_values['event']) &&
634 $totalParticipants > $this->_availableRegistrations
635 ) {
636 $this->_allowWaitlist = TRUE;
637 }
638 $this->set('allowWaitlist', $this->_allowWaitlist);
639 $this->_lineItemParticipantsCount[$addParticipantNum] = $currentCount;
640 }
641
642 if ($button == 'skip') {
643 //hack for free/zero amount event.
644 if ($this->_resetAllowWaitlist) {
645 $this->_allowWaitlist = FALSE;
646 $this->set('allowWaitlist', FALSE);
647 if ($this->_requireApproval) {
648 $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.");
649 }
650 else {
651 $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.");
652 }
653 CRM_Core_Session::setStatus($status);
654 }
655
656 $this->_params[$addParticipantNum] = 'skip';
657 if (isset($this->_lineItem)) {
658 $this->_lineItem[$addParticipantNum] = 'skip';
659 $this->_lineItemParticipantsCount[$addParticipantNum] = 'skip';
660 }
661 }
662 else {
663
664 $config = CRM_Core_Config::singleton();
665 $params['currencyID'] = $config->defaultCurrency;
666
667 if ($this->_values['event']['is_monetary']) {
668
669 //added for discount
670 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
671
672 if (!empty($this->_values['discount'][$discountId])) {
673 $params['discount_id'] = $discountId;
674 $params['amount_level'] = $this->_values['discount'][$discountId][$params['amount']]['label'];
675 $params['amount'] = $this->_values['discount'][$discountId][$params['amount']]['value'];
676 }
677 elseif (empty($params['priceSetId'])) {
678 $params['amount_level'] = $this->_values['fee'][$params['amount']]['label'];
679 $params['amount'] = $this->_values['fee'][$params['amount']]['value'];
680 }
681 else {
682 $lineItem = array();
683 CRM_Price_BAO_Set::processAmount($this->_values['fee'], $params, $lineItem);
684
685 //build the line item..
686 if (array_key_exists($addParticipantNum, $this->_lineItem)) {
687 $this->_lineItem[$addParticipantNum] = $lineItem;
688 }
689 else {
690 $this->_lineItem[] = $lineItem;
691 }
692 }
693 }
694
695 if (array_key_exists('participant_role', $params)) {
696 $params['participant_role_id'] = $params['participant_role'];
697 }
698
699 if (!CRM_Utils_Array::value('participant_role_id', $params) && $this->_values['event']['default_role_id']) {
700 $params['participant_role_id'] = $this->_values['event']['default_role_id'];
701 }
702
703 if (CRM_Utils_Array::value('is_pay_later', $this->_params[0])) {
704 $params['is_pay_later'] = 1;
705 }
706
707 //carry additional participant id, contact id if pre-registered.
708 if ($this->_allowConfirmation && $this->_additionalParticipantId) {
709 $params['contact_id'] = $this->_contactId;
710 $params['participant_id'] = $this->_additionalParticipantId;
711 }
712
713 //build the params array.
714 if (array_key_exists($addParticipantNum, $this->_params)) {
715 $this->_params[$addParticipantNum] = $params;
716 }
717 else {
718 $this->_params[] = $params;
719 }
720 }
721
722 //finally set the params.
723 $this->set('params', $this->_params);
724 //set the line item.
725 if ($this->_lineItem) {
726 $this->set('lineItem', $this->_lineItem);
727 $this->set('lineItemParticipantsCount', $this->_lineItemParticipantsCount);
728 }
729
730 $participantNo = count($this->_params);
731 if ($button != 'skip') {
732 $statusMsg = ts('Registration information for participant %1 has been saved.', array(1 => $participantNo));
733 CRM_Core_Session::setStatus($statusMsg, ts('Registration Saved'), 'success');
734 }
735
736 //to check whether call processRegistration()
737 if (!$this->_values['event']['is_monetary']
738 && CRM_Utils_Array::value('additional_participants', $this->_params[0])
739 && $this->isLastParticipant()
740 ) {
741 CRM_Event_Form_Registration_Register::processRegistration($this->_params, NULL);
742 }
743 }
744
745 function &getPages($additionalParticipant) {
746 $details = array();
747 for ($i = 1; $i <= $additionalParticipant; $i++) {
748 $details["Participant_{$i}"] = array(
749 'className' => 'CRM_Event_Form_Registration_AdditionalParticipant',
750 'title' => "Register Additional Participant {$i}",
751 );
752 }
753 return $details;
754 }
755
756 /**
757 * check whether call current participant is last one
758 *
759 * @return boolean ture on success.
760 * @access public
761 */
762 function isLastParticipant($isButtonJs = FALSE) {
763 $participant = $isButtonJs ? $this->_params[0]['additional_participants'] : $this->_params[0]['additional_participants'] + 1;
764 if (count($this->_params) == $participant) {
765 return TRUE;
766 }
767 return FALSE;
768 }
769
770 /**
771 * Reset values for all options those are full.
772 *
773 **/
774 function resetElementValue($optionFullIds = array(
775 )) {
776 if (!is_array($optionFullIds) ||
777 empty($optionFullIds) ||
778 !$this->isSubmitted()
779 ) {
780 return;
781 }
782
783 foreach ($optionFullIds as $fldId => $optIds) {
784 $name = "price_$fldId";
785 if (!$this->elementExists($name)) {
786 continue;
787 }
788
789 $element = $this->getElement($name);
790 $eleType = $element->getType();
791
792 $resetSubmitted = FALSE;
793 switch ($eleType) {
794 case 'text':
795 if ($element->isFrozen()) {
796 $element->setValue('');
797 $resetSubmitted = TRUE;
798 }
799 break;
800
801 case 'group':
802 if (is_array($element->_elements)) {
803 foreach ($element->_elements as $child) {
804 $childType = $child->getType();
805 $methodName = 'getName';
806 if ($childType) {
807 $methodName = 'getValue';
808 }
809 if (in_array($child->{$methodName}(), $optIds) && $child->isFrozen()) {
810 $resetSubmitted = TRUE;
811 $child->updateAttributes(array('checked' => NULL));
812 }
813 }
814 }
815 break;
816
817 case 'select':
818 $resetSubmitted = TRUE;
819 $element->_values = array();
820 break;
821 }
822
823 //finally unset values from submitted.
824 if ($resetSubmitted) {
825 $this->resetSubmittedValue($name, $optIds);
826 }
827 }
828 }
829
830 function resetSubmittedValue($elementName, $optionIds = array(
831 )) {
832 if (empty($elementName) ||
833 !$this->elementExists($elementName) ||
834 !$this->getSubmitValue($elementName)
835 ) {
836 return;
837 }
838 foreach (array(
839 'constantValues', 'submitValues', 'defaultValues') as $val) {
840 $values = &$this->{"_$val"};
841 if (!is_array($values) || empty($values)) {
842 continue;
843 }
844 $eleVal = CRM_Utils_Array::value($elementName, $values);
845 if (empty($eleVal)) {
846 continue;
847 }
848 if (is_array($eleVal)) {
849 $found = FALSE;
850 foreach ($eleVal as $keyId => $ignore) {
851 if (in_array($keyId, $optionIds)) {
852 $found = TRUE;
853 unset($values[$elementName][$keyId]);
854 }
855 }
856 if ($found && empty($values[$elementName][$keyId])) {
857 $values[$elementName][$keyId] = NULL;
858 }
859 }
860 else {
861 $values[$elementName][$keyId] = NULL;
862 }
863 }
864 }
865}
866