-
Notifications
You must be signed in to change notification settings - Fork 13
/
GridFieldExportReportButton.php
97 lines (82 loc) · 3.65 KB
/
GridFieldExportReportButton.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
namespace SilverStripe\SecurityReport\Forms;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
/**
* An extension to GridFieldExportButton to support downloading a custom Report as a CSV file
*
* This code was adapted from a solution posted in SilverStripe.org forums:
* http://www.silverstripe.org/customising-the-cms/show/38202
*
* @package securityreport
* @author Michael Armstrong <http://www.silverstripe.org/ForumMemberProfile/show/30887>
* @author Michael Parkhill <[email protected]>
*/
class GridFieldExportReportButton extends GridFieldExportButton
{
/**
* Generate export fields for CSV.
*
* Replaces the definition in GridFieldExportButton, this is the same as original except
* it sources the {@link List} from $gridField->getList() instead of $gridField->getManipulatedList()
*
* @param GridField $gridField
* @return array
*/
public function generateExportFileData($gridField)
{
$separator = $this->csvSeparator;
$csvColumns = ($this->exportColumns)
? $this->exportColumns
: singleton($gridField->getModelClass())->summaryFields();
$fileData = '';
$columnData = array();
if ($this->csvHasHeader) {
$headers = array();
// determine the CSV headers. If a field is callable (e.g. anonymous function) then use the
// source name as the header instead
foreach ($csvColumns as $columnSource => $columnHeader) {
if (is_array($columnHeader) && array_key_exists('title', $columnHeader ?? [])) {
$headers[] = $columnHeader['title'];
} else {
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader))
? $columnSource
: $columnHeader;
}
}
$fileData .= "\"" . implode("\"{$separator}\"", array_values($headers ?? [])) . "\"";
$fileData .= "\n";
}
// The is the only variation from the parent, using getList() instead of getManipulatedList()
$items = $gridField->getList();
// @todo should GridFieldComponents change behaviour based on whether others are available in the config?
foreach ($gridField->getConfig()->getComponents() as $component) {
if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) {
$items = $component->getManipulatedData($gridField, $items);
}
}
foreach ($items->limit(null) as $item) {
$columnData = array();
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 {
$value = $gridField->getDataFieldValue($item, $columnSource);
}
$value = str_replace(array("\r", "\n"), "\n", $value ?? '');
$columnData[] = '"' . str_replace('"', '\"', $value ?? '') . '"';
}
$fileData .= implode($separator ?? '', $columnData);
$fileData .= "\n";
$item->destroy();
}
return $fileData;
}
}