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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations)
Thu Oct 7 14:16:39 2004 UTC (19 years, 11 months ago) by jonen
Branch: MAIN
Changes since 1.6: +17 -9 lines
+ updates related to  HACK at DataSource/Generic (page-control!)

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

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