1 |
<?php |
2 |
/** |
3 |
*-------------------------------------------------------- |
4 |
* This file contains the DataListSource class |
5 |
* that gets the data as an hash. |
6 |
* |
7 |
* |
8 |
* |
9 |
* $Id: MemoryDataListSource.inc,v 1.1 2003/02/28 17:17:59 jonen Exp $ |
10 |
*-------------------------------------------------------- |
11 |
* $Log: MemoryDataListSource.inc,v $ |
12 |
* Revision 1.1 2003/02/28 17:17:59 jonen |
13 |
* + renamed from flibRPCDataListSource.inc |
14 |
* |
15 |
* |
16 |
* |
17 |
*-------------------------------------------------------- |
18 |
*/ |
19 |
|
20 |
|
21 |
|
22 |
/** |
23 |
* |
24 |
* This DataListSource child class gets the date from an hash, |
25 |
* eg an hashed remote object |
26 |
* |
27 |
* |
28 |
*/ |
29 |
class MemoryDataListSource extends DataListSource { |
30 |
|
31 |
|
32 |
/** |
33 |
* given data hash |
34 |
* |
35 |
*/ |
36 |
var $_data_list = array(); |
37 |
|
38 |
|
39 |
|
40 |
/** |
41 |
* this holds the headers read |
42 |
* array keys |
43 |
*/ |
44 |
var $_data_keys = array(); |
45 |
|
46 |
|
47 |
/** |
48 |
* The constructor. |
49 |
* |
50 |
* @param hash - a hashed data object |
51 |
* |
52 |
*/ |
53 |
function MemoryDataListSource( $data_list ) { |
54 |
$this->_data_list = $data_list; |
55 |
//print_r($object_list); |
56 |
} |
57 |
|
58 |
|
59 |
/** |
60 |
* The prequery. We use this to read the file |
61 |
* into memory so we can do operations on the data |
62 |
* (search, sort, etc.) |
63 |
*/ |
64 |
function do_prequery() { |
65 |
$this->_get_header(); |
66 |
} |
67 |
|
68 |
/** |
69 |
* This function does the query |
70 |
* and search/sort |
71 |
*/ |
72 |
function do_query() { |
73 |
foreach ($this->_data_list as $value) { |
74 |
if ($this->add_data_row( $value)) { |
75 |
$count++; |
76 |
} |
77 |
} |
78 |
|
79 |
$this->set_total_rows( $count ); |
80 |
$this->sort(); |
81 |
} |
82 |
|
83 |
/** |
84 |
* This function returns the next row of |
85 |
* valid data. |
86 |
* |
87 |
*/ |
88 |
function get_next_data_row() { |
89 |
$index = $this->get_data_index(); |
90 |
$limit = $this->get_limit(); |
91 |
$offset = $this->get_offset(); |
92 |
|
93 |
if ($limit == -1) { |
94 |
//don't limit the data |
95 |
if ($index > $this->get_total_rows()) { |
96 |
return NULL; |
97 |
} else { |
98 |
return $this->_data[$index]; |
99 |
} |
100 |
} else { |
101 |
$left_to_show = $limit - ($index - $offset); |
102 |
if ($left_to_show > 0) { |
103 |
return $this->_data[$index]; |
104 |
} else { |
105 |
return NULL; |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
/** |
114 |
* This file trys to get the Object Hash Keys. |
115 |
* |
116 |
*/ |
117 |
function _get_header() { |
118 |
foreach($this->_data_list[0] as $key => $value) { |
119 |
array_push($this->_data_keys, $key); |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
function get_header() { |
125 |
$this->_get_header(); |
126 |
return $this->_data_keys; |
127 |
} |
128 |
|
129 |
|
130 |
function get_data_source() { |
131 |
|
132 |
} |
133 |
|
134 |
|
135 |
/** |
136 |
* This function adds a row of data |
137 |
* if necesarry to the data array |
138 |
* |
139 |
*/ |
140 |
function add_data_row($row) { |
141 |
if ($row != NULL) { |
142 |
if ($this->get_searchby_value()) { |
143 |
//user wants to search the data |
144 |
if ($this->_find_data($row) ) { |
145 |
$this->_data[] = $row; |
146 |
return TRUE; |
147 |
} else { |
148 |
return FALSE; |
149 |
} |
150 |
} else { |
151 |
$this->_data[] = $row; |
152 |
return TRUE; |
153 |
} |
154 |
} else { |
155 |
return FALSE; |
156 |
} |
157 |
} |
158 |
|
159 |
function sort() { |
160 |
$sortby = $this->get_orderby(); |
161 |
//ok we need to sort the data by the |
162 |
//column in the table |
163 |
if ($this->_is_column_sortable($sortby)) { |
164 |
//looks like we can sort this column |
165 |
usort($this->_data, array($this, "cmp")); |
166 |
} |
167 |
} |
168 |
|
169 |
function cmp($data1, $data2) { |
170 |
$sortby = $this->get_orderby(); |
171 |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
172 |
if ($this->get_reverseorder() == "true") { |
173 |
$ret *= -1; |
174 |
} |
175 |
return $ret; |
176 |
} |
177 |
|
178 |
function _find_data( $row_data ) { |
179 |
//look for the 'needle' in the 'haystack' |
180 |
|
181 |
$needle = $this->get_searchby_value(); |
182 |
$haystack = $row_data[$this->get_searchby()]; |
183 |
|
184 |
switch ($this->get_simplesearch_modifier()) { |
185 |
case "BEGINS": |
186 |
if (strncasecmp($needle, $haystack, strlen($needle)) == 0) { |
187 |
return TRUE; |
188 |
} else { |
189 |
return FALSE; |
190 |
} |
191 |
break; |
192 |
|
193 |
case "CONTAINS": |
194 |
if (stristr($haystack, $needle)) { |
195 |
return TRUE; |
196 |
} else { |
197 |
return FALSE; |
198 |
} |
199 |
break; |
200 |
case "EXACT": |
201 |
if (strcasecmp($needle, $haystack) == 0) { |
202 |
return TRUE; |
203 |
} else { |
204 |
return FALSE; |
205 |
} |
206 |
break; |
207 |
case "ENDS": |
208 |
$needle_len = strlen( $needle ); |
209 |
$haystack_len = strlen( $haystack ); |
210 |
if ($needle_len > $haystack_len) { |
211 |
return FALSE; |
212 |
} else if ($needle_len == $haystack_len) { |
213 |
$tmp = $haystack; |
214 |
} else { |
215 |
$tmp = substr($haystack, $haystack_len - $needle_len); |
216 |
} |
217 |
if (strncasecmp($needle, $tmp, $needle_len) == 0) { |
218 |
return TRUE; |
219 |
} else { |
220 |
return FALSE; |
221 |
} |
222 |
break; |
223 |
} |
224 |
} |
225 |
|
226 |
} |
227 |
?> |