| @@ -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. |
| @@ -58,53 +59,31 @@ | ||
| 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 | - if ( ! is_array( $labels ) || ! is_array( $types ) ) { | |
| 81 | - $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 ) { | |
| 82 | 71 | return false; |
| 83 | 72 | } |
| 84 | 73 | |
| 85 | - $labels = array_filter( $labels ); | |
| 86 | - $types = array_filter( $types ); | |
| 87 | - | |
| 88 | - if ( ! $labels || ! $types ) { | |
| 89 | - $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' ); | |
| 90 | - return false; | |
| 74 | + // if no types were setup, re read labels and empty types array | |
| 75 | + if ( !self::_validateTypes( $types ) ) { | |
| 76 | + fseek( $handle, 0 ); | |
| 77 | + $labels = fgetcsv( $handle ); | |
| 78 | + $types = array(); | |
| 91 | 79 | } |
| 92 | 80 | |
| 93 | - $types = array_map( 'trim', $types ); | |
| 94 | - if ( ! self::_validateTypes( $types ) ) { | |
| 95 | - $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' ); | |
| 96 | - return false; | |
| 97 | - } | |
| 98 | - | |
| 99 | 81 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 100 | - $default_type = $i === 0 ? 'string' : 'number'; | |
| 101 | - | |
| 102 | - $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); | |
| 103 | - | |
| 82 | + $default_type = $i == 0 ? 'string' : 'number'; | |
| 104 | 83 | $this->_series[] = array( |
| 105 | - 'label' => sanitize_text_field( wp_strip_all_tags( $labels[ $i ] ) ), | |
| 106 | - 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type, | |
| 84 | + 'label' => $labels[$i], | |
| 85 | + 'type' => isset( $types[$i] ) ? $types[$i] : $default_type, | |
| 107 | 86 | ); |
| 108 | 87 | } |
| 109 | 88 | |
| 110 | 89 | return true; |
| @@ -110,22 +89,8 @@ | ||
| 110 | 89 | return true; |
| 111 | 90 | } |
| 112 | 91 | |
| 113 | 92 | /** |
| 114 | - * Returns file handle to fetch data from. | |
| 115 | - * | |
| 116 | - * @since 1.4.2 | |
| 117 | - * | |
| 118 | - * @access protected | |
| 119 | - * @param string $filename Optional file name to get handle. If omitted, $_filename is used. | |
| 120 | - * @return resource File handle resource on success, otherwise FALSE. | |
| 121 | - */ | |
| 122 | - protected function _get_file_handle( $filename = false ) { | |
| 123 | - // open file and return handle | |
| 124 | - return fopen( $filename ? $filename : $this->_filename, 'rb' ); | |
| 125 | - } | |
| 126 | - | |
| 127 | - /** | |
| 128 | 93 | * Fetches information from source, parses it and builds series and data arrays. |
| 129 | 94 | * |
| 130 | 95 | * @since 1.0.0 |
| 131 | 96 | * |
| @@ -134,25 +99,25 @@ | ||
| 134 | 99 | */ |
| 135 | 100 | public function fetch() { |
| 136 | 101 | // if filename is empty return false |
| 137 | 102 | if ( empty( $this->_filename ) ) { |
| 138 | - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 139 | 103 | return false; |
| 140 | 104 | } |
| 141 | 105 | |
| 106 | + // set line endings auto detect mode | |
| 107 | + @ini_set( 'auto_detect_line_endings', true ); | |
| 108 | + | |
| 142 | 109 | // read file and fill arrays |
| 143 | - $handle = $this->_get_file_handle(); | |
| 110 | + $handle = fopen( $this->_filename, 'rb' ); | |
| 144 | 111 | if ( $handle ) { |
| 145 | 112 | // fetch series |
| 146 | - if ( ! $this->_fetchSeries( $handle ) ) { | |
| 113 | + if ( !$this->_fetchSeries( $handle ) ) { | |
| 147 | 114 | return false; |
| 148 | 115 | } |
| 149 | 116 | |
| 150 | 117 | // fetch data |
| 151 | - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 152 | - while ( $data !== false ) { | |
| 118 | + while ( ( $data = fgetcsv( $handle ) ) !== false ) { | |
| 153 | 119 | $this->_data[] = $this->_normalizeData( $data ); |
| 154 | - $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 155 | 120 | } |
| 156 | 121 | |
| 157 | 122 | // close file handle |
| 158 | 123 | fclose( $handle ); |
| @@ -172,46 +137,5 @@ | ||
| 172 | 137 | public function getSourceName() { |
| 173 | 138 | return __CLASS__; |
| 174 | 139 | } |
| 175 | 140 | |
| 176 | - /** | |
| 177 | - * Adds support for QueryLanguage https://developers.google.com/chart/interactive/docs/querylanguage | |
| 178 | - * 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 | |
| 179 | - * to get the subset of data specified by the query | |
| 180 | - * this will conflate the heading and the type into one value viz. for heading XXX and type string, the value will become "XXX string" | |
| 181 | - * so we need to split them apart logically | |
| 182 | - * also the $types variable now contains the first row because the header and the type got conflated | |
| 183 | - */ | |
| 184 | - private function _fetchSeriesForGoogleQueryLanguage( $labels, $types = array() ) { | |
| 185 | - $new_labels = array(); | |
| 186 | - $new_types = array(); | |
| 187 | - $abort = false; | |
| 188 | - foreach ( $labels as $label ) { | |
| 189 | - // get the index of the last space | |
| 190 | - $index = strrpos( $label, ' ' ); | |
| 191 | - if ( $index === false ) { | |
| 192 | - // no space here? something has gone wrong; abort the entire process. | |
| 193 | - $abort = true; | |
| 194 | - break; | |
| 195 | - } | |
| 196 | - $type = trim( substr( $label, $index + 1 ) ); | |
| 197 | - if ( ! self::_validateTypes( array( $type ) ) ) { | |
| 198 | - // some other data type? abort the entire process. | |
| 199 | - $abort = true; | |
| 200 | - break; | |
| 201 | - } | |
| 202 | - $label = substr( $label, 0, $index ); | |
| 203 | - $new_labels[] = $label; | |
| 204 | - $new_types[] = $type; | |
| 205 | - } | |
| 206 | - if ( ! $abort ) { | |
| 207 | - $labels = $new_labels; | |
| 208 | - $types = $new_types; | |
| 209 | - } | |
| 210 | - | |
| 211 | - return array( | |
| 212 | - 'abort' => $abort, | |
| 213 | - 'labels' => $labels, | |
| 214 | - 'types' => $types, | |
| 215 | - ); | |
| 216 | - } | |
| 217 | -} | |
| 141 | +} | |