Skip to content

Commit

Permalink
Export class, another function extraction & reduction of variable pas…
Browse files Browse the repository at this point in the history
…sing as part of code readability
  • Loading branch information
eileenmcnaughton committed Jun 9, 2018
1 parent 5f5b9c2 commit 47e0bdd
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 68 deletions.
131 changes: 84 additions & 47 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ class CRM_Export_BAO_Export {

protected static $relationshipReturnProperties = [];

/**
* 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.
*
* e.g. ['8_b_a' => 'Household Member Is', '8_a_b = 'Household Member Of'.....]
*
* @var
*/
protected static $relationshipTypes = [];

/**
* @param $value
* @param $locationTypeFields
Expand Down Expand Up @@ -377,7 +404,7 @@ public static function exportComponents(
// 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');
$contactRelationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
self::$relationshipTypes = CRM_Contact_BAO_Relationship::getContactRelationshipType(
NULL,
NULL,
NULL,
Expand All @@ -386,6 +413,9 @@ public static function exportComponents(
'name',
FALSE
);
//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 = self::getQueryMode($exportMode);

Expand Down Expand Up @@ -415,7 +445,7 @@ public static function exportComponents(
continue;
}

if (array_key_exists($fieldName, $contactRelationshipTypes) && (!empty($value[2]) || !empty($value[4]))) {
if (array_key_exists($fieldName, self::$relationshipTypes) && (!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);
Expand Down Expand Up @@ -584,26 +614,22 @@ public static function exportComponents(
$returnProperties['id'] = 1;
}

//also merge Head of Household
$relationKeyMOH = CRM_Utils_Array::key('Household Member of', $contactRelationshipTypes);
$relationKeyHOH = CRM_Utils_Array::key('Head of Household for', $contactRelationshipTypes);

foreach ($returnProperties as $key => $value) {
if (!array_key_exists($key, $contactRelationshipTypes)) {
$returnProperties[$relationKeyMOH][$key] = $value;
$returnProperties[$relationKeyHOH][$key] = $value;
if (!array_key_exists($key, self::$relationshipTypes) && !in_array($key, [
'location_type',
'im_provider',
'location_type',
'im_provider',
])) {
$returnProperties[self::$headOfHouseholdRelationshipKey][$key] = $value;
$returnProperties[self::$memberOfHouseholdRelationshipKey][$key] = $value;
}
}

unset($returnProperties[$relationKeyMOH]['location_type']);
unset($returnProperties[$relationKeyMOH]['im_provider']);
unset($returnProperties[$relationKeyHOH]['location_type']);
unset($returnProperties[$relationKeyHOH]['im_provider']);
}

$allRelContactArray = $relationQuery = array();

foreach ($contactRelationshipTypes as $rel => $dnt) {
foreach (array_keys(self::$relationshipTypes) as $rel) {
if ($relationReturnProperties = CRM_Utils_Array::value($rel, $returnProperties)) {
$allRelContactArray[$rel] = array();
// build Query for each relationship
Expand All @@ -620,31 +646,7 @@ public static function exportComponents(
$contactA = 'contact_id_b';
$contactB = 'contact_id_a';
}
if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
$relIDs = $ids;
}
elseif ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
$sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
$dao = CRM_Core_DAO::executeQuery("
SELECT contact_id FROM civicrm_activity_contact
WHERE activity_id IN ( " . implode(',', $ids) . ") AND
record_type_id = {$sourceID}
");

while ($dao->fetch()) {
$relIDs[] = $dao->contact_id;
}
}
else {
$component = self::exportComponent($exportMode);

if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
$relIDs = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
}
else {
$relIDs = CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
}
}
$relIDs = self::getIDsForRelatedContact($ids, $exportMode);

$relationshipJoin = $relationshipClause = '';
if (!$selectAll && $componentTable) {
Expand Down Expand Up @@ -778,7 +780,7 @@ public static function exportComponents(
// for CRM-3157 purposes
$i18n = CRM_Core_I18n::singleton();

list($outputColumns, $headerRows, $sqlColumns, $metadata) = self::getExportStructureArrays($returnProperties, $query, $contactRelationshipTypes, $relationQuery, $selectedPaymentFields);
list($outputColumns, $headerRows, $sqlColumns, $metadata) = self::getExportStructureArrays($returnProperties, $query, $relationQuery, $selectedPaymentFields);

$limitReached = FALSE;
while (!$limitReached) {
Expand Down Expand Up @@ -837,7 +839,7 @@ public static function exportComponents(
elseif ($field == 'pledge_next_pay_amount') {
$row[$field] = $iterationDAO->pledge_next_pay_amount + $iterationDAO->pledge_outstanding_amount;
}
elseif (array_key_exists($field, $contactRelationshipTypes)) {
elseif (array_key_exists($field, self::$relationshipTypes)) {
$relDAO = CRM_Utils_Array::value($iterationDAO->contact_id, $allRelContactArray[$field]);
$relationQuery[$field]->convertToPseudoNames($relDAO);
self::fetchRelationshipDetails($relDAO, $value, $field, $row);
Expand Down Expand Up @@ -997,8 +999,8 @@ public static function exportComponents(

// merge the records if they have corresponding households
if ($mergeSameHousehold) {
self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyMOH);
self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, $relationKeyHOH);
self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, self::$memberOfHouseholdRelationshipKey);
self::mergeSameHousehold($exportTempTable, $headerRows, $sqlColumns, self::$headOfHouseholdRelationshipKey);
}

// call export hook
Expand Down Expand Up @@ -2005,7 +2007,6 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $query, $
*
* @param array $returnProperties
* @param CRM_Contact_BAO_Contact $query
* @param array $contactRelationshipTypes
* @param string $relationQuery
* @param array $selectedPaymentFields
* @return array
Expand All @@ -2024,11 +2025,11 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $query, $
* - b) this code is old & outdated. Submit your answers to circular bin or better
* yet find a way to comment them for posterity.
*/
public static function getExportStructureArrays($returnProperties, $query, $contactRelationshipTypes, $relationQuery, $selectedPaymentFields) {
public static function getExportStructureArrays($returnProperties, $query, $relationQuery, $selectedPaymentFields) {
$metadata = $headerRows = $outputColumns = $sqlColumns = array();
$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');

$contactRelationshipTypes = self::$relationshipTypes;
foreach ($returnProperties as $key => $value) {
if ($key != 'location' || !is_array($value)) {
$outputColumns[$key] = $value;
Expand Down Expand Up @@ -2186,4 +2187,40 @@ private static function fetchRelationshipDetails($relDAO, $value, $field, &$row)
}
}

/**
* Get the ids that we want to get related contact details for.
*
* @param array $ids
* @param int $exportMode
*
* @return array
*/
protected static function getIDsForRelatedContact($ids, $exportMode) {
if ($exportMode == CRM_Export_Form_Select::CONTACT_EXPORT) {
return $ids;
}
if ($exportMode == CRM_Export_Form_Select::ACTIVITY_EXPORT) {
$relIDs = [];
$sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source');
$dao = CRM_Core_DAO::executeQuery("
SELECT contact_id FROM civicrm_activity_contact
WHERE activity_id IN ( " . implode(',', $ids) . ") AND
record_type_id = {$sourceID}
");

while ($dao->fetch()) {
$relIDs[] = $dao->contact_id;
}
return $relIDs;
}
$component = self::exportComponent($exportMode);

if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
return CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
}
else {
return CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
}
}

}
90 changes: 69 additions & 21 deletions tests/phpunit/CRM/Export/BAO/ExportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
protected $masterAddressID;

public function tearDown() {
$this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address']);
$this->quickCleanup(['civicrm_contact', 'civicrm_email', 'civicrm_address', 'civicrm_relationship']);
$this->quickCleanUpFinancialEntities();
parent::tearDown();
}
Expand Down Expand Up @@ -374,26 +374,7 @@ public function testExportPseudoFieldCampaign() {
* This is to ensure that CRM-13995 remains fixed.
*/
public function testExportRelationshipsMergeToHousehold() {
$this->setUpContactExportData();
$householdID = $this->householdCreate(['api.Address.create' => ['city' => 'Portland', 'state_province_id' => 'Maine', 'location_type_id' => 'Home']]);

$relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values'];
$houseHoldTypeID = NULL;
foreach ($relationshipTypes as $id => $relationshipType) {
if ($relationshipType['name_a_b'] === 'Household Member of') {
$houseHoldTypeID = $relationshipType['id'];
}
}
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $this->contactIDs[0],
'contact_id_b' => $householdID,
'relationship_type_id' => $houseHoldTypeID,
]);
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $this->contactIDs[1],
'contact_id_b' => $householdID,
'relationship_type_id' => $houseHoldTypeID,
]);
list($householdID, $houseHoldTypeID) = $this->setUpHousehold();

$selectedFields = [
['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''],
Expand Down Expand Up @@ -428,6 +409,40 @@ public function testExportRelationshipsMergeToHousehold() {

}

/**
* Test exporting relationships.
*/
public function testExportRelationshipsMergeToHouseholdAllFields() {
$this->markTestIncomplete('Does not yet work under CI due to mysql limitation (number of columns in table). Works on some boxes');
list($householdID) = $this->setUpHousehold();
list($tableName) = CRM_Export_BAO_Export::exportComponents(
FALSE,
$this->contactIDs,
[],
NULL,
NULL,
NULL,
CRM_Export_Form_Select::CONTACT_EXPORT,
"contact_a.id IN (" . implode(",", $this->contactIDs) . ")",
NULL,
FALSE,
TRUE,
[
'exportOption' => CRM_Export_Form_Select::CONTACT_EXPORT,
'suppress_csv_for_testing' => TRUE,
]
);
$dao = CRM_Core_DAO::executeQuery("SELECT * FROM {$tableName}");
while ($dao->fetch()) {
$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);
$this->assertEquals('Unit Test Household', $dao->addressee);
$this->assertEquals('Unit Test Household', $dao->display_name);
}
}

/**
* Test master_address_id field.
*/
Expand Down Expand Up @@ -539,4 +554,37 @@ public function testExportDeceasedDoNotMail() {
CRM_Core_DAO::executeQuery($sql);
}

/**
* @return array
*/
protected function setUpHousehold() {
$this->setUpContactExportData();
$householdID = $this->householdCreate([
'api.Address.create' => [
'city' => 'Portland',
'state_province_id' => 'Maine',
'location_type_id' => 'Home'
]
]);

$relationshipTypes = $this->callAPISuccess('RelationshipType', 'get', [])['values'];
$houseHoldTypeID = NULL;
foreach ($relationshipTypes as $id => $relationshipType) {
if ($relationshipType['name_a_b'] === 'Household Member of') {
$houseHoldTypeID = $relationshipType['id'];
}
}
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $this->contactIDs[0],
'contact_id_b' => $householdID,
'relationship_type_id' => $houseHoldTypeID,
]);
$this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $this->contactIDs[1],
'contact_id_b' => $householdID,
'relationship_type_id' => $houseHoldTypeID,
]);
return array($householdID, $houseHoldTypeID);
}

}

0 comments on commit 47e0bdd

Please sign in to comment.