Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export minor refactor Move household relationship types to the processor #12579

Merged
merged 2 commits into from
Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 29 additions & 50 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 @@ -140,10 +122,8 @@ public static function exportComponent($exportMode) {

/**
* Get Query Group By Clause
* @param int $exportMode
* @param \CRM_Export_BAO_ExportProcessor $processor
* Export Mode
* @param string $queryMode
* Query Mode
* @param array $returnProperties
* Return Properties
* @param object $query
Expand All @@ -152,8 +132,10 @@ public static function exportComponent($exportMode) {
* @return string $groupBy
* Group By Clause
*/
public static function getGroupBy($exportMode, $queryMode, $returnProperties, $query) {
public static function getGroupBy($processor, $returnProperties, $query) {
$groupBy = '';
$exportMode = $processor->getExportMode();
$queryMode = $processor->getQueryMode();
if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) ||
CRM_Utils_Array::value('notes', $returnProperties) ||
// CRM-9552
Expand Down Expand Up @@ -244,17 +226,14 @@ 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();

$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
// Warning - this imProviders var is used in a somewhat fragile way - don't rename it
// 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 @@ -376,18 +355,16 @@ 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);
list($relationQuery, $allRelContactArray) = self::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable, $returnProperties);

// make sure the groups stuff is included only if specifically specified
// by the fields param (CRM-1969), else we limit the contacts outputted to only
Expand Down Expand Up @@ -432,7 +409,7 @@ public static function exportComponents(

$queryString = "$select $from $where $having";

$groupBy = self::getGroupBy($exportMode, $queryMode, $returnProperties, $query);
$groupBy = self::getGroupBy($processor, $returnProperties, $query);

$queryString .= $groupBy;

Expand Down Expand Up @@ -620,8 +597,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 @@ -633,7 +611,7 @@ public static function exportComponents(
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);
}

Expand Down Expand Up @@ -1171,18 +1149,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 @@ -1214,9 +1189,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]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
$query .= implode(",\n", $clause);
$query .= " WHERE {$replaced['civicrm_primary_id']} != ''";
Expand Down Expand Up @@ -1415,6 +1387,9 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
// @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'];
}
Expand All @@ -1438,7 +1413,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 Expand Up @@ -1723,15 +1701,16 @@ protected static function getIDsForRelatedContact($ids, $exportMode) {
/**
* @param $selectAll
* @param $ids
* @param $exportMode
* @param \CRM_Export_BAO_ExportProcessor $processor
* @param $componentTable
* @param $returnProperties
* @param $queryMode
*
* @return array
*/
protected static function buildRelatedContactArray($selectAll, $ids, $exportMode, $componentTable, $returnProperties, $queryMode) {
protected static function buildRelatedContactArray($selectAll, $ids, $processor, $componentTable, $returnProperties) {
$allRelContactArray = $relationQuery = array();

$queryMode = $processor->getQueryMode();
$exportMode = $processor->getExportMode();
foreach (self::$relationshipTypes as $rel => $dnt) {
if ($relationReturnProperties = CRM_Utils_Array::value($rel, $returnProperties)) {
$allRelContactArray[$rel] = array();
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 Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -158,6 +167,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 @@ -167,6 +204,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