Skip to content

Commit 7181018

Browse files
authored
Merge pull request #616 from benderl/chargelog-download
fix empty files in yearly download
2 parents 60fe579 + 7acf6a1 commit 7181018

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

public/downloadChargeLog.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
$month = $_GET["month"];
4747
// Format the file name based on the year and month
4848
$file_name = sprintf('%04d%02d', $year, $month);
49+
$output_file_name = $file_name;
4950
$charge_log_file = $charge_log_path . $file_name . ".json";
5051

5152
// Attempt to load the charge log file for the specific month
@@ -66,12 +67,15 @@
6667
for ($month = 1; $month <= 12; $month++) {
6768
// Format the file name based on the year and month
6869
$file_name = sprintf('%04d%02d', $year, $month);
70+
$output_file_name = sprintf('%04d', $year);
6971
$charge_log_file = $charge_log_path . $file_name . ".json";
7072

7173
// Append data from each month's charge log file if it exists
7274
if (file_exists($charge_log_file)) {
7375
$monthly_data = json_decode(file_get_contents($charge_log_file), true);
74-
$charge_log_data = array_merge($charge_log_data, $monthly_data);
76+
if (is_array($monthly_data)) {
77+
$charge_log_data = array_merge($charge_log_data, $monthly_data);
78+
}
7579
}
7680
}
7781
}
@@ -159,7 +163,7 @@ function formatEnergy($value)
159163

160164
if (is_array($charge_log_data)) {
161165
header("Content-Type: text/csv");
162-
header("Content-Disposition: attachment; filename=ChargeLog-" . $file_name . ".csv");
166+
header("Content-Disposition: attachment; filename=ChargeLog-" . $output_file_name . ".csv");
163167

164168
# Output CSV header
165169
$csv_header = newRow();

0 commit comments

Comments
 (0)