Merge pull request #3845 from dnadesign/gridfieldexportbutton

FIX GridFieldExportButton should honour can method.
This commit is contained in:
Daniel Hensby 2015-02-03 22:03:56 +00:00
commit 7923b88652
3 changed files with 63 additions and 16 deletions

View File

@ -132,26 +132,28 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
}
foreach($items->limit(null) as $item) {
$columnData = array();
if($item->hasMethod('canView') && $item->canView()) {
$columnData = array();
foreach($csvColumns as $columnSource => $columnHeader) {
if(!is_string($columnHeader) && is_callable($columnHeader)) {
if($item->hasMethod($columnSource)) {
$relObj = $item->{$columnSource}();
foreach($csvColumns as $columnSource => $columnHeader) {
if(!is_string($columnHeader) && is_callable($columnHeader)) {
if($item->hasMethod($columnSource)) {
$relObj = $item->{$columnSource}();
} else {
$relObj = $item->relObject($columnSource);
}
$value = $columnHeader($relObj);
} else {
$relObj = $item->relObject($columnSource);
$value = $gridField->getDataFieldValue($item, $columnSource);
}
$value = $columnHeader($relObj);
} else {
$value = $gridField->getDataFieldValue($item, $columnSource);
$value = str_replace(array("\r", "\n"), "\n", $value);
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
}
$value = str_replace(array("\r", "\n"), "\n", $value);
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
$fileData .= implode($separator, $columnData);
$fileData .= "\n";
}
$fileData .= implode($separator, $columnData);
$fileData .= "\n";
$item->destroy();
}

View File

@ -1,4 +1,9 @@
<?php
/**
* @package framework
* @subpackage tests
*/
class GridFieldExportButtonTest extends SapphireTest {
protected $list;
@ -10,7 +15,8 @@ class GridFieldExportButtonTest extends SapphireTest {
protected static $fixture_file = 'GridFieldExportButtonTest.yml';
protected $extraDataObjects = array(
'GridFieldExportButtonTest_Team'
'GridFieldExportButtonTest_Team',
'GridFieldExportButtonTest_NoView'
);
public function setUp() {
@ -22,6 +28,21 @@ class GridFieldExportButtonTest extends SapphireTest {
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
}
public function testCanView() {
$list = new DataList('GridFieldExportButtonTest_NoView');
$button = new GridFieldExportButton();
$button->setExportColumns(array('Name' => 'My Name'));
$config = GridFieldConfig::create()->addComponent(new GridFieldExportButton());
$gridField = new GridField('testfield', 'testfield', $list, $config);
$this->assertEquals(
"\"My Name\"\n",
$button->generateExportFileData($gridField)
);
}
public function testGenerateFileDataBasicFields() {
$button = new GridFieldExportButton();
$button->setExportColumns(array('Name' => 'My Name'));
@ -94,8 +115,12 @@ class GridFieldExportButtonTest extends SapphireTest {
$button->generateExportFileData($this->gridField)
);
}
}
/**
* @package framework
* @subpackage tests
*/
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
private static $db = array(
@ -109,3 +134,20 @@ class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
}
/**
* @package framework
* @subpackage tests
*/
class GridFieldExportButtonTest_NoView extends DataObject implements TestOnly {
private static $db = array(
'Name' => 'Varchar',
'City' => 'Varchar'
);
public function canView($member = null) {
return false;
}
}

View File

@ -6,3 +6,6 @@ GridFieldExportButtonTest_Team:
Name: Test2
City: City2
GridFieldExportButtonTest_NoView:
item1:
Name: Foo