mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #3845 from dnadesign/gridfieldexportbutton
FIX GridFieldExportButton should honour can method.
This commit is contained in:
commit
7923b88652
@ -132,26 +132,28 @@ class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionP
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($items->limit(null) as $item) {
|
foreach($items->limit(null) as $item) {
|
||||||
$columnData = array();
|
if($item->hasMethod('canView') && $item->canView()) {
|
||||||
|
$columnData = array();
|
||||||
|
|
||||||
foreach($csvColumns as $columnSource => $columnHeader) {
|
foreach($csvColumns as $columnSource => $columnHeader) {
|
||||||
if(!is_string($columnHeader) && is_callable($columnHeader)) {
|
if(!is_string($columnHeader) && is_callable($columnHeader)) {
|
||||||
if($item->hasMethod($columnSource)) {
|
if($item->hasMethod($columnSource)) {
|
||||||
$relObj = $item->{$columnSource}();
|
$relObj = $item->{$columnSource}();
|
||||||
|
} else {
|
||||||
|
$relObj = $item->relObject($columnSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = $columnHeader($relObj);
|
||||||
} else {
|
} else {
|
||||||
$relObj = $item->relObject($columnSource);
|
$value = $gridField->getDataFieldValue($item, $columnSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $columnHeader($relObj);
|
$value = str_replace(array("\r", "\n"), "\n", $value);
|
||||||
} else {
|
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
|
||||||
$value = $gridField->getDataFieldValue($item, $columnSource);
|
|
||||||
}
|
}
|
||||||
|
$fileData .= implode($separator, $columnData);
|
||||||
$value = str_replace(array("\r", "\n"), "\n", $value);
|
$fileData .= "\n";
|
||||||
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
|
|
||||||
}
|
}
|
||||||
$fileData .= implode($separator, $columnData);
|
|
||||||
$fileData .= "\n";
|
|
||||||
|
|
||||||
$item->destroy();
|
$item->destroy();
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package framework
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
class GridFieldExportButtonTest extends SapphireTest {
|
class GridFieldExportButtonTest extends SapphireTest {
|
||||||
|
|
||||||
protected $list;
|
protected $list;
|
||||||
@ -10,7 +15,8 @@ class GridFieldExportButtonTest extends SapphireTest {
|
|||||||
protected static $fixture_file = 'GridFieldExportButtonTest.yml';
|
protected static $fixture_file = 'GridFieldExportButtonTest.yml';
|
||||||
|
|
||||||
protected $extraDataObjects = array(
|
protected $extraDataObjects = array(
|
||||||
'GridFieldExportButtonTest_Team'
|
'GridFieldExportButtonTest_Team',
|
||||||
|
'GridFieldExportButtonTest_NoView'
|
||||||
);
|
);
|
||||||
|
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
@ -22,6 +28,21 @@ class GridFieldExportButtonTest extends SapphireTest {
|
|||||||
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
$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() {
|
public function testGenerateFileDataBasicFields() {
|
||||||
$button = new GridFieldExportButton();
|
$button = new GridFieldExportButton();
|
||||||
$button->setExportColumns(array('Name' => 'My Name'));
|
$button->setExportColumns(array('Name' => 'My Name'));
|
||||||
@ -94,8 +115,12 @@ class GridFieldExportButtonTest extends SapphireTest {
|
|||||||
$button->generateExportFileData($this->gridField)
|
$button->generateExportFileData($this->gridField)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @package framework
|
||||||
|
* @subpackage tests
|
||||||
|
*/
|
||||||
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
|
class GridFieldExportButtonTest_Team extends DataObject implements TestOnly {
|
||||||
|
|
||||||
private static $db = array(
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -6,3 +6,6 @@ GridFieldExportButtonTest_Team:
|
|||||||
Name: Test2
|
Name: Test2
|
||||||
City: City2
|
City: City2
|
||||||
|
|
||||||
|
GridFieldExportButtonTest_NoView:
|
||||||
|
item1:
|
||||||
|
Name: Foo
|
Loading…
Reference in New Issue
Block a user