Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-04-18-48-05
[civicrm-core.git] / CRM / Event / Form / Registration / Register.php
index cfb1aa15d028f80cfe3127f1e2528e6792332550..9ddc5d8393557a7ed48db2c87dc06748c6977cee 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
 class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
 
   /**
-   * The fields involved in this page
+   * The fields involved in this page.
    */
   public $_fields;
 
   /**
-   * The defaults involved in this page
+   * The defaults involved in this page.
    */
   public $_defaults;
 
@@ -80,7 +80,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   public $_paymentFields = array();
 
   /**
-   * Set variables up before form is built
+   * Set variables up before form is built.
    *
    * @return void
    */
@@ -260,6 +260,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
     if ($this->_priceSetId && !empty($this->_feeBlock)) {
       foreach ($this->_feeBlock as $key => $val) {
+        if (empty($val['options'])) {
+          continue;
+        }
+        $optionFullIds = CRM_Utils_Array::value('option_full_ids', $val, array());
         foreach ($val['options'] as $keys => $values) {
           if ($values['is_default'] && empty($values['is_full'])) {
 
@@ -271,7 +275,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
             }
           }
         }
+        $unsetSubmittedOptions[$val['id']] = $optionFullIds;
       }
+      //reset values for all options those are full.
+      CRM_Event_Form_Registration::resetElementValue($unsetSubmittedOptions, $this);
     }
 
     //set default participant fields, CRM-4320.
@@ -306,7 +313,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -333,8 +340,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     $contactID = $this->getContactID();
-    $this->assign('contact_id', $contactID);
-    $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
+    if ($contactID) {
+      $this->assign('contact_id', $contactID);
+      $this->assign('display_name', CRM_Contact_BAO_Contact::displayName($contactID));
+    }
 
     $this->add('hidden', 'scriptFee', NULL);
     $this->add('hidden', 'scriptArray', NULL);
@@ -737,7 +746,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     $currentOptionsCount = self::getPriceSetOptionCount($form);
     $recordedOptionsCount = CRM_Event_BAO_Participant::priceSetOptionsCount($form->_eventId, $skipParticipants);
     $optionFullTotalAmount = 0;
-
+    $currentParticipantNo = (int) substr($form->_name, 12);
     foreach ($form->_feeBlock as & $field) {
       $optionFullIds = array();
       $fieldId = $field['id'];
@@ -754,7 +763,8 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         $totalCount = $currentTotalCount + $dbTotalCount;
         $isFull = FALSE;
         if ($maxValue &&
-          (($totalCount > $maxValue) || ($totalCount + $count > $maxValue))
+          (($totalCount >= $maxValue) &&
+          (empty($form->_lineItem[$currentParticipantNo][$optId]['price_field_id']) || $dbTotalCount >= $maxValue))
         ) {
           $isFull = TRUE;
           $optionFullIds[$optId] = $optId;
@@ -798,7 +808,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   }
 
   /**
-   * Global form rule
+   * Global form rule.
    *
    * @param array $fields
    *   The input form values.
@@ -850,7 +860,9 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     // priceset validations
-    if (!empty($fields['priceSetId'])) {
+    if (!empty($fields['priceSetId']) &&
+     !$self->_requireApproval && !$self->_allowWaitlist
+     ) {
       //format params.
       $formatted = self::formatPriceSetParams($self, $fields);
       $ppParams = array($formatted);
@@ -930,7 +942,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       if (!empty($self->_paymentFields)) {
         CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors);
       }
-      CRM_Core_Payment_Form::validateCreditCard($fields, $errors);
+      CRM_Core_Payment_Form::validatePaymentInstrument($self->_paymentProcessorID, $fields, $errors, $self);
     }
 
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
@@ -966,7 +978,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   }
 
   /**
-   * Process the form submission
+   * Process the form submission.
    *
    *
    * @return void
@@ -1081,7 +1093,14 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         if ($params['tax_amount']) {
           $this->set('tax_amount', $params['tax_amount']);
         }
-        $this->set('lineItem', array($lineItem));
+        $submittedLineItems = $this->get('lineItem');
+        if (!empty($submittedLineItems) && is_array($submittedLineItems)) {
+          $submittedLineItems[0] = $lineItem;
+        }
+        else {
+          $submittedLineItems = array($lineItem);
+        }
+        $this->set('lineItem', $submittedLineItems);
         $this->set('lineItemParticipantsCount', array($primaryParticipantCount));
       }
 
@@ -1119,9 +1138,14 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         $params['payment_action'] = 'Sale';
         $params['invoiceID'] = $invoiceID;
       }
-
-      $this->_params = array();
-      $this->_params[] = $params;
+      $this->_params = $this->get('params');
+      if (!empty($this->_params) && is_array($this->_params)) {
+        $this->_params[0] = $params;
+      }
+      else {
+        $this->_params = array();
+        $this->_params[] = $params;
+      }
       $this->set('params', $this->_params);
 
       if ($this->_paymentProcessor &&
@@ -1207,7 +1231,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   }
 
   /**
-   * Process Registration of free event
+   * Process Registration of free event.
    *
    * @param array $params
    *   Form values.
@@ -1387,7 +1411,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
   }
 
   /**
-   * Method to check if the user is already registered for the event
+   * Method to check if the user is already registered for the event.
    * and if result found redirect to the event info page
    *
    * @param array $fields
@@ -1491,4 +1515,5 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
   }
+
 }