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