Merge pull request #15283 from christianwach/lab-1200-3
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
index b46e41e283fdb27a2c3fb800a285e0743912c781..9a5d5f92c8d03f19853ec7abc37645b29a4d2aa3 100644 (file)
@@ -173,7 +173,7 @@ class CRM_Export_BAO_ExportProcessor {
       $fields = ['is_deceased', 'do_not_mail', 'street_address', 'supplemental_address_1'];
       foreach ($fields as $index => $field) {
         if (!empty($this->getReturnProperties()[$field])) {
-          unset($field[$index]);
+          unset($fields[$index]);
         }
       }
       $this->additionalFieldsForPostalExport = array_fill_keys($fields, 1);
@@ -803,7 +803,7 @@ class CRM_Export_BAO_ExportProcessor {
             $addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
           }
         }
-        $whereClauses['address'] = $addressWhere;
+        $whereClauses['address'] = '(' . $addressWhere . ')';
       }
     }
 
@@ -2376,6 +2376,7 @@ WHERE  id IN ( $deleteIDString )
 
     $query = "SELECT * FROM $exportTempTable";
 
+    $this->instantiateTempTable($headerRows);
     while (1) {
       $limitQuery = $query . "
 LIMIT $offset, $limit
@@ -2395,16 +2396,38 @@ LIMIT $offset, $limit
         }
         $componentDetails[] = $row;
       }
-      CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
-        $headerRows,
-        $componentDetails,
-        NULL,
-        $writeHeader
-      );
-
-      $writeHeader = FALSE;
+      $this->writeRows($headerRows, $componentDetails);
+
       $offset += $limit;
     }
   }
 
+  /**
+   * Set up the temp table.
+   *
+   * @param array $headerRows
+   */
+  protected function instantiateTempTable(array $headerRows) {
+    CRM_Utils_System::download(CRM_Utils_String::munge($this->getExportFileName()),
+      'text/x-csv',
+      CRM_Core_DAO::$_nullObject,
+      'csv',
+      FALSE
+    );
+    CRM_Core_Report_Excel::makeCSVTable($headerRows, [], NULL, TRUE, TRUE);
+  }
+
+  /**
+   * @param array $headerRows
+   * @param array $componentDetails
+   */
+  protected function writeRows(array $headerRows, array $componentDetails) {
+    CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
+      $headerRows,
+      $componentDetails,
+      NULL,
+      FALSE
+    );
+  }
+
 }