CRM-14398 fix campaign in exports
authorEileen McNaughton <eileen@fuzion.co.nz>
Wed, 20 Aug 2014 04:14:00 +0000 (16:14 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Wed, 20 Aug 2014 10:17:56 +0000 (22:17 +1200)
CRM/Export/BAO/Export.php

index 8930ebe46911c21c7576c0ffee19549c1cf9c02c..b1605f63670fa63410a53d39c6e9630c6d751f5d 100644 (file)
@@ -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']) {