Conditions | 24 |
Paths | 680 |
Total Lines | 257 |
Code Lines | 146 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
36 | public function execute() |
||
37 | { |
||
38 | $options = $this->getOptions(); |
||
39 | $optionsExport = $options['settings']['export']; |
||
40 | |||
41 | $optionsRenderer = $this->getOptionsRenderer(); |
||
42 | |||
43 | $phpExcel = new PHPExcel(); |
||
44 | |||
45 | // Sheet 1 |
||
46 | $phpExcel->setActiveSheetIndex(0); |
||
47 | $sheet = $phpExcel->getActiveSheet(); |
||
48 | $sheet->setTitle($this->translate($optionsRenderer['sheetName'])); |
||
49 | |||
50 | if (true === $optionsRenderer['displayTitle']) { |
||
51 | $sheet->setCellValue('A'.$optionsRenderer['rowTitle'], $this->getTitle()); |
||
52 | $sheet->getStyle('A'.$optionsRenderer['rowTitle']) |
||
53 | ->getFont() |
||
54 | ->setSize(15); |
||
55 | } |
||
56 | |||
57 | /* |
||
58 | * Print settings |
||
59 | */ |
||
60 | $this->setPrinting($phpExcel); |
||
61 | |||
62 | /* |
||
63 | * Calculate column width |
||
64 | */ |
||
65 | $this->calculateColumnWidth($sheet, $this->getColumnsToExport()); |
||
66 | |||
67 | /* |
||
68 | * Header |
||
69 | */ |
||
70 | $xColumn = 0; |
||
71 | $yRow = $optionsRenderer['startRowData']; |
||
72 | foreach ($this->getColumnsToExport() as $col) { |
||
73 | /* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
||
74 | $sheet->setCellValueByColumnAndRow($xColumn, $yRow, $this->translate($col->getLabel())); |
||
75 | |||
76 | $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($xColumn))->setWidth($col->getWidth()); |
||
77 | |||
78 | ++$xColumn; |
||
79 | } |
||
80 | |||
81 | /* |
||
82 | * Data |
||
83 | */ |
||
84 | $yRow = $optionsRenderer['startRowData'] + 1; |
||
85 | foreach ($this->getData() as $row) { |
||
86 | $xColumn = 0; |
||
87 | foreach ($this->getColumnsToExport() as $col) { |
||
88 | /* @var $col \ZfcDatagrid\Column\AbstractColumn */ |
||
89 | |||
90 | $value = $row[$col->getUniqueId()]; |
||
91 | if (is_array($value)) { |
||
92 | $value = implode(PHP_EOL, $value); |
||
93 | } |
||
94 | |||
95 | /* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
||
96 | $currentColumn = PHPExcel_Cell::stringFromColumnIndex($xColumn); |
||
97 | $cell = $sheet->getCell($currentColumn.$yRow); |
||
98 | |||
99 | switch (get_class($col->getType())) { |
||
100 | |||
101 | case 'ZfcDatagrid\Column\Type\Number': |
||
102 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_NUMERIC); |
||
103 | break; |
||
104 | |||
105 | case 'ZfcDatagrid\Column\Type\DateTime': |
||
106 | /* @var $dateType \ZfcDatagrid\Column\Type\DateTime */ |
||
107 | $dateType = $col->getType(); |
||
108 | |||
109 | if (! $value instanceof \DateTime && is_scalar($value)) { |
||
110 | $value = \DateTime::createFromFormat($dateType->getSourceDateTimeFormat(), $value); |
||
111 | $value->setTimezone(new \DateTimeZone($dateType->getSourceTimezone())); |
||
112 | } |
||
113 | |||
114 | $value->setTimezone(new \DateTimeZone($dateType->getOutputTimezone())); |
||
115 | $cell->setValue(\PHPExcel_Shared_Date::PHPToExcel($value)); |
||
116 | |||
117 | if ($dateType->getOutputPattern()) { |
||
|
|||
118 | $outputPattern = $dateType->getOutputPattern(); |
||
119 | } else { |
||
120 | $outputPattern = \PHPExcel_Style_NumberFormat::FORMAT_DATE_DATETIME; |
||
121 | } |
||
122 | |||
123 | $cell->$cell->getStyle() |
||
124 | ->getNumberFormat() |
||
125 | ->setFormatCode($outputPattern); |
||
126 | break; |
||
127 | |||
128 | default: |
||
129 | $cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); |
||
130 | break; |
||
131 | } |
||
132 | |||
133 | $columnStyle = $sheet->getStyle($currentColumn.$yRow); |
||
134 | $columnStyle->getAlignment()->setWrapText(true); |
||
135 | |||
136 | /* |
||
137 | * Styles |
||
138 | */ |
||
139 | $styles = array_merge($this->getRowStyles(), $col->getStyles()); |
||
140 | foreach ($styles as $style) { |
||
141 | /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ |
||
142 | if ($style->isApply($row) === true) { |
||
143 | switch (get_class($style)) { |
||
144 | |||
145 | case 'ZfcDatagrid\Column\Style\Bold': |
||
146 | $columnStyle->getFont()->setBold(true); |
||
147 | break; |
||
148 | |||
149 | case 'ZfcDatagrid\Column\Style\Italic': |
||
150 | $columnStyle->getFont()->setItalic(true); |
||
151 | break; |
||
152 | |||
153 | case 'ZfcDatagrid\Column\Style\Color': |
||
154 | $columnStyle->getFont() |
||
155 | ->getColor() |
||
156 | ->setRGB($style->getRgbHexString()); |
||
157 | break; |
||
158 | |||
159 | case 'ZfcDatagrid\Column\Style\BackgroundColor': |
||
160 | $columnStyle->getFill()->applyFromArray([ |
||
161 | 'type' => \PHPExcel_Style_Fill::FILL_SOLID, |
||
162 | 'color' => [ |
||
163 | 'rgb' => $style->getRgbHexString(), |
||
164 | ], |
||
165 | ]); |
||
166 | break; |
||
167 | |||
168 | case 'ZfcDatagrid\Column\Style\Align': |
||
169 | switch ($style->getAlignment()) { |
||
170 | case \ZfcDatagrid\Column\Style\Align::$RIGHT: |
||
171 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); |
||
172 | break; |
||
173 | case \ZfcDatagrid\Column\Style\Align::$LEFT: |
||
174 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); |
||
175 | break; |
||
176 | case \ZfcDatagrid\Column\Style\Align::$CENTER: |
||
177 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); |
||
178 | break; |
||
179 | case \ZfcDatagrid\Column\Style\Align::$JUSTIFY: |
||
180 | $columnStyle->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); |
||
181 | break; |
||
182 | default: |
||
183 | //throw new \Exception('Not defined yet: "'.get_class($style->getAlignment()).'"'); |
||
184 | break; |
||
185 | } |
||
186 | |||
187 | break; |
||
188 | |||
189 | case 'ZfcDatagrid\Column\Style\Strikethrough': |
||
190 | $columnStyle->getFont()->setStrikethrough(true); |
||
191 | break; |
||
192 | |||
193 | case 'ZfcDatagrid\Column\Style\Html': |
||
194 | // @todo strip the html? |
||
195 | break; |
||
196 | |||
197 | default: |
||
198 | throw new \Exception('Not defined yet: "'.get_class($style).'"'); |
||
199 | break; |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | ++$xColumn; |
||
205 | } |
||
206 | |||
207 | ++$yRow; |
||
208 | } |
||
209 | |||
210 | /* |
||
211 | * Autofilter, freezing, ... |
||
212 | */ |
||
213 | $highest = $sheet->getHighestRowAndColumn(); |
||
214 | |||
215 | // Letzte Zeile merken |
||
216 | |||
217 | // Autofilter + Freeze |
||
218 | $sheet->setAutoFilter('A'.$optionsRenderer['startRowData'].':'.$highest['column'].$highest['row']); |
||
219 | $freezeRow = $optionsRenderer['startRowData'] + 1; |
||
220 | $sheet->freezePane('A'.$freezeRow); |
||
221 | |||
222 | // repeat the data header for each page! |
||
223 | $sheet->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd($optionsRenderer['startRowData'], $optionsRenderer['startRowData']); |
||
224 | |||
225 | // highlight header line |
||
226 | $style = [ |
||
227 | 'font' => [ |
||
228 | 'bold' => true, |
||
229 | ], |
||
230 | |||
231 | 'borders' => [ |
||
232 | 'allborders' => [ |
||
233 | 'style' => PHPExcel_Style_Border::BORDER_MEDIUM, |
||
234 | 'color' => [ |
||
235 | 'argb' => PHPExcel_Style_Color::COLOR_BLACK, |
||
236 | ], |
||
237 | ], |
||
238 | ], |
||
239 | 'fill' => [ |
||
240 | 'type' => PHPExcel_Style_Fill::FILL_SOLID, |
||
241 | 'startcolor' => [ |
||
242 | 'argb' => PHPExcel_Style_Color::COLOR_YELLOW, |
||
243 | ], |
||
244 | ], |
||
245 | ]; |
||
246 | $range = 'A'.$optionsRenderer['startRowData'].':'.$highest['column'].$optionsRenderer['startRowData']; |
||
247 | $sheet->getStyle($range)->applyFromArray($style); |
||
248 | |||
249 | // print borders |
||
250 | $range = 'A'.$freezeRow.':'.$highest['column'].$highest['row']; |
||
251 | $sheet->getStyle($range)->applyFromArray([ |
||
252 | 'borders' => [ |
||
253 | 'allborders' => [ |
||
254 | 'style' => PHPExcel_Style_Border::BORDER_THIN, |
||
255 | ], |
||
256 | ], |
||
257 | ]); |
||
258 | |||
259 | /* |
||
260 | * Save the file |
||
261 | */ |
||
262 | $path = $optionsExport['path']; |
||
263 | $saveFilename = date('Y-m-d_H-i-s').$this->getCacheId().'.xlsx'; |
||
264 | |||
265 | $excelWriter = new \PHPExcel_Writer_Excel2007($phpExcel); |
||
266 | $excelWriter->setPreCalculateFormulas(false); |
||
267 | $excelWriter->save($path.'/'.$saveFilename); |
||
268 | |||
269 | /* |
||
270 | * Send the response stream |
||
271 | */ |
||
272 | $response = new ResponseStream(); |
||
273 | $response->setStream(fopen($path.'/'.$saveFilename, 'r')); |
||
274 | |||
275 | $headers = new Headers(); |
||
276 | $headers->addHeaders([ |
||
277 | 'Content-Type' => [ |
||
278 | 'application/force-download', |
||
279 | 'application/octet-stream', |
||
280 | 'application/download', |
||
281 | ], |
||
282 | 'Content-Length' => filesize($path.'/'.$saveFilename), |
||
283 | 'Content-Disposition' => 'attachment;filename='.$this->getFilename().'.xlsx', |
||
284 | 'Cache-Control' => 'must-revalidate', |
||
285 | 'Pragma' => 'no-cache', |
||
286 | 'Expires' => 'Thu, 1 Jan 1970 00:00:00 GMT', |
||
287 | ]); |
||
288 | |||
289 | $response->setHeaders($headers); |
||
290 | |||
291 | return $response; |
||
292 | } |
||
293 | |||
389 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: