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.3 2003/04/29 06:38:05 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 |
$count = 0; |
100 |
while ($line = fgets($this->_fp, $this->_maxlinelength)) { |
101 |
if ($this->add_data_row( $this->_construct_row(trim($line)))) { |
102 |
$count++; |
103 |
} |
104 |
} |
105 |
|
106 |
//close the file |
107 |
fclose($this->_fp); |
108 |
if ($count > 0) { |
109 |
$this->set_total_rows( $count ); |
110 |
$this->sort(); |
111 |
return true; |
112 |
} else { |
113 |
return false; |
114 |
} |
115 |
} |
116 |
|
117 |
/** |
118 |
* This function returns the next row of |
119 |
* valid data. |
120 |
* |
121 |
*/ |
122 |
function get_next_data_row() { |
123 |
$index = $this->get_data_index(); |
124 |
$limit = $this->get_limit(); |
125 |
$offset = $this->get_offset(); |
126 |
|
127 |
if ($limit == -1) { |
128 |
//don't limit the data |
129 |
if ($index > $this->get_total_rows()) { |
130 |
return NULL; |
131 |
} else { |
132 |
return $this->_data[$index]; |
133 |
} |
134 |
} else { |
135 |
$left_to_show = $limit - ($index - $offset); |
136 |
if ($left_to_show > 0) { |
137 |
return $this->_data[$index]; |
138 |
} else { |
139 |
return NULL; |
140 |
} |
141 |
} |
142 |
} |
143 |
|
144 |
|
145 |
/** |
146 |
* This file trys to get the CSV header. |
147 |
* |
148 |
*/ |
149 |
function _get_header() { |
150 |
$done = FALSE; |
151 |
while (!$done) { |
152 |
$line = trim(fgets($this->_fp,$this->_maxlinelength)); |
153 |
if (strncmp('#', $line, 1) != 0 && |
154 |
strncmp('//', $line, 2) != 0 && !empty($line)) { |
155 |
$this->_csv_headers = explode(',', $line); |
156 |
$done = TRUE; |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
|
162 |
/** |
163 |
* this is used to build a row |
164 |
* from a csv line |
165 |
* |
166 |
* @param string - the original csv line from the file |
167 |
* @return array |
168 |
*/ |
169 |
function _construct_row( $line ) { |
170 |
$row = NULL; |
171 |
if (strncmp('#', $line, 1) != 0 && |
172 |
strncmp('//', $line, 2) != 0 && !empty($line)) { |
173 |
$tmp = explode(",", $line); |
174 |
//xmp_var_dump( $row ); |
175 |
$row = array(); |
176 |
foreach( $this->_csv_headers as $index => $name ) { |
177 |
$row[$name] = $tmp[$index]; |
178 |
} |
179 |
} |
180 |
|
181 |
//xmp_var_dump( $row ); |
182 |
return $row; |
183 |
} |
184 |
|
185 |
|
186 |
|
187 |
/** |
188 |
* This function adds a row of data |
189 |
* if necesarry to the data array |
190 |
* |
191 |
*/ |
192 |
function add_data_row($row) { |
193 |
if ($row != NULL) { |
194 |
if ($this->get_searchby_value()) { |
195 |
//user wants to search the data |
196 |
if ($this->_find_data($row) ) { |
197 |
$this->_data[] = $row; |
198 |
return TRUE; |
199 |
} else { |
200 |
return FALSE; |
201 |
} |
202 |
} else { |
203 |
$this->_data[] = $row; |
204 |
return TRUE; |
205 |
} |
206 |
} else { |
207 |
return FALSE; |
208 |
} |
209 |
} |
210 |
|
211 |
function sort() { |
212 |
$sortby = $this->get_orderby(); |
213 |
//ok we need to sort the data by the |
214 |
//column in the table |
215 |
if ($this->_is_column_sortable($sortby)) { |
216 |
//looks like we can sort this column |
217 |
usort($this->_data, array($this, "cmp")); |
218 |
} |
219 |
} |
220 |
|
221 |
function cmp($data1, $data2) { |
222 |
$sortby = $this->get_orderby(); |
223 |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
224 |
if ($this->get_reverseorder() == "true") { |
225 |
$ret *= -1; |
226 |
} |
227 |
return $ret; |
228 |
} |
229 |
|
230 |
function _find_data( $row_data ) { |
231 |
//look for the 'needle' in the 'haystack' |
232 |
|
233 |
$needle = $this->get_searchby_value(); |
234 |
$haystack = $row_data[$this->get_searchby()]; |
235 |
|
236 |
switch ($this->get_simplesearch_modifier()) { |
237 |
case "BEGINS": |
238 |
if (strncasecmp($needle, $haystack, strlen($needle)) == 0) { |
239 |
return TRUE; |
240 |
} else { |
241 |
return FALSE; |
242 |
} |
243 |
break; |
244 |
|
245 |
case "CONTAINS": |
246 |
if (stristr($haystack, $needle)) { |
247 |
return TRUE; |
248 |
} else { |
249 |
return FALSE; |
250 |
} |
251 |
break; |
252 |
case "EXACT": |
253 |
if (strcasecmp($needle, $haystack) == 0) { |
254 |
return TRUE; |
255 |
} else { |
256 |
return FALSE; |
257 |
} |
258 |
break; |
259 |
case "ENDS": |
260 |
$needle_len = strlen( $needle ); |
261 |
$haystack_len = strlen( $haystack ); |
262 |
if ($needle_len > $haystack_len) { |
263 |
return FALSE; |
264 |
} else if ($needle_len == $haystack_len) { |
265 |
$tmp = $haystack; |
266 |
} else { |
267 |
$tmp = substr($haystack, $haystack_len - $needle_len); |
268 |
} |
269 |
if (strncasecmp($needle, $tmp, $needle_len) == 0) { |
270 |
return TRUE; |
271 |
} else { |
272 |
return FALSE; |
273 |
} |
274 |
break; |
275 |
} |
276 |
} |
277 |
|
278 |
} |
279 |
?> |