From 3df07b08fc3b1b89f33096cfccf4958a5360d6ac Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 16 Mar 2023 11:04:42 +1300 Subject: [PATCH] Create getter for renewalMembershipID, remove if --- CRM/Contribute/Form/ContributionBase.php | 99 +++++++++++++++--------- 1 file changed, 62 insertions(+), 37 deletions(-) diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index cc1abed649..1c3bcf8ec6 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -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; + } + } -- 2.25.1