1 |
joko |
1.1 |
<?php |
2 |
|
|
/** |
3 |
|
|
* This file contains the MemoryDataSource child class |
4 |
|
|
* that inherits from the DataListSource. |
5 |
|
|
* |
6 |
|
|
* @author Sebastian Utz <seut@tunemedia.de> |
7 |
|
|
* @author Andreas Motl <andreas.motl@ilo.de> |
8 |
|
|
* @package phpHtmlLib |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* $Id$ |
13 |
|
|
* |
14 |
|
|
* $Log$ |
15 |
|
|
* |
16 |
|
|
* Revision 1.1 2003/02/28 17:17:59 jonen |
17 |
|
|
* + renamed from flibRPCDataListSource.inc |
18 |
|
|
* |
19 |
|
|
* Revision 1.0 2003/02/xx 00:00:00 jonen |
20 |
|
|
* + copied from CSVFILEDataListSource.inc |
21 |
|
|
* |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
/** |
25 |
|
|
* MemoryDataSource provides a basic framework for handling |
26 |
|
|
* arbitrary data, that is: items, lists and trees. |
27 |
|
|
* It assumes the "list" being *not* the most abstract, but the most |
28 |
|
|
* multipurpose thing to be able to represent/handle all the others. |
29 |
|
|
* (regarding convenience and code-reuse: inherits from DataListSource) |
30 |
|
|
* |
31 |
|
|
* This could be pictured by looking at a list being either |
32 |
|
|
* a part (tree) or a container (item) of another thing. |
33 |
|
|
* e.g.: |
34 |
|
|
* item = part of list (a item is a part of a list) |
35 |
|
|
* list = list (a list is a list) |
36 |
|
|
* tree = nested lists (a part of a tree is a list) |
37 |
|
|
* |
38 |
|
|
* In fact, MemoryDataSource just doesn't care a bit |
39 |
|
|
* about the type of the actual data, so perfectly fits |
40 |
|
|
* as an abstract base for further hackings. |
41 |
|
|
* e.g.: look at ProxyDataSource.inc ..... |
42 |
|
|
* |
43 |
|
|
*/ |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* Todo: |
47 |
|
|
* |
48 |
|
|
* o introduce (g|s)etlabels to set either |
49 |
|
|
* a) columns (for list columns) or |
50 |
|
|
* b) captions (for object attributes) |
51 |
|
|
* |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
class MemoryDataSource extends DataListSource { |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* given data hash |
60 |
|
|
* |
61 |
|
|
*/ |
62 |
|
|
var $_memory = array(); |
63 |
|
|
|
64 |
|
|
var $_result = array(); |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* this holds the headers read |
69 |
|
|
* array keys |
70 |
|
|
*/ |
71 |
|
|
var $_data_keys = array(); |
72 |
|
|
|
73 |
|
|
var $_errors = array(); |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/** |
78 |
|
|
* The constructor. |
79 |
|
|
* |
80 |
|
|
* @param hash - a hashed data object |
81 |
|
|
* |
82 |
|
|
*/ |
83 |
|
|
function MemoryDataSource( &$payload ) { |
84 |
|
|
$this->_memory = &$payload; |
85 |
|
|
//print_r($object_list); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
|
89 |
|
|
/** |
90 |
|
|
* The prequery. We use this to read the file |
91 |
|
|
* into memory so we can do operations on the data |
92 |
|
|
* (search, sort, etc.) |
93 |
|
|
*/ |
94 |
|
|
function do_prequery() { |
95 |
|
|
$this->_get_header(); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/** |
99 |
|
|
* This function does the query |
100 |
|
|
* and search/sort |
101 |
|
|
*/ |
102 |
|
|
function do_query() { |
103 |
|
|
// short-circuit (default behaviour, maybe intercepted by inheriting and overwriting 'do_query') |
104 |
|
|
$this->_result = $this->_memory; |
105 |
|
|
$this->handle_result(); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
function handle_result() { |
109 |
|
|
foreach ($this->_result as $value) { |
110 |
|
|
if ($this->trace) { print Dumper($value); } |
111 |
|
|
if ($this->add_data_row( $value)) { |
112 |
|
|
$count++; |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
$this->set_total_rows( $count ); |
117 |
|
|
$this->sort(); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* This function returns the next row of |
122 |
|
|
* valid data. |
123 |
|
|
* |
124 |
|
|
*/ |
125 |
|
|
function get_next_data_row() { |
126 |
|
|
$index = $this->get_data_index(); |
127 |
|
|
$limit = $this->get_limit(); |
128 |
|
|
$offset = $this->get_offset(); |
129 |
|
|
|
130 |
|
|
if ($limit == -1) { |
131 |
|
|
//don't limit the data |
132 |
|
|
if ($index > $this->get_total_rows()) { |
133 |
|
|
return NULL; |
134 |
|
|
} else { |
135 |
|
|
return $this->_data[$index]; |
136 |
|
|
} |
137 |
|
|
} else { |
138 |
|
|
$left_to_show = $limit - ($index - $offset); |
139 |
|
|
if ($left_to_show > 0) { |
140 |
|
|
return $this->_data[$index]; |
141 |
|
|
} else { |
142 |
|
|
return NULL; |
143 |
|
|
} |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
/** |
151 |
|
|
* This file trys to get the Object Hash Keys. |
152 |
|
|
* |
153 |
|
|
*/ |
154 |
|
|
function _get_header() { |
155 |
|
|
/* |
156 |
|
|
// FIXME: patch to make php not croak if result is empty at this stage |
157 |
|
|
if (!is_array($this->_memory[0])) { |
158 |
|
|
//$this->add_data_row( array( 'empty' => '[empty]') ); |
159 |
|
|
//$this->_memory[0] = array( '' => 'empty' ); |
160 |
|
|
$this->_errors[] = array("warning", "header metadata was empty"); |
161 |
|
|
print "no header!<br/>"; |
162 |
|
|
return; |
163 |
|
|
} |
164 |
|
|
*/ |
165 |
|
|
foreach($this->_memory[0] as $key => $value) { |
166 |
|
|
//print "key: $key<br>"; |
167 |
|
|
array_push($this->_data_keys, $key); |
168 |
|
|
} |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
|
172 |
|
|
function get_header() { |
173 |
|
|
$this->_get_header(); |
174 |
|
|
return $this->_data_keys; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
function set_schema($columns) { |
178 |
|
|
//print "columns: " . Dumper($columns); |
179 |
|
|
$this->_memory[0] = $columns; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
function get_data_source() { |
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 |
|
|
?> |