CRM-8338 implement CRM_Contact_Form_Task::mergeContactIdsByHousehold(), replacing...
authorDave Jenkins <davej+git@circle-interactive.co.uk>
Wed, 9 Oct 2013 21:57:41 +0000 (22:57 +0100)
committerDave Jenkins <davej+git@circle-interactive.co.uk>
Wed, 9 Oct 2013 21:57:41 +0000 (22:57 +0100)
----------------------------------------
* CRM-8338: Mailing Label Household Merge requires Household to be selected as well as individuals
  http://issues.civicrm.org/jira/browse/CRM-8338

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..deebe1898db27cf6e97d383cd66edb191c383d72 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) {