PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.0.9
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.0.9
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
visualizer / classes / Visualizer / Source / Csv.php

Csv.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.0.9, at classes/Visualizer/Source/Csv.php

162 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // read series types
66 $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
67
68 if ( ! $labels || ! $types ) {
69 return false;
70 }
71
72 // if no types were setup, re read labels and empty types array
73 $types = array_map( 'trim', $types );
74 if ( ! self::_validateTypes( $types ) ) {
75 // re open the file
76 fclose( $handle );
77 $handle = $this->_get_file_handle();
78
79 // re read the labels and empty types array
80 $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE );
81 $types = array();
82 }
83
84 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
85 $default_type = $i == 0 ? 'string' : 'number';
86
87 $labels[ $i ] = $this->toUTF8( $labels[ $i ] );
88
89 $this->_series[] = array(
90 'label' => $labels[ $i ],
91 'type' => isset( $types[ $i ] ) ? $types[ $i ] : $default_type,
92 );
93 }
94
95 return true;
96 }
97
98 /**
99 * Returns file handle to fetch data from.
100 *
101 * @since 1.4.2
102 *
103 * @access protected
104 * @param string $filename Optional file name to get handle. If omitted, $_filename is used.
105 * @return resource File handle resource on success, otherwise FALSE.
106 */
107 protected function _get_file_handle( $filename = false ) {
108 // set line endings auto detect mode
109 ini_set( 'auto_detect_line_endings', true );
110 // open file and return handle
111 return fopen( $filename ? $filename : $this->_filename, 'rb' );
112 }
113
114 /**
115 * Fetches information from source, parses it and builds series and data arrays.
116 *
117 * @since 1.0.0
118 *
119 * @access public
120 * @return boolean TRUE on success, otherwise FALSE.
121 */
122 public function fetch() {
123 // if filename is empty return false
124 if ( empty( $this->_filename ) ) {
125 return false;
126 }
127
128 // read file and fill arrays
129 $handle = $this->_get_file_handle();
130 if ( $handle ) {
131 // fetch series
132 if ( ! $this->_fetchSeries( $handle ) ) {
133 return false;
134 }
135
136 // fetch data
137 while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) {
138
139 $this->_data[] = $this->_normalizeData( $data );
140 }
141
142 // close file handle
143 fclose( $handle );
144 }
145
146 return true;
147 }
148
149 /**
150 * Returns source name.
151 *
152 * @since 1.0.0
153 *
154 * @access public
155 * @return string The name of source.
156 */
157 public function getSourceName() {
158 return __CLASS__;
159 }
160
161 }
162