/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/CSVFILEDataListSource.inc
ViewVC logotype

Annotation of /nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/CSVFILEDataListSource.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Thu May 6 16:27:46 2004 UTC (20 years, 4 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +26 -105 lines
 updated all to v2.4.1 - Apr 01, 2004

1 jonen 1.1 <?php
2     /**
3     * This file contains the DataListSource class
4     * that gets the data from a CSV formatted file.
5     *
6     *
7 jonen 1.4 * $Id: CSVFILEDataListSource.inc,v 1.6 2004/03/24 22:44:19 hemna Exp $
8 jonen 1.1 *
9     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
10     * @package phpHtmlLib
11     */
12    
13 jonen 1.4 require_once($phphtmllib."/widgets/data_list/ArrayDataListSource.inc");
14    
15 jonen 1.1
16    
17     /**
18     *
19     * This DataListSource child class gets the data from a
20     * CSV (comma seperated values) file on disk.
21     *
22     * The CSV MUST have a 'header' line before any data.
23     * The 'header' line is a CSV line that provides a name
24     * for each column of data in the file.
25     *
26     * All lines that start with '#' or '//' are ignored as
27     * comments.
28     *
29     *
30     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
31     * @package phpHtmlLib
32     */
33 jonen 1.4 class CSVFILEDataListSource extends ArrayDataListSource {
34 jonen 1.1
35    
36     /**
37     * The csv file name/path on disk
38     *
39     */
40     var $_filename = NULL;
41    
42     /**
43     * The file descriptor pointer
44     *
45     */
46     var $_fp = NULL;
47    
48    
49     /**
50     * this holds the headers read
51     * from the top of the csv file
52     */
53     var $_csv_headers = array();
54    
55     /**
56     * The length of the longest
57     * line in the csv file
58     * (so we read the line properly)
59     */
60     var $_maxlinelength=4096;
61    
62    
63    
64     /**
65     * The constructor.
66     *
67     * @param string - the path to the CSV file on
68     * on disk to use as the data source.
69     *
70     */
71     function CSVFILEDataListSource( $filename, $maxline=4096 ) {
72     if (!file_exists($filename)) {
73     user_error("CSVFILEDataListSource:: (".$filename.") ".
74     "can't be found.");
75     } else {
76     $this->_filename = $filename;
77     $this->_fp = fopen( $filename, 'r');
78     if (!$this->_fp) {
79     user_error("CSVFILEDataListSource:: (".$filename.") ".
80     "cannot be opened");
81     }
82     }
83     $this->_maxlinelength = $maxline;
84     }
85    
86    
87     /**
88     * The prequery. We use this to read the file
89     * into memory so we can do operations on the data
90     * (search, sort, etc.)
91     */
92     function do_prequery() {
93     $this->_get_header();
94     }
95    
96     /**
97     * This function does the query
98     * and search/sort
99     */
100     function do_query() {
101    
102 jonen 1.4 //we always want to filter.
103     //because we read the entry,
104     //then filter it.
105     $this->_prequery_filter();
106    
107     $count = count($this->_data);
108    
109 jonen 1.3 if ($count > 0) {
110     $this->set_total_rows( $count );
111     $this->sort();
112     return true;
113     } else {
114     return false;
115     }
116 jonen 1.1 }
117    
118 jonen 1.4 /**
119     * Lets walk the file and read the entry, and
120     * filter what we don't want.
121     *
122     */
123     function _prequery_filter() {
124     $count=0;
125     while ($line = fgets($this->_fp, $this->_maxlinelength)) {
126     if ($this->add_data_row( $this->_construct_row(trim($line)))) {
127     $count++;
128 jonen 1.1 }
129     }
130 jonen 1.4
131     //close the file
132     fclose($this->_fp);
133     }
134 jonen 1.1
135    
136     /**
137     * This file trys to get the CSV header.
138     *
139     */
140     function _get_header() {
141     $done = FALSE;
142     while (!$done) {
143     $line = trim(fgets($this->_fp,$this->_maxlinelength));
144     if (strncmp('#', $line, 1) != 0 &&
145     strncmp('//', $line, 2) != 0 && !empty($line)) {
146     $this->_csv_headers = explode(',', $line);
147     $done = TRUE;
148     }
149     }
150     }
151    
152    
153     /**
154     * this is used to build a row
155     * from a csv line
156     *
157     * @param string - the original csv line from the file
158     * @return array
159     */
160     function _construct_row( $line ) {
161     $row = NULL;
162     if (strncmp('#', $line, 1) != 0 &&
163     strncmp('//', $line, 2) != 0 && !empty($line)) {
164     $tmp = explode(",", $line);
165 jonen 1.4
166 jonen 1.1 $row = array();
167     foreach( $this->_csv_headers as $index => $name ) {
168     $row[$name] = $tmp[$index];
169     }
170     }
171     return $row;
172     }
173    
174    
175    
176     /**
177     * This function adds a row of data
178     * if necesarry to the data array
179     *
180     */
181     function add_data_row($row) {
182     if ($row != NULL) {
183     if ($this->get_searchby_value()) {
184     //user wants to search the data
185     if ($this->_find_data($row) ) {
186     $this->_data[] = $row;
187     return TRUE;
188     } else {
189     return FALSE;
190     }
191     } else {
192     $this->_data[] = $row;
193     return TRUE;
194     }
195     } else {
196     return FALSE;
197     }
198     }
199     }
200     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed