From bd655ee661460004e8969391c1d433408085a8f6 Mon Sep 17 00:00:00 2001 From: Marco Valente Date: Tue, 9 Jun 2015 10:53:45 +0100 Subject: [PATCH] CRM-15829 and CRM-15881 for CiviCRM 4.6 ----------------------------------- CRM-15829 (Relationships and inherited pending memberships) Add pending memberships to new members of an organisation that has pending memberships CRM/Contact/BAO/Relationship -> line ~1458 CRM/Contact/BAO/Relationship -> line ~1479 CRM/Contact/BAO/Relationship -> line ~1483 Ensure these new pending memberships for an organisation's employees are not set to active CRM/Member/BAO/Membership -> line 265 ----------------------------------- CRM-15881 (Contact relationships (employee of)) Clear current employee when deleting a relationship that is the employer CRM/Contact/BAO/Relationship -> line ~623 Reload the tab ajax views to reflect removed employer display when deleting a relationship that is the employer CRM/Contact/Form/Relationship -> line ~399 If disabling an employer relationship (via the popup modal), unset the is_current_employer, please see comments in code CRM/Contact/Form/Relationship -> line ~444 --- CRM/Contact/BAO/Relationship.php | 37 +++++++++++++++++++++++++++++-- CRM/Contact/Form/Relationship.php | 13 +++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 47f69f6e66..7ae20ffdbc 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -620,7 +620,10 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { $sharedContact->id = $relationship->contact_id_a; $sharedContact->find(TRUE); - if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) { + // CRM-15881 UPDATES + // changed FROM "...relationship->relationship_type_id == 4..." TO "...relationship->relationship_type_id == 5..." + // As the system should be looking for type "employer of" (id 5) and not "sibling of" (id 4) + if ($relationship->relationship_type_id == 5 && $relationship->contact_id_b == $sharedContact->employer_id) { CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a); } } @@ -1452,6 +1455,20 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) } } + // CRM-15829 UPDATES + // If we're looking for active memberships we must consider pending (id: 5) ones too. + // Hence we can't just call CRM_Member_BAO_Membership::getValues below with the active flag, is it would completely miss pending relatioships. + + $query = 'SELECT * FROM `civicrm_membership_status`'; + if ($active) { + $query .= 'WHERE `is_current_member` = 1 OR `id` = 5'; + } + + $dao = CRM_Core_DAO::executeQuery($query); + while ($dao->fetch()) { + $membershipStatusRecordIds[$dao->id] = $dao->id; + } + // Now get the active memberships for all the contacts. // If contact have any valid membership(s), then add it to // 'values' array. @@ -1459,7 +1476,23 @@ LEFT JOIN civicrm_country ON (civicrm_address.country_id = civicrm_country.id) $memParams = array('contact_id' => $cid); $memberships = array(); - CRM_Member_BAO_Membership::getValues($memParams, $memberships, $active, TRUE); + // CRM-15829 UPDATES + // Since we want PENDING memberships as well, the $active flag needs to be set to false so that this will return all memberships and we can then filter the memberships based on the status IDs recieved above. + CRM_Member_BAO_Membership::getValues($memParams, $memberships, FALSE, TRUE); + + // CRM-15829 UPDATES + // filter out the memberships returned by CRM_Member_BAO_Membership::getValues based on the status IDs fetched on line ~1462 + foreach ($memberships as $key => $membership) { + + if (!isset($memberships[$key]['status_id'])) { + continue; + } + + $membershipStatusId = $memberships[$key]['status_id']; + if (!isset($membershipStatusRecordIds[$membershipStatusId])) { + unset($memberships[$key]); + } + } if (empty($memberships)) { continue; diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index badd762924..96c5ca72af 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -395,6 +395,11 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { // action is taken depending upon the mode if ($this->_action & CRM_Core_Action::DELETE) { CRM_Contact_BAO_Relationship::del($this->_relationshipId); + + // CRM-15881 UPDATES + // Since the line above nullifies the organization_name and employer_id fiels in the contact record, we need to reload all blocks to reflect this chage on the user interface. + $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); + return; } @@ -437,6 +442,14 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { // clear the current employer. CRM-3235. $relChanged = $params['relationship_type_id'] != $this->_values['relationship_type_id']; if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) { + + // CRM-15881 UPDATES + // If not is_active then is_current_employer needs to be set false as well! Logically a contact cannot be a current employee of a disabled employer relationship. + // If this is not done, then the below process will go ahead and disable the organization_name and employer_id fields in the contact record (which is what is wanted) but then further down will be re-enabled becuase is_current_employer is not false, therefore undoing what was done correctly. + if (!$params['is_active']) { + $params['is_current_employer'] = FALSE; + } + CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']); // Refresh contact summary if in ajax mode $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content'); -- 2.25.1