CRM-15829 and CRM-15881 for CiviCRM 4.6
authorMarco Valente <marco@compucorp.co.uk>
Tue, 9 Jun 2015 09:53:45 +0000 (10:53 +0100)
committerMarco Valente <marco@compucorp.co.uk>
Tue, 9 Jun 2015 09:53:45 +0000 (10:53 +0100)
-----------------------------------

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
CRM/Contact/Form/Relationship.php

index 47f69f6e66654f26acbc2a53ce5f5ac1ae1b7baa..7ae20ffdbc4622cedb5e022a5b4db39a9236e6ff 100644 (file)
@@ -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;
index badd762924c8215320ed054d43f023dac1fb1226..96c5ca72af6e9ec695c07412d07408af23680259 100644 (file)
@@ -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');