From b7db6051472ce9f259171dfead637b8d15ed50ae Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 27 Jul 2018 01:26:20 +1200 Subject: [PATCH] Move household relationship types to the processor m --- CRM/Export/BAO/Export.php | 58 +++++++-------------- CRM/Export/BAO/ExportProcessor.php | 48 ++++++++++++++++- tests/phpunit/CRM/Export/BAO/ExportTest.php | 13 ++++- 3 files changed, 77 insertions(+), 42 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index baaacb4b92..97a2a33d15 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -41,24 +41,6 @@ class CRM_Export_BAO_Export { // CRM-7675 const EXPORT_ROW_COUNT = 100000; - /** - * Key representing the head of household in the relationship array. - * - * e.g. 8_a_b. - * - * @var string - */ - protected static $headOfHouseholdRelationshipKey; - - /** - * Key representing the head of household in the relationship array. - * - * e.g. 8_a_b. - * - * @var string - */ - protected static $memberOfHouseholdRelationshipKey; - /** * Key representing the head of household in the relationship array. * @@ -244,7 +226,7 @@ class CRM_Export_BAO_Export { $queryOperator = 'AND' ) { - $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator); + $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold); $returnProperties = array(); $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); @@ -252,9 +234,6 @@ class CRM_Export_BAO_Export { // without manually testing the export of IM provider still works. $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); self::$relationshipTypes = $processor->getRelationshipTypes(); - //also merge Head of Household - self::$memberOfHouseholdRelationshipKey = CRM_Utils_Array::key('Household Member of', self::$relationshipTypes); - self::$headOfHouseholdRelationshipKey = CRM_Utils_Array::key('Head of Household for', self::$relationshipTypes); $queryMode = $processor->getQueryMode(); @@ -376,15 +355,13 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c foreach ($returnProperties as $key => $value) { if (!$processor->isRelationshipTypeKey($key)) { - $returnProperties[self::$memberOfHouseholdRelationshipKey][$key] = $value; - $returnProperties[self::$headOfHouseholdRelationshipKey][$key] = $value; + foreach ($processor->getHouseholdRelationshipTypes() as $householdRelationshipType) { + if (!in_array($key, ['location_type', 'im_provider'])) { + $returnProperties[$householdRelationshipType][$key] = $value; + } + } } } - - unset($returnProperties[self::$memberOfHouseholdRelationshipKey]['location_type']); - unset($returnProperties[self::$memberOfHouseholdRelationshipKey]['im_provider']); - unset($returnProperties[self::$headOfHouseholdRelationshipKey]['location_type']); - unset($returnProperties[self::$headOfHouseholdRelationshipKey]['im_provider']); } list($relationQuery, $allRelContactArray) = self::buildRelatedContactArray($selectAll, $ids, $exportMode, $componentTable, $returnProperties, $queryMode); @@ -620,8 +597,9 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c // merge the records if they have corresponding households if ($mergeSameHousehold) { - self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, self::$memberOfHouseholdRelationshipKey); - self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, self::$headOfHouseholdRelationshipKey); + foreach ($processor->getHouseholdRelationshipTypes() as $householdRelationshipType) { + self::mergeSameHousehold($exportTempTable, $sqlColumns, $householdRelationshipType); + } } // call export hook @@ -633,7 +611,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode); } else { - // return tableName and sqlColumns in test context + // return tableName sqlColumns headerRows in test context return array($exportTempTable, $sqlColumns, $headerRows); } @@ -1171,18 +1149,15 @@ WHERE id IN ( $deleteIDString ) * * @param string $exportTempTable * Temporary temp table that stores the records. - * @param array $headerRows - * Array of headers for the export file. * @param array $sqlColumns * Array of names of the table columns of the temp table. * @param string $prefix * Name of the relationship type that is prefixed to the table columns. */ - public static function mergeSameHousehold($exportTempTable, &$headerRows, &$sqlColumns, $prefix) { + public static function mergeSameHousehold($exportTempTable, &$sqlColumns, $prefix) { $prefixColumn = $prefix . '_'; $allKeys = array_keys($sqlColumns); $replaced = array(); - $headerRows = array_values($headerRows); // name map of the non standard fields in header rows & sql columns $mappingFields = array( @@ -1214,9 +1189,6 @@ WHERE id IN ( $deleteIDString ) foreach ($replaced as $from => $to) { $clause[] = "$from = $to "; unset($sqlColumns[$to]); - if ($key = CRM_Utils_Array::key($to, $allKeys)) { - unset($headerRows[$key]); - } } $query .= implode(",\n", $clause); $query .= " WHERE {$replaced['civicrm_primary_id']} != ''"; @@ -1415,6 +1387,9 @@ WHERE {$whereClause}"; // @todo - set this correctly in the xml rather than here. $headerRows[] = ts('Campaign ID'); } + elseif ($processor->isMergeSameHousehold() && $field === 'id') { + $headerRows[] = ts('Household ID'); + } elseif (isset($queryFields[$field]['title'])) { $headerRows[] = $queryFields[$field]['title']; } @@ -1438,7 +1413,10 @@ WHERE {$whereClause}"; } } - $headerRows[] = $headerName; + if (!$processor->isHouseholdMergeRelationshipTypeKey($field)) { + // Do not add to header row if we are only generating for merge reasons. + $headerRows[] = $headerName; + } self::sqlColumnDefn($processor, $sqlColumns, $headerName); } diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index d25fefcb8d..6c82c5ce85 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -72,6 +72,13 @@ class CRM_Export_BAO_ExportProcessor { */ protected $requestedFields; + /** + * Is the contact being merged into a single household. + * + * @var bool + */ + protected $isMergeSameHousehold; + /** * Key representing the head of household in the relationship array. * @@ -99,13 +106,15 @@ class CRM_Export_BAO_ExportProcessor { * @param int $exportMode * @param array|NULL $requestedFields * @param string $queryOperator + * @param bool $isMergeSameHousehold */ - public function __construct($exportMode, $requestedFields, $queryOperator) { + public function __construct($exportMode, $requestedFields, $queryOperator, $isMergeSameHousehold = FALSE) { $this->setExportMode($exportMode); $this->setQueryMode(); $this->setQueryOperator($queryOperator); $this->setRequestedFields($requestedFields); $this->setRelationshipTypes(); + $this->setIsMergeSameHousehold($isMergeSameHousehold); } /** @@ -158,6 +167,34 @@ class CRM_Export_BAO_ExportProcessor { ); } + /** + * @return bool + */ + public function isMergeSameHousehold() { + return $this->isMergeSameHousehold; + } + + /** + * @param bool $isMergeSameHousehold + */ + public function setIsMergeSameHousehold($isMergeSameHousehold) { + $this->isMergeSameHousehold = $isMergeSameHousehold; + } + + /** + * Return relationship types for household merge. + * + * @return mixed + */ + public function getHouseholdRelationshipTypes() { + if (!$this->isMergeSameHousehold()) { + return []; + } + return [ + CRM_Utils_Array::key('Household Member of', $this->getRelationshipTypes()), + CRM_Utils_Array::key('Head of Household for', $this->getRelationshipTypes()), + ]; + } /** * @param $fieldName @@ -167,6 +204,15 @@ class CRM_Export_BAO_ExportProcessor { return array_key_exists($fieldName, $this->relationshipTypes); } + + /** + * @param $fieldName + * @return bool + */ + public function isHouseholdMergeRelationshipTypeKey($fieldName) { + return in_array($fieldName, $this->getHouseholdRelationshipTypes()); + } + /** * @return string */ diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 4d1631b72c..7fe095b7f1 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -427,7 +427,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { ['Individual', 'city', ''], ['Individual', 'state_province', ''], ]; - list($tableName) = CRM_Export_BAO_Export::exportComponents( + list($tableName, $sqlColumns, $headerRows) = CRM_Export_BAO_Export::exportComponents( FALSE, $this->contactIDs, [], @@ -452,6 +452,17 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->assertEquals($householdID, $dao->civicrm_primary_id); } + $this->assertEquals([ + 0 => 'City', + 1 => 'State', + 2 => 'Household ID', + ], $headerRows); + $this->assertEquals( + [ + 'city' => 'city varchar(64)', + 'state_province' => 'state_province varchar(64)', + 'civicrm_primary_id' => 'civicrm_primary_id varchar(16)', + ], $sqlColumns); } /** -- 2.25.1