Merge pull request #5057 from cividesk/CRM-15901-4.5
[civicrm-core.git] / CRM / Event / Form / Registration / Register.php
index bc26ae04e3fdf7122b219ccb4ade221036a9930f..c29dbf1d8bd820640f3138ffc5cb31f5f2ecfd92 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.4                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -29,7 +29,7 @@
  *
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id$
  *
  */
@@ -101,9 +101,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     $this->_allowWaitlist = FALSE;
-    if ($eventFull && !$this->_allowConfirmation &&
-      CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
-    ) {
+    if ($eventFull && !$this->_allowConfirmation && !empty($this->_values['event']['has_waitlist'])) {
       $this->_allowWaitlist = TRUE;
       $this->_waitlistMsg = CRM_Utils_Array::value('waitlist_text', $this->_values['event']);
       if (!$this->_waitlistMsg) {
@@ -138,7 +136,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   function setDefaultValues() {
     if ($this->_ppType && $this->_snippet && !($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM)) {
@@ -156,18 +154,16 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     // country if the person has an address but no country (for anonymous country is set above)
     // this could have implications if the billing profile is filled but hidden.
     // this behaviour has been in place for a while but the use of js to hide things has increased
-    if (!CRM_Utils_Array::value("billing_country_id-{$this->_bltID}", $this->_defaults)) {
+    if (empty($this->_defaults["billing_country_id-{$this->_bltID}"])) {
       $this->_defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
     }
 
     // set default state/province from config if no state/province set
-    if (!CRM_Utils_Array::value("billing_state_province_id-{$this->_bltID}", $this->_defaults)) {
+    if (empty($this->_defaults["billing_state_province_id-{$this->_bltID}"])) {
       $this->_defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
     }
 
     if ($this->_snippet) {
-      // now fix all state country selectors
-      CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults);
       return $this->_defaults;
     }
 
@@ -199,13 +195,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
     }
 
-    // now fix all state country selectors
-    CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults);
-
     // Set default payment processor as default payment_processor radio button value
     if (!empty($this->_paymentProcessors)) {
       foreach ($this->_paymentProcessors as $pid => $value) {
-        if (CRM_Utils_Array::value('is_default', $value)) {
+        if (!empty($value['is_default'])) {
           $this->_defaults['payment_processor'] = $pid;
         }
       }
@@ -214,7 +207,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     //if event is monetary and pay later is enabled and payment
     //processor is not available then freeze the pay later checkbox with
     //default check
-    if (CRM_Utils_Array::value('is_pay_later', $this->_values['event']) &&
+    if (!empty($this->_values['event']['is_pay_later']) &&
       !is_array($this->_paymentProcessor)
     ) {
       $this->_defaults['is_pay_later'] = 1;
@@ -268,9 +261,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     if ($this->_priceSetId && !empty($this->_feeBlock)) {
       foreach ($this->_feeBlock as $key => $val) {
         foreach ($val['options'] as $keys => $values) {
-          if ($values['is_default'] &&
-            !CRM_Utils_Array::value('is_full', $values)
-          ) {
+          if ($values['is_default'] && empty($values['is_full'])) {
 
             if ($val['html_type'] == 'CheckBox') {
               $this->_defaults["price_{$key}"][$keys] = 1;
@@ -317,7 +308,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   /**
    * Function to build the form
    *
-   * @return None
+   * @return void
    * @access public
    */
   public function buildQuickForm() {
@@ -329,7 +320,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     if (!empty($this->_fields) && !empty($this->_values['custom_pre_id'])) {
       $profileAddressFields = array();
       foreach ($this->_fields as $key => $value) {
-        CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, array('uf_group_id' => $this->_values['custom_pre_id']));      }
+        CRM_Core_BAO_UFField::assignAddressField($key, $profileAddressFields, array(
+          'uf_group_id' => $this->_values['custom_pre_id']
+        ));
+      }
       $this->set('profileAddressFields', $profileAddressFields);
     }
 
@@ -370,13 +364,11 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     //hack to allow group to register w/ waiting
-    if ((CRM_Utils_Array::value('is_multiple_registrations', $this->_values['event']) ||
+    if ((!empty($this->_values['event']['is_multiple_registrations']) ||
         $this->_priceSetId
       ) &&
       !$this->_allowConfirmation &&
-      is_numeric($this->_availableRegistrations)
-      && CRM_Utils_Array::value('has_waitlist', $this->_values['event'])
-    ) {
+      is_numeric($this->_availableRegistrations) && !empty($this->_values['event']['has_waitlist'])) {
       $bypassPayment = TRUE;
       //case might be group become as a part of waitlist.
       //If not waitlist then they require admin approve.
@@ -428,7 +420,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       //@todo we are blocking for multiple registrations because we haven't tested
       $this->addCidZeroOptions($onlinePaymentProcessorEnabled);
     }
-    if (CRM_Utils_Array::value('is_pay_later', $this->_values['event']) &&
+    if (!empty($this->_values['event']['is_pay_later']) &&
       ($this->_allowConfirmation || (!$this->_requireApproval && !$this->_allowWaitlist))
     ) {
       $pps[0] = $this->_values['event']['pay_later_text'];
@@ -505,20 +497,32 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       $allAreBillingModeProcessors = FALSE;
     }
 
-    if (!$allAreBillingModeProcessors ||
-      CRM_Utils_Array::value('is_pay_later', $this->_values['event']) || $bypassPayment
+    if (!$allAreBillingModeProcessors || !empty($this->_values['event']['is_pay_later']) || $bypassPayment
     ) {
 
       //freeze button to avoid multiple calls.
       $js = NULL;
 
-      if (!CRM_Utils_Array::value('is_monetary', $this->_values['event'])) {
+      if (empty($this->_values['event']['is_monetary'])) {
         $js = array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');");
       }
+
+      // CRM-11182 - Optional confirmation screen
+      // Change button label depending on whether the next action is confirm or register
+      if (
+        !$this->_values['event']['is_multiple_registrations']
+        && !$this->_values['event']['is_monetary']
+        && !$this->_values['event']['is_confirm_enabled']
+      ) {
+        $buttonLabel = ts('Register >>');
+      } else {
+        $buttonLabel = ts('Continue >>');
+      }
+
       $this->addButtons(array(
           array(
             'type' => 'upload',
-            'name' => ts('Continue >>'),
+            'name' => $buttonLabel,
             'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
             'isDefault' => TRUE,
             'js' => $js,
@@ -547,6 +551,12 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    * @static
    */
   static public function buildAmount(&$form, $required = TRUE, $discountId = NULL) {
+    // build amount only when needed, skip incase of event full and waitlisting is enabled
+    // and few other conditions check preProcess()
+    if (property_exists($form, '_noFees') && $form->_noFees) {
+      return;
+    }
+
     //if payment done, no need to build the fee block.
     if (!empty($form->_paymentId)) {
       //fix to display line item in update mode.
@@ -591,14 +601,23 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       //format price set fields across option full.
       self::formatFieldsForOptionFull($form);
 
-      if (CRM_Utils_Array::value('is_quick_config', $form->_priceSet)) {
+      if (!empty($form->_priceSet['is_quick_config'])) {
         $form->_quickConfig = $form->_priceSet['is_quick_config'];
       }
       $form->add('hidden', 'priceSetId', $form->_priceSetId);
 
+      // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions
+      $adminFieldVisible = false;
+      if (CRM_Core_Permission::check('administer CiviCRM')) {
+        $adminFieldVisible = true;
+      }
+
       foreach ($form->_feeBlock as $field) {
+        // public AND admin visibility fields are included for back-office registration and back-office change selections
         if (CRM_Utils_Array::value('visibility', $field) == 'public' ||
-          $className == 'CRM_Event_Form_Participant'
+          (CRM_Utils_Array::value('visibility', $field) == 'admin' && $adminFieldVisible == true) ||
+          $className == 'CRM_Event_Form_Participant' ||
+          $className == 'CRM_Event_Form_ParticipantFeeSelection'
         ) {
           $fieldId = $field['id'];
           $elementName = 'price_' . $fieldId;
@@ -668,26 +687,33 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
   }
 
+  /**
+   * @param $form
+   */
   public static function formatFieldsForOptionFull(&$form) {
     $priceSet = $form->get('priceSet');
     $priceSetId = $form->get('priceSetId');
+    $defaultPricefieldIds = array();
+    if (!empty($form->_values['line_items'])) {
+      foreach ($form->_values['line_items'] as $lineItem) {
+        $defaultPricefieldIds[] = $lineItem['price_field_value_id'];
+      }
+    }
     if (!$priceSetId ||
       !is_array($priceSet) ||
-      empty($priceSet) ||
-      !CRM_Utils_Array::value('optionsMaxValueTotal', $priceSet)
-    ) {
+      empty($priceSet) || empty($priceSet['optionsMaxValueTotal'])) {
       return;
     }
 
     $skipParticipants = $formattedPriceSetDefaults = array();
-    if ($form->_allowConfirmation && (isset($form->_pId) || isset($form->_additionalParticipantId))) {
+    if (!empty($form->_allowConfirmation) && (isset($form->_pId) || isset($form->_additionalParticipantId))) {
       $participantId = isset($form->_pId) ? $form->_pId : $form->_additionalParticipantId;
       $pricesetDefaults = CRM_Event_Form_EventFees::setDefaultPriceSet($participantId,
         $form->_eventId
       );
       // modify options full to respect the selected fields
       // options on confirmation.
-      $formattedPriceSetDefaults = self::formatPriceSetParams($form, $pricesetDefaultOptions);
+      $formattedPriceSetDefaults = self::formatPriceSetParams($form, $pricesetDefaults);
 
       // to skip current registered participants fields option count on confirmation.
       $skipParticipants[] = $form->_participantId;
@@ -701,6 +727,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     //get the current price event price set options count.
     $currentOptionsCount = self::getPriceSetOptionCount($form);
     $recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, $skipParticipants);
+    $optionFullTotalAmount = 0;
 
     foreach ($form->_feeBlock as & $field) {
       $optionFullIds = array();
@@ -730,18 +757,25 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         ) {
           $isFull = TRUE;
           $optionFullIds[$optId] = $optId;
+          if ($field['html_type'] != 'Select') {
+            if (in_array($optId, $defaultPricefieldIds)) {
+              $optionFullTotalAmount += CRM_Utils_Array::value('amount', $option);
+            }
+          }
+          else {
+            if (!empty($defaultPricefieldIds) && in_array($optId, $defaultPricefieldIds)) {
+              unset($optionFullIds[$optId]);
+            }
+          }
         }
-
         //here option is not full,
         //but we don't want to allow participant to increase
         //seats at the time of re-walking registration.
         if ($count &&
-          $form->_allowConfirmation &&
+          !empty($form->_allowConfirmation) &&
           !empty($formattedPriceSetDefaults)
         ) {
-          if (!CRM_Utils_Array::value("price_{$field}", $formattedPriceSetDefaults) ||
-            !CRM_Utils_Array::value($opId, $formattedPriceSetDefaults["price_{$fieldId}"])
-          ) {
+          if (empty($formattedPriceSetDefaults["price_{$field}"]) || empty($formattedPriceSetDefaults["price_{$fieldId}"][$optId])) {
             $optionFullIds[$optId] = $optId;
             $isFull = TRUE;
           }
@@ -759,14 +793,17 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       //finally get option ids in.
       $field['option_full_ids'] = $optionFullIds;
     }
+    $form->assign('optionFullTotalAmount', $optionFullTotalAmount);
   }
 
   /**
    * global form rule
    *
-   * @param array $fields  the input form values
-   * @param array $files   the uploaded files if any
-   * @param array $options additional user data
+   * @param array $fields the input form values
+   * @param array $files the uploaded files if any
+   * @param $self
+   *
+   * @internal param array $options additional user data
    *
    * @return true if no errors, else array of errors
    * @access public
@@ -781,8 +818,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       self::checkRegistration($fields, $self);
     }
     //check for availability of registrations.
-    if (!$self->_allowConfirmation &&
-      !CRM_Utils_Array::value('bypass_payment', $fields) &&
+    if (!$self->_allowConfirmation && empty($fields['bypass_payment']) &&
       is_numeric($self->_availableRegistrations) &&
       CRM_Utils_Array::value('additional_participants', $fields) >= $self->_availableRegistrations
     ) {
@@ -790,8 +826,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     // during confirmation don't allow to increase additional participants, CRM-4320
-    if ($self->_allowConfirmation &&
-      CRM_Utils_Array::value('additional_participants', $fields) &&
+    if ($self->_allowConfirmation && !empty($fields['additional_participants']) &&
       is_array($self->_additionalParticipantIds) &&
       $fields['additional_participants'] > count($self->_additionalParticipantIds)
     ) {
@@ -799,22 +834,22 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     //don't allow to register w/ waiting if enough spaces available.
-    if (CRM_Utils_Array::value('bypass_payment', $fields)) {
+    if (!empty($fields['bypass_payment'])) {
       if (!is_numeric($self->_availableRegistrations) ||
-        (!CRM_Utils_Array::value('priceSetId', $fields) && CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations)
+        (empty($fields['priceSetId']) && CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations)
       ) {
         $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list.");
       }
     }
 
-    if (CRM_Utils_Array::value('additional_participants', $fields) &&
+    if (!empty($fields['additional_participants']) &&
       !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])
     ) {
       $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
     }
 
     // priceset validations
-    if (CRM_Utils_Array::value('priceSetId', $fields)) {
+    if (!empty($fields['priceSetId'])) {
       //format params.
       $formatted = self::formatPriceSetParams($self, $fields);
       $ppParams = array($formatted);
@@ -825,11 +860,11 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, array()));
 
       $totalParticipants = $primaryParticipantCount;
-      if (CRM_Utils_Array::value('additional_participants', $fields)) {
+      if (!empty($fields['additional_participants'])) {
         $totalParticipants += $fields['additional_participants'];
       }
 
-      if (!CRM_Utils_Array::value('bypass_payment', $fields) &&
+      if (empty($fields['bypass_payment']) &&
         !$self->_allowConfirmation &&
         is_numeric($self->_availableRegistrations) &&
         $self->_availableRegistrations < $totalParticipants
@@ -849,7 +884,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         $errors['payment_processor'] = ts('Please select a Payment Method');
       }
       if (is_array($self->_paymentProcessor)) {
-        $payment = CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this);
+        $payment = CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor);
         $error = $payment->checkConfig($self->_mode);
         if ($error) {
           $errors['_qf_default'] = $error;
@@ -859,8 +894,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       if ($self->_paymentProcessor &&
         $self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON
       ) {
-        if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) ||
-          CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) ||
+        if (!empty($fields[$self->_expressButtonName . '_x']) || !empty($fields[$self->_expressButtonName . '_y']) ||
           CRM_Utils_Array::value($self->_expressButtonName, $fields)
         ) {
           return empty($errors) ? TRUE : $errors;
@@ -868,19 +902,19 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
 
       $isZeroAmount = $skipPaymentValidation = FALSE;
-      if (CRM_Utils_Array::value('priceSetId', $fields)) {
+      if (!empty($fields['priceSetId'])) {
         if (CRM_Utils_Array::value('amount', $fields) == 0) {
           $isZeroAmount = TRUE;
         }
       }
-      elseif (CRM_Utils_Array::value('amount', $fields) &&
+      elseif (!empty($fields['amount']) &&
         (isset($self->_values['discount'][$fields['amount']])
           && CRM_Utils_Array::value('value', $self->_values['discount'][$fields['amount']]) == 0
         )
       ) {
         $isZeroAmount = TRUE;
       }
-      elseif (CRM_Utils_Array::value('amount', $fields) &&
+      elseif (!empty($fields['amount']) &&
         (isset($self->_values['fee'][$fields['amount']])
           && CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) == 0
         )
@@ -888,13 +922,12 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         $isZeroAmount = TRUE;
       }
 
-      if ($isZeroAmount && !($self->_forcePayement && CRM_Utils_Array::value('additional_participants', $fields))) {
+      if ($isZeroAmount && !($self->_forcePayement && !empty($fields['additional_participants']))) {
         $skipPaymentValidation = TRUE;
       }
 
       // also return if paylater mode or zero fees for valid members
-      if (CRM_Utils_Array::value('is_pay_later', $fields) ||
-        CRM_Utils_Array::value('bypass_payment', $fields) ||
+      if (!empty($fields['is_pay_later']) || !empty($fields['bypass_payment']) ||
         $skipPaymentValidation ||
         (!$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist))
       ) {
@@ -910,7 +943,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
         $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
         if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) {
-          $errors[$customizedGreeting] = ts('Custom %1 is a required field if %1 is of type Customized.',
+          $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.',
             array(1 => ucwords(str_replace('_', ' ', $greeting)))
           );
         }
@@ -931,9 +964,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
 
-    if (!$email && !(CRM_Utils_Array::value('first_name', $fields) &&
-        CRM_Utils_Array::value('last_name', $fields)
-      )) {
+    if (!$email && !(!empty($fields['first_name']) && !empty($fields['last_name']))) {
       $defaults = $params = array('id' => $eventId);
       CRM_Event_BAO_Event::retrieve($params, $defaults);
       $message = ts("Mandatory fields (first name and last name, OR email address) are missing from this form.");
@@ -946,7 +977,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    *
    * @access public
    *
-   * @return None
+   * @return void
    */
   public function postProcess() {
     // get the submitted form values.
@@ -979,7 +1010,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
 
-    if (CRM_Utils_Array::value('image_URL', $params)) {
+    if (!empty($params['image_URL'])) {
       CRM_Contact_BAO_Contact::processImageParams($params);
     }
 
@@ -995,11 +1026,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     $primaryParticipantCount = self::getParticipantCount($this, $params);
 
     $totalParticipants = $primaryParticipantCount;
-    if (CRM_Utils_Array::value('additional_participants', $params)) {
+    if (!empty($params['additional_participants'])) {
       $totalParticipants += $params['additional_participants'];
     }
-    if (!$this->_allowConfirmation &&
-      CRM_Utils_Array::value('bypass_payment', $params) &&
+    if (!$this->_allowConfirmation && !empty($params['bypass_payment']) &&
       is_numeric($this->_availableRegistrations) &&
       $totalParticipants > $this->_availableRegistrations
     ) {
@@ -1020,7 +1050,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     if (array_key_exists('participant_role_id', $params)) {
       $params['defaultRole'] = 0;
     }
-    if (!CRM_Utils_Array::value('participant_role_id', $params) &&
+    if (empty($params['participant_role_id']) &&
       $this->_values['event']['default_role_id']
     ) {
       $params['participant_role_id'] = $this->_values['event']['default_role_id'];
@@ -1108,8 +1138,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
               $this->_expressButtonName . '_x',
               $this->_expressButtonName . '_y',
             )
-          ) &&
-            !CRM_Utils_Array::value('is_pay_later', $params) &&
+          ) && empty($params['is_pay_later']) &&
           !$this->_allowWaitlist &&
           !$this->_requireApproval
         ) {
@@ -1165,7 +1194,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       $this->_params[] = $params;
       $this->set('params', $this->_params);
 
-      if (!CRM_Utils_Array::value('additional_participants', $params)) {
+      if (
+        empty($params['additional_participants'])
+        && !$this->_values['event']['is_confirm_enabled'] // CRM-11182 - Optional confirmation screen
+      ) {
         self::processRegistration($this->_params);
       }
     }
@@ -1184,10 +1216,14 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    * @param  array $param Form valuess
    * @param  int contactID
    *
-   * @return None
+   * @return void
    * access public
    *
    */
+  /**
+   * @param $params
+   * @param null $contactID
+   */
   public function processRegistration($params, $contactID = NULL) {
     $session = CRM_Core_Session::singleton();
     $this->_participantInfo = array();
@@ -1213,7 +1249,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         $fields = NULL;
 
         // setting register by Id and unset contactId.
-        if (!CRM_Utils_Array::value('is_primary', $value)) {
+        if (empty($value['is_primary'])) {
           $contactID = NULL;
           $registerByID = $this->get('registerByID');
           if ($registerByID) {
@@ -1233,26 +1269,26 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
             $this->_participantInfo[] = $value['first_name'] . ' ' . $value['last_name'];
           }
         }
-        elseif (CRM_Utils_Array::value('contact_id', $value)) {
+        elseif (!empty($value['contact_id'])) {
           $contactID = $value['contact_id'];
         }
         else {
           $contactID = $this->getContactID();
         }
 
-        CRM_Event_Form_Registration_Confirm::fixLocationFields($value, $fields);
+        CRM_Event_Form_Registration_Confirm::fixLocationFields($value, $fields, $this);
         //for free event or additional participant, dont create billing email address.
-        if (!CRM_Utils_Array::value('is_primary', $value) || !$this->_values['event']['is_monetary']) {
+        if (empty($value['is_primary']) || !$this->_values['event']['is_monetary']) {
           unset($value["email-{$this->_bltID}"]);
         }
 
-        $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields);
+        $contactID = CRM_Event_Form_Registration_Confirm::updateContactFields($contactID, $value, $fields, $this);
 
         // lets store the contactID in the session
         // we dont store in userID in case the user is doing multiple
         // transactions etc
         // for things like tell a friend
-        if (!$this->getContactID() && CRM_Utils_Array::value('is_primary', $value)) {
+        if (!$this->getContactID() && !empty($value['is_primary'])) {
           $session->set('transaction.userID', $contactID);
         }
 
@@ -1389,33 +1425,21 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
 
     if (!$contactID && is_array($fields) && $fields) {
 
-      //CRM-6996
-      //as we are allowing w/ same email address,
-      //lets check w/ other contact params.
-      if ($self->_values['event']['allow_same_participant_emails']) {
-        $params = $fields;
-        $level = ($isAdditional) ? 'Supervised' : 'Unsupervised';
+      //CRM-14134 use Unsupervised rule for everyone
+      $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual');
 
-        $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
+      // disable permission based on cache since event registration is public page/feature.
+      $dedupeParams['check_permission'] = FALSE;
 
-        // disable permission based on cache since event registration is public page/feature.
-        $dedupeParams['check_permission'] = FALSE;
-        $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', $level);
-        $contactID = CRM_Utils_Array::value(0, $ids);
+      // find event dedupe rule
+      if (CRM_Utils_Array::value('dedupe_rule_group_id', $self->_values['event'], 0) > 0) {
+        $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised', array(), $self->_values['event']['dedupe_rule_group_id']);
       }
       else {
-        foreach ($fields as $fieldname => $fieldvalue) {
-          if (substr($fieldname, 0, 6) == 'email-') {
-            $emailString = trim($fieldvalue);
-            if (!empty($emailString)) {
-              $match = CRM_Contact_BAO_Contact::matchContactOnEmail($emailString, 'Individual');
-              if (!empty($match)) {
-                $contactID = $match->contact_id;
-              }
-            }
-          }
-        }
+        $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
       }
+      $contactID = CRM_Utils_Array::value(0, $ids);
+
     }
 
     if ($returnContactId) {
@@ -1473,4 +1497,3 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
   }
 }
-