Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-04-18-48-05
[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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 /**
66f9e52b 44 * The defaults involved in this page.
6a488035
TO
45 */
46 public $_defaults = array();
47
48 /**
100fef9d 49 * Pre-registered additional participant id.
6a488035
TO
50 */
51 public $additionalParticipantId = NULL;
52
53 /**
66f9e52b 54 * Set variables up before form is built.
6a488035
TO
55 *
56 * @return void
6a488035 57 */
00be9182 58 public function preProcess() {
6a488035
TO
59 parent::preProcess();
60
61 $participantNo = substr($this->_name, 12);
62
63 //lets process in-queue participants.
64 if ($this->_participantId && $this->_additionalParticipantIds) {
65 $this->_additionalParticipantId = CRM_Utils_Array::value($participantNo, $this->_additionalParticipantIds);
66 }
67
68 $participantCnt = $participantNo + 1;
69 $this->assign('formId', $participantNo);
70 $this->_params = array();
71 $this->_params = $this->get('params');
72
73 $participantTot = $this->_params[0]['additional_participants'] + 1;
74 $skipCount = count(array_keys($this->_params, "skip"));
75 if ($skipCount) {
76 $this->assign('skipCount', $skipCount);
77 }
78 CRM_Utils_System::setTitle(ts('Register Participant %1 of %2', array(1 => $participantCnt, 2 => $participantTot)));
79
80 //CRM-4320, hack to check last participant.
81 $this->_lastParticipant = FALSE;
82 if ($participantTot == $participantCnt) {
83 $this->_lastParticipant = TRUE;
84 }
85 $this->assign('lastParticipant', $this->_lastParticipant);
86 }
87
88 /**
c490a46a 89 * Set default values for the form. For edit/view mode
6a488035
TO
90 * the default values are retrieved from the database
91 *
6a488035 92 *
355ba699 93 * @return void
6a488035 94 */
00be9182 95 public function setDefaultValues() {
6a488035
TO
96 $defaults = $unsetSubmittedOptions = array();
97 $discountId = NULL;
98 //fix for CRM-3088, default value for discount set.
99 if (!empty($this->_values['discount'])) {
100 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
8cc574cf 101 if ($discountId && !empty($this->_values['event']['default_discount_fee_id'])) {
0479b4c8 102 $discountKey = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionValue", $this->_values['event']['default_discount_fee_id'], 'weight', 'id'
6a488035
TO
103 );
104 $defaults['amount'] = key(array_slice($this->_values['discount'][$discountId], $discountKey - 1, $discountKey, TRUE));
105 }
106 }
107 if ($this->_priceSetId) {
108 foreach ($this->_feeBlock as $key => $val) {
a7488080 109 if (empty($val['options'])) {
6a488035
TO
110 continue;
111 }
112
113 $optionsFull = CRM_Utils_Array::value('option_full_ids', $val, array());
114 foreach ($val['options'] as $keys => $values) {
115 if ($values['is_default'] && !in_array($keys, $optionsFull)) {
116 if ($val['html_type'] == 'CheckBox') {
117 $defaults["price_{$key}"][$keys] = 1;
118 }
119 else {
120 $defaults["price_{$key}"] = $keys;
121 }
122 }
123 }
124 if (!empty($optionsFull)) {
125 $unsetSubmittedOptions[$val['id']] = $optionsFull;
126 }
127 }
128 }
129
130 //CRM-4320, setdefault additional participant values.
131 if ($this->_allowConfirmation && $this->_additionalParticipantId) {
132 //hack to get set default from eventFees.php
353ffa53
TO
133 $this->_discountId = $discountId;
134 $this->_pId = $this->_additionalParticipantId;
135 $this->_contactId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_additionalParticipantId, 'contact_id');
6a488035
TO
136 $participantDefaults = CRM_Event_Form_EventFees::setDefaultValues($this);
137 $participantDefaults = array_merge($this->_defaults, $participantDefaults);
138 // use primary email address if billing email address is empty
139 if (empty($this->_defaults["email-{$this->_bltID}"]) &&
140 !empty($this->_defaults["email-Primary"])
141 ) {
142 $participantDefaults["email-{$this->_bltID}"] = $this->_defaults["email-Primary"];
143 }
144 $defaults = array_merge($defaults, $participantDefaults);
145 }
146
147 $defaults = array_merge($this->_defaults, $defaults);
148
149 //reset values for all options those are full.
0dc0b759 150 CRM_Event_Form_Registration::resetElementValue($unsetSubmittedOptions, $this);
6a488035
TO
151
152 //load default campaign from page.
153 if (array_key_exists('participant_campaign_id', $this->_fields)) {
154 $defaults['participant_campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
155 }
6a488035
TO
156
157 return $defaults;
158 }
159
160 /**
66f9e52b 161 * Build the form object.
6a488035 162 *
355ba699 163 * @return void
6a488035
TO
164 */
165 public function buildQuickForm() {
166 $config = CRM_Core_Config::singleton();
167 $button = substr($this->controller->getButtonName(), -4);
168
169 $this->add('hidden', 'scriptFee', NULL);
170 $this->add('hidden', 'scriptArray', NULL);
171
172 if ($this->_values['event']['is_monetary']) {
173 CRM_Event_Form_Registration_Register::buildAmount($this);
174 }
175 $first_name = $last_name = NULL;
176 $pre = $post = array();
177 foreach (array(
353ffa53 178 'pre',
608e6658 179 'post',
353ffa53 180 ) as $keys) {
6a488035
TO
181 if (isset($this->_values['additional_custom_' . $keys . '_id'])) {
182 $this->buildCustom($this->_values['additional_custom_' . $keys . '_id'], 'additionalCustom' . ucfirst($keys), TRUE);
183 $$keys = CRM_Core_BAO_UFGroup::getFields($this->_values['additional_custom_' . $keys . '_id']);
184 }
185 foreach (array(
353ffa53 186 'first_name',
608e6658 187 'last_name',
353ffa53 188 ) as $name) {
48078478 189 if (array_key_exists($name, $$keys) &&
6a488035
TO
190 CRM_Utils_Array::value('is_required', CRM_Utils_Array::value($name, $$keys))
191 ) {
192 $$name = 1;
193 }
194 }
195 }
196
197 $required = ($button == 'skip' ||
198 $this->_values['event']['allow_same_participant_emails'] == 1 &&
199 ($first_name && $last_name)
200 ) ? FALSE : TRUE;
201
202 //add buttons
203 $js = NULL;
8cc574cf 204 if ($this->isLastParticipant(TRUE) && empty($this->_values['event']['is_monetary'])) {
6a488035
TO
205 $js = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');");
206 }
207
208 //handle case where user might sart with waiting by group
209 //registration and skip some people and now group fit to
210 //become registered so need to take payment from user.
211 //this case only occurs at dynamic waiting status, CRM-4320
212 $statusMessage = NULL;
213 $allowToProceed = TRUE;
214 $includeSkipButton = TRUE;
215 $this->_resetAllowWaitlist = FALSE;
216
9da8dc8c 217 $pricesetFieldsCount = CRM_Price_BAO_PriceSet::getPricesetCount($this->_priceSetId);
6a488035
TO
218
219 if ($this->_lastParticipant || $pricesetFieldsCount) {
220 //get the participant total.
221 $processedCnt = self::getParticipantCount($this, $this->_params, TRUE);
222 }
223
8cc574cf 224 if (!$this->_allowConfirmation && !empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
225 $this->_lastParticipant
226 ) {
227
228 //get the event spaces.
229 $spaces = $this->_availableRegistrations;
230
231 $currentPageMaxCount = 1;
232 if ($pricesetFieldsCount) {
233 $currentPageMaxCount = $pricesetFieldsCount;
234 }
235
236 //we might did reset allow waiting in case of dynamic calculation
a7488080 237 if (!empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
238 is_numeric($spaces) &&
239 $processedCnt > $spaces
240 ) {
241 $this->_allowWaitlist = TRUE;
242 $this->set('allowWaitlist', TRUE);
243 }
244
245 //lets allow to become a part of runtime waiting list, if primary selected pay later.
246 $realPayLater = FALSE;
8cc574cf 247 if (!empty($this->_values['event']['is_monetary']) && !empty($this->_values['event']['is_pay_later'])) {
6a488035
TO
248 $realPayLater = CRM_Utils_Array::value('is_pay_later', $this->_params[0]);
249 }
250
251 //truly spaces are greater than required.
252 if (is_numeric($spaces) && $spaces >= ($processedCnt + $currentPageMaxCount)) {
253 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0 || $this->_requireApproval) {
254 $this->_allowWaitlist = FALSE;
255 $this->set('allowWaitlist', $this->_allowWaitlist);
256 if ($this->_requireApproval) {
353ffa53
TO
257 $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(
258 1 => ++$processedCnt,
608e6658 259 2 => $spaces,
353ffa53 260 ));
6a488035
TO
261 }
262 else {
353ffa53
TO
263 $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(
264 1 => ++$processedCnt,
608e6658 265 2 => $spaces,
353ffa53 266 ));
6a488035
TO
267 }
268 }
269 else {
353ffa53
TO
270 $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(
271 1 => ++$processedCnt,
608e6658 272 2 => $spaces,
353ffa53 273 ));
6a488035
TO
274 $allowToProceed = FALSE;
275 }
276 CRM_Core_Session::setStatus($statusMessage, ts('Registration Error'), 'error');
277 }
278 elseif ($processedCnt == $spaces) {
279 if (CRM_Utils_Array::value('amount', $this->_params[0], 0) == 0
280 || $realPayLater || $this->_requireApproval
281 ) {
282 $this->_resetAllowWaitlist = TRUE;
283 if ($this->_requireApproval) {
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). Registration for this event requires approval. You will receive an email once your registration has been reviewed.");
285 }
286 else {
287 $statusMessage = ts("If you skip this participant there will be enough spaces in the event for your group (you will not be wait listed).");
288 }
289 }
290 else {
291 //hey there is enough space and we require payment.
292 $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.");
293 $includeSkipButton = FALSE;
294 }
295 }
296 }
297
298 // for priceset with count
8cc574cf 299 if ($pricesetFieldsCount && !empty($this->_values['event']['has_waitlist']) &&
6a488035
TO
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;
a7488080 325 if (!empty($this->_params[0]['bypass_payment']) &&
6a488035
TO
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(
0479b4c8 340 array(
f212d37d
CW
341 'type' => 'back',
342 'name' => ts('Go Back'),
6a488035
TO
343 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
344 ),
345 );
346
347 //CRM-4320
348 if ($allowToProceed) {
349 $buttons = array_merge($buttons, array(
353ffa53 350 array(
f212d37d
CW
351 'type' => 'next',
352 'name' => ts('Continue'),
6a488035
TO
353 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
354 'isDefault' => TRUE,
355 'js' => $js,
356 ),
357 )
358 );
359 if ($includeSkipButton) {
360 $buttons = array_merge($buttons, array(
353ffa53
TO
361 array(
362 'type' => 'next',
363 'name' => ts('Skip Participant'),
364 'subName' => 'skip',
365 'icon' => 'seek-next',
608e6658 366 ),
353ffa53 367 )
6a488035
TO
368 );
369 }
370 }
371 $this->addButtons($buttons);
372 $this->addFormRule(array('CRM_Event_Form_Registration_AdditionalParticipant', 'formRule'), $this);
373 }
374
375 /**
66f9e52b 376 * Global form rule.
6a488035 377 *
d4dd1e85
TO
378 * @param array $fields
379 * The input form values.
380 * @param array $files
381 * The uploaded files if any.
77b97be7
EM
382 * @param $self
383 *
6a488035 384 *
72b3a70c
CW
385 * @return bool|array
386 * true if no errors, else array of errors
6a488035 387 */
00be9182 388 public static function formRule($fields, $files, $self) {
6a488035
TO
389 $errors = array();
390 //get the button name.
391 $button = substr($self->controller->getButtonName(), -4);
392
393 $realPayLater = FALSE;
8cc574cf 394 if (!empty($self->_values['event']['is_monetary']) && !empty($self->_values['event']['is_pay_later'])) {
6a488035
TO
395 $realPayLater = CRM_Utils_Array::value('is_pay_later', $self->_params[0]);
396 }
397
398 if ($button != 'skip') {
399 //Check that either an email or firstname+lastname is included in the form(CRM-9587)
400 CRM_Event_Form_Registration_Register::checkProfileComplete($fields, $errors, $self->_eventId);
401
402 //Additional Participant can also register for an event only once
403 $isRegistered = CRM_Event_Form_Registration_Register::checkRegistration($fields, $self, TRUE);
404 if ($isRegistered) {
405 if ($self->_values['event']['allow_same_participant_emails']) {
406 $errors['_qf_default'] = ts('A person is already registered for this event.');
407 }
408 else {
409 $errors["email-{$self->_bltID}"] = ts('A person with this email address is already registered for this event.');
410 }
411 }
412
413 //get the complete params.
414 $params = $self->get('params');
415
416 //take the participant instance.
417 $addParticipantNum = substr($self->_name, 12);
418
419 if (is_array($params)) {
420 foreach ($params as $key => $value) {
421 if ($key != $addParticipantNum) {
422 if (!$self->_values['event']['allow_same_participant_emails']) {
423 //collect all email fields
424 $existingEmails = array();
425 $additionalParticipantEmails = array();
426 if (is_array($value)) {
0479b4c8
TO
427 foreach ($value as $key => $val) {
428 if (substr($key, 0, 6) == 'email-' && $val) {
429 $existingEmails[] = $val;
430 }
6a488035
TO
431 }
432 }
6a488035
TO
433 foreach ($fields as $key => $val) {
434 if (substr($key, 0, 6) == 'email-' && $val) {
435 $additionalParticipantEmails[] = $val;
436 $mailKey = $key;
437 }
438 }
439 //check if any emails are common to both arrays
440 if (count(array_intersect($existingEmails, $additionalParticipantEmails))) {
441 $errors[$mailKey] = ts('The email address must be unique for each participant.');
442 break;
443 }
444 }
445 else {
446 // check with first_name and last_name for additional participants
33ab1d69 447 if (!empty($value['first_name']) && ($value['first_name'] == CRM_Utils_Array::value('first_name', $fields)) &&
0479b4c8 448 (CRM_Utils_Array::value('last_name', $value) == CRM_Utils_Array::value('last_name', $fields))
6a488035
TO
449 ) {
450 $errors['first_name'] = ts('The first name and last name must be unique for each participant.');
451 break;
452 }
453 }
454 }
455 }
456 }
457
458 //check for atleast one pricefields should be selected
a7488080 459 if (!empty($fields['priceSetId'])) {
6a488035
TO
460 $allParticipantParams = $params;
461
462 //format current participant params.
463 $allParticipantParams[$addParticipantNum] = self::formatPriceSetParams($self, $fields);
464 $totalParticipants = self::getParticipantCount($self, $allParticipantParams);
465
466 //validate price field params.
467 $priceSetErrors = self::validatePriceSet($self, $allParticipantParams);
468 $errors = array_merge($errors, CRM_Utils_Array::value($addParticipantNum, $priceSetErrors, array()));
469
470 if (!$self->_allowConfirmation &&
471 is_numeric($self->_availableRegistrations)
472 ) {
a7488080 473 if (!empty($self->_params[0]['bypass_payment']) &&
6a488035
TO
474 !$self->_allowWaitlist &&
475 !$realPayLater &&
476 !$self->_requireApproval &&
477 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0) &&
478 $totalParticipants < $self->_availableRegistrations
479 ) {
480 $errors['_qf_default'] = ts("Your event registration will be confirmed. Please go back to the main registration page, to complete payment information.");
481 }
482 //check for availability of registrations.
8cc574cf 483 if (!$self->_allowConfirmation && empty($self->_values['event']['has_waitlist']) &&
6a488035
TO
484 $totalParticipants > $self->_availableRegistrations
485 ) {
353ffa53
TO
486 $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(
487 1 => $totalParticipants,
608e6658 488 2 => $self->_availableRegistrations,
353ffa53 489 ));
6a488035
TO
490 }
491 }
492 }
493 }
494
8cc574cf 495 if ($button == 'skip' && $self->_lastParticipant && !empty($fields['priceSetId'])) {
9da8dc8c 496 $pricesetFieldsCount = CRM_Price_BAO_PriceSet::getPricesetCount($fields['priceSetId']);
6a488035
TO
497 if (($pricesetFieldsCount < 1) || $self->_allowConfirmation) {
498 return $errors;
499 }
500
8cc574cf 501 if (!empty($self->_values['event']['has_waitlist']) && !empty($self->_params[0]['bypass_payment']) &&
6a488035
TO
502 !$self->_allowWaitlist &&
503 !$realPayLater &&
504 !$self->_requireApproval &&
505 !(CRM_Utils_Array::value('amount', $self->_params[0], 0) == 0)
506 ) {
507 $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.");
508 }
509 }
510
6a488035
TO
511 if ($button != 'skip' &&
512 $self->_values['event']['is_monetary'] &&
513 !isset($errors['_qf_default']) &&
514 !$self->validatePaymentValues($self, $fields)
515 ) {
516 $errors['_qf_default'] = ts("Your payment information looks incomplete. Please go back to the main registration page, to complete payment information.");
517 $self->set('forcePayement', TRUE);
518 }
519 elseif ($button == 'skip') {
520 $self->set('forcePayement', TRUE);
521 }
522
523 return $errors;
524 }
525
0cf587a7
EM
526 /**
527 * @param $self
528 * @param $fields
529 *
530 * @return bool
531 */
00be9182 532 public function validatePaymentValues($self, $fields) {
6a488035 533
a7488080 534 if (!empty($self->_params[0]['bypass_payment']) ||
6a488035
TO
535 $self->_allowWaitlist ||
536 empty($self->_fields) ||
537 CRM_Utils_Array::value('amount', $self->_params[0]) > 0
538 ) {
539 return TRUE;
540 }
541
542 $validatePayement = FALSE;
a7488080 543 if (!empty($fields['priceSetId'])) {
6a488035 544 $lineItem = array();
9da8dc8c 545 CRM_Price_BAO_PriceSet::processAmount($self->_values['fee'], $fields, $lineItem);
6a488035
TO
546 if ($fields['amount'] > 0) {
547 $validatePayement = TRUE;
548 // $self->_forcePayement = true;
549 // return false;
550 }
551 }
a7488080 552 elseif (!empty($fields['amount']) &&
6a488035
TO
553 (CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) > 0)
554 ) {
555 $validatePayement = TRUE;
556 }
557
558 if (!$validatePayement) {
6a488035 559 return TRUE;
6a488035
TO
560 }
561
7cb3d4f0
CW
562 $errors = array();
563
564 CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
6a488035 565
a479fe60 566 // validate supplied payment instrument values (e.g. credit card number and cvv)
2ae4d103 567 $payment_processor_id = $self->_params[0]['payment_processor'];
a479fe60 568 CRM_Core_Payment_Form::validatePaymentInstrument($payment_processor_id, $self->_params[0], $errors, $self);
6a488035 569
0dc0b759 570 if (!empty($errors)) {
7cb3d4f0 571 return FALSE;
6a488035 572 }
7cb3d4f0 573
6a488035
TO
574 foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
575 if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) {
576 $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
577 if ($customizedValue == $greetingType && empty($self->_params[0][$greeting . '_custom'])) {
578 return FALSE;
579 }
580 }
581 }
2ae4d103 582 return TRUE;
6a488035
TO
583 }
584
585 /**
66f9e52b 586 * Process the form submission.
6a488035 587 *
6a488035 588 *
355ba699 589 * @return void
6a488035
TO
590 */
591 public function postProcess() {
592 //get the button name.
593 $button = substr($this->controller->getButtonName(), -4);
594
595 //take the participant instance.
596 $addParticipantNum = substr($this->_name, 12);
597
598 //user submitted params.
599 $params = $this->controller->exportValues($this->_name);
600
601 if (!$this->_allowConfirmation) {
602 // check if the participant is already registered
603 $params['contact_id'] = CRM_Event_Form_Registration_Register::checkRegistration($params, $this, TRUE, TRUE);
604 }
605
606 //carry campaign to partcipants.
607 if (array_key_exists('participant_campaign_id', $params)) {
608 $params['campaign_id'] = $params['participant_campaign_id'];
609 }
610 else {
611 $params['campaign_id'] = CRM_Utils_Array::value('campaign_id', $this->_values['event']);
612 }
613
614 // if waiting is enabled
615 if (!$this->_allowConfirmation &&
616 is_numeric($this->_availableRegistrations)
617 ) {
618 $this->_allowWaitlist = FALSE;
619 //get the current page count.
620 $currentCount = self::getParticipantCount($this, $params);
621 if ($button == 'skip') {
622 $currentCount = 'skip';
623 }
624
625 //get the total count.
626 $previousCount = self::getParticipantCount($this, $this->_params, TRUE);
627 $totalParticipants = $previousCount;
628 if (is_numeric($currentCount)) {
629 $totalParticipants += $currentCount;
630 }
a7488080 631 if (!empty($this->_values['event']['has_waitlist']) &&
6a488035
TO
632 $totalParticipants > $this->_availableRegistrations
633 ) {
634 $this->_allowWaitlist = TRUE;
635 }
636 $this->set('allowWaitlist', $this->_allowWaitlist);
637 $this->_lineItemParticipantsCount[$addParticipantNum] = $currentCount;
638 }
639
640 if ($button == 'skip') {
641 //hack for free/zero amount event.
642 if ($this->_resetAllowWaitlist) {
643 $this->_allowWaitlist = FALSE;
644 $this->set('allowWaitlist', FALSE);
645 if ($this->_requireApproval) {
646 $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.");
647 }
648 else {
649 $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.");
650 }
651 CRM_Core_Session::setStatus($status);
652 }
653
654 $this->_params[$addParticipantNum] = 'skip';
655 if (isset($this->_lineItem)) {
656 $this->_lineItem[$addParticipantNum] = 'skip';
657 $this->_lineItemParticipantsCount[$addParticipantNum] = 'skip';
658 }
659 }
660 else {
661
662 $config = CRM_Core_Config::singleton();
663 $params['currencyID'] = $config->defaultCurrency;
664
665 if ($this->_values['event']['is_monetary']) {
666
667 //added for discount
668 $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
669
670 if (!empty($this->_values['discount'][$discountId])) {
671 $params['discount_id'] = $discountId;
672 $params['amount_level'] = $this->_values['discount'][$discountId][$params['amount']]['label'];
673 $params['amount'] = $this->_values['discount'][$discountId][$params['amount']]['value'];
674 }
675 elseif (empty($params['priceSetId'])) {
676 $params['amount_level'] = $this->_values['fee'][$params['amount']]['label'];
677 $params['amount'] = $this->_values['fee'][$params['amount']]['value'];
678 }
679 else {
680 $lineItem = array();
9da8dc8c 681 CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem);
6a488035 682
0dc0b759 683 //build line item array..
684 //if requireApproval/waitlist is enabled we hide fees for primary participant
685 // (and not for additional participant which might be is a bug)
686 //lineItem are not correctly build for primary participant
687 //this results in redundancy since now lineItems for additional participant will be build against primary participantNum
688 //therefore lineItems must always be build against current participant No
689 $this->_lineItem[$addParticipantNum] = $lineItem;
6a488035
TO
690 }
691 }
692
693 if (array_key_exists('participant_role', $params)) {
694 $params['participant_role_id'] = $params['participant_role'];
695 }
696
a7488080 697 if (empty($params['participant_role_id']) && $this->_values['event']['default_role_id']) {
6a488035
TO
698 $params['participant_role_id'] = $this->_values['event']['default_role_id'];
699 }
700
a7488080 701 if (!empty($this->_params[0]['is_pay_later'])) {
6a488035
TO
702 $params['is_pay_later'] = 1;
703 }
704
705 //carry additional participant id, contact id if pre-registered.
706 if ($this->_allowConfirmation && $this->_additionalParticipantId) {
707 $params['contact_id'] = $this->_contactId;
708 $params['participant_id'] = $this->_additionalParticipantId;
709 }
710
711 //build the params array.
0dc0b759 712 $this->_params[$addParticipantNum] = $params;
6a488035
TO
713 }
714
715 //finally set the params.
716 $this->set('params', $this->_params);
717 //set the line item.
718 if ($this->_lineItem) {
719 $this->set('lineItem', $this->_lineItem);
720 $this->set('lineItemParticipantsCount', $this->_lineItemParticipantsCount);
721 }
722
723 $participantNo = count($this->_params);
724 if ($button != 'skip') {
725 $statusMsg = ts('Registration information for participant %1 has been saved.', array(1 => $participantNo));
726 CRM_Core_Session::setStatus($statusMsg, ts('Registration Saved'), 'success');
727 }
728
1909126f 729 // Check whether to process the registration now, calling processRegistration()
730 if (
731 !$this->_values['event']['is_confirm_enabled'] // CRM-11182 - Optional confirmation screen
d6121d3e 732 && !$this->_values['event']['is_monetary']
1909126f 733 && CRM_Utils_Array::value('additional_participants', $this->_params[0])
734 && $this->isLastParticipant()
6a488035
TO
735 ) {
736 CRM_Event_Form_Registration_Register::processRegistration($this->_params, NULL);
737 }
738 }
739
0cf587a7
EM
740 /**
741 * @param $additionalParticipant
742 *
743 * @return array
744 */
5dd94759 745 public static function &getPages($additionalParticipant) {
6a488035
TO
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 /**
66f9e52b 757 * Check whether call current participant is last one.
6a488035 758 *
2a6da8d7
EM
759 * @param bool $isButtonJs
760 *
608e6658 761 * @return bool
a6c01b45 762 * ture on success.
6a488035 763 */
00be9182 764 public function isLastParticipant($isButtonJs = FALSE) {
6a488035
TO
765 $participant = $isButtonJs ? $this->_params[0]['additional_participants'] : $this->_params[0]['additional_participants'] + 1;
766 if (count($this->_params) == $participant) {
767 return TRUE;
768 }
769 return FALSE;
770 }
a14eff52 771
6a488035 772}