From 24ab7a35f0058034f7a3e33c4fbebe908cba9a65 Mon Sep 17 00:00:00 2001 From: dlobo Date: Mon, 14 Oct 2013 07:15:47 -0700 Subject: [PATCH] Merge pull request #1773 from davejenx/CRM-8338-merge-by-household CRM-8338 implement CRM_Contact_Form_Task::mergeContactIdsByHousehold(), ... --- CRM/Contact/Form/Task.php | 69 +++++++++++++++++++++++++++++++++ CRM/Contact/Form/Task/Label.php | 47 ++++++---------------- 2 files changed, 80 insertions(+), 36 deletions(-) diff --git a/CRM/Contact/Form/Task.php b/CRM/Contact/Form/Task.php index fd97eba14f..bd7f7215a6 100644 --- a/CRM/Contact/Form/Task.php +++ b/CRM/Contact/Form/Task.php @@ -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(); + } + } } diff --git a/CRM/Contact/Form/Task/Label.php b/CRM/Contact/Form/Task/Label.php index 5d69eb2640..d36c4efd6f 100644 --- a/CRM/Contact/Form/Task/Label.php +++ b/CRM/Contact/Form/Task/Label.php @@ -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; - } } -- 2.25.1