3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * This class generates form components for processing Event.
20 class CRM_Event_Form_Registration_Register
extends CRM_Event_Form_Registration
{
23 * The fields involved in this page.
30 * The status message that user view.
34 protected $_waitlistMsg;
35 protected $_requireApprovalMsg;
38 * Deprecated parameter that we hope to remove.
45 * Skip duplicate check.
47 * This can be set using hook_civicrm_buildForm() to override the registration dupe check.
50 * @see https://issues.civicrm.org/jira/browse/CRM-7604
52 public $_skipDupeRegistrationCheck = FALSE;
54 public $_paymentProcessorID;
57 * Show fee block or not.
71 * Array of payment related fields to potentially display on this form (generally credit card or debit card fields).
73 * This is rendered via billingBlock.tpl.
77 public $_paymentFields = [];
80 * Is this submission incurring no costs.
82 * @param array $fields
83 * @param \CRM_Event_Form_Registration_Register $form
87 protected static function isZeroAmount($fields, $form): bool {
88 $isZeroAmount = FALSE;
89 if (!empty($fields['priceSetId'])) {
90 if (empty($fields['amount'])) {
94 elseif (!empty($fields['amount']) &&
95 (isset($form->_values
['discount'][$fields['amount']])
96 && CRM_Utils_Array
::value('value', $form->_values
['discount'][$fields['amount']]) == 0
101 elseif (!empty($fields['amount']) &&
102 (isset($form->_values
['fee'][$fields['amount']])
103 && CRM_Utils_Array
::value('value', $form->_values
['fee'][$fields['amount']]) == 0
106 $isZeroAmount = TRUE;
108 return $isZeroAmount;
112 * Get the contact id for the registration.
114 * @param array $fields
115 * @param CRM_Event_Form_Registration $form
116 * @param bool $isAdditional
120 public static function getRegistrationContactID($fields, $form, $isAdditional) {
122 if (!$isAdditional) {
123 $contactID = $form->getContactID();
125 if (!$contactID && is_array($fields) && $fields) {
126 $contactID = CRM_Contact_BAO_Contact
::getFirstDuplicateContact($fields, 'Individual', 'Unsupervised', [], FALSE, CRM_Utils_Array
::value('dedupe_rule_group_id', $form->_values
['event']), ['event_id' => CRM_Utils_Array
::value('id', $form->_values
['event'])]);
132 * Set variables up before form is built.
134 * @throws \CRM_Core_Exception
136 public function preProcess() {
137 parent
::preProcess();
140 //here we can't use parent $this->_allowWaitlist as user might
141 //walk back and we might set this value in this postProcess.
142 //(we set when spaces < group count and want to allow become part of waiting )
143 $eventFull = CRM_Event_BAO_Participant
::eventFull($this->_eventId
, FALSE, CRM_Utils_Array
::value('has_waitlist', $this->_values
['event']));
145 // Get payment processors if appropriate for this event
146 // We hide the payment fields if the event is full or requires approval,
147 // and the current user has not yet been approved CRM-12279
148 $this->_noFees
= (($eventFull ||
$this->_requireApproval
) && !$this->_allowConfirmation
);
149 $this->_paymentProcessors
= $this->_noFees ?
[] : $this->get('paymentProcessors');
150 $this->preProcessPaymentOptions();
152 $this->_allowWaitlist
= FALSE;
153 if ($eventFull && !$this->_allowConfirmation
&& !empty($this->_values
['event']['has_waitlist'])) {
154 $this->_allowWaitlist
= TRUE;
155 $this->_waitlistMsg
= $this->_values
['event']['waitlist_text'] ??
NULL;
156 if (!$this->_waitlistMsg
) {
157 $this->_waitlistMsg
= ts('This event is currently full. However you can register now and get added to a waiting list. You will be notified if spaces become available.');
160 $this->set('allowWaitlist', $this->_allowWaitlist
);
162 //To check if the user is already registered for the event(CRM-2426)
163 if (!$this->_skipDupeRegistrationCheck
) {
164 self
::checkRegistration(NULL, $this);
167 $this->assign('availableRegistrations', $this->_availableRegistrations
);
169 // get the participant values from EventFees.php, CRM-4320
170 if ($this->_allowConfirmation
) {
171 CRM_Event_Form_EventFees
::preProcess($this);
176 * Set default values for the form.
180 * @throws \CRM_Core_Exception
181 * @throws \CiviCRM_API3_Exception
183 public function setDefaultValues() {
184 $this->_defaults
= [];
185 if (!$this->_allowConfirmation
&& $this->_requireApproval
) {
186 $this->_defaults
['bypass_payment'] = 1;
188 $contactID = $this->getContactID();
189 CRM_Core_Payment_Form
::setDefaultValues($this, $contactID);
191 CRM_Event_BAO_Participant
::formatFieldsAndSetProfileDefaults($contactID, $this);
193 // Set default payment processor as default payment_processor radio button value
194 if (!empty($this->_paymentProcessors
)) {
195 foreach ($this->_paymentProcessors
as $pid => $value) {
196 if (!empty($value['is_default'])) {
197 $this->_defaults
['payment_processor_id'] = $pid;
202 //if event is monetary and pay later is enabled and payment
203 //processor is not available then freeze the pay later checkbox with
205 if (!empty($this->_values
['event']['is_pay_later']) &&
206 !is_array($this->_paymentProcessor
)
208 $this->_defaults
['is_pay_later'] = 1;
211 //set custom field defaults
212 if (!empty($this->_fields
)) {
213 //load default campaign from page.
214 if (array_key_exists('participant_campaign_id', $this->_fields
)) {
215 $this->_defaults
['participant_campaign_id'] = CRM_Utils_Array
::value('campaign_id',
216 $this->_values
['event']
220 foreach ($this->_fields
as $name => $field) {
221 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
223 if (!isset($this->_defaults
[$name])) {
224 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID, $name, $this->_defaults
,
225 NULL, CRM_Profile_Form
::MODE_REGISTER
232 //fix for CRM-3088, default value for discount set.
234 if (!empty($this->_values
['discount'])) {
235 $discountId = CRM_Core_BAO_Discount
::findSet($this->_eventId
, 'civicrm_event');
237 if (isset($this->_values
['event']['default_discount_fee_id'])) {
238 $discountKey = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_OptionValue',
239 $this->_values
['event']['default_discount_fee_id'],
243 $this->_defaults
['amount'] = key(array_slice($this->_values
['discount'][$discountId],
244 $discountKey - 1, $discountKey, TRUE
250 // add this event's default participant role to defaults array
251 // (for cases where participant_role field is included in form via profile)
252 if ($this->_values
['event']['default_role_id']) {
253 $this->_defaults
['participant_role']
254 = $this->_defaults
['participant_role_id'] = $this->_values
['event']['default_role_id'];
256 if ($this->_priceSetId
&& !empty($this->_feeBlock
)) {
257 foreach ($this->_feeBlock
as $key => $val) {
258 if (empty($val['options'])) {
261 $optionFullIds = CRM_Utils_Array
::value('option_full_ids', $val, []);
262 foreach ($val['options'] as $keys => $values) {
263 if ($values['is_default'] && empty($values['is_full'])) {
265 if ($val['html_type'] == 'CheckBox') {
266 $this->_defaults
["price_{$key}"][$keys] = 1;
269 $this->_defaults
["price_{$key}"] = $keys;
273 $unsetSubmittedOptions[$val['id']] = $optionFullIds;
275 //reset values for all options those are full.
276 CRM_Event_Form_Registration
::resetElementValue($unsetSubmittedOptions, $this);
279 //set default participant fields, CRM-4320.
280 $hasAdditionalParticipants = FALSE;
281 if ($this->_allowConfirmation
) {
282 $this->_contactId
= $contactID;
283 $this->_discountId
= $discountId;
284 $forcePayLater = CRM_Utils_Array
::value('is_pay_later', $this->_defaults
, FALSE);
285 $this->_defaults
= array_merge($this->_defaults
, CRM_Event_Form_EventFees
::setDefaultValues($this));
286 $this->_defaults
['is_pay_later'] = $forcePayLater;
288 if ($this->_additionalParticipantIds
) {
289 $hasAdditionalParticipants = TRUE;
290 $this->_defaults
['additional_participants'] = count($this->_additionalParticipantIds
);
293 $this->assign('hasAdditionalParticipants', $hasAdditionalParticipants);
295 // //hack to simplify credit card entry for testing
296 // $this->_defaults['credit_card_type'] = 'Visa';
297 // $this->_defaults['credit_card_number'] = '4807731747657838';
298 // $this->_defaults['cvv2'] = '000';
299 // $this->_defaults['credit_card_exp_date'] = array( 'Y' => '2010', 'M' => '05' );
301 // to process Custom data that are appended to URL
302 $getDefaults = CRM_Core_BAO_CustomGroup
::extractGetParams($this, "'Contact', 'Individual', 'Contribution', 'Participant'");
303 if (!empty($getDefaults)) {
304 $this->_defaults
= array_merge($this->_defaults
, $getDefaults);
307 return $this->_defaults
;
311 * Build the form object.
313 public function buildQuickForm() {
314 // build profiles first so that we can determine address fields etc
315 // and then show copy address checkbox
316 $this->buildCustom($this->_values
['custom_pre_id'], 'customPre');
317 $this->buildCustom($this->_values
['custom_post_id'], 'customPost');
319 // CRM-18399: used by template to pass pre profile id as a url arg
320 $this->assign('custom_pre_id', $this->_values
['custom_pre_id']);
322 CRM_Core_Payment_ProcessorForm
::buildQuickForm($this);
324 $contactID = $this->getContactID();
326 $this->assign('contact_id', $contactID);
327 $this->assign('display_name', CRM_Contact_BAO_Contact
::displayName($contactID));
330 $bypassPayment = $allowGroupOnWaitlist = $isAdditionalParticipants = FALSE;
331 if ($this->_values
['event']['is_multiple_registrations']) {
332 // don't allow to add additional during confirmation if not preregistered.
333 if (!$this->_allowConfirmation ||
$this->_additionalParticipantIds
) {
334 // CRM-17745: Make maximum additional participants configurable
335 // Label is value + 1, since the code sees this is ADDITIONAL participants (in addition to "self")
336 $additionalOptions = [];
337 $additionalOptions[''] = 1;
338 for ($i = 1; $i <= $this->_values
['event']['max_additional_participants']; $i++
) {
339 $additionalOptions[$i] = $i +
1;
341 $this->add('select', 'additional_participants',
342 ts('How many people are you registering?'),
345 ['onChange' => "allowParticipant()"]
347 $isAdditionalParticipants = TRUE;
351 if (!$this->_allowConfirmation
) {
352 $bypassPayment = TRUE;
355 //hack to allow group to register w/ waiting
356 if ((!empty($this->_values
['event']['is_multiple_registrations']) ||
359 !$this->_allowConfirmation
&&
360 is_numeric($this->_availableRegistrations
) && !empty($this->_values
['event']['has_waitlist'])
362 $bypassPayment = TRUE;
363 //case might be group become as a part of waitlist.
364 //If not waitlist then they require admin approve.
365 $allowGroupOnWaitlist = TRUE;
366 $this->_waitlistMsg
= ts("This event has only %1 space(s) left. If you continue and register more than %1 people (including yourself ), the whole group will be wait listed. Or, you can reduce the number of people you are registering to %1 to avoid being put on the waiting list.", [1 => $this->_availableRegistrations
]);
368 if ($this->_requireApproval
) {
369 $this->_requireApprovalMsg
= CRM_Utils_Array
::value('approval_req_text', $this->_values
['event'],
370 ts('Registration for this event requires approval. Once your registration(s) have been reviewed, you will receive an email with a link to a web page where you can complete the registration process.')
375 //case where only approval needed - no waitlist.
376 if ($this->_requireApproval
&&
377 !$this->_allowWaitlist
&& !$bypassPayment
379 $this->_requireApprovalMsg
= CRM_Utils_Array
::value('approval_req_text', $this->_values
['event'],
380 ts('Registration for this event requires 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.')
384 //lets display status to primary page only.
385 $this->assign('waitlistMsg', $this->_waitlistMsg
);
386 $this->assign('requireApprovalMsg', $this->_requireApprovalMsg
);
387 $this->assign('allowGroupOnWaitlist', $allowGroupOnWaitlist);
388 $this->assign('isAdditionalParticipants', $isAdditionalParticipants);
390 if ($this->_values
['event']['is_monetary']) {
391 self
::buildAmount($this);
394 if ($contactID === 0 && !$this->_values
['event']['is_multiple_registrations']) {
395 //@todo we are blocking for multiple registrations because we haven't tested
396 $this->addCIDZeroOptions();
399 if ($this->_values
['event']['is_monetary']) {
400 $this->addPaymentProcessorFieldsToForm();
403 $this->addElement('hidden', 'bypass_payment', NULL, ['id' => 'bypass_payment']);
405 $this->assign('bypassPayment', $bypassPayment);
408 $createCMSUser = FALSE;
410 if ($this->_values
['custom_pre_id']) {
411 $profileID = $this->_values
['custom_pre_id'];
412 $createCMSUser = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $profileID, 'is_cms_user');
415 if (!$createCMSUser &&
416 $this->_values
['custom_post_id']
418 if (!is_array($this->_values
['custom_post_id'])) {
419 $profileIDs = [$this->_values
['custom_post_id']];
422 $profileIDs = $this->_values
['custom_post_id'];
424 foreach ($profileIDs as $pid) {
425 if (CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $pid, 'is_cms_user')) {
427 $createCMSUser = TRUE;
433 if ($createCMSUser) {
434 CRM_Core_BAO_CMSUser
::buildForm($this, $profileID, TRUE);
438 //we have to load confirm contribution button in template
439 //when multiple payment processor as the user
440 //can toggle with payment processor selection
441 $billingModePaymentProcessors = 0;
442 if (!CRM_Utils_System
::isNull($this->_paymentProcessors
)) {
443 foreach ($this->_paymentProcessors
as $key => $values) {
444 if ($values['billing_mode'] == CRM_Core_Payment
::BILLING_MODE_BUTTON
) {
445 $billingModePaymentProcessors++
;
450 if ($billingModePaymentProcessors && count($this->_paymentProcessors
) == $billingModePaymentProcessors) {
451 $allAreBillingModeProcessors = TRUE;
454 $allAreBillingModeProcessors = FALSE;
457 if (!$allAreBillingModeProcessors ||
!empty($this->_values
['event']['is_pay_later']) ||
$bypassPayment
460 //freeze button to avoid multiple calls.
461 if (empty($this->_values
['event']['is_monetary'])) {
462 $this->submitOnce
= TRUE;
465 // CRM-11182 - Optional confirmation screen
466 // Change button label depending on whether the next action is confirm or register
469 'spacing' => ' ',
473 !$this->_values
['event']['is_multiple_registrations']
474 && !$this->_values
['event']['is_monetary']
475 && !$this->_values
['event']['is_confirm_enabled']
477 $buttonParams['name'] = ts('Register');
480 $buttonParams['name'] = ts('Review your registration');
481 $buttonParams['icon'] = 'fa-chevron-right';
484 $this->addButtons([$buttonParams]);
487 $this->addFormRule(['CRM_Event_Form_Registration_Register', 'formRule'], $this);
488 $this->unsavedChangesWarn
= TRUE;
492 CRM_PCP_BAO_PCP
::buildPcp($this->_pcpId
, $this);
497 * Build the radio/text form elements for the amount field
499 * @param CRM_Event_Form_Registration_Register $form
501 * @param bool $required
502 * True if you want to add formRule.
503 * @param int $discountId
504 * Discount id for the event.
506 * @throws \CRM_Core_Exception
508 public static function buildAmount(&$form, $required = TRUE, $discountId = NULL) {
509 // build amount only when needed, skip incase of event full and waitlisting is enabled
510 // and few other conditions check preProcess()
511 if (property_exists($form, '_noFees') && $form->_noFees
) {
515 //if payment done, no need to build the fee block.
516 if (!empty($form->_paymentId
)) {
517 //fix to display line item in update mode.
518 $form->assign('priceSet', $form->_priceSet ??
NULL);
522 $feeFields = $form->_values
['fee'] ??
NULL;
524 if (is_array($feeFields)) {
525 $form->_feeBlock
= &$form->_values
['fee'];
528 //check for discount.
529 $discountedFee = $form->_values
['discount'] ??
NULL;
530 if (is_array($discountedFee) && !empty($discountedFee)) {
532 $form->_discountId
= $discountId = CRM_Core_BAO_Discount
::findSet($form->_eventId
, 'civicrm_event');
535 $form->_feeBlock
= &$form->_values
['discount'][$discountId];
538 if (!is_array($form->_feeBlock
)) {
539 $form->_feeBlock
= [];
542 //its time to call the hook.
543 CRM_Utils_Hook
::buildAmount('event', $form, $form->_feeBlock
);
545 //reset required if participant is skipped.
546 $button = substr($form->controller
->getButtonName(), -4);
547 if ($required && $button == 'skip') {
551 $className = CRM_Utils_System
::getClassName($form);
553 //build the priceset fields.
554 if (isset($form->_priceSetId
) && $form->_priceSetId
) {
556 //format price set fields across option full.
557 self
::formatFieldsForOptionFull($form);
559 if (!empty($form->_priceSet
['is_quick_config'])) {
560 $form->_quickConfig
= $form->_priceSet
['is_quick_config'];
562 $form->add('hidden', 'priceSetId', $form->_priceSetId
);
564 // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
565 $adminFieldVisible = FALSE;
566 if (CRM_Core_Permission
::check('administer CiviCRM')) {
567 $adminFieldVisible = TRUE;
570 $hideAdminValues = TRUE;
571 if (CRM_Core_Permission
::check('edit event participants')) {
572 $hideAdminValues = FALSE;
575 foreach ($form->_feeBlock
as $field) {
576 // public AND admin visibility fields are included for back-office registration and back-office change selections
577 if (CRM_Utils_Array
::value('visibility', $field) == 'public' ||
578 (CRM_Utils_Array
::value('visibility', $field) == 'admin' && $adminFieldVisible == TRUE) ||
579 $className == 'CRM_Event_Form_Participant' ||
580 $className == 'CRM_Event_Form_ParticipantFeeSelection'
582 $fieldId = $field['id'];
583 $elementName = 'price_' . $fieldId;
585 $isRequire = $field['is_required'] ??
NULL;
586 if ($button == 'skip') {
590 //user might modified w/ hook.
591 $options = $field['options'] ??
NULL;
592 $formClasses = ['CRM_Event_Form_Participant', 'CRM_Event_Form_ParticipantFeeSelection'];
594 if (!is_array($options)) {
597 elseif ($hideAdminValues && !in_array($className, $formClasses)) {
598 $publicVisibilityID = CRM_Price_BAO_PriceField
::getVisibilityOptionID('public');
599 $adminVisibilityID = CRM_Price_BAO_PriceField
::getVisibilityOptionID('admin');
601 foreach ($options as $key => $currentOption) {
602 $optionVisibility = CRM_Utils_Array
::value('visibility_id', $currentOption, $publicVisibilityID);
603 if ($optionVisibility == $adminVisibilityID) {
604 unset($options[$key]);
609 $optionFullIds = CRM_Utils_Array
::value('option_full_ids', $field, []);
611 //soft suppress required rule when option is full.
612 if (!empty($optionFullIds) && (count($options) == count($optionFullIds))) {
615 if (!empty($options)) {
617 CRM_Price_BAO_PriceField
::addQuickFormElement($form,
629 $form->_priceSet
['id'] = $form->_priceSet
['id'] ??
$form->_priceSetId
;
630 $form->assign('priceSet', $form->_priceSet
);
633 $eventFeeBlockValues = [];
634 foreach ($form->_feeBlock
as $fee) {
635 if (is_array($fee)) {
638 $totalAmountJs = NULL;
639 if ($className == 'CRM_Event_Form_Participant') {
640 $totalAmountJs = ['onClick' => "fillTotalAmount(" . $fee['value'] . ")"];
643 $eventFeeBlockValues['amount_id_' . $fee['amount_id']] = $fee['value'];
644 $elements[] = &$form->createElement('radio', NULL, '',
645 CRM_Utils_Money
::format($fee['value']) . ' ' .
652 $form->assign('eventFeeBlockValues', json_encode($eventFeeBlockValues));
654 $form->_defaults
['amount'] = $form->_values
['event']['default_fee_id'] ??
NULL;
655 $element = &$form->addGroup($elements, 'amount', ts('Event Fee(s)'), '<br />');
656 if (isset($form->_online
) && $form->_online
) {
660 $form->addRule('amount', ts('Fee Level is a required field.'), 'required');
666 * @param CRM_Event_Form_Registration $form
668 public static function formatFieldsForOptionFull(&$form) {
669 $priceSet = $form->get('priceSet');
670 $priceSetId = $form->get('priceSetId');
671 $defaultPricefieldIds = [];
672 if (!empty($form->_values
['line_items'])) {
673 foreach ($form->_values
['line_items'] as $lineItem) {
674 $defaultPricefieldIds[] = $lineItem['price_field_value_id'];
678 !is_array($priceSet) ||
679 empty($priceSet) ||
empty($priceSet['optionsMaxValueTotal'])
684 $skipParticipants = $formattedPriceSetDefaults = [];
685 if (!empty($form->_allowConfirmation
) && (isset($form->_pId
) ||
isset($form->_additionalParticipantId
))) {
686 $participantId = $form->_pId ??
$form->_additionalParticipantId
;
687 $pricesetDefaults = CRM_Event_Form_EventFees
::setDefaultPriceSet($participantId,
690 // modify options full to respect the selected fields
691 // options on confirmation.
692 $formattedPriceSetDefaults = self
::formatPriceSetParams($form, $pricesetDefaults);
694 // to skip current registered participants fields option count on confirmation.
695 $skipParticipants[] = $form->_participantId
;
696 if (!empty($form->_additionalParticipantIds
)) {
697 $skipParticipants = array_merge($skipParticipants, $form->_additionalParticipantIds
);
701 $className = CRM_Utils_System
::getClassName($form);
703 //get the current price event price set options count.
704 $currentOptionsCount = self
::getPriceSetOptionCount($form);
705 $recordedOptionsCount = CRM_Event_BAO_Participant
::priceSetOptionsCount($form->_eventId
, $skipParticipants);
706 $optionFullTotalAmount = 0;
707 $currentParticipantNo = (int) substr($form->_name
, 12);
708 foreach ($form->_feeBlock
as & $field) {
710 $fieldId = $field['id'];
711 if (!is_array($field['options'])) {
714 foreach ($field['options'] as & $option) {
715 $optId = $option['id'];
716 $count = CRM_Utils_Array
::value('count', $option, 0);
717 $maxValue = CRM_Utils_Array
::value('max_value', $option, 0);
718 $dbTotalCount = CRM_Utils_Array
::value($optId, $recordedOptionsCount, 0);
719 $currentTotalCount = CRM_Utils_Array
::value($optId, $currentOptionsCount, 0);
721 $totalCount = $currentTotalCount +
$dbTotalCount;
724 (($totalCount >= $maxValue) &&
725 (empty($form->_lineItem
[$currentParticipantNo][$optId]['price_field_id']) ||
$dbTotalCount >= $maxValue))
728 $optionFullIds[$optId] = $optId;
729 if ($field['html_type'] != 'Select') {
730 if (in_array($optId, $defaultPricefieldIds)) {
731 $optionFullTotalAmount +
= CRM_Utils_Array
::value('amount', $option);
735 if (!empty($defaultPricefieldIds) && in_array($optId, $defaultPricefieldIds)) {
736 unset($optionFullIds[$optId]);
740 //here option is not full,
741 //but we don't want to allow participant to increase
742 //seats at the time of re-walking registration.
744 !empty($form->_allowConfirmation
) &&
745 !empty($formattedPriceSetDefaults)
747 if (empty($formattedPriceSetDefaults["price_{$field}"]) ||
empty($formattedPriceSetDefaults["price_{$fieldId}"][$optId])) {
748 $optionFullIds[$optId] = $optId;
752 $option['is_full'] = $isFull;
753 $option['db_total_count'] = $dbTotalCount;
754 $option['total_option_count'] = $dbTotalCount +
$currentTotalCount;
757 //ignore option full for offline registration.
758 if ($className == 'CRM_Event_Form_Participant') {
762 //finally get option ids in.
763 $field['option_full_ids'] = $optionFullIds;
765 $form->assign('optionFullTotalAmount', $optionFullTotalAmount);
771 * @param array $fields
772 * The input form values.
773 * @param array $files
774 * The uploaded files if any.
775 * @param \CRM_Event_Form_Registration_Register $form
778 * true if no errors, else array of errors
780 * @throws \CRM_Core_Exception
782 public static function formRule($fields, $files, $form) {
784 //check that either an email or firstname+lastname is included in the form(CRM-9587)
785 self
::checkProfileComplete($fields, $errors, $form->_eventId
);
786 //To check if the user is already registered for the event(CRM-2426)
787 if (!$form->_skipDupeRegistrationCheck
) {
788 self
::checkRegistration($fields, $form);
790 //check for availability of registrations.
791 if (!$form->_allowConfirmation
&& empty($fields['bypass_payment']) &&
792 is_numeric($form->_availableRegistrations
) &&
793 CRM_Utils_Array
::value('additional_participants', $fields) >= $form->_availableRegistrations
795 $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", [1 => $form->_availableRegistrations
]);
798 $numberAdditionalParticipants = $fields['additional_participants'] ??
0;
800 if ($numberAdditionalParticipants && !CRM_Utils_Rule
::positiveInteger($fields['additional_participants'])) {
801 $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
804 // during confirmation don't allow to increase additional participants, CRM-4320
805 if ($form->_allowConfirmation
&& $numberAdditionalParticipants &&
806 is_array($form->_additionalParticipantIds
) &&
807 $numberAdditionalParticipants > count($form->_additionalParticipantIds
)
809 $errors['additional_participants'] = ts("Oops. It looks like you are trying to increase the number of additional people you are registering for. You can confirm registration for a maximum of %1 additional people.", [1 => count($form->_additionalParticipantIds
)]);
812 //don't allow to register w/ waiting if enough spaces available.
813 if (!empty($fields['bypass_payment']) && $form->_allowConfirmation
) {
814 if (!is_numeric($form->_availableRegistrations
) ||
815 (empty($fields['priceSetId']) && CRM_Utils_Array
::value('additional_participants', $fields) < $form->_availableRegistrations
)
817 $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list.");
821 // priceset validations
822 if (!empty($fields['priceSetId']) &&
823 !$form->_requireApproval
&& !$form->_allowWaitlist
826 $formatted = self
::formatPriceSetParams($form, $fields);
827 $ppParams = [$formatted];
828 $priceSetErrors = self
::validatePriceSet($form, $ppParams);
829 $primaryParticipantCount = self
::getParticipantCount($form, $ppParams);
831 //get price set fields errors in.
832 $errors = array_merge($errors, CRM_Utils_Array
::value(0, $priceSetErrors, []));
834 $totalParticipants = $primaryParticipantCount;
835 if ($numberAdditionalParticipants) {
836 $totalParticipants +
= $numberAdditionalParticipants;
839 if (empty($fields['bypass_payment']) &&
840 !$form->_allowConfirmation
&&
841 is_numeric($form->_availableRegistrations
) &&
842 $form->_availableRegistrations
< $totalParticipants
844 $errors['_qf_default'] = ts("Only %1 Registrations available.", [1 => $form->_availableRegistrations
]);
848 CRM_Price_BAO_PriceSet
::processAmount($form->_values
['fee'], $fields, $lineItem);
850 $minAmt = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceSet', $fields['priceSetId'], 'min_amount');
851 if ($fields['amount'] < 0) {
852 $errors['_qf_default'] = ts('Event Fee(s) can not be less than zero. Please select the options accordingly');
854 elseif (!empty($minAmt) && $fields['amount'] < $minAmt) {
855 $errors['_qf_default'] = ts('A minimum amount of %1 should be selected from Event Fee(s).', [
856 1 => CRM_Utils_Money
::format($minAmt),
860 foreach (CRM_Contact_BAO_Contact
::$_greetingTypes as $greeting) {
861 if ($greetingType = CRM_Utils_Array
::value($greeting, $fields)) {
862 $customizedValue = CRM_Core_PseudoConstant
::getKey('CRM_Contact_BAO_Contact', $greeting . '_id', 'Customized');
863 if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) {
864 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
865 [1 => ucwords(str_replace('_', ' ', $greeting))]
871 // @todo - can we remove the 'is_monetary' concept?
872 if ($form->_values
['event']['is_monetary']) {
873 if (empty($form->_requireApproval
) && !empty($fields['amount']) && $fields['amount'] > 0 &&
874 !isset($fields['payment_processor_id'])) {
875 $errors['payment_processor_id'] = ts('Please select a Payment Method');
878 if (self
::isZeroAmount($fields, $form)) {
879 return empty($errors) ?
TRUE : $errors;
882 // also return if zero fees for valid members
883 if (!empty($fields['bypass_payment']) ||
884 (!$form->_allowConfirmation
&& ($form->_requireApproval ||
$form->_allowWaitlist
))
886 return empty($errors) ?
TRUE : $errors;
888 CRM_Core_Payment_Form
::validatePaymentInstrument(
889 $fields['payment_processor_id'],
892 (!$form->_isBillingAddressRequiredForPayLater ?
NULL : 'billing')
896 return empty($errors) ?
TRUE : $errors;
900 * Check if profiles are complete when event registration occurs(CRM-9587).
902 * @param array $fields
903 * @param array $errors
904 * @param int $eventId
906 public static function checkProfileComplete($fields, &$errors, $eventId) {
908 foreach ($fields as $fieldname => $fieldvalue) {
909 if (substr($fieldname, 0, 6) == 'email-' && $fieldvalue) {
910 $email = $fieldvalue;
914 if (!$email && !(!empty($fields['first_name']) && !empty($fields['last_name']))) {
915 $defaults = $params = ['id' => $eventId];
916 CRM_Event_BAO_Event
::retrieve($params, $defaults);
917 $message = ts("Mandatory fields (first name and last name, OR email address) are missing from this form.");
918 $errors['_qf_default'] = $message;
923 * Process the form submission.
925 public function postProcess() {
926 // get the submitted form values.
927 $params = $this->controller
->exportValues($this->_name
);
929 //set as Primary participant
930 $params['is_primary'] = 1;
932 if ($this->_values
['event']['is_pay_later']
933 && (!array_key_exists('hidden_processor', $params) ||
$params['payment_processor_id'] == 0)
935 $params['is_pay_later'] = 1;
938 $params['is_pay_later'] = 0;
941 $this->set('is_pay_later', $params['is_pay_later']);
943 // assign pay later stuff
944 $this->_params
['is_pay_later'] = CRM_Utils_Array
::value('is_pay_later', $params, FALSE);
945 $this->assign('is_pay_later', $params['is_pay_later']);
946 if ($params['is_pay_later']) {
947 $this->assign('pay_later_text', $this->_values
['event']['pay_later_text']);
948 $this->assign('pay_later_receipt', $this->_values
['event']['pay_later_receipt']);
951 if (!$this->_allowConfirmation
) {
952 // check if the participant is already registered
953 if (!$this->_skipDupeRegistrationCheck
) {
954 $params['contact_id'] = self
::getRegistrationContactID($params, $this, FALSE);
958 if (!empty($params['image_URL'])) {
959 CRM_Contact_BAO_Contact
::processImageParams($params);
962 //carry campaign to partcipants.
963 if (array_key_exists('participant_campaign_id', $params)) {
964 $params['campaign_id'] = $params['participant_campaign_id'];
967 $params['campaign_id'] = $this->_values
['event']['campaign_id'] ??
NULL;
970 //hack to allow group to register w/ waiting
971 $primaryParticipantCount = self
::getParticipantCount($this, $params);
973 $totalParticipants = $primaryParticipantCount;
974 if (!empty($params['additional_participants'])) {
975 $totalParticipants +
= $params['additional_participants'];
977 if (!$this->_allowConfirmation
&& !empty($params['bypass_payment']) &&
978 is_numeric($this->_availableRegistrations
) &&
979 $totalParticipants > $this->_availableRegistrations
981 $this->_allowWaitlist
= TRUE;
982 $this->set('allowWaitlist', TRUE);
985 //carry participant id if pre-registered.
986 if ($this->_allowConfirmation
&& $this->_participantId
) {
987 $params['participant_id'] = $this->_participantId
;
990 $params['defaultRole'] = 1;
991 if (array_key_exists('participant_role', $params)) {
992 $params['participant_role_id'] = $params['participant_role'];
995 if (array_key_exists('participant_role_id', $params)) {
996 $params['defaultRole'] = 0;
998 if (empty($params['participant_role_id']) &&
999 $this->_values
['event']['default_role_id']
1001 $params['participant_role_id'] = $this->_values
['event']['default_role_id'];
1004 $config = CRM_Core_Config
::singleton();
1005 $params['currencyID'] = $config->defaultCurrency
;
1007 if ($this->_values
['event']['is_monetary']) {
1008 // we first reset the confirm page so it accepts new values
1009 $this->controller
->resetPage('Confirm');
1011 //added for discount
1012 $discountId = CRM_Core_BAO_Discount
::findSet($this->_eventId
, 'civicrm_event');
1013 $params['amount_level'] = $this->getAmountLevel($params, $discountId);
1014 if (!empty($this->_values
['discount'][$discountId])) {
1015 $params['discount_id'] = $discountId;
1016 $params['amount'] = $this->_values
['discount'][$discountId][$params['amount']]['value'];
1018 elseif (empty($params['priceSetId'])) {
1019 if (!empty($params['amount'])) {
1020 $params['amount'] = $this->_values
['fee'][$params['amount']]['value'];
1023 $params['amount'] = '';
1028 CRM_Price_BAO_PriceSet
::processAmount($this->_values
['fee'], $params, $lineItem);
1029 if ($params['tax_amount']) {
1030 $this->set('tax_amount', $params['tax_amount']);
1032 $submittedLineItems = $this->get('lineItem');
1033 if (!empty($submittedLineItems) && is_array($submittedLineItems)) {
1034 $submittedLineItems[0] = $lineItem;
1037 $submittedLineItems = [$lineItem];
1039 $submittedLineItems = array_filter($submittedLineItems);
1040 $this->set('lineItem', $submittedLineItems);
1041 $this->set('lineItemParticipantsCount', [$primaryParticipantCount]);
1044 $this->set('amount', $params['amount']);
1045 $this->set('amount_level', $params['amount_level']);
1047 // generate and set an invoiceID for this transaction
1048 $invoiceID = md5(uniqid(rand(), TRUE));
1049 $this->set('invoiceID', $invoiceID);
1051 if ($this->_paymentProcessor
) {
1052 $payment = $this->_paymentProcessor
['object'];
1053 $payment->setBaseReturnUrl('civicrm/event/register');
1056 // ContributeMode is a deprecated concept. It is short-hand for a bunch of
1057 // assumptions we are working to remove.
1058 $this->set('contributeMode', 'direct');
1060 if ($this->_values
['event']['is_monetary']) {
1061 $params['currencyID'] = $config->defaultCurrency
;
1062 $params['invoiceID'] = $invoiceID;
1064 $this->_params
= $this->get('params');
1065 // Set the button so we know what
1066 $params['button'] = $this->controller
->getButtonName();
1067 if (!empty($this->_params
) && is_array($this->_params
)) {
1068 $this->_params
[0] = $params;
1071 $this->_params
= [];
1072 $this->_params
[] = $params;
1074 $this->set('params', $this->_params
);
1075 if ($this->_paymentProcessor
&&
1076 // Actually we don't really need to check if it supports pre-approval - we could just call
1077 // it regardless as the function we call re-acts tot the rests of the preApproval call.
1078 $this->_paymentProcessor
['object']->supports('preApproval')
1079 && !$this->_allowWaitlist
&&
1080 !$this->_requireApproval
1083 // The concept of contributeMode is deprecated - but still needs removal from the message templates.
1084 $this->set('contributeMode', 'express');
1086 // Send Event Name & Id in Params
1087 $params['eventName'] = $this->_values
['event']['title'];
1088 $params['eventId'] = $this->_values
['event']['id'];
1090 $params['cancelURL'] = CRM_Utils_System
::url('civicrm/event/register',
1091 "_qf_Register_display=1&qfKey={$this->controller->_key}",
1094 if (!empty($params['additional_participants'])) {
1095 $urlArgs = "_qf_Participant_1_display=1&rfp=1&qfKey={$this->controller->_key}";
1098 $urlArgs = "_qf_Confirm_display=1&rfp=1&qfKey={$this->controller->_key}";
1100 $params['returnURL'] = CRM_Utils_System
::url('civicrm/event/register',
1104 $params['invoiceID'] = $invoiceID;
1106 $params['component'] = 'event';
1107 // This code is duplicated multiple places and should be consolidated.
1108 $params = $this->prepareParamsForPaymentProcessor($params);
1109 $this->handlePreApproval($params);
1111 elseif ($this->_paymentProcessor
&&
1112 (int) $this->_paymentProcessor
['billing_mode'] & CRM_Core_Payment
::BILLING_MODE_NOTIFY
1114 // The concept of contributeMode is deprecated - but still needs removal from the message templates.
1115 $this->set('contributeMode', 'notify');
1119 $params['description'] = ts('Online Event Registration') . ' ' . $this->_values
['event']['title'];
1121 $this->_params
= [];
1122 $this->_params
[] = $params;
1123 $this->set('params', $this->_params
);
1126 empty($params['additional_participants'])
1127 // CRM-11182 - Optional confirmation screen
1128 && !$this->_values
['event']['is_confirm_enabled']
1130 $this->processRegistration($this->_params
);
1134 // If registering > 1 participant, give status message
1135 if (!empty($params['additional_participants'])) {
1136 $statusMsg = ts('Registration information for participant 1 has been saved.');
1137 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');
1142 * Method to check if the user is already registered for the event.
1143 * and if result found redirect to the event info page
1145 * @param array $fields
1146 * The input form values(anonymous user).
1147 * @param CRM_Event_Form_Registration_Register $form
1149 * @param bool $isAdditional
1150 * Treat isAdditional participants a bit differently.
1154 public static function checkRegistration($fields, $form, $isAdditional = FALSE) {
1155 // CRM-3907, skip check for preview registrations
1156 // CRM-4320 participant need to walk wizard
1158 ($form->getPaymentMode() === 'test' ||
$form->_allowConfirmation
)
1163 $contactID = self
::getRegistrationContactID($fields, $form, $isAdditional);
1166 $participant = new CRM_Event_BAO_Participant();
1167 $participant->contact_id
= $contactID;
1168 $participant->event_id
= $form->_values
['event']['id'];
1169 if (!empty($fields['participant_role']) && is_numeric($fields['participant_role'])) {
1170 $participant->role_id
= $fields['participant_role'];
1173 $participant->role_id
= $form->_values
['event']['default_role_id'];
1175 $participant->is_test
= 0;
1176 $participant->find();
1177 // Event#30 - Anyone whose status type has `is_counted` OR is on the waitlist should be considered as registered.
1178 $statusTypes = CRM_Event_PseudoConstant
::participantStatus(NULL, 'is_counted = 1') + CRM_Event_PseudoConstant
::participantStatus(NULL, "name = 'On waitlist'");
1179 while ($participant->fetch()) {
1180 if (array_key_exists($participant->status_id
, $statusTypes)) {
1181 if (!$isAdditional && !$form->_values
['event']['allow_same_participant_emails']) {
1182 $registerUrl = CRM_Utils_System
::url('civicrm/event/register',
1183 "reset=1&id={$form->_values['event']['id']}&cid=0"
1185 if ($form->_pcpId
) {
1186 $registerUrl .= '&pcpId=' . $form->_pcpId
;
1188 $registrationType = (CRM_Event_PseudoConstant
::getKey('CRM_Event_BAO_Participant', 'participant_status_id', 'On waitlist') == $participant->status_id
) ?
'waitlisted' : 'registered';
1189 $status = ts("It looks like you are already %1 for this event. If you want to change your registration, or you feel that you've received this message in error, please contact the site administrator.", [1 => $registrationType]);
1190 $status .= ' ' . ts('You can also <a href="%1">register another participant</a>.', [1 => $registerUrl]);
1191 CRM_Core_Session
::singleton()->setStatus($status, ts('Oops.'), 'alert');
1192 $url = CRM_Utils_System
::url('civicrm/event/info',
1193 "reset=1&id={$form->_values['event']['id']}&noFullMsg=true"
1195 if ($form->_action
& CRM_Core_Action
::PREVIEW
) {
1196 $url .= '&action=preview';
1199 if ($form->_pcpId
) {
1200 $url .= '&pcpId=' . $form->_pcpId
;
1203 CRM_Utils_System
::redirect($url);
1206 if ($isAdditional) {
1207 $status = ts("It looks like this participant is already registered for this event. If you want to change your registration, or you feel that you've received this message in error, please contact the site administrator.");
1208 CRM_Core_Session
::singleton()->setStatus($status, ts('Oops.'), 'alert');
1209 return $participant->id
;