INFRA-132 - Put "else" and "catch" on new line
[civicrm-core.git] / CRM / Event / Form / Registration.php
index 41f2b87e91caa4fb145f319be778d7fd399a61cf..ddb881f5716c7b98e5fc967e5d8336174219ea84 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
 class CRM_Event_Form_Registration extends CRM_Core_Form {
 
   /**
-   * how many locationBlocks should we display?
+   * How many locationBlocks should we display?
    *
    * @var int
    * @const
    */
-  CONST LOCATION_BLOCKS = 1;
+  const LOCATION_BLOCKS = 1;
 
   /**
-   * the id of the event we are proceessing
+   * The id of the event we are proceessing
    *
    * @var int
-   * @protected
    */
   public $_eventId;
 
   /**
-   * the array of ids of all the participant we are proceessing
+   * The array of ids of all the participant we are proceessing
    *
    * @var int
-   * @protected
    */
   protected $_participantIDS = NULL;
 
   /**
-   * the id of the participant we are proceessing
+   * The id of the participant we are proceessing
    *
    * @var int
-   * @protected
    */
   protected $_participantId;
 
   /**
-   * is participant able to walk registration wizard.
+   * Is participant able to walk registration wizard.
    *
    * @var Boolean
-   * @protected
    */
   public $_allowConfirmation;
 
   /**
-   * is participant requires approval
+   * Is participant requires approval
    *
    * @var Boolean
-   * @public
    */
   public $_requireApproval;
 
   /**
-   * is event configured for waitlist.
+   * Is event configured for waitlist.
    *
    * @var Boolean
-   * @public
    */
   public $_allowWaitlist;
 
   /**
-   * store additional participant ids
+   * Store additional participant ids
    * when there are pre-registered.
    *
    * @var array
-   * @public
    */
   public $_additionalParticipantIds;
 
   /**
-   * the mode that we are in
+   * The mode that we are in
    *
    * @var string
    * @protect
@@ -114,18 +107,16 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   public $_mode;
 
   /**
-   * the values for the contribution db object
+   * The values for the contribution db object
    *
    * @var array
-   * @protected
    */
   public $_values;
 
   /**
-   * the paymentProcessor attributes for this page
+   * The paymentProcessor attributes for this page
    *
    * @var array
-   * @protected
    */
   public $_paymentProcessor;
 
@@ -133,7 +124,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
    * The params submitted by the form and computed by the app
    *
    * @var array
-   * @protected
    */
   protected $_params;
 
@@ -141,7 +131,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
    * The fields involved in this contribution page
    *
    * @var array
-   * @protected
    */
   public $_fields;
 
@@ -149,7 +138,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
    * The billing location id for this contribiution page
    *
    * @var int
-   * @protected
    */
   public $_bltID;
 
@@ -157,7 +145,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
    * Price Set ID, if the new price set method is used
    *
    * @var int
-   * @protected
    */
   public $_priceSetId = NULL;
 
@@ -165,7 +152,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
    * Array of fields for the price set
    *
    * @var array
-   * @protected
    */
   public $_priceSet;
 
@@ -176,7 +162,6 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   /* Is event already full.
      *
      * @var boolean
-     * @protected
      */
 
   public $_isEventFull;
@@ -187,13 +172,14 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
 
   public $_forcePayement;
 
+  public $_isBillingAddressRequiredForPayLater;
+
   /**
-   * Function to set variables up before form is built
+   * Set variables up before form is built
    *
    * @return void
-   * @access public
    */
-  function preProcess() {
+  public function preProcess() {
     $this->_eventId = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
     $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
 
@@ -398,10 +384,8 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
       }
       $this->set('bltID', $this->_bltID);
 
-      if ($this->_values['event']['is_monetary'] &&
-        ($this->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_FORM)
-      ) {
-        CRM_Core_Payment_Form::setCreditCardFields($this);
+      if ($this->_values['event']['is_monetary']) {
+        CRM_Core_Payment_Form::setPaymentFieldsByProcessor($this, $this->_paymentProcessor);
       }
       $params = array('entity_id' => $this->_eventId, 'entity_table' => 'civicrm_event');
       $this->_values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
@@ -466,15 +450,20 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
     if ($campID && CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Campaign', $campID)) {
       $this->_values['event']['campaign_id'] = $campID;
     }
+
+    // check if billing block is required for pay later
+    if (CRM_Utils_Array::value('is_pay_later', $this->_values['event'])) {
+      $this->_isBillingAddressRequiredForPayLater = CRM_Utils_Array::value('is_billing_required', $this->_values['event']);
+      $this->assign('isBillingAddressRequiredForPayLater', $this->_isBillingAddressRequiredForPayLater);
+    }
   }
 
   /**
-   * assign the minimal set of variables to the template
+   * Assign the minimal set of variables to the template
    *
    * @return void
-   * @access public
    */
-  function assignToTemplate() {
+  public function assignToTemplate() {
     //process only primary participant params
     $this->_params = $this->get('params');
     if (isset($this->_params[0])) {
@@ -565,16 +554,15 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   }
 
   /**
-   * Function to add the custom fields
+   * Add the custom fields
    *
-   * @param $id
-   * @param $name
+   * @param int $id
+   * @param string $name
    * @param bool $viewOnly
    *
    * @return void
-   * @access public
    */
-  function buildCustom($id, $name, $viewOnly = FALSE) {
+  public function buildCustom($id, $name, $viewOnly = FALSE) {
     if ($id) {
       $button    = substr($this->controller->getButtonName(), -4);
       $cid       = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
@@ -669,12 +657,12 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   }
 
   /**
-   * @param $form
-   * @param $eventID
+   * @param CRM_Core_Form $form
+   * @param int $eventID
    *
    * @throws Exception
    */
-  static function initEventFee(&$form, $eventID) {
+  public static function initEventFee(&$form, $eventID) {
     // get price info
 
     // retrive all active price set fields.
@@ -727,16 +715,15 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   }
 
   /**
-   * Function to handle process after the confirmation of payment by User
+   * Handle process after the confirmation of payment by User
    *
-   * @param null $contactID
+   * @param int $contactID
    * @param null $contribution
    * @param null $payment
    *
    * @return void
-   * @access public
    */
-  function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
+  public function confirmPostProcess($contactID = NULL, $contribution = NULL, $payment = NULL) {
     // add/update contact information
     $fields = array();
     unset($this->_params['note']);
@@ -804,7 +791,8 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
 
       if (array_key_exists('email-5', $this->_params)) {
       $mail = 'email-5';
-      } else {
+      }
+      else {
         foreach ($this->_params as $name => $dontCare) {
           if (substr($name, 0, 5) == 'email') {
             $mail = $name;
@@ -832,11 +820,10 @@ class CRM_Event_Form_Registration extends CRM_Core_Form {
   /**
    * Process the participant
    *
-   * @param $params
-   * @param $contactID
+   * @param array $params
+   * @param int $contactID
    *
    * @return void
-   * @access public
    */
   public static function addParticipant(&$form, $contactID) {
     if (empty($form->_params)) {
@@ -938,14 +925,14 @@ WHERE  v.option_group_id = g.id
 
   /* Calculate the total participant count as per params.
    *
-   * @param  array $params user params.
+   * @param array $params
+   *   User params.
    *
    * @return $totalCount total participant count.
-   * @access public
    */
   /**
-   * @param $form
-   * @param $params
+   * @param CRM_Core_Form $form
+   * @param array $params
    * @param bool $skipCurrent
    *
    * @return int|string
@@ -1036,15 +1023,15 @@ WHERE  v.option_group_id = g.id
   /* Format user submitted price set params.
    * Convert price set each param as an array.
    *
-   * @param $params an array of user submitted params.
+   * @param $params
+   *   An array of user submitted params.
    *
    *
    * @return array $formatted, formatted price set params.
-   * @access public
    */
   /**
-   * @param $form
-   * @param $params
+   * @param CRM_Core_Form $form
+   * @param array $params
    *
    * @return mixed
    */
@@ -1086,11 +1073,11 @@ WHERE  v.option_group_id = g.id
   /* Calculate total count for each price set options.
    * those are currently selected by user.
    *
-   * @param $form form object.
+   * @param $form
+   *   Form object.
    *
    *
    * @return array $optionsCount, array of each option w/ count total.
-   * @access public
    */
   /**
    * @param $form
@@ -1124,7 +1111,7 @@ WHERE  v.option_group_id = g.id
     $addParticipantNum = substr($form->_name, 12);
     foreach ($params as $pCnt => $values) {
       if ($values == 'skip' ||
-        $pCnt == $addParticipantNum
+        $pCnt === $addParticipantNum
       ) {
         continue;
       }
@@ -1167,7 +1154,7 @@ WHERE  v.option_group_id = g.id
    *
    * @return null|string
    */
-  function checkTemplateFileExists($suffix = '') {
+  public function checkTemplateFileExists($suffix = '') {
     if ($this->_eventId) {
       $templateName = $this->_name;
       if (substr($templateName, 0, 12) == 'Participant_') {
@@ -1186,7 +1173,7 @@ WHERE  v.option_group_id = g.id
   /**
    * @return null|string
    */
-  function getTemplateFileName() {
+  public function getTemplateFileName() {
     $fileName = $this->checkTemplateFileExists();
     return $fileName ? $fileName : parent::getTemplateFileName();
   }
@@ -1194,7 +1181,7 @@ WHERE  v.option_group_id = g.id
   /**
    * @return null|string
    */
-  function overrideExtraTemplateFileName() {
+  public function overrideExtraTemplateFileName() {
     $fileName = $this->checkTemplateFileExists('extra.');
     return $fileName ? $fileName : parent::overrideExtraTemplateFileName();
   }
@@ -1206,7 +1193,7 @@ WHERE  v.option_group_id = g.id
    * @param unknown_type $params
    * @return multitype:|Ambigous <multitype:, string, string>
    */
-  static function validatePriceSet(&$form, $params) {
+  public static function validatePriceSet(&$form, $params) {
     $errors = array();
     $hasOptMaxValue = FALSE;
     if (!is_array($params) || empty($params)) {
@@ -1309,6 +1296,7 @@ WHERE  v.option_group_id = g.id
         $opDbCount = CRM_Utils_Array::value('db_total_count', $options[$optId], 0);
         $total += $opDbCount;
         if ($optMax && $total > $optMax) {
+          $errors['soldOutOptions'][] = ts('Option %1 has sold out.', array(1 => $feeBlock[$fieldId]['options'][$optId]['label']));
           if ($opDbCount && ($opDbCount >= $optMax)) {
             $errors[$currentParticipantNum]["price_{$fieldId}"] =
               ts('Sorry, this option is currently sold out.');
@@ -1340,9 +1328,9 @@ WHERE  v.option_group_id = g.id
 
   // set the first participant ID if not set, CRM-10032
   /**
-   * @param $participantID
+   * @param int $participantID
    */
-  function processFirstParticipant($participantID) {
+  public function processFirstParticipant($participantID) {
     $this->_participantId = $participantID;
     $this->set('participantId', $this->_participantId);
 
@@ -1369,7 +1357,7 @@ WHERE  v.option_group_id = g.id
    *
    * @param string $redirect
    */
-  function checkValidEvent($redirect = NULL) {
+  public function checkValidEvent($redirect = NULL) {
     // is the event active (enabled)?
     if (!$this->_values['event']['is_active']) {
       // form is inactive, die a fatal death
@@ -1401,12 +1389,15 @@ WHERE  v.option_group_id = g.id
     $endDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('registration_end_date',
         $this->_values['event']
       ));
+    $eventEndDate = CRM_Utils_Date::processDate(CRM_Utils_Array::value('event_end_date', $this->_values['event']));
     if (
       $endDate &&
       $endDate < $now
     ) {
       CRM_Core_Error::statusBounce(ts('Registration for this event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('registration_end_date', $this->_values['event'])))), $redirect);
     }
+    if (!empty($eventEndDate) && $eventEndDate < $now) {
+      CRM_Core_Error::statusBounce(ts('Event ended on %1', array(1 => CRM_Utils_Date::customFormat(CRM_Utils_Array::value('event_end_date', $this->_values['event'])))), $redirect);
+    }
   }
 }
-