Merge pull request #14913 from eileenmcnaughton/export
[civicrm-core.git] / CRM / Export / BAO / Export.php
index d8cee057ec0b1f096efd3e236dc9c571c0ae9df0..58350c2431346e5eb0e6cda8e5b35e7d52ca1010 100644 (file)
@@ -41,88 +41,6 @@ class CRM_Export_BAO_Export {
   // CRM-7675
   const EXPORT_ROW_COUNT = 100000;
 
-  /**
-   * Get Export component
-   *
-   * @param int $exportMode
-   *   Export mode.
-   *
-   * @return string
-   *   CiviCRM Export Component
-   */
-  public static function exportComponent($exportMode) {
-    switch ($exportMode) {
-      case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
-        $component = 'civicrm_contribution';
-        break;
-
-      case CRM_Export_Form_Select::EVENT_EXPORT:
-        $component = 'civicrm_participant';
-        break;
-
-      case CRM_Export_Form_Select::MEMBER_EXPORT:
-        $component = 'civicrm_membership';
-        break;
-
-      case CRM_Export_Form_Select::PLEDGE_EXPORT:
-        $component = 'civicrm_pledge';
-        break;
-
-      case CRM_Export_Form_Select::GRANT_EXPORT:
-        $component = 'civicrm_grant';
-        break;
-    }
-    return $component;
-  }
-
-  /**
-   * Get Query Group By Clause
-   * @param \CRM_Export_BAO_ExportProcessor $processor
-   *   Export Mode
-   * @param object $query
-   *   CRM_Contact_BAO_Query
-   *
-   * @return string
-   *   Group By Clause
-   */
-  public static function getGroupBy($processor, $query) {
-    $groupBy = NULL;
-    $returnProperties = $processor->getReturnProperties();
-    $exportMode = $processor->getExportMode();
-    $queryMode = $processor->getQueryMode();
-    if (!empty($returnProperties['tags']) || !empty($returnProperties['groups']) ||
-      CRM_Utils_Array::value('notes', $returnProperties) ||
-      // CRM-9552
-      ($queryMode & CRM_Contact_BAO_Query::MODE_CONTACTS && $query->_useGroupBy)
-    ) {
-      $groupBy = "contact_a.id";
-    }
-
-    switch ($exportMode) {
-      case CRM_Export_Form_Select::CONTRIBUTE_EXPORT:
-        $groupBy = 'civicrm_contribution.id';
-        if (CRM_Contribute_BAO_Query::isSoftCreditOptionEnabled()) {
-          // especial group by  when soft credit columns are included
-          $groupBy = ['contribution_search_scredit_combined.id', 'contribution_search_scredit_combined.scredit_id'];
-        }
-        break;
-
-      case CRM_Export_Form_Select::EVENT_EXPORT:
-        $groupBy = 'civicrm_participant.id';
-        break;
-
-      case CRM_Export_Form_Select::MEMBER_EXPORT:
-        $groupBy = "civicrm_membership.id";
-        break;
-    }
-
-    if ($queryMode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
-      $groupBy = "civicrm_activity.id ";
-    }
-
-    return $groupBy ? ' GROUP BY ' . implode(', ', (array) $groupBy) : '';
-  }
-
   /**
    * Get the list the export fields.
    *
@@ -194,77 +112,24 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       CRM_Contact_BAO_ProximityQuery::fixInputParams($params);
     }
     // @todo everything from this line up should go back to the calling functions.
-    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress);
+    $processor = new CRM_Export_BAO_ExportProcessor($exportMode, $fields, $queryOperator, $mergeSameHousehold, $isPostalOnly, $mergeSameAddress, $exportParams);
     if ($moreReturnProperties) {
       $processor->setAdditionalRequestedReturnProperties($moreReturnProperties);
     }
+    $processor->setComponentTable($componentTable);
+    $processor->setComponentClause($componentClause);
 
-    list($query, $select, $from, $where, $having) = $processor->runQuery($params, $order);
+    list($query, $queryString) = $processor->runQuery($params, $order);
 
     // This perhaps only needs calling when $mergeSameHousehold == 1
     self::buildRelatedContactArray($selectAll, $ids, $processor, $componentTable);
 
-    $whereClauses = ['trash_clause' => "contact_a.is_deleted != 1"];
-    if (!$selectAll && $componentTable) {
-      $from .= " INNER JOIN $componentTable ctTable ON ctTable.contact_id = contact_a.id ";
-    }
-    elseif ($componentClause) {
-      $whereClauses[] = $componentClause;
-    }
-
-    // CRM-13982 - check if is deleted
-    foreach ($params as $value) {
-      if ($value[0] == 'contact_is_deleted') {
-        unset($whereClauses['trash_clause']);
-      }
-    }
-
-    if (empty($where)) {
-      $where = "WHERE " . implode(' AND ', $whereClauses);
-    }
-    else {
-      $where .= " AND " . implode(' AND ', $whereClauses);
-    }
-
-    $queryString = "$select $from $where $having";
-
-    $groupBy = self::getGroupBy($processor, $query);
-
-    $queryString .= $groupBy;
-
-    if ($order) {
-      // always add contact_a.id to the ORDER clause
-      // so the order is deterministic
-      //CRM-15301
-      if (strpos('contact_a.id', $order) === FALSE) {
-        $order .= ", contact_a.id";
-      }
-
-      list($field, $dir) = explode(' ', $order, 2);
-      $field = trim($field);
-      if (!empty($processor->getReturnProperties()[$field])) {
-        //CRM-15301
-        $queryString .= " ORDER BY $order";
-      }
-    }
-
     $addPaymentHeader = FALSE;
 
-    list($outputColumns, $metadata) = self::getExportStructureArrays($processor);
+    list($outputColumns, $metadata) = $processor->getExportStructureArrays();
 
     if ($processor->isMergeSameAddress()) {
-      //make sure the addressee fields are selected
-      //while using merge same address feature
-      // some columns are required for assistance incase they are not already present
-      $exportParams['merge_same_address']['temp_columns'] = $processor->getAdditionalFieldsForSameAddressMerge();
-      // This is silly - we should do this at the point when the array is used...
-      if (isset($exportParams['merge_same_address']['temp_columns']['id'])) {
-        unset($exportParams['merge_same_address']['temp_columns']['id']);
-        $exportParams['merge_same_address']['temp_columns']['civicrm_primary_id'] = 1;
-      }
-      // @todo - this is a temp fix  - ideally later we don't set stuff only to unset it.
-      // test exists covering this...
-      foreach (array_keys($exportParams['merge_same_address']['temp_columns']) as $field) {
+      foreach (array_keys($processor->getAdditionalFieldsForSameAddressMerge()) as $field) {
         $processor->setColumnAsCalculationOnly($field);
       }
     }
@@ -295,9 +160,8 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
 
     $count = -1;
 
-    $headerRows = $processor->getHeaderRows();
     $sqlColumns = $processor->getSQLColumns();
-    $processor->setTemporaryTable(self::createTempTable($sqlColumns));
+    $processor->createTempTable();
     $limitReached = FALSE;
 
     while (!$limitReached) {
@@ -312,7 +176,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
       while ($iterationDAO->fetch()) {
         $count++;
         $rowsThisIteration++;
-        $row = $processor->buildRow($query, $iterationDAO, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader, $processor);
+        $row = $processor->buildRow($query, $iterationDAO, $outputColumns, $metadata, $paymentDetails, $addPaymentHeader);
         if ($row === FALSE) {
           continue;
         }
@@ -334,29 +198,21 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
     }
 
     if ($processor->getTemporaryTable()) {
-      self::writeDetailsToTable($processor, $componentDetails, $sqlColumns);
+      self::writeDetailsToTable($processor, $componentDetails);
 
       // do merge same address and merge same household processing
       if ($mergeSameAddress) {
-        self::mergeSameAddress($processor, $sqlColumns, $exportParams);
-      }
-
-      // call export hook
-      $table = $processor->getTemporaryTable();
-      CRM_Utils_Hook::export($table, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
-      if ($table !== $processor->getTemporaryTable()) {
-        CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
-        $processor->setTemporaryTable($table);
+        $processor->mergeSameAddress();
       }
 
       // In order to be able to write a unit test against this function we need to suppress
       // the csv writing. In future hopefully the csv writing & the main processing will be in separate functions.
       if (empty($exportParams['suppress_csv_for_testing'])) {
-        self::writeCSVFromTable($headerRows, $sqlColumns, $processor);
+        self::writeCSVFromTable($processor);
       }
       else {
         // return tableName sqlColumns headerRows in test context
-        return [$processor->getTemporaryTable(), $sqlColumns, $headerRows, $processor];
+        return [$processor->getTemporaryTable(), $sqlColumns, $processor->getHeaderRows(), $processor];
       }
 
       // delete the export temp table and component table
@@ -464,9 +320,8 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c
   /**
    * @param \CRM_Export_BAO_ExportProcessor $processor
    * @param $details
-   * @param $sqlColumns
    */
-  public static function writeDetailsToTable($processor, $details, $sqlColumns) {
+  public static function writeDetailsToTable($processor, $details) {
     $tableName = $processor->getTemporaryTable();
     if (empty($details)) {
       return;
@@ -497,7 +352,7 @@ FROM   $tableName
       }
       $sqlClause[] = '(' . implode(',', $valueString) . ')';
     }
-    $sqlColumns = array_merge(['id' => 1], $sqlColumns);
+    $sqlColumns = array_merge(['id' => 1], $processor->getSQLColumns());
     $sqlColumnString = '(' . implode(',', array_keys($sqlColumns)) . ')';
 
     $sqlValueString = implode(",\n", $sqlClause);
@@ -509,180 +364,19 @@ VALUES $sqlValueString
     CRM_Core_DAO::executeQuery($sql);
   }
 
-  /**
-   * @param $sqlColumns
-   *
-   * @return string
-   */
-  public static function createTempTable($sqlColumns) {
-    //creating a temporary table for the search result that need be exported
-    $exportTempTable = CRM_Utils_SQL_TempTable::build()->setDurable()->setCategory('export');
-
-    // also create the sql table
-    $exportTempTable->drop();
-
-    $sql = " id int unsigned NOT NULL AUTO_INCREMENT, ";
-    if (!empty($sqlColumns)) {
-      $sql .= implode(",\n", array_values($sqlColumns)) . ',';
-    }
-
-    $sql .= "\n PRIMARY KEY ( id )";
-
-    // add indexes for street_address and household_name if present
-    $addIndices = [
-      'street_address',
-      'household_name',
-      'civicrm_primary_id',
-    ];
-
-    foreach ($addIndices as $index) {
-      if (isset($sqlColumns[$index])) {
-        $sql .= ",
-  INDEX index_{$index}( $index )
-";
-      }
-    }
-
-    $exportTempTable->createWithColumns($sql);
-    return $exportTempTable->getName();
-  }
-
   /**
    * @param \CRM_Export_BAO_ExportProcessor $processor
-   * @param $sqlColumns
-   * @param array $exportParams
    */
-  public static function mergeSameAddress($processor, &$sqlColumns, $exportParams) {
-    $greetingOptions = CRM_Export_Form_Select::getGreetingOptions();
-
-    if (!empty($greetingOptions)) {
-      // Greeting options is keyed by 'postal_greeting' or 'addressee'.
-      foreach ($greetingOptions as $key => $value) {
-        if ($option = CRM_Utils_Array::value($key, $exportParams)) {
-          if ($greetingOptions[$key][$option] == ts('Other')) {
-            $exportParams[$key] = $exportParams["{$key}_other"];
-          }
-          elseif ($greetingOptions[$key][$option] == ts('List of names')) {
-            $exportParams[$key] = '';
-          }
-          else {
-            $exportParams[$key] = $greetingOptions[$key][$option];
-          }
-        }
-      }
-    }
-    $tableName = $processor->getTemporaryTable();
-    // check if any records are present based on if they have used shared address feature,
-    // and not based on if city / state .. matches.
-    $sql = "
-SELECT    r1.id                 as copy_id,
-          r1.civicrm_primary_id as copy_contact_id,
-          r1.addressee          as copy_addressee,
-          r1.addressee_id       as copy_addressee_id,
-          r1.postal_greeting    as copy_postal_greeting,
-          r1.postal_greeting_id as copy_postal_greeting_id,
-          r2.id                 as master_id,
-          r2.civicrm_primary_id as master_contact_id,
-          r2.postal_greeting    as master_postal_greeting,
-          r2.postal_greeting_id as master_postal_greeting_id,
-          r2.addressee          as master_addressee,
-          r2.addressee_id       as master_addressee_id
-FROM      $tableName r1
-INNER JOIN civicrm_address adr ON r1.master_id   = adr.id
-INNER JOIN $tableName      r2  ON adr.contact_id = r2.civicrm_primary_id
-ORDER BY  r1.id";
-    $linkedMerge = $processor->buildMasterCopyArray($sql, $exportParams, TRUE);
-
-    // find all the records that have the same street address BUT not in a household
-    // require match on city and state as well
-    $sql = "
-SELECT    r1.id                 as master_id,
-          r1.civicrm_primary_id as master_contact_id,
-          r1.postal_greeting    as master_postal_greeting,
-          r1.postal_greeting_id as master_postal_greeting_id,
-          r1.addressee          as master_addressee,
-          r1.addressee_id       as master_addressee_id,
-          r2.id                 as copy_id,
-          r2.civicrm_primary_id as copy_contact_id,
-          r2.postal_greeting    as copy_postal_greeting,
-          r2.postal_greeting_id as copy_postal_greeting_id,
-          r2.addressee          as copy_addressee,
-          r2.addressee_id       as copy_addressee_id
-FROM      $tableName r1
-LEFT JOIN $tableName r2 ON ( r1.street_address = r2.street_address AND
-               r1.city = r2.city AND
-               r1.state_province_id = r2.state_province_id )
-WHERE     ( r1.household_name IS NULL OR r1.household_name = '' )
-AND       ( r2.household_name IS NULL OR r2.household_name = '' )
-AND       ( r1.street_address != '' )
-AND       r2.id > r1.id
-ORDER BY  r1.id
-";
-    $merge = $processor->buildMasterCopyArray($sql, $exportParams);
-
-    // unset ids from $merge already present in $linkedMerge
-    foreach ($linkedMerge as $masterID => $values) {
-      $keys = [$masterID];
-      $keys = array_merge($keys, array_keys($values['copy']));
-      foreach ($merge as $mid => $vals) {
-        if (in_array($mid, $keys)) {
-          unset($merge[$mid]);
-        }
-        else {
-          foreach ($values['copy'] as $copyId) {
-            if (in_array($copyId, $keys)) {
-              unset($merge[$mid]['copy'][$copyId]);
-            }
-          }
-        }
-      }
-    }
-    $merge = $merge + $linkedMerge;
-
-    foreach ($merge as $masterID => $values) {
-      $sql = "
-UPDATE $tableName
-SET    addressee = %1, postal_greeting = %2, email_greeting = %3
-WHERE  id = %4
-";
-      $params = [
-        1 => [$values['addressee'], 'String'],
-        2 => [$values['postalGreeting'], 'String'],
-        3 => [$values['emailGreeting'], 'String'],
-        4 => [$masterID, 'Integer'],
-      ];
-      CRM_Core_DAO::executeQuery($sql, $params);
-
-      // delete all copies
-      $deleteIDs = array_keys($values['copy']);
-      $deleteIDString = implode(',', $deleteIDs);
-      $sql = "
-DELETE FROM $tableName
-WHERE  id IN ( $deleteIDString )
-";
-      CRM_Core_DAO::executeQuery($sql);
-    }
-
-    // unset temporary columns that were added for postal mailing format
-    // @todo - this part is pretty close to ready to be removed....
-    if (!empty($exportParams['merge_same_address']['temp_columns'])) {
-      $unsetKeys = array_keys($sqlColumns);
-      foreach ($unsetKeys as $headerKey => $sqlColKey) {
-        if (array_key_exists($sqlColKey, $exportParams['merge_same_address']['temp_columns'])) {
-          unset($sqlColumns[$sqlColKey]);
-        }
-      }
+  public static function writeCSVFromTable($processor) {
+    // call export hook
+    $headerRows = $processor->getHeaderRows();
+    $exportTempTable = $processor->getTemporaryTable();
+    CRM_Utils_Hook::export($exportTempTable, $headerRows, $sqlColumns, $exportMode, $componentTable, $ids);
+    if ($exportTempTable !== $processor->getTemporaryTable()) {
+      CRM_Core_Error::deprecatedFunctionWarning('altering the export table in the hook is deprecated (in some flows the table itself will be)');
+      $processor->setTemporaryTable($exportTempTable);
     }
-  }
-
-  /**
-   * @param $headerRows
-   * @param $sqlColumns
-   * @param \CRM_Export_BAO_ExportProcessor $processor
-   */
-  public static function writeCSVFromTable($headerRows, $sqlColumns, $processor) {
     $exportTempTable = $processor->getTemporaryTable();
-    $exportMode = $processor->getExportMode();
     $writeHeader = TRUE;
     $offset = 0;
     $limit = self::EXPORT_ROW_COUNT;
@@ -703,7 +397,7 @@ LIMIT $offset, $limit
       while ($dao->fetch()) {
         $row = [];
 
-        foreach ($sqlColumns as $column => $dontCare) {
+        foreach (array_keys($processor->getSQLColumns()) as $column) {
           $row[$column] = $dao->$column;
         }
         $componentDetails[] = $row;
@@ -720,220 +414,6 @@ LIMIT $offset, $limit
     }
   }
 
-  /**
-   * Build componentPayment fields.
-   *
-   * This is no longer used by export but BAO_Mapping still calls it & we
-   * should find a generic way to handle this or move this to that class.
-   *
-   * @deprecated
-   */
-  public static function componentPaymentFields() {
-    static $componentPaymentFields;
-    if (!isset($componentPaymentFields)) {
-      $componentPaymentFields = [
-        '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'),
-      ];
-    }
-    return $componentPaymentFields;
-  }
-
-  /**
-   * Get the various arrays that we use to structure our output.
-   *
-   * The extraction of these has been moved to a separate function for clarity and so that
-   * tests can be added - in particular on the $outputHeaders array.
-   *
-   * However it still feels a bit like something that I'm too polite to write down and this should be seen
-   * as a step on the refactoring path rather than how it should be.
-   *
-   * @param \CRM_Export_BAO_ExportProcessor $processor
-   *
-   * @return array
-   *   - outputColumns Array of columns to be exported. The values don't matter but the key must match the
-   *   alias for the field generated by BAO_Query object.
-   *   - headerRows Array of the column header strings to put in the csv header - non-associative.
-   *   - sqlColumns Array of column names for the temp table. Not too sure why outputColumns can't be used here.
-   *   - metadata Array of fields with specific parameters to pass to the translate function or another hacky nasty solution
-   *    I'm too embarassed to discuss here.
-   *    The keys need
-   *    - to match the outputColumns keys (yes, the fact we ignore the output columns values & then pass another array with values
-   *    we could use does suggest further refactors. However, you future improver, do remember that every check you do
-   *    in the main DAO loop is done once per row & that coule be 100,000 times.)
-   *    Finally a pop quiz: We need the translate context because we use a function other than ts() - is this because
-   *    - a) the function used is more efficient or
-   *    - b) this code is old & outdated. Submit your answers to circular bin or better
-   *       yet find a way to comment them for posterity.
-   */
-  public static function getExportStructureArrays($processor) {
-    $outputColumns = $metadata = [];
-    $queryFields = $processor->getQueryFields();
-    foreach ($processor->getReturnProperties() as $key => $value) {
-      if (($key != 'location' || !is_array($value)) && !$processor->isRelationshipTypeKey($key)) {
-        $outputColumns[$key] = $value;
-        $processor->addOutputSpecification($key);
-      }
-      elseif ($processor->isRelationshipTypeKey($key)) {
-        $outputColumns[$key] = $value;
-        foreach ($value as $relationField => $relationValue) {
-          // below block is same as primary block (duplicate)
-          if (isset($queryFields[$relationField]['title'])) {
-            $processor->addOutputSpecification($relationField, $key);
-          }
-          elseif (is_array($relationValue) && $relationField == 'location') {
-            // fix header for location type case
-            foreach ($relationValue as $ltype => $val) {
-              foreach (array_keys($val) as $fld) {
-                $type = explode('-', $fld);
-                $processor->addOutputSpecification($type[0], $key, $ltype, CRM_Utils_Array::value(1, $type));
-              }
-            }
-          }
-        }
-      }
-      else {
-        foreach ($value as $locationType => $locationFields) {
-          foreach (array_keys($locationFields) as $locationFieldName) {
-            $type = explode('-', $locationFieldName);
-
-            $actualDBFieldName = $type[0];
-            $daoFieldName = CRM_Utils_String::munge($locationType) . '-' . $actualDBFieldName;
-
-            if (!empty($type[1])) {
-              $daoFieldName .= "-" . $type[1];
-            }
-            $processor->addOutputSpecification($actualDBFieldName, NULL, $locationType, CRM_Utils_Array::value(1, $type));
-            $metadata[$daoFieldName] = $processor->getMetaDataForField($actualDBFieldName);
-            $outputColumns[$daoFieldName] = TRUE;
-          }
-        }
-      }
-    }
-    return [$outputColumns, $metadata];
-  }
-
-  /**
-   * Get the values of linked household contact.
-   *
-   * @param CRM_Core_DAO $relDAO
-   * @param array $value
-   * @param string $field
-   * @param array $row
-   */
-  private static function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
-    $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
-    $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id');
-    $i18n = CRM_Core_I18n::singleton();
-    $field = $field . '_';
-
-    foreach ($value as $relationField => $relationValue) {
-      if (is_object($relDAO) && property_exists($relDAO, $relationField)) {
-        $fieldValue = $relDAO->$relationField;
-        if ($relationField == 'phone_type_id') {
-          $fieldValue = $phoneTypes[$relationValue];
-        }
-        elseif ($relationField == 'provider_id') {
-          $fieldValue = CRM_Utils_Array::value($relationValue, $imProviders);
-        }
-        // CRM-13995
-        elseif (is_object($relDAO) && in_array($relationField, [
-          'email_greeting',
-          'postal_greeting',
-          'addressee',
-        ])) {
-          //special case for greeting replacement
-          $fldValue = "{$relationField}_display";
-          $fieldValue = $relDAO->$fldValue;
-        }
-      }
-      elseif (is_object($relDAO) && $relationField == 'state_province') {
-        $fieldValue = CRM_Core_PseudoConstant::stateProvince($relDAO->state_province_id);
-      }
-      elseif (is_object($relDAO) && $relationField == 'country') {
-        $fieldValue = CRM_Core_PseudoConstant::country($relDAO->country_id);
-      }
-      else {
-        $fieldValue = '';
-      }
-      $relPrefix = $field . $relationField;
-
-      if (is_object($relDAO) && $relationField == 'id') {
-        $row[$relPrefix] = $relDAO->contact_id;
-      }
-      elseif (is_array($relationValue) && $relationField == 'location') {
-        foreach ($relationValue as $ltype => $val) {
-          // If the location name has a space in it the we need to handle that. This
-          // is kinda hacky but specifically covered in the ExportTest so later efforts to
-          // improve it should be secure in the knowled it will be caught.
-          $ltype = str_replace(' ', '_', $ltype);
-          foreach (array_keys($val) as $fld) {
-            $type = explode('-', $fld);
-            $fldValue = "{$ltype}-" . $type[0];
-            if (!empty($type[1])) {
-              $fldValue .= "-" . $type[1];
-            }
-            // CRM-3157: localise country, region (both have ‘country’ context)
-            // and state_province (‘province’ context)
-            switch (TRUE) {
-              case (!is_object($relDAO)):
-                $row[$field . '_' . $fldValue] = '';
-                break;
-
-              case in_array('country', $type):
-              case in_array('world_region', $type):
-                $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
-                  ['context' => 'country']
-                );
-                break;
-
-              case in_array('state_province', $type):
-                $row[$field . '_' . $fldValue] = $i18n->crm_translate($relDAO->$fldValue,
-                  ['context' => 'province']
-                );
-                break;
-
-              default:
-                $row[$field . '_' . $fldValue] = $relDAO->$fldValue;
-                break;
-            }
-          }
-        }
-      }
-      elseif (isset($fieldValue) && $fieldValue != '') {
-        //check for custom data
-        if ($cfID = CRM_Core_BAO_CustomField::getKeyID($relationField)) {
-          $row[$relPrefix] = CRM_Core_BAO_CustomField::displayValue($fieldValue, $cfID);
-        }
-        else {
-          //normal relationship fields
-          // CRM-3157: localise country, region (both have ‘country’ context) and state_province (‘province’ context)
-          switch ($relationField) {
-            case 'country':
-            case 'world_region':
-              $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'country']);
-              break;
-
-            case 'state_province':
-              $row[$relPrefix] = $i18n->crm_translate($fieldValue, ['context' => 'province']);
-              break;
-
-            default:
-              $row[$relPrefix] = $fieldValue;
-              break;
-          }
-        }
-      }
-      else {
-        // if relation field is empty or null
-        $row[$relPrefix] = '';
-      }
-    }
-  }
-
   /**
    * Get the ids that we want to get related contact details for.
    *
@@ -960,13 +440,19 @@ LIMIT $offset, $limit
       }
       return $relIDs;
     }
-    $component = self::exportComponent($exportMode);
+    $componentMapping = [
+      CRM_Export_Form_Select::CONTRIBUTE_EXPORT => 'civicrm_contribution',
+      CRM_Export_Form_Select::EVENT_EXPORT => 'civicrm_participant',
+      CRM_Export_Form_Select::MEMBER_EXPORT => 'civicrm_membership',
+      CRM_Export_Form_Select::PLEDGE_EXPORT => 'civicrm_pledge',
+      CRM_Export_Form_Select::GRANT_EXPORT => 'civicrm_grant',
+    ];
 
     if ($exportMode == CRM_Export_Form_Select::CASE_EXPORT) {
       return CRM_Case_BAO_Case::retrieveContactIdsByCaseId($ids);
     }
     else {
-      return CRM_Core_DAO::getContactIDsFromComponent($ids, $component);
+      return CRM_Core_DAO::getContactIDsFromComponent($ids, $componentMapping[$exportMode]);
     }
   }
 
@@ -1027,10 +513,10 @@ LIMIT $offset, $limit
         $relationQuery->convertToPseudoNames($allRelContactDAO);
         $row = [];
         // @todo pass processor to fetchRelationshipDetails and set fields directly within it.
-        self::fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row);
+        $processor->fetchRelationshipDetails($allRelContactDAO, $relationReturnProperties, $relationshipKey, $row);
         foreach (array_keys($relationReturnProperties) as $property) {
           if ($property === 'location') {
-            // @todo - simplify location in self::fetchRelationshipDetails - remove handling here. Or just call
+            // @todo - simplify location in fetchRelationshipDetails - remove handling here. Or just call
             // $processor->setRelationshipValue from fetchRelationshipDetails
             foreach ($relationReturnProperties['location'] as $locationName => $locationValues) {
               foreach (array_keys($locationValues) as $locationValue) {