Merge pull request #4726 from atif-shaikh/CRM-5039
authorMonish Deb <monish.deb@webaccessglobal.com>
Mon, 29 Dec 2014 10:41:47 +0000 (16:11 +0530)
committerMonish Deb <monish.deb@webaccessglobal.com>
Mon, 29 Dec 2014 10:41:47 +0000 (16:11 +0530)
CRM-5039 - Events don't always limit registrations to "max participants"...

1  2 
CRM/Event/Form/Registration.php
CRM/Event/Form/Registration/Confirm.php
CRM/Event/Form/Registration/Register.php
CRM/Price/BAO/PriceField.php

index efd2bf879a3032f753f802e0605cc1ef4539d126,ca541b36ee315ce75bda06bc0bc4ca72352323e1..1b77822202329ae8d871dc4532546eb2afe76196
@@@ -1,7 -1,7 +1,7 @@@
  <?php
  /*
   +--------------------------------------------------------------------+
 - | CiviCRM version 4.5                                                |
 + | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
@@@ -1130,7 -1130,7 +1130,7 @@@ WHERE  v.option_group_id = g.i
      $addParticipantNum = substr($form->_name, 12);
      foreach ($params as $pCnt => $values) {
        if ($values == 'skip' ||
-         $pCnt == $addParticipantNum
+         $pCnt === $addParticipantNum
        ) {
          continue;
        }
          $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.');
index 63be34167313bdfdea3f67c9f1b800c3a074b182,b118210a0cd43f6bf902759e6ead6030c6847527..b65f4846c47b48e82038adbcee6224f1bf29552b
@@@ -1,7 -1,7 +1,7 @@@
  <?php
  /*
   +--------------------------------------------------------------------+
 - | CiviCRM version 4.5                                                |
 + | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
@@@ -100,7 -100,7 +100,7 @@@ class CRM_Event_Form_Registration_Confi
          else
            CRM_Core_Error::fatal($paymentObjError);
  
 -        $params['payer'] = $expressParams['payer'];
 +        $params['payer'] = CRM_Utils_Array::value('payer', $expressParams);
          $params['payer_id'] = $expressParams['payer_id'];
          $params['payer_status'] = $expressParams['payer_status'];
  
            $params['discountAmount'] = $this->_params[0]['discountAmount'];
            $params['discountMessage'] = $this->_params[0]['discountMessage'];
          }
 +        if (!empty($this->_params[0]['amount_priceset_level_radio'])) {
 +          $params['amount_priceset_level_radio'] = $this->_params[0]['amount_priceset_level_radio'];
 +        }
          $params['amount_level'] = $this->_params[0]['amount_level'];
          $params['currencyID'] = $this->_params[0]['currencyID'];
          $params['payment_action'] = 'Sale';
  
      // Assign Participant Count to Lineitem Table
      $this->assign('pricesetFieldsCount', CRM_Price_BAO_PriceSet::getPricesetCount($this->_priceSetId));
+     $this->addFormRule(array('CRM_Event_Form_Registration_Confirm', 'formRule'), $this);
    }
  
+   static function formRule($fields, $files, $self) {
+     $errors = array();
+     $eventFull = CRM_Event_BAO_Participant::eventFull($self->_eventId, FALSE, CRM_Utils_Array::value('has_waitlist', $self->_values['event']));
+     if ($eventFull && empty($self->_allowConfirmation)) {
+       if (empty($self->_allowWaitlist)) {
+         CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "reset=1&id={$self->_eventId}",FALSE, NULL, FALSE, TRUE));
+       }
+     }
+     $self->_feeBlock = $self->_values['fee'];
+     CRM_Event_Form_Registration_Register::formatFieldsForOptionFull($self);
+     if (!empty($self->_priceSetId)) {
+       $priceSetErrors = self::validatePriceSet($self, $self->_params);
+       //get price set fields errors in.
+       $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, array()));
+     }
+     if (!empty($errors)) {
+       $soldOutOptions = implode("<br>", $priceSetErrors['soldOutOptions']);
+       CRM_Core_Session::setStatus(ts('You have been returned to the start of the registration process and any sold out events have been removed from your selections. You will not be able to continue until you review your booking and select different events if you wish. The following events were sold out:') , ts('Unfortunately some of your options have now sold out for one or more participants.') , 'error');
+       CRM_Core_Session::setStatus(ts("{$soldOutOptions}") , ts('Sold out:') , 'error');
+       CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/event/register', "_qf_Register_display=true&qfKey=" . $fields['qfKey']));
+     }
+     return empty($errors) ? TRUE : $errors;
+   }
    /**
     * Process the form submission
     *
index 0238d32753077715455acdd037ee6b119acb15ce,b5ef225490be91a13baddf2fe519988aff5ad487..7a921d8509f7bb2ee91590626f9ff90fab1c84af
@@@ -1,7 -1,7 +1,7 @@@
  <?php
  /*
   +--------------------------------------------------------------------+
 - | CiviCRM version 4.5                                                |
 + | CiviCRM version 4.6                                                |
   +--------------------------------------------------------------------+
   | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
@@@ -745,18 -745,11 +745,11 @@@ class CRM_Event_Form_Registration_Regis
          $dbTotalCount      = CRM_Utils_Array::value($optId, $recordedOptionsCount, 0);
          $currentTotalCount = CRM_Utils_Array::value($optId, $currentOptionsCount, 0);
  
-         // Do not consider current count for select field,
-         // since we are not going to freeze the options.
-         if ($field['html_type'] == 'Select') {
-           $totalCount = $dbTotalCount;
-         }
-         else {
-           $totalCount = $currentTotalCount + $dbTotalCount;
-         }
  
+         $totalCount = $currentTotalCount + $dbTotalCount;
          $isFull = FALSE;
          if ($maxValue &&
-           (($totalCount >= $maxValue) || ($totalCount + $count > $maxValue))
+           (($totalCount > $maxValue) || ($totalCount + $count > $maxValue))
          ) {
            $isFull = TRUE;
            $optionFullIds[$optId] = $optId;
index b8cb4ab5aada2e79164d1e6dd7d7460c4717fbdd,fda67ad3c91024cf97934d59eb86dfb24bef08c1..96b97ff2a00c7c6736096ae375ffbb3557bab88b
@@@ -1,7 -1,7 +1,7 @@@
  <?php
  /*
    +--------------------------------------------------------------------+
 -  | CiviCRM version 4.5                                                |
 +  | CiviCRM version 4.6                                                |
    +--------------------------------------------------------------------+
    | Copyright CiviCRM LLC (c) 2004-2014                                |
    +--------------------------------------------------------------------+
@@@ -196,6 -196,13 +196,13 @@@ class CRM_Price_BAO_PriceField extends 
      return CRM_Core_DAO::setFieldValue('CRM_Price_DAO_PriceField', $id, 'is_active', $is_active);
    }
  
+   static function freezeIfEnabled(&$element, $fieldOptions) {
+     if (!empty($fieldOptions['is_full'])) {
+       $element->freeze();
+     }
+     return;
+   }
    /**
     * Get the field title.
     *
  
          // CRM-6902 - Add "max" option for a price set field
          if (in_array($optionKey, $freezeOptions)) {
-           $element->freeze();
-           // CRM-14696 - Styling for sold out Text input fields is handled in PriceSet.tpl
+           self::freezeIfEnabled($element, $freezeOptions);
+           // CRM-14696 - Improve display for sold out price set options
+           $element->setLabel($label . '&nbsp;<span class="sold-out-option">' . ts('Sold out') . '</span>');
          }
  
          //CRM-10117
  
            // CRM-6902 - Add "max" option for a price set field
            if (in_array($opId, $freezeOptions)) {
-             $choice[$opId]->freeze();
+             self::freezeIfEnabled($choice[$opId], $customOption[$opId]);
              // CRM-14696 - Improve display for sold out price set options
              $choice[$opId]->setText('<span class="sold-out-option">' . $choice[$opId]->getText() . '&nbsp;(' . ts('Sold out') . ')</span>');
            }
                $opt['label'] = $opt['label'] . CRM_Utils_Money::format($opt[$valueFieldName]);
              }
            }
-           
            $selectOption[$opt['id']] = $opt['label'];
            $priceVal[$opt['id']] = implode($seperator, array($opt[$valueFieldName] + $taxAmount, $count, $max_value));
-                                       
            if (!in_array($opt['id'], $freezeOptions)) {
              $allowedOptions[] = $opt['id'];
            }
            else {
              $opt['label'] = $opt['label'] . ' (' . ts('Sold out') . ')';
            }
-                                       
            $selectOption[$opt['id']] = $opt['label'];
  
            if ($is_pay_later) {
              $qf->add( 'text', 'txt-'.$elementName, $label, array( 'size' => '4'));
            }
          }
-         
          $element = &$qf->add('select', $elementName, $label,
            array(
              '' => ts('- select -')) + $selectOption,
            }
            // CRM-6902 - Add "max" option for a price set field
            if (in_array($opId, $freezeOptions)) {
-             $check[$opId]->freeze();
+             self::freezeIfEnabled($check[$opId], $customOption[$opId]);
              // CRM-14696 - Improve display for sold out price set options
              $check[$opId]->setText('<span class="sold-out-option">' . $check[$opId]->getText() . '&nbsp;(' . ts('Sold out') . ')</span>');
            }