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