1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Broarm\EventTickets; |
4
|
|
|
|
5
|
|
|
use ExcelGridFieldExportButton; |
6
|
|
|
use ExcelImportExport; |
7
|
|
|
use Exception; |
8
|
|
|
use FileNameFilter; |
9
|
|
|
use GridField; |
10
|
|
|
use GridField_FormAction; |
11
|
|
|
use GridFieldFilterHeader; |
12
|
|
|
use GridFieldSortableHeader; |
13
|
|
|
use PHPExcel; |
14
|
|
|
use PHPExcel_Cell; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class GuestListExportButton |
18
|
|
|
*/ |
19
|
|
|
class GuestListExportButton extends ExcelGridFieldExportButton |
20
|
|
|
{ |
21
|
|
|
public function getHTMLFragments($gridField) { |
22
|
|
|
$title = $this->buttonTitle ?: _t('TableListField.XLSEXPORT', 'Export to Excel'); |
23
|
|
|
$name = $this->getActionName(); |
24
|
|
|
$button = new GridField_FormAction($gridField, $name, $title, $name, null); |
|
|
|
|
25
|
|
|
$button->addExtraClass('btn btn-secondary font-icon-down-circled btn--icon-large action_export no-ajax'); |
26
|
|
|
$button->setForm($gridField->getForm()); |
27
|
|
|
|
28
|
|
|
return array( |
29
|
|
|
$this->targetFragment => '<p class="grid-excel-button">' . $button->Field() . '</p>', |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generate export fields for Excel. |
35
|
|
|
* |
36
|
|
|
* @param GridField $gridField |
37
|
|
|
* @return PHPExcel |
38
|
|
|
* @throws \PHPExcel_Exception |
39
|
|
|
*/ |
40
|
|
|
public function generateExportFileData($gridField) |
41
|
|
|
{ |
42
|
|
|
$class = $gridField->getModelClass(); |
43
|
|
|
$columns = $this->exportColumns ?: ExcelImportExport::exportFieldsForClass($class); |
44
|
|
|
$plural = $class ? singleton($class)->i18n_plural_name() : ''; |
45
|
|
|
|
46
|
|
|
$filter = new FileNameFilter(); |
47
|
|
|
$this->exportName = $filter->filter($this->exportName ?: 'export-' . $plural); |
48
|
|
|
|
49
|
|
|
$excel = new PHPExcel(); |
50
|
|
|
$excelProperties = $excel->getProperties(); |
51
|
|
|
$excelProperties->setTitle($this->exportName); |
52
|
|
|
|
53
|
|
|
$sheet = $excel->getActiveSheet(); |
54
|
|
|
if ($plural) { |
55
|
|
|
$sheet->setTitle($plural); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$row = 1; |
59
|
|
|
$col = 0; |
60
|
|
|
|
61
|
|
|
if ($this->hasHeader) { |
62
|
|
|
$headers = array(); |
63
|
|
|
|
64
|
|
|
// determine the headers. If a field is callable (e.g. anonymous function) then use the |
65
|
|
|
// source name as the header instead |
66
|
|
|
foreach ($columns as $columnSource => $columnHeader) { |
67
|
|
|
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) |
68
|
|
|
? $columnSource |
69
|
|
|
: $columnHeader; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
foreach ($headers as $header) { |
73
|
|
|
$sheet->setCellValueByColumnAndRow($col, $row, $header); |
74
|
|
|
$col++; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$endCol = PHPExcel_Cell::stringFromColumnIndex($col - 1); |
78
|
|
|
$sheet->setAutoFilter("A1:{$endCol}1"); |
79
|
|
|
$sheet->getStyle("A1:{$endCol}1")->getFont()->setBold(true); |
80
|
|
|
|
81
|
|
|
$col = 0; |
82
|
|
|
$row++; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Autosize |
86
|
|
|
$cellIterator = $sheet->getRowIterator()->current()->getCellIterator(); |
87
|
|
|
try { |
88
|
|
|
$cellIterator->setIterateOnlyExistingCells(true); |
89
|
|
|
} catch (Exception $ex) { |
90
|
|
|
// Ignore exceptions |
91
|
|
|
} |
92
|
|
|
foreach ($cellIterator as $cell) { |
93
|
|
|
$sheet->getColumnDimension($cell->getColumn())->setAutoSize(true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//Remove GridFieldPaginator as we're going to export the entire list. |
97
|
|
|
$gridField->getConfig()->removeComponentsByType('GridFieldPaginator'); |
98
|
|
|
$items = $gridField->getManipulatedList(); |
99
|
|
|
|
100
|
|
|
// @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
101
|
|
|
foreach ($gridField->getConfig()->getComponents() as $component) { |
102
|
|
|
if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
103
|
|
|
$items = $component->getManipulatedData($gridField, $items); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$list = $items->limit(null); |
108
|
|
|
if (!empty($this->listFilters)) { |
109
|
|
|
$list = $list->filter($this->listFilters); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** @var Attendee $item */ |
113
|
|
|
foreach ($list as $item) { |
114
|
|
|
if (!$this->checkCanView || !$item->hasMethod('canView') || $item->canView()) { |
115
|
|
|
foreach ($columns as $columnSource => $columnHeader) { |
116
|
|
|
if (!is_string($columnHeader) && is_callable($columnHeader)) { |
117
|
|
|
if ($item->hasMethod($columnSource)) { |
118
|
|
|
$relObj = $item->{$columnSource}(); |
119
|
|
|
} else { |
120
|
|
|
$relObj = $item->relObject($columnSource); |
121
|
|
|
} |
122
|
|
|
$value = $columnHeader($relObj); |
123
|
|
|
} elseif ($field = $item->Fields()->byID($columnSource)) { |
124
|
|
|
$value = $field->getValue(); |
125
|
|
|
} elseif ($field = $item->Fields()->find('Name', $columnSource)) { |
126
|
|
|
$value = $field->getValue(); |
127
|
|
|
} else { |
128
|
|
|
$value = $gridField->getDataFieldValue($item, $columnSource); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$sheet->setCellValueByColumnAndRow($col, $row, $value); |
132
|
|
|
$col++; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($item->hasMethod('destroy')) { |
137
|
|
|
$item->destroy(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$col = 0; |
141
|
|
|
$row++; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $excel; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: