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