From 55f37718274b8296bc81206e4478483b43c9cb79 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 20 Aug 2014 16:14:00 +1200 Subject: [PATCH] CRM-14398 fix campaign in exports --- CRM/Export/BAO/Export.php | 53 ++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 8930ebe469..b1605f6367 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -78,9 +78,7 @@ class CRM_Export_BAO_Export { $headerRows = $returnProperties = array(); $primary = $paymentFields = $selectedPaymentFields = FALSE; $origFields = $fields; - $queryMode = $relationField = NULL; - - $allCampaigns = array(); + $relationField = NULL; $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); @@ -604,7 +602,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c $addPaymentHeader = TRUE; } } - // If we have seleted specific payment fields, leave the payment headers + // 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 { @@ -613,21 +611,6 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c $nullContributionDetails = array_fill_keys(array_keys($paymentHeaders), NULL); } - // Split campaign into 2 fields for id and title - $campaignReturnProperties = array(); - foreach ($returnProperties as $fld => $true) { - $campaignReturnProperties[$fld] = $true; - if (substr($fld, -11) == 'campaign_id') { - $exportCampaign = TRUE; - $campaignReturnProperties[substr($fld, 0, -3)] = 1; - } - } - $returnProperties = $campaignReturnProperties; - //get all campaigns. - if (isset($exportCampaign)) { - $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); - } - $componentDetails = $headerRows = $sqlColumns = array(); $setHeader = TRUE; @@ -640,7 +623,17 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c // for CRM-3157 purposes $i18n = CRM_Core_I18n::singleton(); - + $outputColumns = array(); + //@todo - it would be clearer to start defining output columns earlier in this function rather than stick with return properties until this point + // as the array is not actually 'returnProperties' after the sql query is formed - making the alterations to it confusing + foreach ($returnProperties as $key => $value) { + $outputColumns[$key] = 1; + if (substr($key, -11) == 'campaign_id') { + // the field $dao->x_campaign_id_id holds the id whereas the field $dao->campaign_id + // we want to insert it directly after campaign id + $outputColumns[$key . '_id'] = 1; + } + } while (1) { $limitQuery = "{$queryString} LIMIT {$offset}, {$rowCount}"; $dao = CRM_Core_DAO::executeQuery($limitQuery); @@ -655,17 +648,17 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c //convert the pseudo constants $query->convertToPseudoNames($dao); - //first loop through returnproperties so that we return what is required, and in same order. + //first loop through output columns so that we return what is required, and in same order. $relationshipField = 0; - foreach ($returnProperties as $field => $value) { + foreach ($outputColumns as $field => $value) { //we should set header only once if ($setHeader) { $sqlDone = FALSE; // Split campaign into 2 fields for id and title - if (substr($field, -8) == 'campaign') { + if (substr($field, -11) == 'campaign_id') { $headerRows[] = ts('Campaign Title'); } - elseif (substr($field, -11) == 'campaign_id') { + elseif (substr($field, -14) == 'campaign_id_id') { $headerRows[] = ts('Campaign ID'); } elseif (isset($query->_fields[$field]['title'])) { @@ -988,6 +981,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c CRM_Core_OptionGroup::lookupValues($paramsNew, $name, FALSE); $row[$field] = $paramsNew[$field]; } + elseif (in_array($field, array( 'email_greeting', 'postal_greeting', 'addressee'))) { //special case for greeting replacement @@ -1018,10 +1012,6 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c } } } - elseif (substr($field, -8) == 'campaign') { - $campIdFld = "{$field}_id"; - $row[$field] = CRM_Utils_Array::value($dao->$campIdFld, $allCampaigns, ''); - } elseif ($selectedPaymentFields && array_key_exists($field, self::componentPaymentFields())) { $paymentData = CRM_Utils_Array::value($dao->$paymentTableId, $paymentDetails); $payFieldMapper = array( @@ -1274,6 +1264,13 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c return; } + if (substr($fieldName, -11) == 'campaign_id') { + // CRM-14398 + $sqlColumns[$fieldName] = "$fieldName varchar(128)"; + $sqlColumns[$fieldName . '_id'] = "{$fieldName}_id varchar(16)"; + return; + } + // set the sql columns if (isset($query->_fields[$field]['type'])) { switch ($query->_fields[$field]['type']) { -- 2.25.1