From 51e89def4f84556c4fff593de83a2ee0d51120e2 Mon Sep 17 00:00:00 2001 From: Deepak Srivastava Date: Tue, 1 Oct 2013 19:20:25 +0530 Subject: [PATCH] CRM-13333 - renewal of inherited memberships other than on-behalf-of organization --- CRM/Contact/BAO/Relationship.php | 47 ++++++++++++++------ CRM/Contribute/Form/Contribution/Confirm.php | 9 ++++ CRM/Contribute/Form/ContributionBase.php | 12 ++++- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index a560b786bd..23472e6b26 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -1417,38 +1417,59 @@ WHERE id IN ( {$contacts} ) * @return array array of employers. * */ - static function getPermissionedEmployer($contactID, $name = '%') { - $employers = array(); - + static function getPermissionedEmployer($contactID, $name = NULL) { //get the relationship id $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b' ); + return self::getPermissionedContacts($contactID, $relTypeId, $name); + } + + + /** + * Function to return list of permissioned contacts for a given contact and relationship type + * + * @param $contactID int contact id whose permissioned contacts are to be found. + * @param $relTypeId relationship type id + * @param $name string + * + * @static + * + * @return array of contacts + */ + static function getPermissionedContacts($contactID, $relTypeId, $name = NULL) { + $contacts = array(); + if ($relTypeId) { $query = " SELECT cc.id as id, cc.sort_name as name FROM civicrm_relationship cr, civicrm_contact cc -WHERE cr.contact_id_a = $contactID AND -cr.relationship_type_id = $relTypeId AND -cr.is_permission_a_b = 1 AND +WHERE +cr.contact_id_a = %1 AND +cr.relationship_type_id = %2 AND +cr.is_permission_a_b = 1 AND IF(cr.end_date IS NULL, 1, (DATEDIFF( CURDATE( ), cr.end_date ) <= 0)) AND cr.is_active = 1 AND -cc.id = cr.contact_id_b AND -cc.sort_name LIKE '%$name%'"; +cc.id = cr.contact_id_b"; + + if (!empty($name)) { + $name = CRM_Utils_Type::escape($name, 'String'); + $query .= " +AND cc.sort_name LIKE '%$name%'"; + } - $nullArray = array(); - $dao = CRM_Core_DAO::executeQuery($query, $nullArray); + $args = array(1 => array($contactID, 'Integer'), 2 => array($relTypeId, 'Integer')); + $dao = CRM_Core_DAO::executeQuery($query, $args); while ($dao->fetch()) { - $employers[$dao->id] = array( + $contacts[$dao->id] = array( 'name' => $dao->name, 'value' => $dao->id, ); } } - - return $employers; + return $contacts; } static function getValidContactTypeList($relType) { diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 8e7ee1e63d..487d93067c 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -822,6 +822,15 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr ); } + if (!empty($this->_membershipContactID) && $contactID != $this->_membershipContactID) { + // this is an onbehalf renew case for inherited membership. For e.g a permissioned member of household, + // store current user id as related contact for later use for mailing / activity.. + $this->_values['related_contact'] = $contactID; + $this->_params['related_contact'] = $contactID; + // swap contact like we do for on-behalf-org case, so parent/primary membership is affected + $contactID = $this->_membershipContactID; + } + // lets store the contactID in the session // for things like tell a friend $session = CRM_Core_Session::singleton(); diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index d2d853e532..1891b09e7a 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -237,7 +237,17 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } else { - $this->_membershipContactID = $membership->contact_id; + $membershipType = new CRM_Member_BAO_MembershipType(); + $membershipType->id = $membership->membership_type_id; + if ($membershipType->find(TRUE)) { + $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; + } + 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'); + } + } } } } -- 2.25.1