/[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.3 - (hide annotations)
Mon Mar 3 21:24:45 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.2: +9 -1 lines
updated todo

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

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