mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 15:05:42 +00:00
BUGFIX: encode the csv file by hand, fputcsv is not parsing newlines properly
This commit is contained in:
parent
4e6a9ec412
commit
078043f189
@ -124,16 +124,12 @@ class SubmittedFormReportField extends FormField {
|
|||||||
$csvRowItems = array();
|
$csvRowItems = array();
|
||||||
foreach ($csvHeaders as $columnName=>$columnTitle) {
|
foreach ($csvHeaders as $columnName=>$columnTitle) {
|
||||||
if (!isset($row[$columnName])) $csvRowItems[] = ""; // This submission did not have that column, insert blank
|
if (!isset($row[$columnName])) $csvRowItems[] = ""; // This submission did not have that column, insert blank
|
||||||
else $csvRowItems[] = $row[$columnName];
|
else $csvRowItems[] = str_replace('"', '""', $row[$columnName]);
|
||||||
}
|
}
|
||||||
$csvRowItems[] = $row['Submitted'];
|
$csvRowItems[] = $row['Submitted'];
|
||||||
|
|
||||||
// Encode the row
|
// Encode the row by hand (fputcsv fails to encode newlines properly)
|
||||||
$fp = fopen('php://memory', 'r+');
|
if (count($csvRowItems)) $csvData .= "\"".implode($csvRowItems, "\", \"")."\"\n";
|
||||||
fputcsv($fp, $csvRowItems);
|
|
||||||
rewind($fp);
|
|
||||||
$csvData .= fgets($fp);
|
|
||||||
fclose($fp);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
user_error("No submissions to export.", E_USER_ERROR);
|
user_error("No submissions to export.", E_USER_ERROR);
|
||||||
|
@ -48,35 +48,38 @@ class SubmittedFormTest extends FunctionalTest {
|
|||||||
function testCSVExport() {
|
function testCSVExport() {
|
||||||
$export = $this->field->export($this->page->ID);
|
$export = $this->field->export($this->page->ID);
|
||||||
|
|
||||||
// export it back to an array (rather than string)
|
// Pretend we are opening via file
|
||||||
$exportLines = explode("\n", $export);
|
$fp = fopen('php://memory', 'w+');
|
||||||
array_pop($exportLines); // Remove trailing empty line
|
fwrite($fp, $export);
|
||||||
|
rewind($fp);
|
||||||
|
|
||||||
$data = array();
|
$data = array();
|
||||||
foreach($exportLines as $line) {
|
while($data[] = fgetcsv($fp));
|
||||||
$fp = fopen('php://memory', 'w+');
|
array_pop($data);
|
||||||
fputs($fp, $line);
|
fclose($fp);
|
||||||
rewind($fp);
|
|
||||||
$data[] = fgetcsv($fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check the headers are fine and include every legacy field. They should also be ordered
|
// Check the headers are fine and include every legacy field. They should also be ordered
|
||||||
// according to the latest form layout.
|
// according to the latest form layout.
|
||||||
$this->assertEquals($data[0], array(
|
$this->assertEquals($data[0], array(
|
||||||
'First', 'Submitted Title 2', 'Submitted Title', 'Submitted'
|
'First', 'Submitted Title 2', 'Submitted Title', 'Submitted'
|
||||||
));
|
));
|
||||||
|
|
||||||
// check the number of records in the export
|
// Check the number of records in the export
|
||||||
$this->assertEquals(count($data), 12);
|
$this->assertEquals(count($data), 12);
|
||||||
|
|
||||||
|
// Make sure the number of columns matches
|
||||||
$this->assertEquals(count($data[1]), 4);
|
$this->assertEquals(count($data[1]), 4);
|
||||||
$this->assertEquals(count($data[2]), 4);
|
$this->assertEquals(count($data[2]), 4);
|
||||||
|
$this->assertEquals(count($data[3]), 4);
|
||||||
$this->assertEquals(count($data[11]), 4);
|
$this->assertEquals(count($data[11]), 4);
|
||||||
|
|
||||||
|
// Specific value tests
|
||||||
$this->assertEquals($data[1][1], 'quote " and comma , test');
|
$this->assertEquals($data[1][1], 'quote " and comma , test');
|
||||||
$this->assertEquals($data[1][2], 'Value 1');
|
$this->assertEquals($data[1][2], 'Value 1');
|
||||||
$this->assertEquals($data[2][1], 'Value 2');
|
$this->assertEquals($data[2][1], 'Value 2');
|
||||||
|
|
||||||
|
$this->assertEquals($data[3][1], "multi\nline\ntest");
|
||||||
|
|
||||||
$this->assertEquals($data[11][0], 'First');
|
$this->assertEquals($data[11][0], 'First');
|
||||||
$this->assertEquals($data[11][1], 'Second');
|
$this->assertEquals($data[11][1], 'Second');
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ SubmittedForm:
|
|||||||
|
|
||||||
long-11:
|
long-11:
|
||||||
|
|
||||||
|
long-12:
|
||||||
|
|
||||||
|
|
||||||
UserDefinedForm:
|
UserDefinedForm:
|
||||||
form-page:
|
form-page:
|
||||||
@ -78,6 +80,15 @@ SubmittedFormField:
|
|||||||
Title: Submitted Title 2
|
Title: Submitted Title 2
|
||||||
Value: Value 2
|
Value: Value 2
|
||||||
|
|
||||||
|
long-submitted-3:
|
||||||
|
Name: Submitted Name 2
|
||||||
|
Title: Submitted Title 2
|
||||||
|
Parent: =>SubmittedForm.long-3
|
||||||
|
Value: |
|
||||||
|
multi
|
||||||
|
line
|
||||||
|
test
|
||||||
|
|
||||||
long-submitted-11a:
|
long-submitted-11a:
|
||||||
Parent: =>SubmittedForm.long-11
|
Parent: =>SubmittedForm.long-11
|
||||||
Name: First
|
Name: First
|
||||||
|
Loading…
x
Reference in New Issue
Block a user