| @@ -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 |
| @@ -61,30 +62,18 @@ | ||
| 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 | - if ( ! $labels || ! $types ) { | |
| 69 | + if ( !$labels || !$types ) { | |
| 81 | 70 | return false; |
| 82 | 71 | } |
| 83 | 72 | |
| 84 | 73 | // if no types were setup, re read labels and empty types array |
| 85 | 74 | $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 | 78 | $handle = $this->_get_file_handle(); |
| 90 | 79 | |
| @@ -94,14 +83,11 @@ | ||
| 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; |
| @@ -117,9 +103,9 @@ | ||
| 117 | 103 | * @return resource File handle resource on success, otherwise FALSE. |
| 118 | 104 | */ |
| 119 | 105 | protected function _get_file_handle( $filename = false ) { |
| 120 | 106 | // set line endings auto detect mode |
| 121 | - ini_set( 'auto_detect_line_endings', true ); | |
| 107 | + @ini_set( 'auto_detect_line_endings', true ); | |
| 122 | 108 | // open file and return handle |
| 123 | 109 | return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| 124 | 110 | } |
| 125 | 111 | |
| @@ -140,9 +126,9 @@ | ||
| 140 | 126 | // read file and fill arrays |
| 141 | 127 | $handle = $this->_get_file_handle(); |
| 142 | 128 | if ( $handle ) { |
| 143 | 129 | // fetch series |
| 144 | - if ( ! $this->_fetchSeries( $handle ) ) { | |
| 130 | + if ( !$this->_fetchSeries( $handle ) ) { | |
| 145 | 131 | return false; |
| 146 | 132 | } |
| 147 | 133 | |
| 148 | 134 | // fetch data |
| @@ -169,46 +155,5 @@ | ||
| 169 | 155 | public function getSourceName() { |
| 170 | 156 | return __CLASS__; |
| 171 | 157 | } |
| 172 | 158 | |
| 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 | -} | |
| 159 | +} | |