From aeb7a7e9c6686ab1c83869009799a3f47a003a66 Mon Sep 17 00:00:00 2001
From: Luis <luis.calvo@artica.es>
Date: Tue, 1 Dec 2020 11:56:15 +0100
Subject: [PATCH] Add decimal separator to all pandora csv

---
 .../godmode/setup/setup_visuals.php           |  2 +-
 .../include/class/ModuleTemplates.class.php   | 20 ++++++++++++++++---
 pandora_console/include/functions.php         | 16 +++++++++++----
 .../include/graphs/export_data.php            | 12 ++++++++++-
 4 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/pandora_console/godmode/setup/setup_visuals.php b/pandora_console/godmode/setup/setup_visuals.php
index 8c07381452..d1230f8c35 100755
--- a/pandora_console/godmode/setup/setup_visuals.php
+++ b/pandora_console/godmode/setup/setup_visuals.php
@@ -1482,7 +1482,7 @@ $row++;
         '.' => '.',
         ',' => ',',
     ];
-    $table_other->data[$row][0] = __('CSV decimal separator').ui_print_help_tip(__('Only for csv reports'), true);
+    $table_other->data[$row][0] = __('CSV decimal separator');
     $table_other->data[$row][1] = html_print_select($decimal_separator, 'csv_decimal_separator', $config['csv_decimal_separator'], '', '', '', true, false, false);
 
     $row++;
diff --git a/pandora_console/include/class/ModuleTemplates.class.php b/pandora_console/include/class/ModuleTemplates.class.php
index f9573b6c0e..8d89519486 100644
--- a/pandora_console/include/class/ModuleTemplates.class.php
+++ b/pandora_console/include/class/ModuleTemplates.class.php
@@ -409,6 +409,8 @@ class ModuleTemplates extends HTML
                 case 'export':
                     global $config;
 
+                    enterprise_include_once('include/functions_reporting_csv.php');
+
                     $id_network_profile = safe_int($this->id_np);
                     if (empty($id_network_profile)) {
                         return false;
@@ -500,7 +502,7 @@ class ModuleTemplates extends HTML
                     }
 
                     // Then print the first line (row names)
-                    echo '"'.implode('","', $row_names).'"';
+                    echo '"'.implode('"'.$config['csv_divider'].'"', $row_names).'"';
                     echo "\n";
 
                     // Then print the rest of the data. Encapsulate in quotes in case we have comma's in any of the descriptions
@@ -509,7 +511,19 @@ class ModuleTemplates extends HTML
                             unset($row[$bad_key]);
                         }
 
-                        echo '"'.implode('","', $row).'"';
+                        if ($config['csv_decimal_separator'] !== '.') {
+                            foreach ($row as $name => $data) {
+                                if (is_numeric($data)) {
+                                    // Get the number of decimals, if > 0, format dec comma.
+                                    $decimals = strlen(substr(strrchr($data, '.'), 1));
+                                    if ($decimals !== 0) {
+                                        $row[$name] = csv_format_numeric((float) $data, $decimals, true);
+                                    }
+                                }
+                            }
+                        }
+
+                        echo '"'.implode('"'.$config['csv_divider'].'"', $row).'"';
                         echo "\n";
                     }
 
@@ -845,7 +859,7 @@ class ModuleTemplates extends HTML
                 $row['id_np'],
                 '',
                 true,
-                ['title' => 'Export to CSV']
+                ['title' => 'Export tdaso CSV']
             );
             $data[3] = '<a href="'.$this->baseUrl.'&action=delete&id_np='.$row['id_np'].'" onclick="if (!confirm(\''.__('Are you sure?').'\')) return false;">'.html_print_image('images/cross.png', true, ['title' => __('Delete')]).'</a>';
             $data[3] .= '<a href="'.$this->baseUrl.'&action=export&id_np='.$row['id_np'].'">'.html_print_image('images/csv.png', true, ['title' => __('Export to CSV')]).'</a>';
diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php
index b1c537c6aa..f79c56b764 100644
--- a/pandora_console/include/functions.php
+++ b/pandora_console/include/functions.php
@@ -3904,17 +3904,23 @@ function series_type_graph_array($data, $show_elements_graph)
                 $data_return['legend'][$key] .= __('Min:').remove_right_zeros(
                     number_format(
                         $value['min'],
-                        $config['graph_precision']
+                        $config['graph_precision'],
+                        $config['csv_decimal_separator'],
+                        $config['csv_decimal_separator'] == ',' ? '.' : ','
                     )
                 ).' '.__('Max:').remove_right_zeros(
                     number_format(
                         $value['max'],
-                        $config['graph_precision']
+                        $config['graph_precision'],
+                        $config['csv_decimal_separator'],
+                        $config['csv_decimal_separator'] == ',' ? '.' : ','
                     )
                 ).' '._('Avg:').remove_right_zeros(
                     number_format(
                         $value['avg'],
-                        $config['graph_precision']
+                        $config['graph_precision'],
+                        $config['csv_decimal_separator'],
+                        $config['csv_decimal_separator'] == ',' ? '.' : ','
                     )
                 ).' '.$str;
 
@@ -3978,7 +3984,8 @@ function series_type_graph_array($data, $show_elements_graph)
                     $data_return['legend'][$key] .= remove_right_zeros(
                         number_format(
                             $value['avg'],
-                            $config['graph_precision']
+                            $config['graph_precision'],
+                            $config['csv_decimal_separator']
                         )
                     ).' '.$str;
                 }
@@ -5911,4 +5918,5 @@ function send_test_email(
     }
 
     return $result;
+
 }
diff --git a/pandora_console/include/graphs/export_data.php b/pandora_console/include/graphs/export_data.php
index 8aca5c04b5..cab03220d5 100644
--- a/pandora_console/include/graphs/export_data.php
+++ b/pandora_console/include/graphs/export_data.php
@@ -15,6 +15,7 @@
 // This file only uses data retrieved in a request.
 require_once '../../include/config.php';
 require_once '../../include/functions.php';
+enterprise_include_once('include/functions_reporting_csv.php');
 
 global $config;
 
@@ -49,7 +50,10 @@ $filename = io_safe_output($filename);
  * );
  */
 $output_csv = function ($data, $filename) {
-    $separator = (string) get_parameter('separator', ';');
+    global $config;
+
+    $separator = (string) $config['csv_divider'];
+
     $excel_encoding = (bool) get_parameter('excel_encoding', false);
 
     // CSV Output
@@ -68,9 +72,15 @@ $output_csv = function ($data, $filename) {
                 throw new Exception(__('An error occured exporting the data'));
         }
 
+        // Get key for item value.
+        $value_key = array_search('value', $items['head']);
+
         $head_line = implode($separator, $items['head']);
         echo $head_line."\n";
         foreach ($items['data'] as $item) {
+             // Find value and replace csv decimal separator.
+            $item[$value_key] = csv_format_numeric($item[$value_key]);
+
             $item = str_replace('--> '.__('Selected'), '', $item);
             $line = implode($separator, $item);