Merge pull request #15094 from mattwire/devcore792_membershipcontributionfail
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
index 30bd8e1f7d5f5d853559fa73efbbae29a1e2a91e..9aca8f4fcca83e4e18169adc7653a7690096f41e 100644 (file)
@@ -589,7 +589,10 @@ class CRM_Export_BAO_ExportProcessor {
    * @return array
    */
   public function getQueryFields() {
-    return $this->queryFields;
+    return array_merge(
+      $this->queryFields,
+      $this->getComponentPaymentFields()
+    );
   }
 
   /**
@@ -602,6 +605,7 @@ class CRM_Export_BAO_ExportProcessor {
     $queryFields['country']['context'] = 'country';
     $queryFields['world_region']['context'] = 'country';
     $queryFields['state_province']['context'] = 'province';
+    $queryFields['contact_id'] = ['title' => ts('Contact ID'), 'type' => CRM_Utils_Type::T_INT];
     $this->queryFields = $queryFields;
   }
 
@@ -724,7 +728,7 @@ class CRM_Export_BAO_ExportProcessor {
       return $this->getQueryFields()[$field]['title'];
     }
     elseif ($this->isExportPaymentFields() && array_key_exists($field, $this->getcomponentPaymentFields())) {
-      return CRM_Utils_Array::value($field, $this->getcomponentPaymentFields());
+      return CRM_Utils_Array::value($field, $this->getcomponentPaymentFields())['title'];
     }
     else {
       return $field;
@@ -1256,11 +1260,11 @@ class CRM_Export_BAO_ExportProcessor {
    */
   public function getComponentPaymentFields() {
     return [
-      'componentPaymentField_total_amount' => ts('Total Amount'),
-      'componentPaymentField_contribution_status' => ts('Contribution Status'),
-      'componentPaymentField_received_date' => ts('Date Received'),
-      'componentPaymentField_payment_instrument' => ts('Payment Method'),
-      'componentPaymentField_transaction_id' => ts('Transaction ID'),
+      'componentPaymentField_total_amount' => ['title' => ts('Total Amount'), 'type' => CRM_Utils_Type::T_MONEY],
+      'componentPaymentField_contribution_status' => ['title' => ts('Contribution Status'), 'type' => CRM_Utils_Type::T_STRING],
+      'componentPaymentField_received_date' => ['title' => ts('Date Received'), 'type' => CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME],
+      'componentPaymentField_payment_instrument' => ['title' => ts('Payment Method'), 'type' => CRM_Utils_Type::T_STRING],
+      'componentPaymentField_transaction_id' => ['title' => ts('Transaction ID'), 'type' => CRM_Utils_Type::T_STRING],
     ];
   }
 
@@ -1272,7 +1276,7 @@ class CRM_Export_BAO_ExportProcessor {
    */
   public function getPaymentHeaders() {
     if ($this->isExportPaymentFields() && !$this->isExportSpecifiedPaymentFields()) {
-      return $this->getcomponentPaymentFields();
+      return CRM_Utils_Array::collect('title', $this->getcomponentPaymentFields());
     }
     return [];
   }
@@ -1397,24 +1401,21 @@ class CRM_Export_BAO_ExportProcessor {
       return "$fieldName varchar(128)";
     }
 
-    if (substr($fieldName, -11) == 'campaign_id') {
-      // CRM-14398
-      return "$fieldName varchar(128)";
-    }
-
     $queryFields = $this->getQueryFields();
-    $lookUp = ['prefix_id', 'suffix_id'];
+    // @todo remove the enotice avoidance here, ensure all columns are declared.
+    // tests will fail on the enotices until they all are & then all the 'else'
+    // below can go.
+    $fieldSpec = $queryFields[$columnName] ?? [];
+
     // set the sql columns
-    if (isset($queryFields[$columnName]['type'])) {
-      switch ($queryFields[$columnName]['type']) {
+    if (isset($fieldSpec['type'])) {
+      switch ($fieldSpec['type']) {
         case CRM_Utils_Type::T_INT:
         case CRM_Utils_Type::T_BOOLEAN:
-          if (in_array($columnName, $lookUp)) {
+          if (in_array(CRM_Utils_Array::value('data_type', $fieldSpec), ['Country', 'StateProvince', 'ContactReference'])) {
             return "$fieldName varchar(255)";
           }
-          else {
-            return "$fieldName varchar(16)";
-          }
+          return "$fieldName varchar(16)";
 
         case CRM_Utils_Type::T_STRING:
           if (isset($queryFields[$columnName]['maxlength'])) {
@@ -1470,8 +1471,6 @@ class CRM_Export_BAO_ExportProcessor {
                 $length = max(512, CRM_Utils_Array::value('text_length', $queryFields[$columnName]));
                 return "$fieldName varchar($length)";
 
-              case 'Country':
-              case 'StateProvince':
               case 'Link':
                 return "$fieldName varchar(255)";
 
@@ -2324,4 +2323,57 @@ WHERE  id IN ( $deleteIDString )
     }
   }
 
+  /**
+   * Write to the csv from the temp table.
+   */
+  public function writeCSVFromTable() {
+    // call export hook
+    $headerRows = $this->getHeaderRows();
+    $exportTempTable = $this->getTemporaryTable();
+    CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
+    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);
+    }
+    $exportTempTable = $this->getTemporaryTable();
+    $writeHeader = TRUE;
+    $offset = 0;
+    // increase this number a lot to avoid making too many queries
+    // LIMIT is not much faster than a no LIMIT query
+    // CRM-7675
+    $limit = 100000;
+
+    $query = "SELECT * FROM $exportTempTable";
+
+    while (1) {
+      $limitQuery = $query . "
+LIMIT $offset, $limit
+";
+      $dao = CRM_Core_DAO::executeQuery($limitQuery);
+
+      if ($dao->N <= 0) {
+        break;
+      }
+
+      $componentDetails = [];
+      while ($dao->fetch()) {
+        $row = [];
+
+        foreach (array_keys($this->getSQLColumns()) as $column) {
+          $row[$column] = $dao->$column;
+        }
+        $componentDetails[] = $row;
+      }
+      CRM_Core_Report_Excel::writeCSVFile($this->getExportFileName(),
+        $headerRows,
+        $componentDetails,
+        NULL,
+        $writeHeader
+      );
+
+      $writeHeader = FALSE;
+      $offset += $limit;
+    }
+  }
+
 }