/[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.2 - (hide annotations)
Sat Feb 22 21:08:34 2003 UTC (21 years, 6 months ago) by jonen
Branch: MAIN
Changes since 1.1: +0 -0 lines
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

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     * $Id: CSVFILEDataListSource.inc,v 1.2 2002/11/04 21:22:00 hemna Exp $
8     *
9     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
10     * @package phpHtmlLib
11     */
12    
13    
14    
15     /**
16     *
17     * This DataListSource child class gets the data from a
18     * CSV (comma seperated values) file on disk.
19     *
20     * The CSV MUST have a 'header' line before any data.
21     * The 'header' line is a CSV line that provides a name
22     * for each column of data in the file.
23     *
24     * All lines that start with '#' or '//' are ignored as
25     * comments.
26     *
27     *
28     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
29     * @package phpHtmlLib
30     */
31     class CSVFILEDataListSource extends DataListSource {
32    
33    
34     /**
35     * The csv file name/path on disk
36     *
37     */
38     var $_filename = NULL;
39    
40     /**
41     * The file descriptor pointer
42     *
43     */
44     var $_fp = NULL;
45    
46    
47     /**
48     * this holds the headers read
49     * from the top of the csv file
50     */
51     var $_csv_headers = array();
52    
53     /**
54     * The length of the longest
55     * line in the csv file
56     * (so we read the line properly)
57     */
58     var $_maxlinelength=4096;
59    
60    
61    
62     /**
63     * The constructor.
64     *
65     * @param string - the path to the CSV file on
66     * on disk to use as the data source.
67     *
68     */
69     function CSVFILEDataListSource( $filename, $maxline=4096 ) {
70     if (!file_exists($filename)) {
71     user_error("CSVFILEDataListSource:: (".$filename.") ".
72     "can't be found.");
73     } else {
74     $this->_filename = $filename;
75     $this->_fp = fopen( $filename, 'r');
76     if (!$this->_fp) {
77     user_error("CSVFILEDataListSource:: (".$filename.") ".
78     "cannot be opened");
79     }
80     }
81     $this->_maxlinelength = $maxline;
82     }
83    
84    
85     /**
86     * The prequery. We use this to read the file
87     * into memory so we can do operations on the data
88     * (search, sort, etc.)
89     */
90     function do_prequery() {
91     $this->_get_header();
92     }
93    
94     /**
95     * This function does the query
96     * and search/sort
97     */
98     function do_query() {
99     while ($line = fgets($this->_fp, $this->_maxlinelength)) {
100     if ($this->add_data_row( $this->_construct_row(trim($line)))) {
101     $count++;
102     }
103     }
104    
105     //close the file
106     fclose($this->_fp);
107     $this->set_total_rows( $count );
108     $this->sort();
109     }
110    
111     /**
112     * This function returns the next row of
113     * valid data.
114     *
115     */
116     function get_next_data_row() {
117     $index = $this->get_data_index();
118     $limit = $this->get_limit();
119     $offset = $this->get_offset();
120    
121     if ($limit == -1) {
122     //don't limit the data
123     if ($index > $this->get_total_rows()) {
124     return NULL;
125     } else {
126     return $this->_data[$index];
127     }
128     } else {
129     $left_to_show = $limit - ($index - $offset);
130     if ($left_to_show > 0) {
131     return $this->_data[$index];
132     } else {
133     return NULL;
134     }
135     }
136     }
137    
138    
139     /**
140     * This file trys to get the CSV header.
141     *
142     */
143     function _get_header() {
144     $done = FALSE;
145     while (!$done) {
146     $line = trim(fgets($this->_fp,$this->_maxlinelength));
147     if (strncmp('#', $line, 1) != 0 &&
148     strncmp('//', $line, 2) != 0 && !empty($line)) {
149     $this->_csv_headers = explode(',', $line);
150     $done = TRUE;
151     }
152     }
153     }
154    
155    
156     /**
157     * this is used to build a row
158     * from a csv line
159     *
160     * @param string - the original csv line from the file
161     * @return array
162     */
163     function _construct_row( $line ) {
164     $row = NULL;
165     if (strncmp('#', $line, 1) != 0 &&
166     strncmp('//', $line, 2) != 0 && !empty($line)) {
167     $tmp = explode(",", $line);
168     //xmp_var_dump( $row );
169     $row = array();
170     foreach( $this->_csv_headers as $index => $name ) {
171     $row[$name] = $tmp[$index];
172     }
173     }
174    
175     //xmp_var_dump( $row );
176     return $row;
177     }
178    
179    
180    
181     /**
182     * This function adds a row of data
183     * if necesarry to the data array
184     *
185     */
186     function add_data_row($row) {
187     if ($row != NULL) {
188     if ($this->get_searchby_value()) {
189     //user wants to search the data
190     if ($this->_find_data($row) ) {
191     $this->_data[] = $row;
192     return TRUE;
193     } else {
194     return FALSE;
195     }
196     } else {
197     $this->_data[] = $row;
198     return TRUE;
199     }
200     } else {
201     return FALSE;
202     }
203     }
204    
205     function sort() {
206     $sortby = $this->get_orderby();
207     //ok we need to sort the data by the
208     //column in the table
209     if ($this->_is_column_sortable($sortby)) {
210     //looks like we can sort this column
211     usort($this->_data, array($this, "cmp"));
212     }
213     }
214    
215     function cmp($data1, $data2) {
216     $sortby = $this->get_orderby();
217     $ret = strnatcmp( $data1[$sortby], $data2[$sortby]);
218     if ($this->get_reverseorder() == "true") {
219     $ret *= -1;
220     }
221     return $ret;
222     }
223    
224     function _find_data( $row_data ) {
225     //look for the 'needle' in the 'haystack'
226    
227     $needle = $this->get_searchby_value();
228     $haystack = $row_data[$this->get_searchby()];
229    
230     switch ($this->get_simplesearch_modifier()) {
231     case "BEGINS":
232     if (strncasecmp($needle, $haystack, strlen($needle)) == 0) {
233     return TRUE;
234     } else {
235     return FALSE;
236     }
237     break;
238    
239     case "CONTAINS":
240     if (stristr($haystack, $needle)) {
241     return TRUE;
242     } else {
243     return FALSE;
244     }
245     break;
246     case "EXACT":
247     if (strcasecmp($needle, $haystack) == 0) {
248     return TRUE;
249     } else {
250     return FALSE;
251     }
252     break;
253     case "ENDS":
254     $needle_len = strlen( $needle );
255     $haystack_len = strlen( $haystack );
256     if ($needle_len > $haystack_len) {
257     return FALSE;
258     } else if ($needle_len == $haystack_len) {
259     $tmp = $haystack;
260     } else {
261     $tmp = substr($haystack, $haystack_len - $needle_len);
262     }
263     if (strncasecmp($needle, $tmp, $needle_len) == 0) {
264     return TRUE;
265     } else {
266     return FALSE;
267     }
268     break;
269     }
270     }
271    
272     }
273     ?>

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