Skip to content

Commit

Permalink
Rationalise selectedPaymentFields.
Browse files Browse the repository at this point in the history
The selected payments field variable is primarily used when exporting participants
with contributions. This gets the definition of it out of the returnProperties definition
and calculates it more sensibly.
  • Loading branch information
eileenmcnaughton committed Jul 23, 2018
1 parent 300fe26 commit c41bd1e
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 62 deletions.
48 changes: 21 additions & 27 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,6 @@ public static function exportComponents(

$processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator);
$returnProperties = array();
$selectedPaymentFields = FALSE;
// @todo - this variable is overwritten later - it should be wholly definable in the processor fn.
$paymentTableId = $processor->getPaymentTableID();

$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
Expand Down Expand Up @@ -372,14 +369,6 @@ public static function exportComponents(
if ($fieldName == 'event_id') {
$returnProperties['event_id'] = 1;
}
elseif (
$exportMode == CRM_Export_Form_Select::EVENT_EXPORT &&
array_key_exists($fieldName, self::componentPaymentFields())
) {
$selectedPaymentFields = TRUE;
$paymentTableId = 'participant_id';
$returnProperties[$fieldName] = 1;
}
else {
$returnProperties[$fieldName] = 1;
}
Expand All @@ -393,6 +382,9 @@ public static function exportComponents(
else {
$returnProperties = $processor->getDefaultReturnProperties();
}
// @todo - we are working towards this being entirely a property of the processor
$processor->setReturnProperties($returnProperties);
$paymentTableId = $processor->getPaymentTableID();

if ($mergeSameAddress) {
//make sure the addressee fields are selected
Expand Down Expand Up @@ -543,14 +535,14 @@ public static function exportComponents(
$addPaymentHeader = FALSE;

$paymentDetails = array();
if ($processor->isExportPaymentFields() || $selectedPaymentFields) {
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 (!$selectedPaymentFields) {
if (!$processor->isExportSpecifiedPaymentFields()) {
$paymentHeaders = self::componentPaymentFields();
if (!empty($paymentDetails)) {
$addPaymentHeader = TRUE;
Expand Down Expand Up @@ -578,7 +570,7 @@ public static function exportComponents(
// for CRM-3157 purposes
$i18n = CRM_Core_I18n::singleton();

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

$limitReached = FALSE;
while (!$limitReached) {
Expand Down Expand Up @@ -629,7 +621,7 @@ public static function exportComponents(
self::fetchRelationshipDetails($relDAO, $value, $field, $row);
}
else {
$row[$field] = self::getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $selectedPaymentFields, $paymentDetails, $paymentTableId);
$row[$field] = self::getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $paymentDetails, $processor);
}
}

Expand All @@ -654,7 +646,7 @@ public static function exportComponents(
// data will already be in $row. Otherwise, add payment related
// information, if appropriate.
if ($addPaymentHeader) {
if (!$selectedPaymentFields) {
if (!$processor->isExportSpecifiedPaymentFields()) {
if ($processor->isExportPaymentFields()) {
$paymentData = CRM_Utils_Array::value($row[$paymentTableId], $paymentDetails);
if (!is_array($paymentData) || empty($paymentData)) {
Expand Down Expand Up @@ -1608,10 +1600,10 @@ public static function componentPaymentFields() {
* @param array $phoneTypes
* @param array $imProviders
* @param string $relationQuery
* @param array $selectedPaymentFields
*
* @return array
*/
public static function setHeaderRows($field, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery, $selectedPaymentFields) {
public static function setHeaderRows($field, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery) {

$queryFields = $processor->getQueryFields();
// Split campaign into 2 fields for id and title
Expand Down Expand Up @@ -1690,7 +1682,7 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
}
self::manipulateHeaderRows($headerRows);
}
elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
elseif ($processor->isExportPaymentFields() && array_key_exists($field, self::componentPaymentFields())) {
$headerRows[] = CRM_Utils_Array::value($field, self::componentPaymentFields());
}
else {
Expand All @@ -1714,7 +1706,7 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
* @param array $returnProperties
* @param \CRM_Export_BAO_ExportProcessor $processor
* @param string $relationQuery
* @param array $selectedPaymentFields
*
* @return array
* - outputColumns Array of columns to be exported. The values don't matter but the key must match the
* alias for the field generated by BAO_Query object.
Expand All @@ -1731,7 +1723,7 @@ public static function setHeaderRows($field, $headerRows, $sqlColumns, $processo
* - 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, $processor, $relationQuery, $selectedPaymentFields) {
public static function getExportStructureArrays($returnProperties, $processor, $relationQuery) {
$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');
Expand All @@ -1740,7 +1732,7 @@ public static function getExportStructureArrays($returnProperties, $processor, $
foreach ($returnProperties as $key => $value) {
if ($key != 'location' || !is_array($value)) {
$outputColumns[$key] = $value;
list($headerRows, $sqlColumns) = self::setHeaderRows($key, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery, $selectedPaymentFields);
list($headerRows, $sqlColumns) = self::setHeaderRows($key, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery);
}
else {
foreach ($value as $locationType => $locationFields) {
Expand All @@ -1765,7 +1757,7 @@ public static function getExportStructureArrays($returnProperties, $processor, $
$metadata[$daoFieldName]['pseudoconstant']['var'] = 'imProviders';
}
self::sqlColumnDefn($processor, $sqlColumns, $outputFieldName);
list($headerRows, $sqlColumns) = self::setHeaderRows($outputFieldName, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery, $selectedPaymentFields);
list($headerRows, $sqlColumns) = self::setHeaderRows($outputFieldName, $headerRows, $sqlColumns, $processor, $value, $phoneTypes, $imProviders, $relationQuery);
if ($actualDBFieldName == 'country' || $actualDBFieldName == 'world_region') {
$metadata[$daoFieldName] = array('context' => 'country');
}
Expand Down Expand Up @@ -2000,12 +1992,13 @@ protected static function buildRelatedContactArray($selectAll, $ids, $exportMode
* @param $fieldValue
* @param $i18n
* @param $metadata
* @param $selectedPaymentFields
* @param $paymentDetails
* @param string $paymentTableId
*
* @param \CRM_Export_BAO_ExportProcessor $processor
*
* @return string
*/
protected static function getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $selectedPaymentFields, $paymentDetails, $paymentTableId) {
protected static function getTransformedFieldValue($field, $iterationDAO, $fieldValue, $i18n, $metadata, $paymentDetails, $processor) {

if ($field == 'id') {
return $iterationDAO->contact_id;
Expand Down Expand Up @@ -2082,7 +2075,8 @@ protected static function getTransformedFieldValue($field, $iterationDAO, $field
}
}
}
elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) {
elseif ($processor->isExportSpecifiedPaymentFields() && array_key_exists($field, self::componentPaymentFields())) {
$paymentTableId = $processor->getPaymentTableID();
$paymentData = CRM_Utils_Array::value($iterationDAO->$paymentTableId, $paymentDetails);
$payFieldMapper = array(
'componentPaymentField_total_amount' => 'total_amount',
Expand Down
69 changes: 69 additions & 0 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class CRM_Export_BAO_ExportProcessor {
*/
protected $relationshipTypes = [];

/**
* @var array
*/
protected $returnProperties = [];

/**
* CRM_Export_BAO_ExportProcessor constructor.
*
Expand Down Expand Up @@ -110,6 +115,21 @@ public function setRequestedFields($requestedFields) {
$this->requestedFields = $requestedFields;
}


/**
* @return array
*/
public function getReturnProperties() {
return $this->returnProperties;
}

/**
* @param array $returnProperties
*/
public function setReturnProperties($returnProperties) {
$this->returnProperties = $returnProperties;
}

/**
* @return array
*/
Expand Down Expand Up @@ -303,9 +323,25 @@ public function isExportPaymentFields() {
])) {
return TRUE;
}
elseif ($this->isExportSpecifiedPaymentFields()) {
return TRUE;
}
return FALSE;
}

/**
* Has specific payment fields been requested (as opposed to via all fields).
*
* If specific fields have been requested then they get added at various points.
*
* @return bool
*/
public function isExportSpecifiedPaymentFields() {
if ($this->getRequestedFields() !== NULL && $this->hasRequestedComponentPaymentFields()) {
return TRUE;
}
}

/**
* Get the name of the id field in the table that connects contributions to the export entity.
*/
Expand All @@ -318,9 +354,42 @@ public function getPaymentTableID() {
];
return isset($mapping[$this->getQueryMode()]) ? $mapping[$this->getQueryMode()] : '';
}
elseif ($this->hasRequestedComponentPaymentFields()) {
return 'participant_id';
}
return FALSE;
}

/**
* Have component payment fields been requested.
*
* @return bool
*/
protected function hasRequestedComponentPaymentFields() {
if ($this->getQueryMode() === CRM_Contact_BAO_Query::MODE_EVENT) {
$participantPaymentFields = array_intersect_key($this->getComponentPaymentFields(), $this->getReturnProperties());
if (!empty($participantPaymentFields)) {
return TRUE;
}
}
return FALSE;
}

/**
* Get fields that indicate payment fields have been requested for a component.
*
* @return array
*/
protected function getComponentPaymentFields() {
return [
'componentPaymentField_total_amount' => ts('Total Amount'),
'componentPaymentField_contribution_status' => ts('Contribution Status'),
'componentPaymentField_received_date' => ts('Date Received'),
'componentPaymentField_payment_instrument' => ts('Payment Method'),
'componentPaymentField_transaction_id' => ts('Transaction ID'),
];
}

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

0 comments on commit c41bd1e

Please sign in to comment.