Extract code that defines the renewal membership
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Nov 2023 21:16:27 +0000 (10:16 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 23 Nov 2023 21:16:27 +0000 (10:16 +1300)
CRM/Contribute/Form/ContributionBase.php

index 236cfde3974621179c2cce65e6194bd92e87c840..0b86afebb5b4bc751ba4cb69b222892117cb2a21 100644 (file)
@@ -305,46 +305,9 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->_emailExists = $this->get('emailExists');
 
     $this->_contactID = $this->_membershipContactID = $this->getContactID();
-    $this->getRenewalMembershipID();
 
     if ($this->getRenewalMembershipID()) {
-      $membership = new CRM_Member_DAO_Membership();
-      $membership->id = $this->getRenewalMembershipID();
-
-      if ($membership->find(TRUE)) {
-        $this->_defaultMemTypeId = $membership->membership_type_id;
-        if ($membership->contact_id != $this->_contactID) {
-          $validMembership = FALSE;
-          $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->getAuthenticatedContactID(), NULL, NULL, 'Organization');
-          if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) {
-            $this->_membershipContactID = $membership->contact_id;
-            $this->assign('membershipContactID', $this->_membershipContactID);
-            $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']);
-            $validMembership = TRUE;
-          }
-          else {
-            $membershipType = new CRM_Member_BAO_MembershipType();
-            $membershipType->id = $membership->membership_type_id;
-            if ($membershipType->find(TRUE)) {
-              // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
-              // Convert to comma separated list.
-              $inheritedRelTypes = implode(',', CRM_Utils_Array::explodePadded($membershipType->relationship_type_id));
-              $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->getAuthenticatedContactID(), $membershipType->relationship_type_id);
-              if (array_key_exists($membership->contact_id, $permContacts)) {
-                $this->_membershipContactID = $membership->contact_id;
-                $validMembership = TRUE;
-              }
-            }
-          }
-          if (!$validMembership) {
-            CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
-          }
-        }
-      }
-      else {
-        CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
-      }
-      unset($membership);
+      $this->defineRenewalMembership();
     }
 
     // we do not want to display recently viewed items, so turn off
@@ -1428,4 +1391,43 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->order->setForm($this);
   }
 
+  protected function defineRenewalMembership(): void {
+    $membership = new CRM_Member_DAO_Membership();
+    $membership->id = $this->getRenewalMembershipID();
+
+    if ($membership->find(TRUE)) {
+      $this->_defaultMemTypeId = $membership->membership_type_id;
+      if ($membership->contact_id != $this->_contactID) {
+        $validMembership = FALSE;
+        $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->getAuthenticatedContactID(), NULL, NULL, 'Organization');
+        if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) {
+          $this->_membershipContactID = $membership->contact_id;
+          $this->assign('membershipContactID', $this->_membershipContactID);
+          $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']);
+          $validMembership = TRUE;
+        }
+        else {
+          $membershipType = new CRM_Member_BAO_MembershipType();
+          $membershipType->id = $membership->membership_type_id;
+          if ($membershipType->find(TRUE)) {
+            // CRM-14051 - membership_type.relationship_type_id is a CTRL-A padded string w one or more ID values.
+            // Convert to comma separated list.
+            $inheritedRelTypes = implode(',', CRM_Utils_Array::explodePadded($membershipType->relationship_type_id));
+            $permContacts = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->getAuthenticatedContactID(), $membershipType->relationship_type_id);
+            if (array_key_exists($membership->contact_id, $permContacts)) {
+              $this->_membershipContactID = $membership->contact_id;
+              $validMembership = TRUE;
+            }
+          }
+        }
+        if (!$validMembership) {
+          CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
+        }
+      }
+    }
+    else {
+      CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Membership Invalid'), 'alert');
+    }
+  }
+
 }