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