Merge pull request #15144 from JKingsnorth/copying-events-contribution-pages
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
index 9aca8f4fcca83e4e18169adc7653a7690096f41e..9a5d5f92c8d03f19853ec7abc37645b29a4d2aa3 100644 (file)
@@ -100,6 +100,13 @@ class CRM_Export_BAO_ExportProcessor {
    */
   protected $contactGreetingFields = [];
 
+  /**
+   * An array of primary IDs of the entity being exported.
+   *
+   * @var array
+   */
+  protected $ids = [];
+
   /**
    * Get additional non-visible fields for address merge purposes.
    *
@@ -166,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);
@@ -585,6 +592,20 @@ class CRM_Export_BAO_ExportProcessor {
     $this->queryOperator = $queryOperator;
   }
 
+  /**
+   * @return array
+   */
+  public function getIds() {
+    return $this->ids;
+  }
+
+  /**
+   * @param array $ids
+   */
+  public function setIds($ids) {
+    $this->ids = $ids;
+  }
+
   /**
    * @return array
    */
@@ -782,7 +803,7 @@ class CRM_Export_BAO_ExportProcessor {
             $addressWhere .= " OR civicrm_address.supplemental_address_1 <> ''";
           }
         }
-        $whereClauses['address'] = $addressWhere;
+        $whereClauses['address'] = '(' . $addressWhere . ')';
       }
     }
 
@@ -2330,7 +2351,17 @@ WHERE  id IN ( $deleteIDString )
     // call export hook
     $headerRows = $this->getHeaderRows();
     $exportTempTable = $this->getTemporaryTable();
+    $exportMode = $this->getExportMode();
+    $sqlColumns = $this->getSQLColumns();
+    $componentTable = $this->getComponentTable();
+    $ids = $this->getIds();
     CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
+    if ($exportMode !== $this->getExportMode() || $componentTable !== $this->getComponentTable()) {
+      CRM_Core_Error::deprecatedFunctionWarning('altering the export mode and/or component table in the hook is no longer supported.');
+    }
+    if ($ids !== $this->getIds()) {
+      CRM_Core_Error::deprecatedFunctionWarning('altering the ids in the hook is no longer supported.');
+    }
     if ($exportTempTable !== $this->getTemporaryTable()) {
       CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
       $this->setTemporaryTable($exportTempTable);
@@ -2345,6 +2376,7 @@ WHERE  id IN ( $deleteIDString )
 
     $query = "SELECT * FROM $exportTempTable";
 
+    $this->instantiateTempTable($headerRows);
     while (1) {
       $limitQuery = $query . "
 LIMIT $offset, $limit
@@ -2359,21 +2391,43 @@ LIMIT $offset, $limit
       while ($dao->fetch()) {
         $row = [];
 
-        foreach (array_keys($this->getSQLColumns()) as $column) {
+        foreach (array_keys($sqlColumns) as $column) {
           $row[$column] = $dao->$column;
         }
         $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
+    );
+  }
+
 }