Skip to content

Commit

Permalink
Merge pull request #12272 from eileenmcnaughton/export_house
Browse files Browse the repository at this point in the history
Towards fixing household merge export, extract function, add test, fix prev
  • Loading branch information
monishdeb authored Jun 7, 2018
2 parents a57aaa0 + 81649c4 commit c898924
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 52 deletions.
115 changes: 67 additions & 48 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,68 @@ class CRM_Export_BAO_Export {
// CRM-7675
const EXPORT_ROW_COUNT = 100000;

protected static $relationshipReturnProperties = [];

/**
* @param $value
* @param $locationTypeFields
* @param $relationshipTypes
*
* @return array
*/
protected static function setRelationshipReturnProperties($value, $locationTypeFields, $relationshipTypes) {
$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$relPhoneTypeId = $relIMProviderId = NULL;
if (!empty($value[2])) {
$relationField = CRM_Utils_Array::value(2, $value);
if (trim(CRM_Utils_Array::value(3, $value))) {
$relLocTypeId = CRM_Utils_Array::value(3, $value);
}
else {
$relLocTypeId = 'Primary';
}

if ($relationField == 'phone') {
$relPhoneTypeId = CRM_Utils_Array::value(4, $value);
}
elseif ($relationField == 'im') {
$relIMProviderId = CRM_Utils_Array::value(4, $value);
}
}
elseif (!empty($value[4])) {
$relationField = CRM_Utils_Array::value(4, $value);
$relLocTypeId = CRM_Utils_Array::value(5, $value);
if ($relationField == 'phone') {
$relPhoneTypeId = CRM_Utils_Array::value(6, $value);
}
elseif ($relationField == 'im') {
$relIMProviderId = CRM_Utils_Array::value(6, $value);
}
}
if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) {
if ($relPhoneTypeId) {
self::$relationshipReturnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1;
}
elseif ($relIMProviderId) {
self::$relationshipReturnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['im-' . $relIMProviderId] = 1;
}
else {
self::$relationshipReturnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1;
}
}
else {
self::$relationshipReturnProperties[$relationshipTypes][$relationField] = 1;
}
return array($relationField);
}

/**
* @return array
*/
public static function getRelationshipReturnProperties() {
return self::relationshipReturnProperties;
}

/**
* Get Querymode based on ExportMode
*
Expand Down Expand Up @@ -348,58 +410,15 @@ public static function exportComponents(
);

foreach ($fields as $key => $value) {
$relationField = NULL;
$relationshipTypes = $fieldName = CRM_Utils_Array::value(1, $value);
$fieldName = CRM_Utils_Array::value(1, $value);
if (!$fieldName) {
continue;
}

if (array_key_exists($relationshipTypes, $contactRelationshipTypes) && (!empty($value[2]) || empty($value[4]))) {
$relPhoneTypeId = $relIMProviderId = NULL;
if (!empty($value[2])) {
$relationField = CRM_Utils_Array::value(2, $value);
if (trim(CRM_Utils_Array::value(3, $value))) {
$relLocTypeId = CRM_Utils_Array::value(3, $value);
}
else {
$relLocTypeId = 'Primary';
}

if ($relationField == 'phone') {
$relPhoneTypeId = CRM_Utils_Array::value(4, $value);
}
elseif ($relationField == 'im') {
$relIMProviderId = CRM_Utils_Array::value(4, $value);
}
}
elseif (!empty($value[4])) {
$relationField = CRM_Utils_Array::value(4, $value);
$relLocTypeId = CRM_Utils_Array::value(5, $value);
if ($relationField == 'phone') {
$relPhoneTypeId = CRM_Utils_Array::value(6, $value);
}
elseif ($relationField == 'im') {
$relIMProviderId = CRM_Utils_Array::value(6, $value);
}
}
if (in_array($relationField, $locationTypeFields) && is_numeric($relLocTypeId)) {
if ($relPhoneTypeId) {
$returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['phone-' . $relPhoneTypeId] = 1;
}
elseif ($relIMProviderId) {
$returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]]['im-' . $relIMProviderId] = 1;
}
else {
$returnProperties[$relationshipTypes]['location'][$locationTypes[$relLocTypeId]][$relationField] = 1;
}
}
else {
$returnProperties[$relationshipTypes][$relationField] = 1;
}
}

if ($relationField) {
// already handled.
if (array_key_exists($fieldName, $contactRelationshipTypes) && (!empty($value[2]) || !empty($value[4]))) {
self::setRelationshipReturnProperties($value, $locationTypeFields, $fieldName);
// @todo we can later not add this to this array but maintain a separate array.
$returnProperties = array_merge($returnProperties, self::$relationshipReturnProperties);
}
elseif (is_numeric(CRM_Utils_Array::value(2, $value))) {
$locTypeId = $value[2];
Expand Down
9 changes: 5 additions & 4 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function testExportPseudoFieldCampaign() {
*/
public function testExportRelationshipsMergeToHousehold() {
$this->setUpContactExportData();
$householdID = $this->householdCreate(['city' => 'Portland', 'state_province_id' => 'Oregan']);
$householdID = $this->householdCreate(['api.Address.create' => ['city' => 'Portland', 'state_province_id' => 'Maine', 'location_type_id' => 'Home']]);

$relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values'];
$houseHoldTypeID = NULL;
Expand Down Expand Up @@ -420,9 +420,10 @@ public function testExportRelationshipsMergeToHousehold() {
);
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM {$tableName}");
while ($dao->fetch()) {
// Do some checks here
// $this->assertEquals('Portland', $dao->city);
// $this->assertEquals('Oregan', $dao0>state_province);
$this->assertEquals('Portland', $dao->city);
$this->assertEquals('ME', $dao->state_province);
$this->assertEquals($householdID, $dao->civicrm_primary_id);
$this->assertEquals($householdID, $dao->civicrm_primary_id);
}

}
Expand Down

0 comments on commit c898924

Please sign in to comment.