}
$contactType = $contact->contact_type;
-
- if ($restore) {
- return self::contactTrashRestore($contact, TRUE);
- }
-
// currently we only clear employer cache.
- // we are not deleting inherited membership if any.
+ // we are now deleting inherited membership if any.
if ($contact->contact_type == 'Organization') {
+ $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;
+ $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id);
+ if (!empty($relationshipDtls)) {
+ foreach ($relationshipDtls as $rId => $details) {
+ CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action);
+ }
+ }
CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id);
}
+ if ($restore) {
+ return self::contactTrashRestore($contact, TRUE);
+ }
+
// start a new transaction
$transaction = new CRM_Core_Transaction();
'contactTarget' => $relationship->contact_id_b,
'contact' => $params['contact_id_a'],
);
- CRM_Contact_BAO_Relationship::relatedMemberships($params['contact_id_a'], $params, $ids, (empty($params['id']) ?
- CRM_Core_Action::ADD : CRM_Core_Action::UPDATE));
- }
- // Alter related membership if the is_active param is changed.
- if (!empty($params['id']) && array_key_exists('is_active', $params)) {
- $action = CRM_Core_Action::DISABLE;
- if (!empty($params['is_active'])) {
- $action = CRM_Core_Action::ENABLE;
- }
- CRM_Contact_BAO_Relationship::disableEnableRelationship($params['id'], $action);
+ //CRM-16087 removed additional call to function relatedMemberships which is already called by disableEnableRelationship
+ //resulting in memership being created twice
+ self::disableEnableRelationship($params['id'], $action, $params, $ids);
}
self::addRecent($params, $relationship);
*
* @param $action
*/
- public static function disableEnableRelationship($id, $action) {
+ public static function disableEnableRelationship($id, $action, $params = array(), $ids = array(), $active = FALSE) {
$relationship = self::clearCurrentEmployer($id, $action);
if (CRM_Core_Permission::access('CiviMember')) {
// create $params array which is required to delete memberships
// of the related contacts.
- $params = array(
- 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
- 'contact_check' => array($relationship->contact_id_b => 1),
- );
-
- $ids = array();
+ if (empty($params)) {
+ $params = array(
+ 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b",
+ 'contact_check' => array($relationship->contact_id_b => 1),
+ );
+ }
+ else {
+ if (array_key_exists('is_active', $params) && empty($params['is_active'])) {
+ $action = CRM_Core_Action::DISABLE;
+ }
+ else {
+ $action = CRM_Core_Action::ENABLE;
+ $active = TRUE;
+ }
+ }
+ $contact_id_a = empty($params['contact_id_a']) ? $relationship->contact_id_a : $params['contact_id_a'];
// calling relatedMemberships to delete/add the memberships of
// related contacts.
if ($action & CRM_Core_Action::DISABLE) {
- CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a,
+ CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
$params,
$ids,
CRM_Core_Action::DELETE,
- FALSE
+ $active
);
}
elseif ($action & CRM_Core_Action::ENABLE) {
$ids['contact'] = $relationship->contact_id_a;
- CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a,
+ CRM_Contact_BAO_Relationship::relatedMemberships($contact_id_a,
$params,
$ids,
- CRM_Core_Action::ADD,
- FALSE
+ empty($params['id']) ? CRM_Core_Action::ADD : CRM_Core_Action::UPDATE,
+ $active
);
}
}
$relDirection = $dao->relationship_direction;
}
$relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
- if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds)) {
- CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
+ if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds) && !empty($membershipValues['owner_membership_id'])) {
+ CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
}
continue;
}
// membership=>relationship then we need to
// delete the membership record created for
// previous relationship.
-
- if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, CRM_Utils_Array::value('relationship_ids', $params))) {
- CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
+ // CRM-16087 we need to pass ownerMembershipId to deleteRelatedMemberships function
+ if (empty($params['relationship_ids']) && !empty($params['id'])) {
+ $relIds = array($params['id']);
+ }
+ else {
+ $relIds = CRM_Utils_Array::value('relationship_ids', $params);
+ }
+ if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId,
+ $relIds
+ ) && !empty($membershipValues['owner_membership_id'])) {
+ CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
}
}
}