Skip to content

Commit

Permalink
Move household relationship types to the processor
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jul 28, 2018
1 parent 9ebfb6f commit 47df9ca
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 43 deletions.
60 changes: 19 additions & 41 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -244,7 +226,7 @@ public static function exportComponents(
$queryOperator = 'AND'
) {

$processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator);
$processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold);
$returnProperties = array();
$selectedPaymentFields = FALSE;
// @todo - this variable is overwritten later - it should be wholly definable in the processor fn.
Expand All @@ -255,9 +237,6 @@ public static function exportComponents(
// 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();

Expand Down Expand Up @@ -384,15 +363,13 @@ public static function exportComponents(

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);
Expand Down Expand Up @@ -628,8 +605,9 @@ public static function exportComponents(

// 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
Expand All @@ -641,8 +619,8 @@ public static function exportComponents(
self::writeCSVFromTable($exportTempTable, $headerRows, $sqlColumns, $exportMode);
}
else {
// return tableName and sqlColumns in test context
return array($exportTempTable, $sqlColumns);
// return tableName sqlColumns headerRows in test context
return array($exportTempTable, $sqlColumns, $headerRows);
}

// delete the export temp table and component table
Expand Down Expand Up @@ -1179,18 +1157,15 @@ public static function _buildMasterCopyArray($sql, $exportParams, $sharedAddress
*
* @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(
Expand Down Expand Up @@ -1222,9 +1197,6 @@ public static function mergeSameHousehold($exportTempTable, &$headerRows, &$sqlC
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']} != ''";
Expand Down Expand Up @@ -1429,6 +1401,9 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
elseif (substr($field, -11) == 'campaign_id') {
$headerRows[] = ts('Campaign ID');
}
elseif ($processor->isMergeSameHousehold() && $field === 'id') {
$headerRows[] = ts('Household ID');
}
elseif (isset($queryFields[$field]['title'])) {
$headerRows[] = $queryFields[$field]['title'];
}
Expand All @@ -1454,7 +1429,10 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
}
}

$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);
}
Expand Down
48 changes: 47 additions & 1 deletion CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -94,13 +101,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) {
$this->setExportMode($exportMode);
$this->setQueryMode();
$this->setQueryOperator($queryOperator);
$this->setRequestedFields($requestedFields);
$this->setRelationshipTypes();
$this->setIsMergeSameHousehold($isMergeSameHousehold);
}

/**
Expand Down Expand Up @@ -138,6 +147,34 @@ public function setRelationshipTypes() {
);
}

/**
* @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
Expand All @@ -147,6 +184,15 @@ public function isRelationshipTypeKey($fieldName) {
return array_key_exists($fieldName, $this->relationshipTypes);
}


/**
* @param $fieldName
* @return bool
*/
public function isHouseholdMergeRelationshipTypeKey($fieldName) {
return in_array($fieldName, $this->getHouseholdRelationshipTypes());
}

/**
* @return string
*/
Expand Down
13 changes: 12 additions & 1 deletion tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public function testExportRelationshipsMergeToHousehold() {
['Individual', 'city', ''],
['Individual', 'state_province', ''],
];
list($tableName) = CRM_Export_BAO_Export::exportComponents(
list($tableName, $sqlColumns, $headerRows) = CRM_Export_BAO_Export::exportComponents(
FALSE,
$this->contactIDs,
[],
Expand All @@ -452,6 +452,17 @@ public function testExportRelationshipsMergeToHousehold() {
$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);
}

/**
Expand Down

0 comments on commit 47df9ca

Please sign in to comment.