Create getter for renewalMembershipID, remove if
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 15 Mar 2023 22:04:42 +0000 (11:04 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 15 Mar 2023 22:04:42 +0000 (11:04 +1300)
CRM/Contribute/Form/ContributionBase.php

index cc1abed649bb46935d76a763670d20daa335fed2..1c3bcf8ec6856cb091ab7f05562b956cf65a337b 100644 (file)
@@ -218,6 +218,13 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
    */
   public $_ccid;
 
+  /**
+   * ID of a membership to be renewed (pass in by url)
+   *
+   * @var int
+   */
+  protected $renewalMembershipID;
+
   /**
    * Is the price set quick config.
    *
@@ -273,48 +280,48 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->_userID = CRM_Core_Session::getLoggedInContactID();
 
     $this->_contactID = $this->_membershipContactID = $this->getContactID();
-    $this->_mid = NULL;
-    if ($this->_contactID) {
-      $this->_mid = CRM_Utils_Request::retrieve('mid', 'Positive', $this);
-      if ($this->_mid) {
-        $membership = new CRM_Member_DAO_Membership();
-        $membership->id = $this->_mid;
-
-        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->_userID, 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->_userID, $membershipType->relationship_type_id);
-                if (array_key_exists($membership->contact_id, $permContacts)) {
-                  $this->_membershipContactID = $membership->contact_id;
-                  $validMembership = TRUE;
-                }
+    $this->getRenewalMembershipID();
+
+    // @todo - this mid needs to go - just making sure references are updated first.
+    $this->_mid = $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->_userID, 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->_userID, $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');
-            }
+          }
+          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);
       }
+      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);
     }
 
     // we do not want to display recently viewed items, so turn off
@@ -1289,4 +1296,22 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     return $this->getMembershipBlock() && $this->getMembershipBlock()['is_separate_payment'];
   }
 
+  /**
+   * Get the id of the membership the contact is trying to renew.
+   *
+   * @return bool|int
+   * @throws \CRM_Core_Exception
+   */
+  protected function getRenewalMembershipID() {
+    if ($this->renewalMembershipID === NULL) {
+      if (!$this->getContactID()) {
+        $this->renewalMembershipID = FALSE;
+      }
+      else {
+        $this->renewalMembershipID = CRM_Utils_Request::retrieve('mid', 'Positive', $this) ?: FALSE;
+      }
+    }
+    return $this->renewalMembershipID ?: FALSE;
+  }
+
 }