X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FExport%2FBAO%2FExport.php;h=a95df716d8f73142bfe454d3668ead18e346a5e0;hb=7aed0a9c53d51cc2f21f725d33d8dfb7aa1641c6;hp=6b5fdf29468f8c8217d29de2840ed5338e686718;hpb=e6b7b594b88d8fe6879310284351a531f1edd2e2;p=civicrm-core.git diff --git a/CRM/Export/BAO/Export.php b/CRM/Export/BAO/Export.php index 6b5fdf2946..a95df716d8 100644 --- a/CRM/Export/BAO/Export.php +++ b/CRM/Export/BAO/Export.php @@ -41,40 +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 the list the export fields. * @@ -104,9 +70,6 @@ class CRM_Export_BAO_Export { * @param array $exportParams * @param string $queryOperator * - * @return array|null - * An array can be requested from within a unit test. - * * @throws \CRM_Core_Exception */ public static function exportComponents( @@ -152,6 +115,7 @@ INSERT INTO {$componentTable} SELECT distinct gc.contact_id FROM civicrm_group_c } $processor->setComponentTable($componentTable); $processor->setComponentClause($componentClause); + $processor->setIds($ids); list($query, $queryString) = $processor->runQuery($params, $order); @@ -194,9 +158,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) { @@ -233,30 +196,14 @@ 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) { $processor->mergeSameAddress(); } - // 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); - } - - // 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); - } - else { - // return tableName sqlColumns headerRows in test context - return [$processor->getTemporaryTable(), $sqlColumns, $headerRows, $processor]; - } + $processor->writeCSVFromTable(); // delete the export temp table and component table $sql = "DROP TABLE IF EXISTS " . $processor->getTemporaryTable(); @@ -363,9 +310,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; @@ -396,7 +342,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); @@ -408,228 +354,6 @@ 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 $headerRows - * @param $sqlColumns - * @param \CRM_Export_BAO_ExportProcessor $processor - */ - public static function writeCSVFromTable($headerRows, $sqlColumns, $processor) { - $exportTempTable = $processor->getTemporaryTable(); - $writeHeader = TRUE; - $offset = 0; - $limit = self::EXPORT_ROW_COUNT; - - $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($processor->getSQLColumns()) as $column) { - $row[$column] = $dao->$column; - } - $componentDetails[] = $row; - } - CRM_Core_Report_Excel::writeCSVFile($processor->getExportFileName(), - $headerRows, - $componentDetails, - NULL, - $writeHeader - ); - - $writeHeader = FALSE; - $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 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. * @@ -656,13 +380,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]); } } @@ -723,10 +453,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) {