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 code clean up - extract build row & getTransformed row off to ExportProcessor #13117

Merged
merged 4 commits into from
Nov 18, 2018
Merged
Changes from 1 commit
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
Next Next commit
Extract buildRow
  • Loading branch information
eileenmcnaughton committed Nov 16, 2018
commit e086f1de850204d6f1f1cf2c969c6b74d7d88546
185 changes: 100 additions & 85 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,6 @@ public static function exportComponents(

$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();

if ($fields) {
Expand Down Expand Up @@ -451,9 +446,6 @@ public static function exportComponents(

$count = -1;

// for CRM-3157 purposes
$i18n = CRM_Core_I18n::singleton();

list($outputColumns, $headerRows, $sqlColumns, $metadata) = self::getExportStructureArrays($returnProperties, $processor);

// add payment headers if required
Expand All @@ -479,83 +471,7 @@ public static function exportComponents(
while ($iterationDAO->fetch()) {
$count++;
$rowsThisIteration++;
$row = array();
$query->convertToPseudoNames($iterationDAO);

//first loop through output columns so that we return what is required, and in same order.
foreach ($outputColumns as $field => $value) {

// add im_provider to $dao object
if ($field == 'im_provider' && property_exists($iterationDAO, 'provider_id')) {
$iterationDAO->im_provider = $iterationDAO->provider_id;
}

//build row values (data)
$fieldValue = NULL;
if (property_exists($iterationDAO, $field)) {
$fieldValue = $iterationDAO->$field;
// to get phone type from phone type id
if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) {
$fieldValue = $phoneTypes[$fieldValue];
}
elseif ($field == 'provider_id' || $field == 'im_provider') {
$fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
}
elseif (strstr($field, 'master_id')) {
$masterAddressId = NULL;
if (isset($iterationDAO->$field)) {
$masterAddressId = $iterationDAO->$field;
}
// get display name of contact that address is shared.
$fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId);
}
}

if ($processor->isRelationshipTypeKey($field)) {
foreach (array_keys($value) as $property) {
if ($property === 'location') {
// @todo just undo all this nasty location wrangling!
foreach ($value['location'] as $locationKey => $locationFields) {
foreach (array_keys($locationFields) as $locationField) {
$fieldKey = str_replace(' ', '_', $locationKey . '-' . $locationField);
$row[$field . '_' . $fieldKey] = $processor->getRelationshipValue($field, $iterationDAO->contact_id, $fieldKey);
}
}
}
else {
$row[$field . '_' . $property] = $processor->getRelationshipValue($field, $iterationDAO->contact_id, $property);
}
}
}
else {
$row[$field] = self::getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $paymentDetails, $processor);
}
}

// If specific payment fields have been selected for export, payment
// data will already be in $row. Otherwise, add payment related
// information, if appropriate.
if ($addPaymentHeader) {
if (!$processor->isExportSpecifiedPaymentFields()) {
$nullContributionDetails = array_fill_keys(array_keys($processor->getPaymentHeaders()), NULL);
if ($processor->isExportPaymentFields()) {
$paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
if (!is_array($paymentData) || empty($paymentData)) {
$paymentData = $nullContributionDetails;
}
$row = array_merge($row, $paymentData);
}
elseif (!empty($paymentDetails)) {
$row = array_merge($row, $nullContributionDetails);
}
}
}
//remove organization name for individuals if it is set for current employer
if (!empty($row['contact_type']) &&
$row['contact_type'] == 'Individual' && array_key_exists('organization_name', $row)
) {
$row['organization_name'] = '';
}
$row = self::buildRow($query, $iterationDAO, $processor, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader, $paymentTableId);

// add component info
// write the row to a file
Expand Down Expand Up @@ -1814,4 +1730,103 @@ protected static function getTransformedFieldValue($field, $iterationDAO, $field
}
}

/**
* Build the row for output.
*
* @param \CRM_Contact_BAO_Query $query
* @param CRM_Core_DAO $iterationDAO
* @param \CRM_Export_BAO_ExportProcessor$processor
* @param array $outputColumns
* @param $metadata
* @param $paymentDetails
* @param $addPaymentHeader
* @param $paymentTableId
*
* @return array
*/
protected static function buildRow($query, $iterationDAO, $processor, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader, $paymentTableId) {
$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
$i18n = CRM_Core_I18n::singleton();

$row = [];
$query->convertToPseudoNames($iterationDAO);

//first loop through output columns so that we return what is required, and in same order.
foreach ($outputColumns as $field => $value) {

// add im_provider to $dao object
if ($field == 'im_provider' && property_exists($iterationDAO, 'provider_id')) {
$iterationDAO->im_provider = $iterationDAO->provider_id;
}

//build row values (data)
$fieldValue = NULL;
if (property_exists($iterationDAO, $field)) {
$fieldValue = $iterationDAO->$field;
// to get phone type from phone type id
if ($field == 'phone_type_id' && isset($phoneTypes[$fieldValue])) {
$fieldValue = $phoneTypes[$fieldValue];
}
elseif ($field == 'provider_id' || $field == 'im_provider') {
$fieldValue = CRM_Utils_Array::value($fieldValue, $imProviders);
}
elseif (strstr($field, 'master_id')) {
$masterAddressId = NULL;
if (isset($iterationDAO->$field)) {
$masterAddressId = $iterationDAO->$field;
}
// get display name of contact that address is shared.
$fieldValue = CRM_Contact_BAO_Contact::getMasterDisplayName($masterAddressId);
}
}

if ($processor->isRelationshipTypeKey($field)) {
foreach (array_keys($value) as $property) {
if ($property === 'location') {
// @todo just undo all this nasty location wrangling!
foreach ($value['location'] as $locationKey => $locationFields) {
foreach (array_keys($locationFields) as $locationField) {
$fieldKey = str_replace(' ', '_', $locationKey . '-' . $locationField);
$row[$field . '_' . $fieldKey] = $processor->getRelationshipValue($field, $iterationDAO->contact_id, $fieldKey);
}
}
}
else {
$row[$field . '_' . $property] = $processor->getRelationshipValue($field, $iterationDAO->contact_id, $property);
}
}
}
else {
$row[$field] = self::getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $paymentDetails, $processor);
}
}

// If specific payment fields have been selected for export, payment
// data will already be in $row. Otherwise, add payment related
// information, if appropriate.
if ($addPaymentHeader) {
if (!$processor->isExportSpecifiedPaymentFields()) {
$nullContributionDetails = array_fill_keys(array_keys($processor->getPaymentHeaders()), NULL);
if ($processor->isExportPaymentFields()) {
$paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
if (!is_array($paymentData) || empty($paymentData)) {
$paymentData = $nullContributionDetails;
}
$row = array_merge($row, $paymentData);
}
elseif (!empty($paymentDetails)) {
$row = array_merge($row, $nullContributionDetails);
}
}
}
//remove organization name for individuals if it is set for current employer
if (!empty($row['contact_type']) &&
$row['contact_type'] == 'Individual' && array_key_exists('organization_name', $row)
) {
$row['organization_name'] = '';
}
return $row;
}

}