/[cvs]/nfo/php/libs/org.netfrag.patches/phphtmllib/drivers/MemoryDataSource.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.patches/phphtmllib/drivers/MemoryDataSource.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (show annotations)
Wed Oct 26 22:40:54 2005 UTC (18 years, 10 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +15 -2 lines
+ fixed page-control is search-value passed, searching/filtering is already done at backend (Memory!)
  FIXME: the method '_find_data_' caused a 0 result if try to search already filtered results, fix this!

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed