Skip to content

Commit

Permalink
Re-add CSV export from stats; fixes glpi-project#3055
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Oct 30, 2017
1 parent b703c61 commit 3ac99b3
Showing 1 changed file with 95 additions and 5 deletions.
100 changes: 95 additions & 5 deletions inc/stat.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1323,19 +1323,21 @@ function getRights($interface = 'central') {
* ['name' => 'a name', 'data' => []],
* ['name' => 'another name', 'data' => []]
* ]
* @param integer $witdh Graph width. Defaults to 900
* @param array $options Options
* @param boolean $display Whether to display directly; defauts to true
*
* @return void
*/
public function displayLineGraph($title, $labels, $series, $options = null, $display = true) {
global $CFG_GLPI;

$param = [
'width' => 900,
'height' => 300,
'tooltip' => true,
'legend' => true,
'animate' => true
'animate' => true,
'csv' => true
];

if (is_array($options) && count($options)) {
Expand All @@ -1346,7 +1348,15 @@ public function displayLineGraph($title, $labels, $series, $options = null, $dis

$slug = str_replace('-', '_', Toolbox::slugify($title));
$this->checkEmptyLabels($labels);
$out = "<h2 class='center'>$title</h2>";
$out = "<h2 class='center'>$title";
if ($param['csv']) {
$csvfilename = $this->generateCsvFile($labels, $series, $options);
$out .= " <a href='".$CFG_GLPI['root_doc'].
"/front/graph.send.php?file=$csvfilename' title='".__s('CSV').
"' class='pointer fa fa-file-text'><span class='sr-only'>".__('CSV').
"</span></a>";
}
$out .= "</h2>";
$out .= "<div id='$slug' class='chart'></div>";
Html::requireJs('charts');
$out .= "<script type='text/javascript'>
Expand Down Expand Up @@ -1438,14 +1448,34 @@ public function displayLineGraph($title, $labels, $series, $options = null, $dis
* ['name' => 'a name', 'data' => []],
* ['name' => 'another name', 'data' => []]
* ]
* @param array $options Options
* @param boolean $display Whether to display directly; defauts to true
*
* @return void
*/
public function displayPieGraph($title, $labels, $series, $display = true) {
public function displayPieGraph($title, $labels, $series, $options = [], $display = true) {
global $CFG_GLPI;
$param = [
'csv' => true
];

if (is_array($options) && count($options)) {
foreach ($options as $key => $val) {
$param[$key] = $val;
}
}

$slug = str_replace('-', '_', Toolbox::slugify($title));
$this->checkEmptyLabels($labels);
$out = "<h2 class='center'>$title</h2>";
$out = "<h2 class='center'>$title";
if ($param['csv']) {
$csvfilename = $this->generateCsvFile($labels, $series, $options);
$out .= " <a href='".$CFG_GLPI['root_doc'];
"/front/graph.send.php?file=$csvfilename' title='".__s('CSV').
"' class='pointer fa fa-file-text'><span class='sr-only'>".__('CSV').
"</span></a>";
}
$out .= "</h2>";
$out .= "<div id='$slug' class='chart'></div>";
$out .= "<script type='text/javascript'>
$(function() {
Expand Down Expand Up @@ -1583,5 +1613,65 @@ private function checkEmptyLabels(&$labels) {
}
}
}

/**
* Generates te CSV file
*
* @param array $labels Labels
* @param array $series Series
* @param array $options Options
*
* @return string filename
*/
private function generateCsvFile($labels, $series, $options = []) {
$uid = Session::getLoginUserID(false);
$csvfilename = $uid.'_'.mt_rand().'.csv';

// Render CSV
if ($fp = fopen(GLPI_GRAPH_DIR.'/'.$csvfilename, 'w')) {
// reformat datas
$values = [];
$headers = [];
$row_num = 0;
foreach ($series as $serie) {
$headers[$row_num] = $serie['name'];
$data = $serie['data'];
//$labels[$row_num] = $label;
if (is_array($data) && count($data)) {
foreach ($data as $key => $val) {
if (!isset($values[$key])) {
$values[$key] = [];
}
if (isset($options['datatype']) && $options['datatype'] == 'average') {
$val = round($val, 2);
}
$values[$key][$row_num] = $val;
}
}
$row_num++;
}
ksort($values);

// Print labels
fwrite($fp, $_SESSION["glpicsv_delimiter"]);
foreach ($headers as $val) {
fwrite($fp, $val.$_SESSION["glpicsv_delimiter"]);
}
fwrite($fp, "\n");

//print values
foreach ($values as $key => $data) {
fwrite($fp, $key.$_SESSION["glpicsv_delimiter"]);
foreach ($data as $value) {
fwrite($fp, $value.$_SESSION["glpicsv_delimiter"]);
}
fwrite($fp, "\n");
}

fclose($fp);
return $csvfilename;
}
return false;
}
}

0 comments on commit 3ac99b3

Please sign in to comment.