| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
/** |
| 23 |
* Source manager for local CSV files. |
| 24 |
* |
| 25 |
* @category Visualizer |
| 26 |
* @package Source |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class Visualizer_Source_Csv extends Visualizer_Source { |
| 31 |
|
| 32 |
/** |
| 33 |
* The path to the file with data. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @access protected |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
protected $_filename; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* |
| 45 |
* @since 1.0.0 |
| 46 |
* |
| 47 |
* @access public |
| 48 |
* @param string $filename The path to the file. |
| 49 |
*/ |
| 50 |
public function __construct( $filename = null ) { |
| 51 |
$this->_filename = trim( $filename ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Fetches series information. |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @access private |
| 60 |
* @param resource $handle The file handle resource. |
| 61 |
*/ |
| 62 |
private function _fetchSeries( &$handle ) { |
| 63 |
// read column titles |
| 64 |
$labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); |
| 65 |
$types = null; |
| 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 |
} |
| 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' ); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 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 |
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 |
$this->_series[] = array( |
| 100 |
'label' => $labels[ $i ], |
| 101 |
'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type, |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 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 |
// set line endings auto detect mode |
| 119 |
ini_set( 'auto_detect_line_endings', true ); |
| 120 |
// open file and return handle |
| 121 |
return fopen( $filename ? $filename : $this->_filename, 'rb' ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Fetches information from source, parses it and builds series and data arrays. |
| 126 |
* |
| 127 |
* @since 1.0.0 |
| 128 |
* |
| 129 |
* @access public |
| 130 |
* @return boolean TRUE on success, otherwise FALSE. |
| 131 |
*/ |
| 132 |
public function fetch() { |
| 133 |
// if filename is empty return false |
| 134 |
if ( empty( $this->_filename ) ) { |
| 135 |
$this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
// read file and fill arrays |
| 140 |
$handle = $this->_get_file_handle(); |
| 141 |
if ( $handle ) { |
| 142 |
// fetch series |
| 143 |
if ( ! $this->_fetchSeries( $handle ) ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
// fetch data |
| 148 |
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
| 149 |
while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { |
| 150 |
$this->_data[] = $this->_normalizeData( $data ); |
| 151 |
} |
| 152 |
|
| 153 |
// close file handle |
| 154 |
fclose( $handle ); |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns source name. |
| 162 |
* |
| 163 |
* @since 1.0.0 |
| 164 |
* |
| 165 |
* @access public |
| 166 |
* @return string The name of source. |
| 167 |
*/ |
| 168 |
public function getSourceName() { |
| 169 |
return __CLASS__; |
| 170 |
} |
| 171 |
|
| 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 |
} |
| 214 |
|