From 0e32ed68b5dc3caa91c53798c9e7ff6843662e22 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 12 Nov 2018 14:04:42 +1300 Subject: [PATCH 1/4] Use getComponentPaymentFields from processorClass --- CRM/Core/BAO/Mapping.php | 1 + CRM/Export/BAO/Export.php | 13 +++++++++---- CRM/Export/BAO/ExportProcessor.php | 4 +++- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 3e8b324a360b..29ea4dcf5f70 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -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) { diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index c5e82945f7b9..57a53f7fbe1a 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -439,7 +439,7 @@ public static function exportComponents( // If we haven't selected specific payment fields, load in all the // payment headers. if (!$processor->isExportSpecifiedPaymentFields()) { - $paymentHeaders = self::componentPaymentFields(); + $paymentHeaders = $processor->getcomponentPaymentFields(); if (!empty($paymentDetails)) { $addPaymentHeader = TRUE; } @@ -1304,6 +1304,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; @@ -1345,8 +1350,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; @@ -1807,7 +1812,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( diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 0b77a52eda6e..2cf81b34db0f 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -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'), From 05ad310f1c2b4835bad9bb78529ebb4e50331489 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 12 Nov 2018 14:49:17 +1300 Subject: [PATCH 2/4] Refactor out getPaymentHeaders function --- CRM/Export/BAO/Export.php | 14 +++----------- CRM/Export/BAO/ExportProcessor.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 57a53f7fbe1a..99ba90ee5a15 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -432,25 +432,16 @@ 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 = $processor->getcomponentPaymentFields(); 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(); @@ -536,8 +527,8 @@ public static function exportComponents( 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) { + $headerRows = array_merge($headerRows, $processor->getPaymentHeaders()); + foreach (array_keys($processor->getPaymentHeaders()) as $paymentHdr) { self::sqlColumnDefn($processor, $sqlColumns, $paymentHdr); } } @@ -554,6 +545,7 @@ public static function exportComponents( // 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)) { diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php index 2cf81b34db0f..1e12629d9e18 100644 --- a/CRM/Export/BAO/ExportProcessor.php +++ b/CRM/Export/BAO/ExportProcessor.php @@ -533,6 +533,19 @@ public 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. * From 63b17b8ee94e2788bff2d7d32c93ef6d347ce3e4 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 12 Nov 2018 14:50:31 +1300 Subject: [PATCH 3/4] Move wrangling payment header outside main row iteration --- CRM/Export/BAO/Export.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 99ba90ee5a15..5699194a4ac5 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -459,6 +459,16 @@ 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); + } + } + $limitReached = FALSE; while (!$limitReached) { $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}"; @@ -523,16 +533,6 @@ 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, $processor->getPaymentHeaders()); - foreach (array_keys($processor->getPaymentHeaders()) as $paymentHdr) { - self::sqlColumnDefn($processor, $sqlColumns, $paymentHdr); - } - } - if ($setHeader) { $exportTempTable = self::createTempTable($sqlColumns); } From 0b94b406e29444793f12cc84403db97c208c8fc1 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 16 Nov 2018 12:31:39 +1300 Subject: [PATCH 4/4] Always create temp table at the start. The saving of not creating an empty file occassionally doesn't warrant the code complexity --- CRM/Export/BAO/Export.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 5699194a4ac5..d7103de352d6 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -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); @@ -445,7 +443,6 @@ public static function exportComponents( } $componentDetails = array(); - $setHeader = TRUE; $rowCount = self::EXPORT_ROW_COUNT; $offset = 0; @@ -469,7 +466,9 @@ public static function exportComponents( } } + $exportTempTable = self::createTempTable($sqlColumns); $limitReached = FALSE; + while (!$limitReached) { $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}"; $iterationDAO = CRM_Core_DAO::executeQuery($limitQuery); @@ -533,13 +532,6 @@ public static function exportComponents( } } - 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.