Skip to content

Commit e92becf

Browse files
author
Eugene Manuilov
committed
Added ability to define custom delimiter, enclosure and escape variables for CSV parsing
1 parent 693d35e commit e92becf

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

classes/Visualizer/Source/Csv.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public function __construct( $filename = null ) {
6262
*/
6363
private function _fetchSeries( &$handle ) {
6464
// read column titles
65-
$labels = fgetcsv( $handle );
65+
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE, VISUALIZER_CSV_ESCAPRE );
6666

6767
// read series types
68-
$types = fgetcsv( $handle );
68+
$types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE, VISUALIZER_CSV_ESCAPRE );
6969

7070
if ( !$labels || !$types ) {
7171
return false;
@@ -78,7 +78,7 @@ private function _fetchSeries( &$handle ) {
7878
$handle = fopen( $this->_filename, 'rb' );
7979

8080
// re read the labels and empty types array
81-
$labels = fgetcsv( $handle );
81+
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE, VISUALIZER_CSV_ESCAPRE );
8282
$types = array();
8383
}
8484

@@ -119,7 +119,7 @@ public function fetch() {
119119
}
120120

121121
// fetch data
122-
while ( ( $data = fgetcsv( $handle ) ) !== false ) {
122+
while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE, VISUALIZER_CSV_ESCAPRE ) ) !== false ) {
123123
$this->_data[] = $this->_normalizeData( $data );
124124
}
125125

index.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ function visualizer_launch() {
7070
define( 'VISUALIZER_ABSURL', plugins_url( '/', __FILE__ ) );
7171
define( 'VISUALIZER_ABSPATH', dirname( __FILE__ ) );
7272

73+
if ( !defined( 'VISUALIZER_CSV_DELIMITER' ) ) {
74+
define( 'VISUALIZER_CSV_DELIMITER', ',' );
75+
}
76+
77+
if ( !defined( 'VISUALIZER_CSV_ENCLOSURE' ) ) {
78+
define( 'VISUALIZER_CSV_ENCLOSURE', '"' );
79+
}
80+
81+
if ( !defined( 'VISUALIZER_CSV_ESCAPRE' ) ) {
82+
define( 'VISUALIZER_CSV_ESCAPRE', '\\' );
83+
}
84+
7385
// don't load the plugin if cron job is running or doing autosave
7486
$doing_autosave = defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE;
7587
$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Pay attention that to turn your shortcodes into graphs, your theme has to have `
6161
= 1.4.1 =
6262
* Fixed issue which prevents the plugin working on SSL backend
6363
* Fixed issue with CSV file uploading in IE and other browsers
64+
* Added ability to define custom delimiter, enclosure and escape variables for CSV parsing
6465

6566
= 1.4 =
6667
* Implemented aggregation target and selection mode options for candlestick chart

0 commit comments

Comments
 (0)