diff --git a/code/web/services/Admin/AJAX.php b/code/web/services/Admin/AJAX.php index b1318c22f3..c262256470 100644 --- a/code/web/services/Admin/AJAX.php +++ b/code/web/services/Admin/AJAX.php @@ -1614,7 +1614,7 @@ public function copyMenuLinks() { public function exportUsageData() { require_once ROOT_DIR . '/services/Admin/UsageGraphs.php'; $aspenUsageGraph = new Admin_UsageGraphs(); - $aspenUsageGraph->buildCSV(); + $aspenUsageGraph->buildCSV('Admin'); // TODO: trigger page refresh } } \ No newline at end of file diff --git a/code/web/services/Admin/AbstractUsageGraphs.php b/code/web/services/Admin/AbstractUsageGraphs.php index 10006aae9e..26e97b75e8 100644 --- a/code/web/services/Admin/AbstractUsageGraphs.php +++ b/code/web/services/Admin/AbstractUsageGraphs.php @@ -40,4 +40,48 @@ public function canView(): bool { ]); } + public function buildCSV(string $section): void { + global $interface; + + $stat = $_REQUEST['stat']; + if (!empty($_REQUEST['instance'])) { + $instanceName = $_REQUEST['instance']; + } else { + $instanceName = ''; + } + $this->getAndSetInterfaceDataSeries($stat, $instanceName); + $dataSeries = $interface->getVariable('dataSeries'); + + $filename = "{$section}UsageData_{$stat}.csv"; + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); + header("Cache-Control: no-store, no-cache, must-revalidate"); + header("Cache-Control: post-check=0, pre-check=0", false); + header("Pragma: no-cache"); + header('Content-Type: text/csv; charset=utf-8'); + header("Content-Disposition: attachment;filename={$filename}"); + $fp = fopen('php://output', 'w'); + $graphTitles = array_keys($dataSeries); + $numGraphTitles = count($dataSeries); + + // builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph + for($i = 0; $i < $numGraphTitles; $i++) { + $dataSerie = $dataSeries[$graphTitles[$i]]; + $numRows = count($dataSerie['data']); + $dates = array_keys($dataSerie['data']); + $header = ['Dates', $graphTitles[$i]]; + fputcsv($fp, $header); + + // builds each subsequent data row - aka the column value + if (empty($numRows)) { + fputcsv($fp, ['no data found']); + } + for($j = 0; $j < $numRows; $j++) { + $date = $dates[$j]; + $value = $dataSerie['data'][$date]; + $row = [$date, $value]; + fputcsv($fp, $row); + } + } + exit(); + } } \ No newline at end of file diff --git a/code/web/services/Admin/UsageGraphs.php b/code/web/services/Admin/UsageGraphs.php index 36d48a81ff..8d64d46a72 100644 --- a/code/web/services/Admin/UsageGraphs.php +++ b/code/web/services/Admin/UsageGraphs.php @@ -21,48 +21,6 @@ function getActiveAdminSection(): string { return 'system_reports'; } - - public function buildCSV() { - global $interface; - - $stat = $_REQUEST['stat']; - if (!empty($_REQUEST['instance'])) { - $instanceName = $_REQUEST['instance']; - } else { - $instanceName = ''; - } - $this->getAndSetInterfaceDataSeries($stat, $instanceName); - $dataSeries = $interface->getVariable('dataSeries'); - - $filename = "AspenUsageData_{$stat}.csv"; - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - header("Cache-Control: no-store, no-cache, must-revalidate"); - header("Cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); - header('Content-Type: text/csv; charset=utf-8'); - header("Content-Disposition: attachment;filename={$filename}"); - $fp = fopen('php://output', 'w'); - $graphTitles = array_keys($dataSeries); - $numGraphTitles = count($dataSeries); - - // builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph - for($i = 0; $i < $numGraphTitles; $i++) { - $dataSerie = $dataSeries[$graphTitles[$i]]; - $numRows = count($dataSerie['data']); - $dates = array_keys($dataSerie['data']); - $header = ['Dates', $graphTitles[$i]]; - fputcsv($fp, $header); - - // builds each subsequent data row - aka the column value - for($j = 0; $j < $numRows; $j++) { - $date = $dates[$j]; - $value = $dataSerie['data'][$date]; - $row = [$date, $value]; - fputcsv($fp, $row); - } - } - exit(); - } private function getAndSetInterfaceDataSeries($stat, $instanceName) { global $interface; global $enabledModules;