forked from dox/energy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
114 lines (94 loc) · 3.89 KB
/
export.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
include_once("inc/include.php");
if ($_GET['type'] == "node") {
$node = new node(filter_var($_GET['filter'], FILTER_SANITIZE_NUMBER_INT));
$thisYearDateFrom = date('Y-m-d', strtotime('100 months ago'));
$thisYearDateTo = date('Y-m-d');
$consumptionLast12Months = array_reverse($node->consumptionBetweenDatesByMonth($thisYearDateFrom, $thisYearDateTo), true);
//echo "consumption per month in " . $node->unit;
$nodeRow['uid'] = $node->uid;
$nodeRow['enabled'] = $node->enabled;
$nodeRow['name'] = $node->cleanName();
$nodeRow['location'] = $location->name;
$nodeRow['type'] = $node->type;
$nodeRow['unit'] = $node->unit;
$nodeRow['photograph'] = $node->photograph;
$nodeRow['geo'] = $node->geo;
$nodeRow['reading_current_date'] = $node->mostRecentReadingDate();
$nodeRow['reading_current_value'] = $node->mostRecentReadingValue();
$nodeRow['reading_previous_date'] = $node->previousReadingDate();
$nodeRow['reading_previous_value'] = $node->previousReadingValue();
$nodeRow['serial'] = showHide($node->serial);
$nodeRow['mprn'] = showHide($node->mprn);
$nodeRow['billed_to_tennet'] = showHide($node->billed);
$nodeRow['supplier'] = showHide($node->supplier);
$nodeRow['address'] = showHide($node->address);
$nodeRow['account_no'] = showHide($node->account_no);
$nodeRow['retention_days'] = $node->retention_days;
$output[0] = $consumptionLast12Months;
$output[1] = array_keys($nodeRow);
$output[2] = $nodeRow;
} elseif ($_GET['type'] == "nodes") {
$nodesClass = new nodes();
$nodes = $nodesClass->all();
foreach ($nodes AS $node) {
$node = new node($node['uid']);
$location = new location($node->location);
$nodeRow['uid'] = $node->uid;
$nodeRow['enabled'] = $node->enabled;
$nodeRow['name'] = $node->cleanName();
$nodeRow['location'] = $location->name;
$nodeRow['type'] = $node->type;
$nodeRow['unit'] = $node->unit;
$nodeRow['photograph'] = $node->photograph;
$nodeRow['geo'] = $node->geo;
$nodeRow['reading_current_date'] = $node->mostRecentReadingDate();
$nodeRow['reading_current_value'] = $node->mostRecentReadingValue();
$nodeRow['reading_previous_date'] = $node->previousReadingDate();
$nodeRow['reading_previous_value'] = $node->previousReadingValue();
$nodeRow['serial'] = showHide($node->serial);
$nodeRow['mprn'] = showHide($node->mprn);
$nodeRow['billed_to_tennet'] = showHide($node->billed);
$nodeRow['supplier'] = showHide($node->supplier);
$nodeRow['address'] = showHide($node->address);
$nodeRow['account_no'] = showHide($node->account_no);
$nodeRow['retention_days'] = $node->retention_days;
$nodeArray[] = $nodeRow;
}
$output = $nodeArray;
} elseif ($_GET['type'] == "readings") {
if ($_GET['filter'] == "all") {
$readings = readings::all();
} else {
$readings = readings::node_all_readings(filter_var($_GET['filter'], FILTER_SANITIZE_NUMBER_INT));
}
foreach ($readings AS $reading) {
$node = new node($reading['node']);
$location = new location($node->location);
$nodeRow['uid'] = $reading['uid'];
$nodeRow['date'] = $reading['date'];
$nodeRow['node_uid'] = $reading['node'];
$nodeRow['node_name'] = $node->cleanName();
$nodeRow['location_uid'] = $location->uid;
$nodeRow['location'] = $location->cleanName();
$nodeRow['reading1'] = $reading['reading1'];
$nodeRow['node_unit'] = $node->unit;
$nodeRow['username'] = showHide($reading['username']);
$nodeRow['node_type'] = $node->type;
$nodeArray[] = $nodeRow;
}
$output = $nodeArray;
} else {
$output[0] = array($_GET['type'] => "This export hasn't been created yet. It's on the roadmap to be developed though");
$output[1] .= $_GET;
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=readings_export.csv');
$outputFile = fopen('php://output', 'w');
// build headers
fputcsv($outputFile, array_keys($output[0]));
// build rows
foreach ($output as $outputRow) {
fputcsv($outputFile, $outputRow);
}
?>