Skip to content

Commit 1bfc797

Browse files
committed
refactor: move buildCSV() to UsageGraphs_UsageGraphs
The buildCSV() method is shared amongst all usagegraphs classes and can therefore be added to the abstract UsageGraphs_UsageGraphs class, provided that it is amended to take in a string as a parameter with the name of the section (eg. 'Admin')
1 parent 535281e commit 1bfc797

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

code/web/services/Admin/AJAX.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ public function copyMenuLinks() {
16141614
public function exportUsageData() {
16151615
require_once ROOT_DIR . '/services/Admin/UsageGraphs.php';
16161616
$aspenUsageGraph = new Admin_UsageGraphs();
1617-
$aspenUsageGraph->buildCSV();
1617+
$aspenUsageGraph->buildCSV('Admin');
16181618
// TODO: trigger page refresh
16191619
}
16201620
}

code/web/services/Admin/AbstractUsageGraphs.php

+44
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,48 @@ public function canView(): bool {
4040
]);
4141
}
4242

43+
public function buildCSV(string $section): void {
44+
global $interface;
45+
46+
$stat = $_REQUEST['stat'];
47+
if (!empty($_REQUEST['instance'])) {
48+
$instanceName = $_REQUEST['instance'];
49+
} else {
50+
$instanceName = '';
51+
}
52+
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
53+
$dataSeries = $interface->getVariable('dataSeries');
54+
55+
$filename = "{$section}UsageData_{$stat}.csv";
56+
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
57+
header("Cache-Control: no-store, no-cache, must-revalidate");
58+
header("Cache-Control: post-check=0, pre-check=0", false);
59+
header("Pragma: no-cache");
60+
header('Content-Type: text/csv; charset=utf-8');
61+
header("Content-Disposition: attachment;filename={$filename}");
62+
$fp = fopen('php://output', 'w');
63+
$graphTitles = array_keys($dataSeries);
64+
$numGraphTitles = count($dataSeries);
65+
66+
// builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph
67+
for($i = 0; $i < $numGraphTitles; $i++) {
68+
$dataSerie = $dataSeries[$graphTitles[$i]];
69+
$numRows = count($dataSerie['data']);
70+
$dates = array_keys($dataSerie['data']);
71+
$header = ['Dates', $graphTitles[$i]];
72+
fputcsv($fp, $header);
73+
74+
// builds each subsequent data row - aka the column value
75+
if (empty($numRows)) {
76+
fputcsv($fp, ['no data found']);
77+
}
78+
for($j = 0; $j < $numRows; $j++) {
79+
$date = $dates[$j];
80+
$value = $dataSerie['data'][$date];
81+
$row = [$date, $value];
82+
fputcsv($fp, $row);
83+
}
84+
}
85+
exit();
86+
}
4387
}

code/web/services/Admin/UsageGraphs.php

-42
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,6 @@ function getActiveAdminSection(): string {
2121
return 'system_reports';
2222
}
2323

24-
25-
public function buildCSV() {
26-
global $interface;
27-
28-
$stat = $_REQUEST['stat'];
29-
if (!empty($_REQUEST['instance'])) {
30-
$instanceName = $_REQUEST['instance'];
31-
} else {
32-
$instanceName = '';
33-
}
34-
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
35-
$dataSeries = $interface->getVariable('dataSeries');
36-
37-
$filename = "AspenUsageData_{$stat}.csv";
38-
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
39-
header("Cache-Control: no-store, no-cache, must-revalidate");
40-
header("Cache-Control: post-check=0, pre-check=0", false);
41-
header("Pragma: no-cache");
42-
header('Content-Type: text/csv; charset=utf-8');
43-
header("Content-Disposition: attachment;filename={$filename}");
44-
$fp = fopen('php://output', 'w');
45-
$graphTitles = array_keys($dataSeries);
46-
$numGraphTitles = count($dataSeries);
47-
48-
// builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph
49-
for($i = 0; $i < $numGraphTitles; $i++) {
50-
$dataSerie = $dataSeries[$graphTitles[$i]];
51-
$numRows = count($dataSerie['data']);
52-
$dates = array_keys($dataSerie['data']);
53-
$header = ['Dates', $graphTitles[$i]];
54-
fputcsv($fp, $header);
55-
56-
// builds each subsequent data row - aka the column value
57-
for($j = 0; $j < $numRows; $j++) {
58-
$date = $dates[$j];
59-
$value = $dataSerie['data'][$date];
60-
$row = [$date, $value];
61-
fputcsv($fp, $row);
62-
}
63-
}
64-
exit();
65-
}
6624
private function getAndSetInterfaceDataSeries($stat, $instanceName) {
6725
global $interface;
6826
global $enabledModules;

0 commit comments

Comments
 (0)