From: Dave Jenkins Date: Wed, 9 Oct 2013 21:57:41 +0000 (+0100) Subject: CRM-8338 implement CRM_Contact_Form_Task::mergeContactIdsByHousehold(), replacing... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=57884c26d08989c6a2d5f8c2cdf69d906c08a856;p=civicrm-core.git CRM-8338 implement CRM_Contact_Form_Task::mergeContactIdsByHousehold(), replacing ids of household members with the id of their household so we can merge labels by household without needing the households to be in the search results. ---------------------------------------- * CRM-8338: Mailing Label Household Merge requires Household to be selected as well as individuals http://issues.civicrm.org/jira/browse/CRM-8338 --- 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..deebe1898d 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) {