/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/data_source/MemoryDataSource.inc
ViewVC logotype

Annotation of /nfo/php/libs/com.newsblob.phphtmllib/widgets/data_source/MemoryDataSource.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Sat Mar 1 15:34:26 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.1: +100 -23 lines
+ introduced new 'label'-mechanism, purged old 'header'-mechanism
+ reworked internal data-structure and introduced new methods 'propagate_xyz'
+ added little debugging helper methods

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

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