CRM-16929 fix hiding on zero dollar contribution pages & events
authoreileen <emcnaughton@wikimedia.org>
Tue, 24 Nov 2015 04:48:09 +0000 (17:48 +1300)
committereileen <emcnaughton@wikimedia.org>
Wed, 2 Dec 2015 06:50:44 +0000 (19:50 +1300)
Change-Id: I204b087279a8d5170057b1e2b06d57ed1508127f

CRM/Event/Form/Registration/AdditionalParticipant.php
CRM/Event/Form/Registration/Register.php
templates/CRM/Contribute/Form/Contribution/Main.tpl
templates/CRM/Event/Form/Registration/Register.tpl
templates/CRM/Price/Form/Calculate.tpl
templates/CRM/common/paymentBlock.tpl

index 0f9190decb390c3e63216d68587dd427d341e86f..5699a977b245ce03f2f2f130ee60b1dfedb24f55 100644 (file)
@@ -556,10 +556,11 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R
 
     CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $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->_isBillingAddressRequiredForPayLater ? NULL : 'billing'));
-
+    if (isset($self->_params[0]['payment_processor'])) {
+      // 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->_isBillingAddressRequiredForPayLater ? NULL : 'billing'));
+    }
     if (!empty($errors)) {
       return FALSE;
     }
index 89c6f573ed7e27785830990247547331051d3665..f5f38e1a6872e169ad99443d2714d537cb09a312 100644 (file)
@@ -126,6 +126,9 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
    */
   public function setDefaultValues() {
     $this->_defaults = array();
+    if (!$this->_allowConfirmation && $this->_requireApproval) {
+      $this->_defaults['bypass_payment'] = 1;
+    }
     $contactID = $this->getContactID();
     CRM_Core_Payment_Form::setDefaultValues($this, $contactID);
 
@@ -333,6 +336,10 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
 
+    if (!$this->_allowConfirmation) {
+      $bypassPayment = TRUE;
+    }
+
     //hack to allow group to register w/ waiting
     if ((!empty($this->_values['event']['is_multiple_registrations']) ||
         $this->_priceSetId
@@ -368,9 +375,6 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     $this->assign('allowGroupOnWaitlist', $allowGroupOnWaitlist);
     $this->assign('isAdditionalParticipants', $isAdditionalParticipants);
 
-    //lets get js on two different qf elements.
-    $showHidePayfieldName = NULL;
-    $showHidePaymentInformation = FALSE;
     if ($this->_values['event']['is_monetary']) {
       self::buildAmount($this);
     }
@@ -410,12 +414,9 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
     }
 
-    //lets add some qf element to bypass payment validations, CRM-4320
-    if ($bypassPayment) {
-      $this->addElement('hidden', 'bypass_payment', NULL, array('id' => 'bypass_payment'));
-    }
+    $this->addElement('hidden', 'bypass_payment', NULL, array('id' => 'bypass_payment'));
+
     $this->assign('bypassPayment', $bypassPayment);
-    $this->assign('showHidePaymentInformation', $showHidePaymentInformation);
 
     $userID = $this->getContactID();
 
@@ -803,7 +804,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     //don't allow to register w/ waiting if enough spaces available.
-    if (!empty($fields['bypass_payment'])) {
+    if (!empty($fields['bypass_payment']) && $self->_allowConfirmation) {
       if (!is_numeric($self->_availableRegistrations) ||
         (empty($fields['priceSetId']) && CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations)
       ) {
index 52e4f25a940eb61dcec85eb02a8d32372e73cb50..795f563a670812ddc2916586a91b866b6979ea30 100644 (file)
     }
   }
 
-  {/literal}
-  {if $form.is_pay_later and $paymentProcessor.payment_processor_type EQ 'PayPal_Express'}
-  showHidePayPalExpressOption();
-  {/if}
-  {literal}
-
   CRM.$(function($) {
     enableHonorType();
     showRecurHelp();
     skipPaymentMethod();
   });
 
-  function showHidePayPalExpressOption() {
-    if (cj('input[name="is_pay_later"]').is(':checked')) {
-      cj("#crm-submit-buttons").show();
-      cj("#paypalExpress").hide();
-    }
-    else {
-      cj("#paypalExpress").show();
-      cj("#crm-submit-buttons").hide();
-    }
-  }
-
-  function showHidePayment(flag) {
-    var payment_options = cj(".payment_options-group");
-    var payment_processor = cj("div.payment_processor-section");
-    var payment_information = cj("div#payment_information");
-    if (flag) {
-      payment_options.hide();
-      payment_processor.hide();
-      payment_information.hide();
-    }
-    else {
-      payment_options.show();
-      payment_processor.show();
-      payment_information.show();
-    }
-  }
-
-  function skipPaymentMethod() {
-    var flag = false;
-    cj('.price-set-option-content input[data-amount]').each( function(){
-      currentTotal = cj(this).attr('data-amount').replace(/[^\/\d]/g,'');
-      if( cj(this).is(':checked') && currentTotal == 0 ) {
-          flag = true;
-      }
-    });
-
-    cj('.price-set-option-content input[data-amount]').change( function () {
-      if (cj(this).attr('data-amount').replace(/[^\/\d]/g,'') == 0 ) {
-        flag = true;
-      } else {
-        flag = false;
-      }
-      showHidePayment(flag);
-    });
-    showHidePayment(flag);
-  }
-
   CRM.$(function($) {
     // highlight price sets
     function updatePriceSetHighlight() {
     $('#priceset input[type="radio"]').change(updatePriceSetHighlight);
     updatePriceSetHighlight();
 
-    function toggleBillingBlockIfFree(){
-      var total_amount_tmp =  $(this).data('raw-total');
-      // Hide billing questions if this is free
-      if (total_amount_tmp == 0){
-        $("#billing-payment-block").hide();
-        $(".payment_options-group").hide();
-      }
-      else {
-        $("#billing-payment-block").show();
-        $(".payment_options-group").show();
-      }
-    }
-
-    $('#pricevalue').each(toggleBillingBlockIfFree).on('change', toggleBillingBlockIfFree);
-  
     // Update pledge contribution amount when pledge checkboxes change
     $("input[name^='pledge_amount']").on('change', function() {
       var total = 0;
index 63628635e2b427a620366d8f0f4c27467115252b..0f373d205eca0c1a9b745f6a5ecf8d0a811d921c 100644 (file)
       </fieldset>
     {/if}
 
-    <div id="billing-payment-block">
-      {* If we have a payment processor, load it - otherwise it happens via ajax *}
-      {if $paymentProcessorID or $isBillingAddressRequiredForPayLater}
-        {include file="CRM/Financial/Form/Payment.tpl" snippet=4}
-      {/if}
-    </div>
-    {include file="CRM/common/paymentBlock.tpl"}
+    {include file='CRM/Core/BillingBlockWrapper.tpl'}
 
     <div class="crm-public-form-item crm-section custom_pre-section">
       {include file="CRM/UF/Form/Block.tpl" fields=$customPost}
       skipPaymentMethod();
     });
 
-    CRM.$(function($) {
-      skipPaymentMethod();
-    });
-
-    // Hides billing and payment options block - but only if a price set is used.
-    // Called from display() in Calculate.tpl, depends on display() having been called.
-    function skipPaymentMethod() {
-      // If we're in quick-config price set, we do not have the pricevalue hidden element, so just return.
-      if (cj('#pricevalue').length == 0) {
-        return;
-      }
-      // CRM-15433 Remove currency symbol, decimal separator so we can check for zero numeric total regardless of localization.
-      currentTotal = cj('#pricevalue').text().replace(/[^\/\d]/g,'');
-      var isMultiple = '{/literal}{$event.is_multiple_registrations}{literal}';
-
-      var flag = 1;
-      var payment_options = cj(".payment_options-group");
-      var payment_processor = cj("div.payment_processor-section");
-      var payment_information = cj("div#payment_information");
-
-      // Do not hide billing and payment blocks if user is registering additional participants, since we do not know total owing.
-      if (isMultiple && cj("#additional_participants").val() && currentTotal == 0) {
-        flag = 0;
-      }
-
-      if (currentTotal == 0 && flag) {
-        payment_options.hide();
-        payment_processor.hide();
-        payment_information.hide();
-        // also unset selected payment methods
-        cj('input[name="payment_processor_id"]').removeProp('checked');
-      }
-      else {
-        payment_options.show();
-        payment_processor.show();
-        payment_information.show();
-      }
-    }
-
-    {/literal}
-  </script>
-
-{literal}
-<script type="text/javascript">
-  {/literal}{if $pcp && $is_honor_roll }pcpAnonymous();
-  {/if}{literal}
+  {/literal}
+  {if $pcp && $is_honor_roll }
+    pcpAnonymous();
+  {/if}
+  {literal}
 
   function allowParticipant() {
     {/literal}{if $allowGroupOnWaitlist}{literal}
     {/literal}{/if}{literal}
   }
 
-  {/literal}{if ($bypassPayment) and $paymentProcessor.payment_processor_type EQ 'PayPal_Express'}
-  {literal}
-  showHidePayPalExpressOption();
-  {/literal}{/if}{literal}
-
-  function showHidePayPalExpressOption() {
-    if (( cj("#bypass_payment").val() == 1 )) {
-      cj("#crm-submit-buttons").show();
-      cj("#paypalExpress").hide();
-    }
-    else {
-      cj("#paypalExpress").show();
-      cj("#crm-submit-buttons").hide();
-    }
-  }
-
-  {/literal}{if ($bypassPayment and $showHidePaymentInformation)}{literal}
-  showHidePaymentInfo();
-  {/literal} {/if}{literal}
-
-  function showHidePaymentInfo() {
-    if (( cj("#bypass_payment").val() == 1 )) {
-      cj('#billing-payment-block').hide();
-    }
-    else {
-      cj('#billing-payment-block').show();
-    }
-  }
-
   {/literal}{if $allowGroupOnWaitlist}{literal}
   allowGroupOnWaitlist(0, 0);
   {/literal}{/if}{literal}
       if (isrequireApproval) {
         cj("#id-waitlist-approval-msg").show();
         cj("#id-waitlist-msg").hide();
+        cj("#bypass_payment").val(1);
       }
       else {
         cj("#id-waitlist-approval-msg").hide();
+        cj("#bypass_payment").val(0);
       }
       //reset value since user don't want or not eligible for waitlist
-      cj("#bypass_payment").val(0);
+      skipPaymentMethod();
     }
-
-    //now call showhide payment info.
-    {/literal}
-    {if ($bypassPayment) and $paymentProcessor.payment_processor_type EQ 'PayPal_Express'}{literal}
-    showHidePayPalExpressOption();
-    {/literal}{/if}
-    {literal}
-
-    {/literal}{if ($bypassPayment) and $showHidePaymentInformation}{literal}
-    showHidePaymentInfo();
-    {/literal}{/if}{literal}
   }
 
   {/literal}
index 1f0c9de8b168c61983d37f30188812aa768df55c..3e3fee807b5dd378c85c263817802fb43c066300 100644 (file)
@@ -46,7 +46,7 @@ var thousandMarker = '{/literal}{$config->monetaryThousandSeparator}{literal}';
 var separator      = '{/literal}{$config->monetaryDecimalPoint}{literal}';
 var symbol         = '{/literal}{$currencySymbol}{literal}';
 var optionSep      = '|';
-var priceSet = price = Array();
+
 cj("#priceset [price]").each(function () {
 
     var elementType =  cj(this).attr('type');
@@ -185,13 +185,14 @@ function display(totalfee) {
     totalfee = Math.round(totalfee*100)/100;
     var totalEventFee  = formatMoney( totalfee, 2, separator, thousandMarker);
     document.getElementById('pricevalue').innerHTML = "<b>"+symbol+"</b> "+totalEventFee;
-    scriptfee   = totalfee;
 
     cj('#total_amount').val( totalfee );
     cj('#pricevalue').data('raw-total', totalfee).trigger('change');
 
     ( totalfee < 0 ) ? cj('table#pricelabel').addClass('disabled') : cj('table#pricelabel').removeClass('disabled');
     if (typeof skipPaymentMethod == 'function') {
+      // Advice to anyone who, like me, feels hatred towards this if construct ... if you remove the if you
+      // get an error on participant 2 of a event that requires approval & permits multiple registrants.
       skipPaymentMethod();
     }
 }
index 042711b40c8cd58dbadaa94f2fdf3a19b0d65f5d..75d15050777878b4a121fdbd3908606e1e6e3b66 100644 (file)
 *}
 {literal}
 <script type="text/javascript">
+  /**
+   * Show or hide payment options.
+   *
+   * @param bool $isHide
+   *   Should the block be hidden.
+   */
+  function showHidePayment(isHide) {
+    var payment_options = cj(".payment_options-group");
+    var payment_processor = cj("div.payment_processor-section");
+    var payment_information = cj("div#payment_information");
+    // I've added a hide for billing block. But, actually the issue
+    // might be that the unselecting of the processor should cause it
+    // to be hidden (or removed) in which case it can go from this function.
+    var billing_block = cj("div#billing-payment-block");
+    if (isHide) {
+      payment_options.hide();
+      payment_processor.hide();
+      payment_information.hide();
+      billing_block.hide();
+      // also unset selected payment methods
+      cj('input[name="payment_processor_id"]').removeProp('checked');
+    }
+    else {
+      payment_options.show();
+      payment_processor.show();
+      payment_information.show();
+      billing_block.show();
+    }
+  }
+
+  /**
+   * Hides or shows billing and payment options block depending on whether payment is required.
+   *
+   * In general incomplete orders or $0 orders do not require a payment block.
+   */
+  function skipPaymentMethod() {
+    var isHide = false;
+    var isMultiple = '{/literal}{$event.is_multiple_registrations}{literal}';
+    var alwaysShowFlag = (isMultiple && cj("#additional_participants").val());
+    var alwaysHideFlag = (cj("#bypass_payment").val() == 1);
+    var total_amount_tmp =  cj('#pricevalue').data('raw-total');
+    // Hide billing questions if this is free
+    if (!alwaysShowFlag && total_amount_tmp == 0){
+      isHide = true;
+    }
+    else {
+      isHide = false;
+    }
+    if (alwaysHideFlag) {
+      isHide = true;
+    }
+    showHidePayment(isHide);
+  }
+  skipPaymentMethod();
 
   CRM.$(function($) {
     function buildPaymentBlock(type) {