PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.0.0
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.0.0
4.0.8 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 All 149 releases
← All changes | classes/Visualizer/Source/Csv.php +19 -102 3.10.151.0.0 View file →
@@ -18,10 +18,11 @@
18 18 // | MA 02110-1301 USA |
19 19 // +----------------------------------------------------------------------+
20 20 // | Author: Eugene Manuilov <[email protected]> |
21 21 // +----------------------------------------------------------------------+
22 +
22 23 /**
23 - * Source manager for local CSV files.
24 + * Source manager for CSV files.
24 25 *
25 26 * @category Visualizer
26 27 * @package Source
27 28 *
@@ -33,12 +34,12 @@
33 34 * The path to the file with data.
34 35 *
35 36 * @since 1.0.0
36 37 *
37 - * @access protected
38 + * @access private
38 39 * @var string
39 40 */
40 - protected $_filename;
41 + private $_filename;
41 42
42 43 /**
43 44 * Constructor.
44 45 *
@@ -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 ) {
52 + $this->_filename = $filename;
52 53 }
53 54
54 55 /**
55 56 * Fetches series information.
@@ -58,48 +59,23 @@
58 59 *
59 60 * @access private
60 61 * @param resource $handle The file handle resource.
61 62 */
62 - private function _fetchSeries( &$handle ) {
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 - $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' );
70 + if ( !$labels || !$types ) {
85 71 return false;
86 72 }
87 73
88 - $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;
92 - }
93 -
94 74 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 -
99 75 $this->_series[] = array(
100 - 'label' => $labels[ $i ],
101 - 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
76 + 'label' => $labels[$i],
77 + 'type' => isset( $types[$i] ) ? $types[$i] : 'string',
102 78 );
103 79 }
104 80
105 81 return true;
@@ -105,22 +81,8 @@
105 81 return true;
106 82 }
107 83
108 84 /**
109 - * Returns file handle to fetch data from.
110 - *
111 - * @since 1.4.2
112 - *
113 - * @access protected
114 - * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
115 - * @return resource File handle resource on success, otherwise FALSE.
116 - */
117 - protected function _get_file_handle( $filename = false ) {
118 - // open file and return handle
119 - return fopen( $filename ? $filename : $this->_filename, 'rb' );
120 - }
121 -
122 - /**
123 85 * Fetches information from source, parses it and builds series and data arrays.
124 86 *
125 87 * @since 1.0.0
126 88 *
@@ -127,25 +89,21 @@
127 89 * @access public
128 90 * @return boolean TRUE on success, otherwise FALSE.
129 91 */
130 92 public function fetch() {
131 - // if filename is empty return false
132 - if ( empty( $this->_filename ) ) {
133 - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' );
134 - return false;
135 - }
93 + // set line endings auto detect mode
94 + @ini_set( 'auto_detect_line_endings', true );
136 95
137 96 // read file and fill arrays
138 - $handle = $this->_get_file_handle();
97 + $handle = fopen( $this->_filename, 'rb' );
139 98 if ( $handle ) {
140 99 // fetch series
141 - if ( ! $this->_fetchSeries( $handle ) ) {
100 + if ( !$this->_fetchSeries( $handle ) ) {
142 101 return false;
143 102 }
144 103
145 104 // fetch data
146 - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
147 - while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
105 + while ( ( $data = fgetcsv( $handle ) ) !== false ) {
148 106 $this->_data[] = $this->_normalizeData( $data );
149 107 }
150 108
151 109 // close file handle
@@ -166,46 +124,5 @@
166 124 public function getSourceName() {
167 125 return __CLASS__;
168 126 }
169 127
170 - /**
171 - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage
172 - * 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
173 - * to get the subset of data specified by the query
174 - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string"
175 - * so we need to split them apart logically
176 - * also the $types variable now contains the first row because the header and the type got conflated
177 - */
178 - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) {
179 - $new_labels = array();
180 - $new_types = array();
181 - $abort = false;
182 - foreach ( $labels as $label ) {
183 - // get the index of the last space
184 - $index = strrpos( $label, ' ' );
185 - if ( $index === false ) {
186 - // no space here? something has gone wrong; abort the entire process.
187 - $abort = true;
188 - break;
189 - }
190 - $type = trim( substr( $label, $index + 1 ) );
191 - if ( ! self::_validateTypes( array( $type ) ) ) {
192 - // some other data type? abort the entire process.
193 - $abort = true;
194 - break;
195 - }
196 - $label = substr( $label, 0, $index );
197 - $new_labels[] = $label;
198 - $new_types[] = $type;
199 - }
200 - if ( ! $abort ) {
201 - $labels = $new_labels;
202 - $types = $new_types;
203 - }
204 -
205 - return array(
206 - 'abort' => $abort,
207 - 'labels' => $labels,
208 - 'types' => $types,
209 - );
210 - }
211 -}
128 +}