Merge pull request #1773 from davejenx/CRM-8338-merge-by-household
authordlobo <lobo@civicrm.org>
Mon, 14 Oct 2013 14:15:47 +0000 (07:15 -0700)
committerkurund <kurund@civicrm.org>
Fri, 14 Mar 2014 08:58:44 +0000 (14:28 +0530)
CRM-8338 implement CRM_Contact_Form_Task::mergeContactIdsByHousehold(), ...

CRM/Contact/Form/Task.php
CRM/Contact/Form/Task/Label.php

index fd97eba14f87f1f844ddb498eb5117a119f855cf..bd7f7215a6e5d1a6aa525f186ec9b8d3bb151bf5 100644 (file)
@@ -390,5 +390,74 @@ class CRM_Contact_Form_Task extends CRM_Core_Form {
       )
     );
   }
+
+  /**
+   * replace ids of household members in $this->_contactIds with the id of their household.
+   * CRM-8338
+   *
+   * @access public
+   *
+   * @return void
+   */
+  public function mergeContactIdsByHousehold() {
+    if (empty($this->_contactIds)) {
+      return;
+    }
+
+    $contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
+      NULL,
+      NULL,
+      NULL,
+      NULL,
+      TRUE,
+      'name',
+      FALSE
+    );
+
+    // Get Head of Household & Household Member relationships
+    $relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes);
+    $relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes);
+    $householdRelationshipTypes = array(
+      $relationKeyMOH => $contactRelationshipTypes[$relationKeyMOH],
+      $relationKeyHOH => $contactRelationshipTypes[$relationKeyHOH],
+    );
+
+    $relID = implode(',', $this->_contactIds);
+
+    foreach ($householdRelationshipTypes as $rel => $dnt) {
+      list($id, $direction) = explode('_', $rel, 2);
+      // identify the relationship direction
+      $contactA = 'contact_id_a';
+      $contactB = 'contact_id_b';
+      if ($direction == 'b_a') {
+        $contactA = 'contact_id_b';
+        $contactB = 'contact_id_a';
+      }
+
+      // Find related households.
+      $relationSelect      = "SELECT contact_household.id as household_id, {$contactA} as refContact ";
+      $relationFrom = " FROM civicrm_contact contact_household
+              INNER JOIN civicrm_relationship crel ON crel.{$contactB} = contact_household.id AND crel.relationship_type_id = {$id} ";
+
+      // Check for active relationship status only.
+      $today               = date('Ymd');
+      $relationActive      = " AND (crel.is_active = 1 AND ( crel.end_date is NULL OR crel.end_date >= {$today} ) )";
+      $relationWhere       = " WHERE contact_household.is_deleted = 0  AND crel.{$contactA} IN ( {$relID} ) {$relationActive}";
+      $relationGroupBy     = " GROUP BY crel.{$contactA}";
+      $relationQueryString = "$relationSelect $relationFrom $relationWhere $relationGroupBy";
+
+      $householdsDAO = CRM_Core_DAO::executeQuery($relationQueryString);
+      while ($householdsDAO->fetch()) {
+        // Remove contact's id from $this->_contactIds and replace with their household's id.
+        foreach (array_keys($this->_contactIds, $householdsDAO->refContact) as $idKey) {
+          unset($this->_contactIds[$idKey]);
+        }
+        if (!in_array($householdsDAO->household_id, $this->_contactIds)) {
+          $this->_contactIds[] = $householdsDAO->household_id;
+        }
+      }
+      $householdsDAO->free();
+    }
+  }
 }
 
index 5d69eb26405817249ebbfef288a9bc11d7ac3265..d36c4efd6fc1c70107725d70c8455d560c87ff31 100644 (file)
@@ -171,6 +171,17 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
       $returnProperties['last_name'] = 1;
     }
 
+    $individualFormat = FALSE;
+
+    /*
+     * CRM-8338: replace ids of household members with the id of their household
+     * so we can merge labels by household.
+     */
+    if (isset($fv['merge_same_household'])) {
+      $this->mergeContactIdsByHousehold();
+      $individualFormat = TRUE;
+    }
+
     //get the contacts information
     $params = array();
     if (CRM_Utils_Array::value('location_type_id', $fv)) {
@@ -307,15 +318,10 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
       }
     }
 
-    $individualFormat = FALSE;
     if (isset($fv['merge_same_address'])) {
       $this->mergeSameAddress($rows);
       $individualFormat = TRUE;
     }
-    if (isset($fv['merge_same_household'])) {
-      $rows = $this->mergeSameHousehold($rows);
-      $individualFormat = TRUE;
-    }
 
     // format the addresses according to CIVICRM_ADDRESS_FORMAT (CRM-1327)
     foreach ($rows as $id => $row) {
@@ -474,36 +480,5 @@ class CRM_Contact_Form_Task_Label extends CRM_Contact_Form_Task {
       $rows[$data['ID']]['addressee'] = $rows[$data['ID']]['addressee_display'] = $rows[$data['ID']]['display_name'] = $processedNames;
     }
   }
-
-  function mergeSameHousehold(&$rows) {
-    # group selected contacts by type
-    $individuals = array();
-    $households = array();
-    foreach ($rows as $contact_id => $row) {
-      if ($row['contact_type'] == 'Household') {
-        $households[$contact_id] = $row;
-      }
-      elseif ($row['contact_type'] == 'Individual') {
-        $individuals[$contact_id] = $row;
-      }
-    }
-
-    # exclude individuals belonging to selected households
-    foreach ($households as $household_id => $row) {
-      $dao = new CRM_Contact_DAO_Relationship();
-      $dao->contact_id_b = $household_id;
-      $dao->find();
-      while ($dao->fetch()) {
-        $individual_id = $dao->contact_id_a;
-        if (array_key_exists($individual_id, $individuals)) {
-          unset($individuals[$individual_id]);
-        }
-      }
-    }
-
-    # merge back individuals and households
-    $rows = array_merge($individuals, $households);
-    return $rows;
-  }
 }