dev/core#4953 Fix failure to show existing membership type in renewal mode
authoreileen <emcnaughton@wikimedia.org>
Fri, 2 Feb 2024 03:03:22 +0000 (16:03 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 8 Feb 2024 04:39:00 +0000 (17:39 +1300)
The previous fix dealt with js layer issues but this affects select boxes in the php layer

Also fixed 2 smarty notices that turned out to not be related

CRM/Contribute/Form/Contribution/Main.php

index baf5578e3d723313e5c99f21d2244e173869dfec..2ad6789084d8dc40925fbaf667b597c339d8fbe9 100644 (file)
@@ -24,7 +24,10 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
   /**
    * Define default MembershipType Id.
+   *
    * @var int
+   *
+   * @deprecated unused
    */
   public $_defaultMemTypeId;
 
@@ -239,14 +242,14 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
     $memtypeID = NULL;
     if ($this->_priceSetId) {
-      if (($this->isMembershipPriceSet() && !$this->isDefined('CurrentMembership')) || $this->_defaultMemTypeId) {
+      if ($this->isMembershipPriceSet()) {
         $selectedCurrentMemTypes = [];
         foreach ($this->_priceSet['fields'] as $key => $val) {
           foreach ($val['options'] as $keys => $values) {
             $opMemTypeId = $values['membership_type_id'] ?? NULL;
             $priceFieldName = 'price_' . $values['price_field_id'];
             $priceFieldValue = CRM_Price_BAO_PriceSet::getPriceFieldValueFromURL($this, $priceFieldName);
-            if (!empty($priceFieldValue)) {
+            if (!empty($priceFieldValue) && !$this->isDefined('RenewableMembership')) {
               CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $priceFieldValue, $val['html_type'], $this->_defaults);
               // break here to prevent overwriting of default due to 'is_default'
               // option configuration or setting of current membership or
@@ -254,15 +257,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
               // The value sent via URL get's higher priority.
               break;
             }
-            elseif ($opMemTypeId &&
-              !empty($this->getExistingMemberships()[$opMemTypeId]) &&
+            if ($opMemTypeId &&
+              // @todo - maybe use the defined renewable membership to avoid lifetime memberships.
+              !empty($this->getExistingMembership($opMemTypeId)) &&
               !in_array($opMemTypeId, $selectedCurrentMemTypes)
             ) {
               CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
               $memtypeID = $selectedCurrentMemTypes[] = $values['membership_type_id'];
             }
             elseif (!empty($values['is_default']) && !$opMemTypeId && (!isset($this->_defaults[$priceFieldName]) ||
-              ($val['html_type'] == 'CheckBox' && !isset($this->_defaults[$priceFieldName][$keys])))) {
+              ($val['html_type'] === 'CheckBox' && !isset($this->_defaults[$priceFieldName][$keys])))) {
               CRM_Price_BAO_PriceSet::setDefaultPriceSetField($priceFieldName, $keys, $val['html_type'], $this->_defaults);
               $memtypeID = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $this->_defaults[$priceFieldName], 'membership_type_id');
             }
@@ -338,6 +342,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     }
 
     $this->applyFilter('__ALL__', 'trim');
+    $this->assign('showMainEmail', !empty($this->_ccid));
     if (empty($this->_ccid)) {
       if ($this->_emailExists == FALSE) {
         $this->add('text', "email-{$this->_bltID}",
@@ -345,7 +350,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
           ['size' => 30, 'maxlength' => 60, 'class' => 'email'],
           TRUE
         );
-        $this->assign('showMainEmail', TRUE);
         $this->addRule("email-{$this->_bltID}", ts('Email is not valid.'), 'email');
       }
     }
@@ -511,6 +515,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
   private function buildPriceSet($form) {
     $validPriceFieldIds = array_keys($this->getPriceFieldMetaData());
     $form->assign('priceSet', $form->_priceSet);
+    $this->assign('membershipFieldID');
 
     // @todo - this hook wrangling can be done earlier if we set the form on $this->>order.
     $feeBlock = &$form->_values['fee'];
@@ -682,7 +687,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
               $this->assign('hasExistingLifetimeMembership', TRUE);
               continue;
             }
-            $this->define('Membership', 'CurrentMembership', $membership);
+            $this->define('Membership', 'RenewableMembership', $membership);
             $memType['current_membership'] = $membership['end_date'];
             if (!$endDate) {
               $endDate = $memType['current_membership'];