|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hospitalplugin\utils; |
|
4
|
|
|
|
|
5
|
|
|
use Hospitalplugin\Entities\InfectionsCRUD; |
|
6
|
|
|
use Hospitalplugin\Entities\Infections; |
|
7
|
|
|
|
|
8
|
|
|
class ExcelExportInfections { |
|
9
|
|
|
private static function getData() { |
|
|
|
|
|
|
10
|
|
|
$wardId = (! empty ( $_POST ['wardId'] ) ? $_POST ['wardId'] : 0); |
|
11
|
|
|
$date = (! empty ( $_POST ['date'] ) ? $_POST ['date'] : (new \DateTime ())->format ( "Y-m" )); |
|
12
|
|
|
$from = new \DateTime ( $date . '-01' ); |
|
13
|
|
|
$fromStr = $from->format ( 'Y-m-01' ); |
|
14
|
|
|
$toStr = $from->format ( 'Y-m-t' ); |
|
15
|
|
|
$infections = InfectionsCRUD::getInfections ( $fromStr, $toStr, $wardId, 'Infections' ); |
|
16
|
|
|
return $infections; |
|
17
|
|
|
} |
|
18
|
|
|
private static function getColumns() { |
|
19
|
|
|
return Infections::getFields (); |
|
20
|
|
|
} |
|
21
|
|
|
private static function printHeaders($objPHPExcel, $cols) { |
|
22
|
|
|
$count = 0; |
|
23
|
|
|
foreach ( $cols as $col => $sym ) { |
|
24
|
|
|
$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, 2, sprintf ( "%7s", $col ) ); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
/** |
|
28
|
|
|
*/ |
|
29
|
|
|
private static function printData($objPHPExcel, $data, $cols) { |
|
30
|
|
|
$row = 3; |
|
31
|
|
|
foreach ( $data as $rowValue ) { |
|
32
|
|
|
$count = 0; |
|
33
|
|
|
foreach ( $cols as $col => $sym ) { |
|
34
|
|
|
$value = $rowValue->$sym; |
|
35
|
|
|
$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $count ++, $row, $value ); |
|
36
|
|
|
} |
|
37
|
|
|
$row ++; |
|
38
|
|
|
} |
|
39
|
|
|
return $row; |
|
40
|
|
|
} |
|
41
|
|
|
private static function printTitle($objPHPExcel, $title) { |
|
42
|
|
|
$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 0, 1, $title ); |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* |
|
46
|
|
|
* @param PHPExcel $objPHPExcel |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function printFooter($objPHPExcel, $cols, $row) { |
|
49
|
|
|
$objPHPExcel->getActiveSheet ()->getStyle ( 'A' . $row . ':AA' . ($row + 1) )->getFont ()->setBold ( true ); |
|
50
|
|
|
// SUM |
|
51
|
|
|
$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( 1, $row, "SUMA" ); |
|
52
|
|
|
for($i = 2; $i < count ( $cols ); $i ++) { |
|
|
|
|
|
|
53
|
|
|
$colLetter = chr ( 65 + $i ); |
|
54
|
|
|
$objPHPExcel->getActiveSheet ()->setCellValueByColumnAndRow ( $i, $row, "=sum(" . $colLetter . "3" . ":" . $colLetter . ($row - 1) . ")" ); |
|
55
|
|
|
} |
|
56
|
|
|
ExcelExport::cellColor ( $objPHPExcel, 'A' . $row . ':AA' . ($row + 1), 'DDDDDD' ); |
|
57
|
|
|
} |
|
58
|
|
|
static function fillData($objPHPExcel) { |
|
|
|
|
|
|
59
|
|
|
$data = ExcelExportInfections::getData (); |
|
60
|
|
|
$cols = ExcelExportInfections::getColumns (); |
|
61
|
|
|
|
|
62
|
|
|
ExcelExportInfections::printTitle ( $objPHPExcel, "Raport" ); |
|
63
|
|
|
ExcelExportInfections::printHeaders ( $objPHPExcel, $cols ); |
|
64
|
|
|
$lastRow = ExcelExportInfections::printData ( $objPHPExcel, $data, $cols ); |
|
65
|
|
|
ExcelExportInfections::printFooter ( $objPHPExcel, $cols, $lastRow ); |
|
66
|
|
|
} |
|
67
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: