PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.4
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
← All changes | classes/Visualizer/Source/Csv.php +17 -86 3.1.31.4 View file →
@@ -18,8 +18,9 @@
18 18 // | MA 02110-1301 USA |
19 19 // +----------------------------------------------------------------------+
20 20 // | Author: Eugene Manuilov <eugene@manuilov.org> |
21 21 // +----------------------------------------------------------------------+
22 +
22 23 /**
23 24 * Source manager for local CSV files.
24 25 *
25 26 * @category Visualizer
@@ -60,48 +61,33 @@
60 61 * @param resource $handle The file handle resource.
61 62 */
62 63 private function _fetchSeries( &$handle ) {
63 64 // read column titles
64 - $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
65 - $types = null;
65 + $labels = fgetcsv( $handle );
66 66
67 - if ( false !== strpos( $this->_filename, 'tqx=out:csv' ) ) {
68 - $attributes = $this->_fetchSeriesForGoogleQueryLanguage( $labels );
69 - if ( ! $attributes['abort'] ) {
70 - $labels = $attributes['labels'];
71 - $types = $attributes['types'];
72 - }
73 - }
67 + // read series types
68 + $types = fgetcsv( $handle );
74 69
75 - if ( is_null( $types ) ) {
76 - // read series types
77 - $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
78 - }
79 -
80 - if ( ! $labels || ! $types ) {
70 + if ( !$labels || !$types ) {
81 71 return false;
82 72 }
83 73
84 74 // if no types were setup, re read labels and empty types array
85 - $types = array_map( 'trim', $types );
86 - if ( ! self::_validateTypes( $types ) ) {
75 + if ( !self::_validateTypes( $types ) ) {
87 76 // re open the file
88 77 fclose( $handle );
89 - $handle = $this->_get_file_handle();
78 + $handle = fopen( $this->_filename, 'rb' );
90 79
91 80 // re read the labels and empty types array
92 - $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
81 + $labels = fgetcsv( $handle );
93 82 $types = array();
94 83 }
95 84
96 85 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
97 86 $default_type = $i == 0 ? 'string' : 'number';
98 -
99 - $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
100 -
101 87 $this->_series[] = array(
102 - 'label' => $labels[ $i ],
103 - 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
88 + 'label' => $labels[$i],
89 + 'type' => isset( $types[$i] ) ? $types[$i] : $default_type,
104 90 );
105 91 }
106 92
107 93 return true;
@@ -107,24 +93,8 @@
107 93 return true;
108 94 }
109 95
110 96 /**
111 - * Returns file handle to fetch data from.
112 - *
113 - * @since 1.4.2
114 - *
115 - * @access protected
116 - * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
117 - * @return resource File handle resource on success, otherwise FALSE.
118 - */
119 - protected function _get_file_handle( $filename = false ) {
120 - // set line endings auto detect mode
121 - ini_set( 'auto_detect_line_endings', true );
122 - // open file and return handle
123 - return fopen( $filename ? $filename : $this->_filename, 'rb' );
124 - }
125 -
126 - /**
127 97 * Fetches information from source, parses it and builds series and data arrays.
128 98 *
129 99 * @since 1.0.0
130 100 *
@@ -136,19 +106,21 @@
136 106 if ( empty( $this->_filename ) ) {
137 107 return false;
138 108 }
139 109
110 + // set line endings auto detect mode
111 + @ini_set( 'auto_detect_line_endings', true );
112 +
140 113 // read file and fill arrays
141 - $handle = $this->_get_file_handle();
114 + $handle = fopen( $this->_filename, 'rb' );
142 115 if ( $handle ) {
143 116 // fetch series
144 - if ( ! $this->_fetchSeries( $handle ) ) {
117 + if ( !$this->_fetchSeries( $handle ) ) {
145 118 return false;
146 119 }
147 120
148 121 // fetch data
149 - while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
150 -
122 + while ( ( $data = fgetcsv( $handle ) ) !== false ) {
151 123 $this->_data[] = $this->_normalizeData( $data );
152 124 }
153 125
154 126 // close file handle
@@ -169,46 +141,5 @@
169 141 public function getSourceName() {
170 142 return __CLASS__;
171 143 }
172 144
173 - /**
174 - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
175 - * where the user can provide something like /gviz/tq?tq=select%20A%2C%20B%20&tqx=out:csv after the URL of the raw spreadsheet
176 - * to get the subset of data specified by the query
177 - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
178 - * so we need to split them apart logically
179 - * also the $types variable now contains the first row because the header and the type got conflated
180 - */
181 - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
182 - $new_labels = array();
183 - $new_types = array();
184 - $abort = false;
185 - foreach ( $labels as $label ) {
186 - // get the index of the last space
187 - $index = strrpos( $label, ' ' );
188 - if ( $index === false ) {
189 - // no space here? something has gone wrong; abort the entire process.
190 - $abort = true;
191 - break;
192 - }
193 - $type = trim( substr( $label, $index + 1 ) );
194 - if ( ! self::_validateTypes( array( $type ) ) ) {
195 - // some other data type? abort the entire process.
196 - $abort = true;
197 - break;
198 - }
199 - $label = substr( $label, 0, $index );
200 - $new_labels[] = $label;
201 - $new_types[] = $type;
202 - }
203 - if ( ! $abort ) {
204 - $labels = $new_labels;
205 - $types = $new_types;
206 - }
207 -
208 - return array(
209 - 'abort' => $abort,
210 - 'labels' => $labels,
211 - 'types' => $types,
212 - );
213 - }
214 -}
145 +}