From de6ff5094a93d6e1a52feb8df16b08e2d326b63e Mon Sep 17 00:00:00 2001
From: eileen <emcnaughton@wikimedia.org>
Date: Mon, 19 Nov 2018 14:04:57 +1300
Subject: [PATCH] Stop setting header rows only to unset them

---
 CRM/Export/BAO/Export.php          | 16 ++++++++--------
 CRM/Export/BAO/ExportProcessor.php | 19 +++++++++++++++----
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php
index fb966a0550..ddf2190eed 100644
--- a/CRM/Export/BAO/Export.php
+++ b/CRM/Export/BAO/Export.php
@@ -287,6 +287,7 @@ class CRM_Export_BAO_Export {
         if (!array_key_exists($column, $returnProperties)) {
           $returnProperties[$column] = 1;
           $column = $column == 'id' ? 'civicrm_primary_id' : $column;
+          $processor->setColumnAsCalculationOnly($column);
           $exportParams['merge_same_address']['temp_columns'][$column] = 1;
         }
       }
@@ -319,6 +320,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
         if (!array_key_exists($column, $returnProperties)) {
           $returnProperties[$column] = 1;
           $exportParams['postal_mailing_export']['temp_columns'][$column] = 1;
+          $processor->setColumnAsCalculationOnly($column);
         }
       }
     }
@@ -493,12 +495,12 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       if (isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
         $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
       ) {
-        self::postalMailingFormat($exportTempTable, $headerRows, $sqlColumns, $exportMode);
+        self::postalMailingFormat($exportTempTable, $sqlColumns, $exportMode);
       }
 
       // do merge same address and merge same household processing
       if ($mergeSameAddress) {
-        self::mergeSameAddress($exportTempTable, $headerRows, $sqlColumns, $exportParams);
+        self::mergeSameAddress($exportTempTable, $sqlColumns, $exportParams);
       }
 
       // merge the records if they have corresponding households
@@ -726,11 +728,10 @@ CREATE TABLE {$exportTempTable} (
 
   /**
    * @param string $tableName
-   * @param $headerRows
    * @param $sqlColumns
    * @param array $exportParams
    */
-  public static function mergeSameAddress($tableName, &$headerRows, &$sqlColumns, $exportParams) {
+  public static function mergeSameAddress($tableName, &$sqlColumns, $exportParams) {
     // check if any records are present based on if they have used shared address feature,
     // and not based on if city / state .. matches.
     $sql = "
@@ -827,7 +828,7 @@ WHERE  id IN ( $deleteIDString )
       $unsetKeys = array_keys($sqlColumns);
       foreach ($unsetKeys as $headerKey => $sqlColKey) {
         if (array_key_exists($sqlColKey, $exportParams['merge_same_address']['temp_columns'])) {
-          unset($sqlColumns[$sqlColKey], $headerRows[$headerKey]);
+          unset($sqlColumns[$sqlColKey]);
         }
       }
     }
@@ -1132,11 +1133,10 @@ LIMIT $offset, $limit
    * Exclude contacts who are deceased, have "Do not mail" privacy setting,
    * or have no street address
    * @param $exportTempTable
-   * @param $headerRows
    * @param $sqlColumns
    * @param $exportParams
    */
-  public static function postalMailingFormat($exportTempTable, &$headerRows, &$sqlColumns, $exportParams) {
+  public static function postalMailingFormat($exportTempTable, &$sqlColumns, $exportParams) {
     $whereClause = array();
 
     if (array_key_exists('is_deceased', $sqlColumns)) {
@@ -1179,7 +1179,7 @@ WHERE  {$whereClause}";
       $unsetKeys = array_keys($sqlColumns);
       foreach ($unsetKeys as $headerKey => $sqlColKey) {
         if (array_key_exists($sqlColKey, $exportParams['postal_mailing_export']['temp_columns'])) {
-          unset($sqlColumns[$sqlColKey], $headerRows[$headerKey]);
+          unset($sqlColumns[$sqlColKey]);
         }
       }
     }
diff --git a/CRM/Export/BAO/ExportProcessor.php b/CRM/Export/BAO/ExportProcessor.php
index 39de1b09f8..c87b8dc99a 100644
--- a/CRM/Export/BAO/ExportProcessor.php
+++ b/CRM/Export/BAO/ExportProcessor.php
@@ -452,9 +452,18 @@ class CRM_Export_BAO_ExportProcessor {
     if ($relationshipType) {
       $labelPrefix = $this->getRelationshipTypes()[$relationshipType] . '-';
     }
-    $this->outputSpecification[$key] = [
-      'header' => $labelPrefix . $label
-    ];
+    $this->outputSpecification[$key]['header'] = $labelPrefix . $label;
+  }
+
+  /**
+   * Mark a column as only required for calculations.
+   *
+   * Do not include the row with headers.
+   *
+   * @param string $column
+   */
+  public function setColumnAsCalculationOnly($column) {
+    $this->outputSpecification[$column]['do_not_output_to_csv'] = TRUE;
   }
 
   /**
@@ -463,7 +472,9 @@ class CRM_Export_BAO_ExportProcessor {
   public function getHeaderRows() {
     $headerRows = [];
     foreach ($this->outputSpecification as $key => $spec) {
-      $headerRows[] = $spec['header'];
+      if (empty($spec['do_not_output_to_csv'])) {
+        $headerRows[] = $spec['header'];
+      }
     }
     return $headerRows;
   }
-- 
2.25.1