PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 1.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v1.4
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 1.4, at classes/Visualizer/Source/Csv.php

145 lines 3.8 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 /**
24 * Source manager for local CSV files.
25 *
26 * @category Visualizer
27 * @package Source
28 *
29 * @since 1.0.0
30 */
31 class Visualizer_Source_Csv extends Visualizer_Source {
32
33 /**
34 * The path to the file with data.
35 *
36 * @since 1.0.0
37 *
38 * @access protected
39 * @var string
40 */
41 protected $_filename;
42
43 /**
44 * Constructor.
45 *
46 * @since 1.0.0
47 *
48 * @access public
49 * @param string $filename The path to the file.
50 */
51 public function __construct( $filename = null ) {
52 $this->_filename = trim( $filename );
53 }
54
55 /**
56 * Fetches series information.
57 *
58 * @since 1.0.0
59 *
60 * @access private
61 * @param resource $handle The file handle resource.
62 */
63 private function _fetchSeries( &$handle ) {
64 // read column titles
65 $labels = fgetcsv( $handle );
66
67 // read series types
68 $types = fgetcsv( $handle );
69
70 if ( !$labels || !$types ) {
71 return false;
72 }
73
74 // if no types were setup, re read labels and empty types array
75 if ( !self::_validateTypes( $types ) ) {
76 // re open the file
77 fclose( $handle );
78 $handle = fopen( $this->_filename, 'rb' );
79
80 // re read the labels and empty types array
81 $labels = fgetcsv( $handle );
82 $types = array();
83 }
84
85 for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) {
86 $default_type = $i == 0 ? 'string' : 'number';
87 $this->_series[] = array(
88 'label' => $labels[$i],
89 'type' => isset( $types[$i] ) ? $types[$i] : $default_type,
90 );
91 }
92
93 return true;
94 }
95
96 /**
97 * Fetches information from source, parses it and builds series and data arrays.
98 *
99 * @since 1.0.0
100 *
101 * @access public
102 * @return boolean TRUE on success, otherwise FALSE.
103 */
104 public function fetch() {
105 // if filename is empty return false
106 if ( empty( $this->_filename ) ) {
107 return false;
108 }
109
110 // set line endings auto detect mode
111 @ini_set( 'auto_detect_line_endings', true );
112
113 // read file and fill arrays
114 $handle = fopen( $this->_filename, 'rb' );
115 if ( $handle ) {
116 // fetch series
117 if ( !$this->_fetchSeries( $handle ) ) {
118 return false;
119 }
120
121 // fetch data
122 while ( ( $data = fgetcsv( $handle ) ) !== false ) {
123 $this->_data[] = $this->_normalizeData( $data );
124 }
125
126 // close file handle
127 fclose( $handle );
128 }
129
130 return true;
131 }
132
133 /**
134 * Returns source name.
135 *
136 * @since 1.0.0
137 *
138 * @access public
139 * @return string The name of source.
140 */
141 public function getSourceName() {
142 return __CLASS__;
143 }
144
145 }