1<?php
2/**
3 * pData - Simplifying data population for pChart
4 * @copyright 2008 Jean-Damien POGOLOTTI
5 * @version 2.0
6 * @copyright 2010 Tim Martin
7 *
8 * http://pchart.sourceforge.net
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 1,2,3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24require_once(dirname(__FILE__).'/pData.php');
25
26/**
27 * @brief Imports data from CSV files into a pData object
28 *
29 * All the methods on this class are static, it's debatable whether it
30 * really needs to be a class, or whether it should be a namespace.
31 */
32class CSVImporter {
33	/**
34	 * @brief Import data into the specified dataobject from a CSV
35	 * file
36	 *
37	 * @param $FileName  Name of the file (with path if necessary)
38	 * @param $DataColumns If specified, this should be an array of
39	 * strings that give the names that will be suffixed to "Serie"
40	 * to name the series of data. If this is left to the default,
41	 * numbers will be used instead.
42	 */
43	static public function importFromCSV(pData $data, $FileName, $Delimiter = ",", $DataColumns = -1, $HasHeader = FALSE, $DataName = -1) {
44		$handle = @fopen ( $FileName, "r" );
45
46		if ($handle == null) {
47			throw new Exception("Failed to open file");
48		}
49
50		$HeaderParsed = FALSE;
51		while ( ! feof ( $handle ) ) {
52			$buffer = fgets ( $handle, 4096 );
53
54			if ($buffer != "") {
55				if ($HasHeader && !$HeaderParsed) {
56					self::importHeaderFromCSV($data, $buffer, $Delimiter, $DataColumns);
57					$HeaderParsed = true;
58				}
59				else {
60					self::importChunkFromCSV($data, $buffer, $Delimiter, $DataColumns, $DataName);
61				}
62			}
63		}
64		fclose ( $handle );
65	}
66
67	static private function importHeaderFromCSV(pData $data, $buffer, $Delimiter, $DataColumns) {
68		$buffer = str_replace ( chr ( 10 ), "", $buffer );
69		$buffer = str_replace ( chr ( 13 ), "", $buffer );
70		$Values = explode ( $Delimiter, $buffer );
71
72		if ($DataColumns == - 1) {
73			$ID = 1;
74			foreach ( $Values as $key => $Value ) {
75				$data->SetSeriesName ( $Value, "Serie" . $ID );
76				$ID ++;
77			}
78		} else {
79			foreach ( $DataColumns as $key => $Value )
80				$data->SetSeriesName ( $Values [$Value], "Serie" . $Value );
81		}
82	}
83
84	/**
85	 * @brief Import CSV data from a partial file chunk
86	 */
87	static private function importChunkFromCSV(pData $data, $buffer, $Delimiter, $DataColumns, $DataName) {
88		$buffer = str_replace ( chr ( 10 ), "", $buffer );
89		$buffer = str_replace ( chr ( 13 ), "", $buffer );
90		$Values = explode ( $Delimiter, $buffer );
91
92		if ($DataColumns == - 1) {
93			$ID = 1;
94			foreach ( $Values as $key => $Value ) {
95				$data->AddPoint ( intval ( $Value ), "Serie" . $ID );
96				$ID ++;
97			}
98		} else {
99			$SerieName = "";
100			if ($DataName != - 1)
101				$SerieName = $Values [$DataName];
102
103			foreach ( $DataColumns as $key => $Value )
104				$data->AddPoint ( $Values [$Value], "Serie" . $Value, $SerieName );
105		}
106	}
107}