Alter CRM_Export_BAO_Export::exportComponents to accept standard MappingField format
authorColeman Watts <coleman@civicrm.org>
Thu, 11 Jul 2019 21:24:51 +0000 (17:24 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 11 Jul 2019 23:36:37 +0000 (19:36 -0400)
CRM/Export/BAO/Export.php
CRM/Export/BAO/ExportProcessor.php
CRM/Export/Form/Map.php
tests/phpunit/CRM/Export/BAO/ExportTest.php

index 82c3715b09c466202e18ff7cb3dedac255faf812..f9bd84c48d90295b70a82c6ef3f99397655bd7e6 100644 (file)
@@ -177,10 +177,6 @@ class CRM_Export_BAO_Export {
       isset($exportParams['postal_mailing_export']['postal_mailing_export']) &&
       $exportParams['postal_mailing_export']['postal_mailing_export'] == 1
     );
-    $mappedFields = [];
-    foreach ((array) $fields as $field) {
-      $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
-    }
 
     if (!$selectAll && $componentTable && !empty($exportParams['additional_group'])) {
       // If an Additional Group is selected, then all contacts in that group are
@@ -192,7 +188,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       );
     }
 
-    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $mappedFields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress);
+    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress);
     if ($moreReturnProperties) {
       $processor->setAdditionalRequestedReturnProperties($moreReturnProperties);
     }
index 2a0379a917f5a42327eb389e7feb3fdb0d40a504..468e2f3baa07eb8134d139755232c4163e8538ec 100644 (file)
@@ -1556,7 +1556,7 @@ class CRM_Export_BAO_ExportProcessor {
       $returnProperties = [];
       foreach ($this->getRequestedFields() as $key => $value) {
         $fieldName = $value['name'];
-        $locationName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $value['location_type_id']);
+        $locationName = !empty($value['location_type_id']) ? CRM_Core_PseudoConstant::getName('CRM_Core_BAO_Address', 'location_type_id', $value['location_type_id']) : NULL;
         $relationshipTypeKey = !empty($value['relationship_type_id']) ? $value['relationship_type_id'] . '_' . $value['relationship_direction'] : NULL;
         if (!$fieldName || $this->isHouseholdMergeRelationshipTypeKey($relationshipTypeKey)) {
           continue;
index ea955205cb4a323f406e024d77161d1bbfa175cf..38b961ae6b9d2743cc57416599a02f97222e0ff1 100644 (file)
@@ -183,14 +183,14 @@ class CRM_Export_Form_Map extends CRM_Core_Form {
 
     $mapperKeys = $params['mapper'][1];
 
-    $checkEmpty = 0;
-    foreach ($mapperKeys as $value) {
-      if ($value[0]) {
-        $checkEmpty++;
+    $mappedFields = [];
+    foreach ((array) $mapperKeys as $field) {
+      if (!empty($field[1])) {
+        $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
       }
     }
 
-    if (!$checkEmpty) {
+    if (!$mappedFields) {
       $this->set('mappingId', NULL);
       CRM_Utils_System::redirect(CRM_Utils_System::url($currentPath, '_qf_Map_display=true' . $urlParams));
     }
@@ -220,7 +220,7 @@ class CRM_Export_Form_Map extends CRM_Core_Form {
       $this->get('componentIds'),
       (array) $this->get('queryParams'),
       $this->get(CRM_Utils_Sort::SORT_ORDER),
-      $mapperKeys,
+      $mappedFields,
       $this->get('returnProperties'),
       $this->get('exportMode'),
       $this->get('componentClause'),
index f0c15879c28f9ca77b139c19d9603e9acd672e37..81cee323d40f3e1ed7cd00913c46f6088f46bfb9 100644 (file)
@@ -229,8 +229,8 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
   public function testExportComponentsActivity() {
     $this->setUpActivityExportData();
     $selectedFields = [
-      ['Individual', 'display_name'],
-      ['Individual', '5_a_b', 'display_name'],
+      ['contact_type' => 'Individual', 'name' => 'display_name'],
+      ['contact_type' => 'Individual', 'relationship_type_id' => '5', 'relationship_direction' => 'a_b', 'name' => 'display_name'],
     ];
 
     list($tableName) = CRM_Export_BAO_Export::exportComponents(
@@ -465,7 +465,10 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $this->setUpContributionExportData();
     $campaign = $this->callAPISuccess('Campaign', 'create', ['title' => 'Big campaign']);
     $this->callAPISuccess('Contribution', 'create', ['campaign_id' => 'Big_campaign', 'id' => $this->contributionIDs[0]]);
-    $selectedFields = [['Individual', 'gender_id'], ['Contribution', 'contribution_campaign_title']];
+    $selectedFields = [
+      ['contact_type' => 'Individual', 'name' => 'gender_id'],
+      ['contact_type' => 'Contribution', 'name' => 'contribution_campaign_title'],
+    ];
     list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
       TRUE,
       $this->contactIDs[1],
@@ -538,11 +541,11 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
       $this->contactIDs[] = $householdID;
     }
     $selectedFields = [
-      ['Individual', $houseHoldTypeID . '_a_b', 'state_province', ''],
-      ['Individual', $houseHoldTypeID . '_a_b', 'city', ''],
-      ['Individual', 'city', ''],
-      ['Individual', 'state_province', ''],
-      ['Individual', 'contact_source', ''],
+      ['contact_type' => 'Individual', 'relationship_type_id' => $houseHoldTypeID, 'relationship_direction' => 'a_b', 'name' => 'state_province', 'location_type_id' => ''],
+      ['contact_type' => 'Individual', 'relationship_type_id' => $houseHoldTypeID, 'relationship_direction' => 'a_b', 'name' => 'city', 'location_type_id' => ''],
+      ['contact_type' => 'Individual', 'name' => 'city', 'location_type_id' => ''],
+      ['contact_type' => 'Individual', 'name' => 'state_province', 'location_type_id' => ''],
+      ['contact_type' => 'Individual', 'name' => 'contact_source', 'location_type_id' => ''],
     ];
     list($tableName, $sqlColumns, $headerRows) = CRM_Export_BAO_Export::exportComponents(
       FALSE,
@@ -957,7 +960,7 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
 
     //export the master address for contact B
     $selectedFields = [
-      ['Individual', 'master_id', 1],
+      ['contact_type' => 'Individual', 'name' => 'master_id', 'location_type_id' => 1],
     ];
     list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
       TRUE,
@@ -1128,12 +1131,16 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
    */
   protected function doExport($selectedFields, $id, $exportMode = CRM_Export_Form_Select::CONTACT_EXPORT) {
     $ids = (array) $id;
+    $mappedFields = [];
+    foreach ((array) $selectedFields as $field) {
+      $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
+    }
     list($tableName, $sqlColumns) = CRM_Export_BAO_Export::exportComponents(
       TRUE,
       $ids,
       [],
       NULL,
-      $selectedFields,
+      $mappedFields,
       NULL,
       $exportMode,
       "contact_a.id IN (" . implode(',', $ids) . ")",
@@ -2661,12 +2668,16 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase {
     $this->startCapturingOutput();
     try {
       $defaultClause = (empty($params['ids']) ? NULL : "contact_a.id IN (" . implode(',', $params['ids']) . ")");
+      $mappedFields = [];
+      foreach (CRM_Utils_Array::value('fields', $params, []) as $field) {
+        $mappedFields[] = CRM_Core_BAO_Mapping::getMappingParams([], $field);
+      }
       CRM_Export_BAO_Export::exportComponents(
         CRM_Utils_Array::value('selectAll', $params, (empty($params['fields']))),
         CRM_Utils_Array::value('ids', $params, []),
         CRM_Utils_Array::value('params', $params, []),
         CRM_Utils_Array::value('order', $params),
-        CRM_Utils_Array::value('fields', $params),
+        $mappedFields,
         CRM_Utils_Array::value('moreReturnProperties', $params),
         CRM_Utils_Array::value('exportMode', $params, CRM_Export_Form_Select::CONTACT_EXPORT),
         CRM_Utils_Array::value('componentClause', $params, $defaultClause),