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 |
* @copyright (c) 2003 - All Rights reserved. |
9 |
* @license GNU LGPL (GNU Lesser General Public License) |
10 |
* |
11 |
* @author-url http://www.netfrag.org/~jonen/ |
12 |
* @author-url http://www.netfrag.org/~joko/ |
13 |
* @license-url http://www.gnu.org/licenses/lgpl.txt |
14 |
* |
15 |
* @package phpHtmlLib |
16 |
* @module widgets::data_source::MemoryDataSource |
17 |
* |
18 |
*/ |
19 |
|
20 |
/** |
21 |
* $Id: MemoryDataListSource.inc,v 1.3 2003/03/05 10:57:10 joko Exp $ |
22 |
* |
23 |
* $Log: MemoryDataListSource.inc,v $ |
24 |
* Revision 1.3 2003/03/05 10:57:10 joko |
25 |
* established here (again?) |
26 |
* |
27 |
* Revision 1.3 2003/03/03 21:24:45 joko |
28 |
* updated todo |
29 |
* |
30 |
* Revision 1.2 2003/03/01 15:34:26 joko |
31 |
* + introduced new 'label'-mechanism, purged old 'header'-mechanism |
32 |
* + reworked internal data-structure and introduced new methods 'propagate_xyz' |
33 |
* + added little debugging helper methods |
34 |
* |
35 |
* Revision 1.1 2003/03/01 03:09:22 joko |
36 |
* + moved here from data_list/MemoryDataListSource.inc |
37 |
* |
38 |
* |
39 |
* Revision 1.1 2003/02/28 17:17:59 jonen |
40 |
* + renamed from flibRPCDataListSource.inc |
41 |
* |
42 |
* Revision 1.0 2003/02/xx 00:00:00 jonen |
43 |
* + copied from CSVFILEDataListSource.inc |
44 |
* |
45 |
*/ |
46 |
|
47 |
/** |
48 |
* MemoryDataSource provides a basic framework for handling |
49 |
* arbitrary data, that is: items, lists and trees. |
50 |
* It assumes the "list" being *not* the most abstract, but the most |
51 |
* multipurpose thing to be able to represent/handle all the others. |
52 |
* (regarding convenience and code-reuse: inherits from DataListSource) |
53 |
* |
54 |
* This could be pictured by looking at a list being either |
55 |
* a part (tree) or a container (item) of another thing. |
56 |
* e.g.: |
57 |
* item = part of list (a item is a part of a list) |
58 |
* list = list (a list is a list) |
59 |
* tree = nested lists (a part of a tree is a list) |
60 |
* |
61 |
* In fact, MemoryDataSource just doesn't care a bit |
62 |
* about the type of the actual data, so perfectly fits |
63 |
* as an abstract base for further hackings. |
64 |
* e.g.: look at ProxyDataSource.inc ..... |
65 |
* |
66 |
*/ |
67 |
|
68 |
/** |
69 |
* Todo: |
70 |
* |
71 |
* o introduce (g|s)etlabels to set either |
72 |
* a) columns (for list columns) or |
73 |
* b) captions (for object attributes) |
74 |
* o maybe use shared memory as an alternative handler |
75 |
* look at the php Shared Memory extension at (e.g.): |
76 |
* http://www.zend.com/manual/ref.shmop.php |
77 |
* |
78 |
*/ |
79 |
|
80 |
|
81 |
|
82 |
// !!!!! THIS HAS TO BE BROKEN AS WELL !!!!! |
83 |
// !!!! compare DataSource::GenericDataSource !!!! |
84 |
// !!!! has to be independent of DataListSource !!!! |
85 |
|
86 |
|
87 |
|
88 |
//class MemoryDataSource extends DataListSource { |
89 |
class MemoryDataListSource extends DataListSource { |
90 |
|
91 |
// it's free now! |
92 |
//class DataSource_Proxy_Memory { |
93 |
|
94 |
|
95 |
/** |
96 |
* These hold data |
97 |
* |
98 |
*/ |
99 |
var $_memory = array(); |
100 |
var $_result = array(); |
101 |
|
102 |
/** |
103 |
* this holds the headers read |
104 |
* array keys |
105 |
*/ |
106 |
var $_data_keys = array(); |
107 |
|
108 |
/** |
109 |
* This holds metadata |
110 |
* |
111 |
*/ |
112 |
var $_schema = array(); |
113 |
|
114 |
/** |
115 |
* This holds some information about the tracing level. |
116 |
* |
117 |
*/ |
118 |
var $_debug = array( |
119 |
notice => 0, |
120 |
trace => 0, |
121 |
payload => 0, |
122 |
); |
123 |
|
124 |
/** |
125 |
* This accumulates errors occouring while processing. |
126 |
* $error = array($level, $message); |
127 |
* |
128 |
*/ |
129 |
var $_errors = array(); |
130 |
|
131 |
|
132 |
|
133 |
/** |
134 |
* The constructor. |
135 |
* |
136 |
* @param hash - a hashed data object |
137 |
* |
138 |
*/ |
139 |
function MemoryDataListSource( &$payload ) { |
140 |
|
141 |
// tracing |
142 |
$this->trace_payload('MemoryDataSource', $payload); |
143 |
|
144 |
// transfer data to "memory" |
145 |
$this->_memory = &$payload; |
146 |
|
147 |
} |
148 |
|
149 |
function read_labels_from_result() { |
150 |
$this->set_labels($this->_result[0]); |
151 |
$this->propagate_schema(); |
152 |
} |
153 |
|
154 |
function debug($method, $message) { |
155 |
if ($this->_debug[notice]) { |
156 |
if ($method) { $prefix = get_class($this) . "->" . $method . ": "; } |
157 |
print $prefix . $message . "<br/>" . "\n"; |
158 |
} |
159 |
} |
160 |
|
161 |
function trace($method, $var) { |
162 |
if ($this->_debug[trace]) { |
163 |
print get_class($this) . "->" . $method . ": " . Dumper($var) . "<br/>" . "\n"; |
164 |
} |
165 |
} |
166 |
|
167 |
function trace_payload($method, $data) { |
168 |
if ($this->_debug[payload]) { |
169 |
$this->trace($method, $data); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* The prequery. We use this to read the file |
175 |
* into memory so we can do operations on the data |
176 |
* (search, sort, etc.) |
177 |
*/ |
178 |
function O_do_prequery() { |
179 |
$this->_read_schema(); |
180 |
} |
181 |
function O_do_prequery() { |
182 |
} |
183 |
|
184 |
/** |
185 |
* This function does the query |
186 |
* and search/sort |
187 |
*/ |
188 |
function do_query() { |
189 |
// short-circuit (default behaviour, maybe intercepted by inheriting and overwriting 'do_query') |
190 |
$this->_result = $this->_memory; |
191 |
$this->handle_result(); |
192 |
} |
193 |
|
194 |
function handle_result() { |
195 |
$this->debug("handle_result", "HANDLE RESULT!<br>"); |
196 |
$this->propagate_result(); |
197 |
} |
198 |
|
199 |
function propagate_result() { |
200 |
$this->debug('propagate_result', "PROPAGATE RESULT!"); |
201 |
foreach ($this->_result as $value) { |
202 |
$this->trace_payload("propagate_result", $value); |
203 |
if ($this->add_data_row( $value)) { |
204 |
$count++; |
205 |
} |
206 |
} |
207 |
$this->set_total_rows( $count ); |
208 |
$this->sort(); |
209 |
} |
210 |
|
211 |
/** |
212 |
* This function returns the next row of |
213 |
* valid data. |
214 |
* |
215 |
*/ |
216 |
function get_next_data_row() { |
217 |
|
218 |
//print "get_row!<br/>"; |
219 |
|
220 |
//print Dumper($this->_proxy->_result); |
221 |
|
222 |
$index = $this->get_data_index(); |
223 |
$limit = $this->get_limit(); |
224 |
$offset = $this->get_offset(); |
225 |
|
226 |
if ($limit == -1) { |
227 |
//don't limit the data |
228 |
if ($index > $this->get_total_rows()) { |
229 |
return NULL; |
230 |
} else { |
231 |
return $this->_data[$index]; |
232 |
} |
233 |
} else { |
234 |
$left_to_show = $limit - ($index - $offset); |
235 |
if ($left_to_show > 0) { |
236 |
return $this->_data[$index]; |
237 |
} else { |
238 |
return NULL; |
239 |
} |
240 |
} |
241 |
} |
242 |
|
243 |
|
244 |
|
245 |
|
246 |
/** |
247 |
* This file trys to get the Object Hash Keys. |
248 |
* |
249 |
*/ |
250 |
function propagate_schema() { |
251 |
$this->debug("propagate_schema", "PROPAGATE SCHEMA!<br>"); |
252 |
/* |
253 |
// FIXME: patch to make php not croak if result is empty at this stage |
254 |
if (!is_array($this->_memory[0])) { |
255 |
//$this->add_data_row( array( 'empty' => '[empty]') ); |
256 |
//$this->_memory[0] = array( '' => 'empty' ); |
257 |
$this->_errors[] = array("warning", "header metadata was empty"); |
258 |
print "no header!<br/>"; |
259 |
return; |
260 |
} |
261 |
*/ |
262 |
foreach($this->_schema[labels] as $value) { |
263 |
$this->debug("propagate_schema", "label: $value"); |
264 |
array_push($this->_data_keys, $value); |
265 |
} |
266 |
} |
267 |
|
268 |
|
269 |
function O_get_header() { |
270 |
$this->_read_schema(); |
271 |
return $this->_data_keys; |
272 |
} |
273 |
|
274 |
function get_labels() { |
275 |
return $this->_data_keys; |
276 |
} |
277 |
|
278 |
function set_labels($labels_or_entry) { |
279 |
if (php::is_hash($labels_or_entry)) { |
280 |
if ($this->_debug[notice]) { |
281 |
$this->debug('set_labels', 'hash was passed in (is_hash == true): just using keys'); |
282 |
} |
283 |
$labels_or_entry = array_keys($labels_or_entry); |
284 |
} |
285 |
$this->trace('set_labels', $labels_or_entry); |
286 |
$this->_schema[labels] = $labels_or_entry; |
287 |
} |
288 |
|
289 |
/* |
290 |
function get_data_source() { |
291 |
|
292 |
} |
293 |
*/ |
294 |
|
295 |
|
296 |
/** |
297 |
* This function adds a row of data |
298 |
* if necesarry to the data array |
299 |
* |
300 |
*/ |
301 |
function add_data_row($row) { |
302 |
|
303 |
//print "add_row!<br/>"; |
304 |
|
305 |
if ($row != NULL) { |
306 |
if ($this->get_searchby_value()) { |
307 |
//user wants to search the data |
308 |
if ($this->_find_data($row) ) { |
309 |
$this->_data[] = $row; |
310 |
return TRUE; |
311 |
} else { |
312 |
return FALSE; |
313 |
} |
314 |
} else { |
315 |
$this->_data[] = $row; |
316 |
return TRUE; |
317 |
} |
318 |
} else { |
319 |
return FALSE; |
320 |
} |
321 |
} |
322 |
|
323 |
function sort() { |
324 |
$sortby = $this->get_orderby(); |
325 |
//ok we need to sort the data by the |
326 |
//column in the table |
327 |
if ($this->_is_column_sortable($sortby)) { |
328 |
//looks like we can sort this column |
329 |
usort($this->_data, array($this, "cmp")); |
330 |
} |
331 |
} |
332 |
|
333 |
function cmp($data1, $data2) { |
334 |
$sortby = $this->get_orderby(); |
335 |
$ret = strnatcmp( $data1[$sortby], $data2[$sortby]); |
336 |
if ($this->get_reverseorder() == "true") { |
337 |
$ret *= -1; |
338 |
} |
339 |
return $ret; |
340 |
} |
341 |
|
342 |
function _find_data( $row_data ) { |
343 |
//look for the 'needle' in the 'haystack' |
344 |
|
345 |
$needle = $this->get_searchby_value(); |
346 |
$haystack = $row_data[$this->get_searchby()]; |
347 |
|
348 |
switch ($this->get_simplesearch_modifier()) { |
349 |
case "BEGINS": |
350 |
if (strncasecmp($needle, $haystack, strlen($needle)) == 0) { |
351 |
return TRUE; |
352 |
} else { |
353 |
return FALSE; |
354 |
} |
355 |
break; |
356 |
|
357 |
case "CONTAINS": |
358 |
if (stristr($haystack, $needle)) { |
359 |
return TRUE; |
360 |
} else { |
361 |
return FALSE; |
362 |
} |
363 |
break; |
364 |
case "EXACT": |
365 |
if (strcasecmp($needle, $haystack) == 0) { |
366 |
return TRUE; |
367 |
} else { |
368 |
return FALSE; |
369 |
} |
370 |
break; |
371 |
case "ENDS": |
372 |
$needle_len = strlen( $needle ); |
373 |
$haystack_len = strlen( $haystack ); |
374 |
if ($needle_len > $haystack_len) { |
375 |
return FALSE; |
376 |
} else if ($needle_len == $haystack_len) { |
377 |
$tmp = $haystack; |
378 |
} else { |
379 |
$tmp = substr($haystack, $haystack_len - $needle_len); |
380 |
} |
381 |
if (strncasecmp($needle, $tmp, $needle_len) == 0) { |
382 |
return TRUE; |
383 |
} else { |
384 |
return FALSE; |
385 |
} |
386 |
break; |
387 |
} |
388 |
} |
389 |
|
390 |
} |
391 |
?> |