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