PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.4.2.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.4.2.2
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 +21 -76 3.10.21.4.2.2 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
@@ -46,10 +47,10 @@
46 47 *
47 48 * @access public
48 49 * @param string $filename The path to the file.
49 50 */
50 - public function __construct( $filename = '' ) {
51 - $this->_filename = trim( (string) $filename );
51 + public function __construct( $filename = null ) {
52 + $this->_filename = trim( $filename );
52 53 }
53 54
54 55 /**
55 56 * Fetches series information.
@@ -61,45 +62,32 @@
61 62 */
62 63 private function _fetchSeries( &$handle ) {
63 64 // read column titles
64 65 $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
65 - $types = null;
66 + // read series types
67 + $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
66 68
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 - }
74 -
75 - if ( is_null( $types ) ) {
76 - // read series types
77 - $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
78 - }
79 -
80 - $labels = array_filter( $labels );
81 - $types = array_filter( $types );
82 -
83 - if ( ! $labels || ! $types ) {
84 - $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' );
69 + if ( !$labels || !$types ) {
85 70 return false;
86 71 }
87 72
73 + // if no types were setup, re read labels and empty types array
88 74 $types = array_map( 'trim', $types );
89 - if ( ! self::_validateTypes( $types ) ) {
90 - $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' );
91 - return false;
75 + if ( !self::_validateTypes( $types ) ) {
76 + // re open the file
77 + fclose( $handle );
78 + $handle = $this->_get_file_handle();
79 +
80 + // re read the labels and empty types array
81 + $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
82 + $types = array();
92 83 }
93 84
94 85 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
95 - $default_type = $i === 0 ? 'string' : 'number';
96 -
97 - $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
98 -
86 + $default_type = $i == 0 ? 'string' : 'number';
99 87 $this->_series[] = array(
100 - 'label' => $labels[ $i ],
101 - 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
88 + 'label' => $labels[$i],
89 + 'type' => isset( $types[$i] ) ? $types[$i] : $default_type,
102 90 );
103 91 }
104 92
105 93 return true;
@@ -115,9 +103,9 @@
115 103 * @return resource File handle resource on success, otherwise FALSE.
116 104 */
117 105 protected function _get_file_handle( $filename = false ) {
118 106 // set line endings auto detect mode
119 - ini_set( 'auto_detect_line_endings', true );
107 + @ini_set( 'auto_detect_line_endings', true );
120 108 // open file and return handle
121 109 return fopen( $filename ? $filename : $this->_filename, 'rb' );
122 110 }
123 111
@@ -131,9 +119,8 @@
131 119 */
132 120 public function fetch() {
133 121 // if filename is empty return false
134 122 if ( empty( $this->_filename ) ) {
135 - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
136 123 return false;
137 124 }
138 125
139 126 // read file and fill arrays
@@ -139,14 +126,13 @@
139 126 // read file and fill arrays
140 127 $handle = $this->_get_file_handle();
141 128 if ( $handle ) {
142 129 // fetch series
143 - if ( ! $this->_fetchSeries( $handle ) ) {
130 + if ( !$this->_fetchSeries( $handle ) ) {
144 131 return false;
145 132 }
146 133
147 134 // fetch data
148 - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
149 135 while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
150 136 $this->_data[] = $this->_normalizeData( $data );
151 137 }
152 138
@@ -168,46 +154,5 @@
168 154 public function getSourceName() {
169 155 return __CLASS__;
170 156 }
171 157
172 - /**
173 - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
174 - * 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
175 - * to get the subset of data specified by the query
176 - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
177 - * so we need to split them apart logically
178 - * also the $types variable now contains the first row because the header and the type got conflated
179 - */
180 - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
181 - $new_labels = array();
182 - $new_types = array();
183 - $abort = false;
184 - foreach ( $labels as $label ) {
185 - // get the index of the last space
186 - $index = strrpos( $label, ' ' );
187 - if ( $index === false ) {
188 - // no space here? something has gone wrong; abort the entire process.
189 - $abort = true;
190 - break;
191 - }
192 - $type = trim( substr( $label, $index + 1 ) );
193 - if ( ! self::_validateTypes( array( $type ) ) ) {
194 - // some other data type? abort the entire process.
195 - $abort = true;
196 - break;
197 - }
198 - $label = substr( $label, 0, $index );
199 - $new_labels[] = $label;
200 - $new_types[] = $type;
201 - }
202 - if ( ! $abort ) {
203 - $labels = $new_labels;
204 - $types = $new_types;
205 - }
206 -
207 - return array(
208 - 'abort' => $abort,
209 - 'labels' => $labels,
210 - 'types' => $types,
211 - );
212 - }
213 -}
158 +}