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