Merge pull request #5057 from cividesk/CRM-15901-4.5
[civicrm-core.git] / CRM / Event / Form / Registration / Register.php
index 2210e7a6533bbb1b265e1ea94c97aff406959511..c29dbf1d8bd820640f3138ffc5cb31f5f2ecfd92 100644 (file)
@@ -164,8 +164,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     if ($this->_snippet) {
-      // now fix all state country selectors
-      CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults);
       return $this->_defaults;
     }
 
@@ -197,9 +195,6 @@ 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) {
@@ -325,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);
     }
 
@@ -689,9 +687,18 @@ 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) || empty($priceSet['optionsMaxValueTotal'])) {
@@ -699,14 +706,14 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     $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;
@@ -720,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();
@@ -749,16 +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 (empty($formattedPriceSetDefaults["price_{$field}"]) || empty($formattedPriceSetDefaults["price_{$fieldId}"][$opId])) {
+          if (empty($formattedPriceSetDefaults["price_{$field}"]) || empty($formattedPriceSetDefaults["price_{$fieldId}"][$optId])) {
             $optionFullIds[$optId] = $optId;
             $isFull = TRUE;
           }
@@ -776,6 +793,7 @@ 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);
   }
 
   /**
@@ -866,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;
@@ -925,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)))
           );
         }
@@ -1202,6 +1220,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    * access public
    *
    */
+  /**
+   * @param $params
+   * @param null $contactID
+   */
   public function processRegistration($params, $contactID = NULL) {
     $session = CRM_Core_Session::singleton();
     $this->_participantInfo = array();