Skip to content
Snippets Groups Projects
Commit fb57665d authored by jaapjansma's avatar jaapjansma
Browse files

Fixed issue with double quotes in export to csv

parent 2ec84165
No related branches found
No related tags found
No related merge requests found
...@@ -239,9 +239,9 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface { ...@@ -239,9 +239,9 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
fwrite($file, "\xEF\xBB\xBF"); // BOF this will make sure excel opens the file correctly. fwrite($file, "\xEF\xBB\xBF"); // BOF this will make sure excel opens the file correctly.
$headerLine = array(); $headerLine = array();
foreach($dataProcessor->getDataFlow()->getOutputFieldHandlers() as $outputHandler) { foreach($dataProcessor->getDataFlow()->getOutputFieldHandlers() as $outputHandler) {
$headerLine[] = $outputHandler->getOutputFieldSpecification()->title; $headerLine[] = self::encodeValue($outputHandler->getOutputFieldSpecification()->title, $configuration['escape_char'], $configuration['enclosure']);
} }
fputcsv($file, $headerLine, $configuration['delimiter'], $configuration['enclosure'], $configuration['escape_char']); fwrite($file, implode($configuration['delimiter'], $headerLine)."\r\n");
fclose($file); fclose($file);
} }
...@@ -251,9 +251,9 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface { ...@@ -251,9 +251,9 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
while($record = $dataProcessor->getDataFlow()->nextRecord()) { while($record = $dataProcessor->getDataFlow()->nextRecord()) {
$row = array(); $row = array();
foreach($record as $field => $value) { foreach($record as $field => $value) {
$row[] = $value->formattedValue; $row[] = self::encodeValue($value->formattedValue, $configuration['escape_char'], $configuration['enclosure']);
} }
fputcsv($file, $row, $configuration['delimiter'], $configuration['enclosure'], $configuration['escape_char']); fwrite($file, implode($configuration['delimiter'], $row)."\r\n");
} }
} catch (\Civi\DataProcessor\DataFlow\EndOfFlowException $e) { } catch (\Civi\DataProcessor\DataFlow\EndOfFlowException $e) {
// Do nothing // Do nothing
...@@ -261,6 +261,15 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface { ...@@ -261,6 +261,15 @@ class CRM_DataprocessorOutputExport_CSV implements ExportOutputInterface {
fclose($file); fclose($file);
} }
protected static function encodeValue($value, $escape, $enclosure) {
///remove any ESCAPED double quotes within string.
$value = str_replace("{$escape}{$enclosure}","{$enclosure}",$value);
//then force escape these same double quotes And Any UNESCAPED Ones.
$value = str_replace("{$enclosure}","{$escape}{$enclosure}",$value);
//force wrap value in quotes and return
return "{$enclosure}{$value}{$enclosure}";
}
public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $outputId, $offset, $limit, $sortFieldName = null, $sortDirection = 'ASC') { public static function exportBatch(CRM_Queue_TaskContext $ctx, $filename, $params, $dataProcessorId, $outputId, $offset, $limit, $sortFieldName = null, $sortDirection = 'ASC') {
$dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $dataProcessorId)); $dataProcessor = civicrm_api3('DataProcessor', 'getsingle', array('id' => $dataProcessorId));
$output = civicrm_api3('DataProcessorOutput', 'getsingle', array('id' => $outputId)); $output = civicrm_api3('DataProcessorOutput', 'getsingle', array('id' => $outputId));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment