Merge pull request #15300 from civicrm/5.18
[civicrm-core.git] / CRM / Export / BAO / ExportProcessor.php
index 83322964c877820fd443802b337e955678db94dd..6983115f7506ec9d193e2b5eba27cb6c7dc7cec8 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.
    *
@@ -585,11 +592,28 @@ 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
    */
   public function getQueryFields() {
-    return $this->queryFields;
+    return array_merge(
+      $this->queryFields,
+      $this->getComponentPaymentFields()
+    );
   }
 
   /**
@@ -602,6 +626,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 +749,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 +1281,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 +1297,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 [];
   }
@@ -1408,6 +1433,9 @@ class CRM_Export_BAO_ExportProcessor {
       switch ($fieldSpec['type']) {
         case CRM_Utils_Type::T_INT:
         case CRM_Utils_Type::T_BOOLEAN:
+          if (in_array(CRM_Utils_Array::value('data_type', $fieldSpec), ['Country', 'StateProvince', 'ContactReference'])) {
+            return "$fieldName varchar(255)";
+          }
           return "$fieldName varchar(16)";
 
         case CRM_Utils_Type::T_STRING:
@@ -1464,8 +1492,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)";
 
@@ -2325,7 +2351,14 @@ 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 ($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);
@@ -2354,7 +2387,7 @@ 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;