Skip to content

Commit

Permalink
Merge pull request #14932 from eileenmcnaughton/export_fn_move
Browse files Browse the repository at this point in the history
[REF] [Export] Move writeToTable fn to exportProcessor
  • Loading branch information
seamuslee001 authored Jul 31, 2019
2 parents e6939f6 + b614951 commit 880200a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 51 deletions.
52 changes: 1 addition & 51 deletions CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public static function exportComponents(
// 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($processor);
$processor->writeCSVFromTable();
}
else {
// return tableName sqlColumns headerRows in test context
Expand Down Expand Up @@ -364,56 +364,6 @@ public static function writeDetailsToTable($processor, $details) {
CRM_Core_DAO::executeQuery($sql);
}

/**
* @param \CRM_Export_BAO_ExportProcessor $processor
*/
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);
}
$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;
}
}

/**
* Get the ids that we want to get related contact details for.
*
Expand Down
53 changes: 53 additions & 0 deletions CRM/Export/BAO/ExportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2323,4 +2323,57 @@ public function fetchRelationshipDetails($relDAO, $value, $field, &$row) {
}
}

/**
* 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;
}
}

}

0 comments on commit 880200a

Please sign in to comment.