Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-04-18-48-05
[civicrm-core.git] / CRM / Event / Form / Registration / AdditionalParticipant.php
index b085e00e3c8866a7046b205e5a26f6c489bdf611..097e7aaacb6630eba9f20110ed83b80439a9a678 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_AdditionalParticipant extends CRM_Event_Form_Registration {
 
   /**
-   * The defaults involved in this page
-   *
+   * The defaults involved in this page.
    */
   public $_defaults = array();
 
   /**
    * Pre-registered additional participant id.
-   *
    */
   public $additionalParticipantId = NULL;
 
   /**
-   * Set variables up before form is built
+   * Set variables up before form is built.
    *
    * @return void
    */
@@ -101,8 +99,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     if (!empty($this->_values['discount'])) {
       $discountId = CRM_Core_BAO_Discount::findSet($this->_eventId, 'civicrm_event');
       if ($discountId && !empty($this->_values['event']['default_discount_fee_id'])) {
-        $discountKey = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionValue", $this->_values['event']['default_discount_fee_id']
-          , 'weight', 'id'
+        $discountKey = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionValue", $this->_values['event']['default_discount_fee_id'], 'weight', 'id'
         );
         $defaults['amount'] = key(array_slice($this->_values['discount'][$discountId], $discountKey - 1, $discountKey, TRUE));
       }
@@ -133,9 +130,9 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     //CRM-4320, setdefault additional participant values.
     if ($this->_allowConfirmation && $this->_additionalParticipantId) {
       //hack to get set default from eventFees.php
-      $this->_discountId   = $discountId;
-      $this->_pId          = $this->_additionalParticipantId;
-      $this->_contactId    = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_additionalParticipantId, 'contact_id');
+      $this->_discountId = $discountId;
+      $this->_pId = $this->_additionalParticipantId;
+      $this->_contactId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $this->_additionalParticipantId, 'contact_id');
       $participantDefaults = CRM_Event_Form_EventFees::setDefaultValues($this);
       $participantDefaults = array_merge($this->_defaults, $participantDefaults);
       // use primary email address if billing email address is empty
@@ -150,9 +147,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     $defaults = array_merge($this->_defaults, $defaults);
 
     //reset values for all options those are full.
-    if (!empty($unsetSubmittedOptions) && empty($_POST)) {
-      $this->resetElementValue($unsetSubmittedOptions);
-    }
+    CRM_Event_Form_Registration::resetElementValue($unsetSubmittedOptions, $this);
 
     //load default campaign from page.
     if (array_key_exists('participant_campaign_id', $this->_fields)) {
@@ -163,7 +158,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
   }
 
   /**
-   * Build the form object
+   * Build the form object.
    *
    * @return void
    */
@@ -180,13 +175,17 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     $first_name = $last_name = NULL;
     $pre = $post = array();
     foreach (array(
-      'pre', 'post') as $keys) {
+               'pre',
+               'post',
+             ) as $keys) {
       if (isset($this->_values['additional_custom_' . $keys . '_id'])) {
         $this->buildCustom($this->_values['additional_custom_' . $keys . '_id'], 'additionalCustom' . ucfirst($keys), TRUE);
         $$keys = CRM_Core_BAO_UFGroup::getFields($this->_values['additional_custom_' . $keys . '_id']);
       }
       foreach (array(
-        'first_name', 'last_name') as $name) {
+                 'first_name',
+                 'last_name',
+               ) as $name) {
         if (array_key_exists($name, $$keys) &&
           CRM_Utils_Array::value('is_required', CRM_Utils_Array::value($name, $$keys))
         ) {
@@ -255,14 +254,23 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
           $this->_allowWaitlist = FALSE;
           $this->set('allowWaitlist', $this->_allowWaitlist);
           if ($this->_requireApproval) {
-            $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));
+            $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,
+              ));
           }
           else {
-            $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));
+            $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,
+              ));
           }
         }
         else {
-          $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));
+          $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,
+            ));
           $allowToProceed = FALSE;
         }
         CRM_Core_Session::setStatus($statusMessage, ts('Registration Error'), 'error');
@@ -329,8 +337,9 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     $this->assign('statusMessage', $statusMessage);
 
     $buttons = array(
-      array('type' => 'back',
-        'name' => ts('<< Go Back'),
+      array(
+        'type' => 'back',
+        'name' => ts('Go Back'),
         'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp',
       ),
     );
@@ -338,8 +347,9 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     //CRM-4320
     if ($allowToProceed) {
       $buttons = array_merge($buttons, array(
-        array('type' => 'next',
-            'name' => ts('Continue >>'),
+          array(
+            'type' => 'next',
+            'name' => ts('Continue'),
             'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
             'isDefault' => TRUE,
             'js' => $js,
@@ -348,9 +358,11 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
       );
       if ($includeSkipButton) {
         $buttons = array_merge($buttons, array(
-          array('type' => 'next',
-              'name' => ts('Skip Participant >>|'),
+            array(
+              'type' => 'next',
+              'name' => ts('Skip Participant'),
               'subName' => 'skip',
+              'icon' => 'seek-next',
             ),
           )
         );
@@ -361,7 +373,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
   }
 
   /**
-   * Global form rule
+   * Global form rule.
    *
    * @param array $fields
    *   The input form values.
@@ -370,8 +382,8 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
    * @param $self
    *
    *
-   * @return true if no errors, else array of errors
-   * @static
+   * @return bool|array
+   *   true if no errors, else array of errors
    */
   public static function formRule($fields, $files, $self) {
     $errors = array();
@@ -412,12 +424,12 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
               $existingEmails = array();
               $additionalParticipantEmails = array();
               if (is_array($value)) {
-              foreach ($value as $key => $val) {
-                if (substr($key, 0, 6) == 'email-' && $val) {
-                  $existingEmails[] = $val;
+                foreach ($value as $key => $val) {
+                  if (substr($key, 0, 6) == 'email-' && $val) {
+                    $existingEmails[] = $val;
+                  }
                 }
               }
-              }
               foreach ($fields as $key => $val) {
                 if (substr($key, 0, 6) == 'email-' && $val) {
                   $additionalParticipantEmails[] = $val;
@@ -433,7 +445,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
             else {
               // check with first_name and last_name for additional participants
               if (!empty($value['first_name']) && ($value['first_name'] == CRM_Utils_Array::value('first_name', $fields)) &&
-                (CRM_Utils_Array::value('last_name',$value) == CRM_Utils_Array::value('last_name', $fields))
+                (CRM_Utils_Array::value('last_name', $value) == CRM_Utils_Array::value('last_name', $fields))
               ) {
                 $errors['first_name'] = ts('The first name and last name must be unique for each participant.');
                 break;
@@ -471,7 +483,10 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
           if (!$self->_allowConfirmation && empty($self->_values['event']['has_waitlist']) &&
             $totalParticipants > $self->_availableRegistrations
           ) {
-            $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));
+            $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,
+              ));
           }
         }
       }
@@ -493,7 +508,6 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
       }
     }
 
-
     if ($button != 'skip' &&
       $self->_values['event']['is_monetary'] &&
       !isset($errors['_qf_default']) &&
@@ -549,10 +563,11 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
 
     CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors);
 
-    // make sure that credit card number and cvv are valid
-    CRM_Core_Payment_Form::validateCreditCard($self->_params[0], $errors);
+    // validate supplied payment instrument values (e.g. credit card number and cvv)
+    $payment_processor_id = $self->_params[0]['payment_processor'];
+    CRM_Core_Payment_Form::validatePaymentInstrument($payment_processor_id, $self->_params[0], $errors, $self);
 
-    if ($errors) {
+    if (!empty($errors)) {
       return FALSE;
     }
 
@@ -564,10 +579,11 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
         }
       }
     }
+    return TRUE;
   }
 
   /**
-   * Process the form submission
+   * Process the form submission.
    *
    *
    * @return void
@@ -664,13 +680,13 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
           $lineItem = array();
           CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem);
 
-          //build the line item..
-          if (array_key_exists($addParticipantNum, $this->_lineItem)) {
-            $this->_lineItem[$addParticipantNum] = $lineItem;
-          }
-          else {
-            $this->_lineItem[] = $lineItem;
-          }
+          //build line item array..
+          //if requireApproval/waitlist is enabled we hide fees for primary participant
+          // (and not for additional participant which might be is a bug)
+          //lineItem are not correctly build for primary participant
+          //this results in redundancy since now lineItems for additional participant will be build against primary participantNum
+          //therefore lineItems must always be build against current participant No
+          $this->_lineItem[$addParticipantNum] = $lineItem;
         }
       }
 
@@ -693,12 +709,7 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
       }
 
       //build the params array.
-      if (array_key_exists($addParticipantNum, $this->_params)) {
-        $this->_params[$addParticipantNum] = $params;
-      }
-      else {
-        $this->_params[] = $params;
-      }
+      $this->_params[$addParticipantNum] = $params;
     }
 
     //finally set the params.
@@ -743,11 +754,12 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
   }
 
   /**
-   * Check whether call current participant is last one
+   * Check whether call current participant is last one.
    *
    * @param bool $isButtonJs
    *
-   * @return boolean ture on success.
+   * @return bool
+   *   ture on success.
    */
   public function isLastParticipant($isButtonJs = FALSE) {
     $participant = $isButtonJs ? $this->_params[0]['additional_participants'] : $this->_params[0]['additional_participants'] + 1;
@@ -757,101 +769,4 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
     return FALSE;
   }
 
-  /**
-   * Reset values for all options those are full.
-   *
-   **/
-  public function resetElementValue($optionFullIds = array()) {
-    if (!is_array($optionFullIds) ||
-      empty($optionFullIds) ||
-      !$this->isSubmitted()
-    ) {
-      return;
-    }
-
-    foreach ($optionFullIds as $fldId => $optIds) {
-      $name = "price_$fldId";
-      if (!$this->elementExists($name)) {
-        continue;
-      }
-
-      $element = $this->getElement($name);
-      $eleType = $element->getType();
-
-      $resetSubmitted = FALSE;
-      switch ($eleType) {
-        case 'text':
-          if ($element->isFrozen()) {
-            $element->setValue('');
-            $resetSubmitted = TRUE;
-          }
-          break;
-
-        case 'group':
-          if (is_array($element->_elements)) {
-            foreach ($element->_elements as $child) {
-              $childType = $child->getType();
-              $methodName = 'getName';
-              if ($childType) {
-                $methodName = 'getValue';
-              }
-              if (in_array($child->{$methodName}(), $optIds) && $child->isFrozen()) {
-                $resetSubmitted = TRUE;
-                $child->updateAttributes(array('checked' => NULL));
-              }
-            }
-          }
-          break;
-
-        case 'select':
-          $resetSubmitted = TRUE;
-          $element->_values = array();
-          break;
-      }
-
-      //finally unset values from submitted.
-      if ($resetSubmitted) {
-        $this->resetSubmittedValue($name, $optIds);
-      }
-    }
-  }
-
-  /**
-   * @param string $elementName
-   * @param array $optionIds
-   */
-  public function resetSubmittedValue($elementName, $optionIds = array()) {
-    if (empty($elementName) ||
-      !$this->elementExists($elementName) ||
-      !$this->getSubmitValue($elementName)
-    ) {
-      return;
-    }
-    foreach (array(
-      'constantValues', 'submitValues', 'defaultValues') as $val) {
-      $values = &$this->{"_$val"};
-      if (!is_array($values) || empty($values)) {
-        continue;
-      }
-      $eleVal = CRM_Utils_Array::value($elementName, $values);
-      if (empty($eleVal)) {
-        continue;
-      }
-      if (is_array($eleVal)) {
-        $found = FALSE;
-        foreach ($eleVal as $keyId => $ignore) {
-          if (in_array($keyId, $optionIds)) {
-            $found = TRUE;
-            unset($values[$elementName][$keyId]);
-          }
-        }
-        if ($found && empty($values[$elementName][$keyId])) {
-          $values[$elementName][$keyId] = NULL;
-        }
-      }
-      else {
-        $values[$elementName][$keyId] = NULL;
-      }
-    }
-  }
 }