Skip to content

Commit

Permalink
Merge pull request #13082 from eileenmcnaughton/export
Browse files Browse the repository at this point in the history
Export code cleanup - Use getComponentPaymentFields from processorClass
  • Loading branch information
eileenmcnaughton authored Nov 16, 2018
2 parents 8bdae93 + 0b94b40 commit 9ab0b9f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
1 change: 1 addition & 0 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ public static function buildMappingForm(&$form, $mappingType, $mappingId, $colum
if (CRM_Core_Permission::access('CiviEvent')) {
$fields['Participant'] = CRM_Event_BAO_Participant::exportableFields();
//get the component payment fields
// @todo - review this - inconsistent with other entities & hacky.
if ($exportMode == CRM_Export_Form_Select::EVENT_EXPORT) {
$componentPaymentFields = array();
foreach (CRM_Export_BAO_Export::componentPaymentFields() as $payField => $payTitle) {
Expand Down
53 changes: 21 additions & 32 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,6 @@ public static function exportComponents(
$imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
self::$relationshipTypes = $processor->getRelationshipTypes();

$queryMode = $processor->getQueryMode();

if ($fields) {
foreach ($fields as $key => $value) {
$fieldName = CRM_Utils_Array::value(1, $value);
Expand Down Expand Up @@ -432,29 +430,19 @@ public static function exportComponents(

$paymentDetails = array();
if ($processor->isExportPaymentFields()) {

// get payment related in for event and members
$paymentDetails = CRM_Contribute_BAO_Contribution::getContributionDetails($exportMode, $ids);
//get all payment headers.
// If we haven't selected specific payment fields, load in all the
// payment headers.
if (!$processor->isExportSpecifiedPaymentFields()) {
$paymentHeaders = self::componentPaymentFields();
if (!empty($paymentDetails)) {
$addPaymentHeader = TRUE;
}
}
// If we have selected specific payment fields, leave the payment headers
// as an empty array; the headers for each selected field will be added
// elsewhere.
else {
$paymentHeaders = array();
}
$nullContributionDetails = array_fill_keys(array_keys($paymentHeaders), NULL);
}

$componentDetails = array();
$setHeader = TRUE;

$rowCount = self::EXPORT_ROW_COUNT;
$offset = 0;
Expand All @@ -468,7 +456,19 @@ public static function exportComponents(

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

// add payment headers if required
if ($addPaymentHeader && $processor->isExportPaymentFields()) {
// @todo rather than do this for every single row do it before the loop starts.
// where other header definitions take place.
$headerRows = array_merge($headerRows, $processor->getPaymentHeaders());
foreach (array_keys($processor->getPaymentHeaders()) as $paymentHdr) {
self::sqlColumnDefn($processor, $sqlColumns, $paymentHdr);
}
}

$exportTempTable = self::createTempTable($sqlColumns);
$limitReached = FALSE;

while (!$limitReached) {
$limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}";
$iterationDAO = CRM_Core_DAO::executeQuery($limitQuery);
Expand Down Expand Up @@ -532,28 +532,12 @@ public static function exportComponents(
}
}

// add payment headers if required
if ($addPaymentHeader && $processor->isExportPaymentFields()) {
// @todo rather than do this for every single row do it before the loop starts.
// where other header definitions take place.
$headerRows = array_merge($headerRows, $paymentHeaders);
foreach (array_keys($paymentHeaders) as $paymentHdr) {
self::sqlColumnDefn($processor, $sqlColumns, $paymentHdr);
}
}

if ($setHeader) {
$exportTempTable = self::createTempTable($sqlColumns);
}

//build header only once
$setHeader = FALSE;

// 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)) {
Expand Down Expand Up @@ -1304,6 +1288,11 @@ public static function postalMailingFormat($exportTempTable, &$headerRows, &$sql

/**
* Build componentPayment fields.
*
* This is no longer used by export but BAO_Mapping still calls it & we
* should find a generic way to handle this or move this to that class.
*
* @deprecated
*/
public static function componentPaymentFields() {
static $componentPaymentFields;
Expand Down Expand Up @@ -1345,8 +1334,8 @@ public static function setHeaderRows($field, $headerRows, $processor) {
// @todo - set this correctly in the xml rather than here.
$headerRows[] = ts('IM Service Provider');
}
elseif ($processor->isExportPaymentFields() && array_key_exists($field, self::componentPaymentFields())) {
$headerRows[] = CRM_Utils_Array::value($field, self::componentPaymentFields());
elseif ($processor->isExportPaymentFields() && array_key_exists($field, $processor->getcomponentPaymentFields())) {
$headerRows[] = CRM_Utils_Array::value($field, $processor->getcomponentPaymentFields());
}
else {
$headerRows[] = $field;
Expand Down Expand Up @@ -1807,7 +1796,7 @@ protected static function getTransformedFieldValue($field, $iterationDAO, $field
}
}
}
elseif ($processor->isExportSpecifiedPaymentFields() && array_key_exists($field, self::componentPaymentFields())) {
elseif ($processor->isExportSpecifiedPaymentFields() && array_key_exists($field, $processor->getcomponentPaymentFields())) {
$paymentTableId = $processor->getPaymentTableID();
$paymentData = CRM_Utils_Array::value($iterationDAO->$paymentTableId, $paymentDetails);
$payFieldMapper = array(
Expand Down
17 changes: 16 additions & 1 deletion CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,11 @@ protected function hasRequestedComponentPaymentFields() {
/**
* Get fields that indicate payment fields have been requested for a component.
*
* Ideally this should be protected but making it temporarily public helps refactoring..
*
* @return array
*/
protected function getComponentPaymentFields() {
public function getComponentPaymentFields() {
return [
'componentPaymentField_total_amount' => ts('Total Amount'),
'componentPaymentField_contribution_status' => ts('Contribution Status'),
Expand All @@ -531,6 +533,19 @@ protected function getComponentPaymentFields() {
];
}

/**
* Get headers for payment fields.
*
* Returns an array of contribution fields when the entity supports payment fields and specific fields
* are not specified. This is a transitional function for refactoring legacy code.
*/
public function getPaymentHeaders() {
if ($this->isExportPaymentFields() && !$this->isExportSpecifiedPaymentFields()) {
return $this->getcomponentPaymentFields();
}
return [];
}

/**
* Get the default properties when not specified.
*
Expand Down

0 comments on commit 9ab0b9f

Please sign in to comment.