Merge pull request #4432 from JorisDebonnet/csv-export

Use fputcsv in GridFieldExportButton
This commit is contained in:
Sam Minnée 2015-07-27 17:28:12 +12:00
commit 7b9bbe17dc
3 changed files with 60 additions and 18 deletions

View File

@ -20,6 +20,11 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
*/ */
protected $csvSeparator = ","; protected $csvSeparator = ",";
/**
* @var string
*/
protected $csvEnclosure = '"';
/** /**
* @var boolean * @var boolean
*/ */
@ -98,13 +103,10 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
* @return array * @return array
*/ */
public function generateExportFileData($gridField) { public function generateExportFileData($gridField) {
$separator = $this->csvSeparator;
$csvColumns = ($this->exportColumns) $csvColumns = ($this->exportColumns)
? $this->exportColumns ? $this->exportColumns
: singleton($gridField->getModelClass())->summaryFields(); : singleton($gridField->getModelClass())->summaryFields();
$fileData = ''; $fileData = array();
$columnData = array();
$fieldItems = new ArrayList();
if($this->csvHasHeader) { if($this->csvHasHeader) {
$headers = array(); $headers = array();
@ -115,8 +117,7 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
} }
$fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\""; $fileData[] = $headers;
$fileData .= "\n";
} }
//Remove GridFieldPaginator as we're going to export the entire list. //Remove GridFieldPaginator as we're going to export the entire list.
@ -152,12 +153,10 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
} }
} }
$value = str_replace(array("\r", "\n"), "\n", $value); $columnData[] = $value;
$columnData[] = '"' . str_replace('"', '""', $value) . '"';
} }
$fileData .= implode($separator, $columnData); $fileData[] = $columnData;
$fileData .= "\n";
} }
if($item->hasMethod('destroy')) { if($item->hasMethod('destroy')) {
@ -165,7 +164,13 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
} }
} }
return $fileData; // Convert the $fileData array into csv by capturing fputcsv's output
$csv = fopen('php://temp', 'r+');
foreach($fileData as $line) {
fputcsv($csv, $line, $this->csvSeparator, $this->csvEnclosure);
}
rewind($csv);
return stream_get_contents($csv);
} }
/** /**
@ -198,6 +203,21 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
return $this; return $this;
} }
/**
* @return string
*/
public function getCsvEnclosure() {
return $this->csvEnclosure;
}
/**
* @param string
*/
public function setCsvEnclosure($enclosure) {
$this->csvEnclosure = $enclosure;
return $this;
}
/** /**
* @return boolean * @return boolean
*/ */

View File

@ -48,7 +48,9 @@ class GridFieldExportButtonTest extends SapphireTest {
$button->setExportColumns(array('Name' => 'My Name')); $button->setExportColumns(array('Name' => 'My Name'));
$this->assertEquals( $this->assertEquals(
"\"My Name\"\n\"Test\"\n\"Test2\"\n", '"My Name"'."\n".
'Test'."\n".
'Test2'."\n",
$button->generateExportFileData($this->gridField) $button->generateExportFileData($this->gridField)
); );
} }
@ -63,7 +65,9 @@ class GridFieldExportButtonTest extends SapphireTest {
)); ));
$this->assertEquals( $this->assertEquals(
"\"Name\",\"City\"\n\"Test\",\"City city\"\n\"Test2\",\"City2 city\"\n", 'Name,City'."\n".
'Test,"City city"'."\n".
'Test2,"Quoted ""City"" 2 city"'."\n",
$button->generateExportFileData($this->gridField) $button->generateExportFileData($this->gridField)
); );
} }
@ -76,7 +80,9 @@ class GridFieldExportButtonTest extends SapphireTest {
)); ));
$this->assertEquals( $this->assertEquals(
"\"Name\",\"strtolower\"\n\"Test\",\"City\"\n\"Test2\",\"City2\"\n", 'Name,strtolower'."\n".
'Test,City'."\n".
'Test2,"Quoted ""City"" 2"'."\n",
$button->generateExportFileData($this->gridField) $button->generateExportFileData($this->gridField)
); );
} }
@ -90,7 +96,8 @@ class GridFieldExportButtonTest extends SapphireTest {
$button->setCsvHasHeader(false); $button->setCsvHasHeader(false);
$this->assertEquals( $this->assertEquals(
"\"Test\",\"City\"\n\"Test2\",\"City2\"\n", 'Test,City'."\n".
'Test2,"Quoted ""City"" 2"'."\n",
$button->generateExportFileData($this->gridField) $button->generateExportFileData($this->gridField)
); );
} }
@ -110,8 +117,23 @@ class GridFieldExportButtonTest extends SapphireTest {
$this->gridField->setList($arrayList); $this->gridField->setList($arrayList);
$this->assertEquals( $this->assertEquals(
"\"ID\"\n\"1\"\n\"2\"\n\"3\"\n\"4\"\n\"5\"\n\"6\"\n\"7\"\n\"8\"\n" "ID\n".
."\"9\"\n\"10\"\n\"11\"\n\"12\"\n\"13\"\n\"14\"\n\"15\"\n\"16\"\n", "1\n".
"2\n".
"3\n".
"4\n".
"5\n".
"6\n".
"7\n".
"8\n".
"9\n".
"10\n".
"11\n".
"12\n".
"13\n".
"14\n".
"15\n".
"16\n",
$button->generateExportFileData($this->gridField) $button->generateExportFileData($this->gridField)
); );
} }

View File

@ -4,7 +4,7 @@ GridFieldExportButtonTest_Team:
City: City City: City
test-team-2: test-team-2:
Name: Test2 Name: Test2
City: City2 City: 'Quoted "City" 2'
GridFieldExportButtonTest_NoView: GridFieldExportButtonTest_NoView:
item1: item1: