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 cleanup - Use getComponentPaymentFields from processorClass #13082

Merged
merged 4 commits into from
Nov 16, 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
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced by $processor->getPaymentHeaders(

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

calculated when it is used

}

$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()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to being before the row-by-row loop

// @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