/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/MemoryDataListSource.inc
ViewVC logotype

Contents of /nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/MemoryDataListSource.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Wed Mar 5 10:57:10 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
Changes since 1.2: +192 -31 lines
established here (again?)

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
79 // !!!!! THIS HAS TO BE BROKEN AS WELL !!!!!
80 // !!!! compare DataSource::GenericDataSource !!!!
81 // !!!! has to be independent of DataListSource !!!!
82
83
84
85 //class MemoryDataSource extends DataListSource {
86 class MemoryDataListSource extends DataListSource {
87
88 // it's free now!
89 //class DataSource_Proxy_Memory {
90
91
92 /**
93 * These hold data
94 *
95 */
96 var $_memory = array();
97 var $_result = array();
98
99 /**
100 * this holds the headers read
101 * array keys
102 */
103 var $_data_keys = array();
104
105 /**
106 * This holds metadata
107 *
108 */
109 var $_schema = array();
110
111 /**
112 * This holds some information about the tracing level.
113 *
114 */
115 var $_debug = array(
116 notice => 0,
117 trace => 0,
118 payload => 0,
119 );
120
121 /**
122 * This accumulates errors occouring while processing.
123 * $error = array($level, $message);
124 *
125 */
126 var $_errors = array();
127
128
129
130 /**
131 * The constructor.
132 *
133 * @param hash - a hashed data object
134 *
135 */
136 function MemoryDataListSource( &$payload ) {
137
138 // tracing
139 $this->trace_payload('MemoryDataSource', $payload);
140
141 // transfer data to "memory"
142 $this->_memory = &$payload;
143
144 }
145
146 function read_labels_from_result() {
147 $this->set_labels($this->_result[0]);
148 $this->propagate_schema();
149 }
150
151 function debug($method, $message) {
152 if ($this->_debug[notice]) {
153 if ($method) { $prefix = get_class($this) . "->" . $method . ": "; }
154 print $prefix . $message . "<br/>" . "\n";
155 }
156 }
157
158 function trace($method, $var) {
159 if ($this->_debug[trace]) {
160 print get_class($this) . "->" . $method . ": " . Dumper($var) . "<br/>" . "\n";
161 }
162 }
163
164 function trace_payload($method, $data) {
165 if ($this->_debug[payload]) {
166 $this->trace($method, $data);
167 }
168 }
169
170 /**
171 * The prequery. We use this to read the file
172 * into memory so we can do operations on the data
173 * (search, sort, etc.)
174 */
175 function O_do_prequery() {
176 $this->_read_schema();
177 }
178 function O_do_prequery() {
179 }
180
181 /**
182 * This function does the query
183 * and search/sort
184 */
185 function do_query() {
186 // short-circuit (default behaviour, maybe intercepted by inheriting and overwriting 'do_query')
187 $this->_result = $this->_memory;
188 $this->handle_result();
189 }
190
191 function handle_result() {
192 $this->debug("handle_result", "HANDLE RESULT!<br>");
193 $this->propagate_result();
194 }
195
196 function propagate_result() {
197 $this->debug('propagate_result', "PROPAGATE RESULT!");
198 foreach ($this->_result as $value) {
199 $this->trace_payload("propagate_result", $value);
200 if ($this->add_data_row( $value)) {
201 $count++;
202 }
203 }
204 $this->set_total_rows( $count );
205 $this->sort();
206 }
207
208 /**
209 * This function returns the next row of
210 * valid data.
211 *
212 */
213 function get_next_data_row() {
214
215 //print "get_row!<br/>";
216
217 //print Dumper($this->_proxy->_result);
218
219 $index = $this->get_data_index();
220 $limit = $this->get_limit();
221 $offset = $this->get_offset();
222
223 if ($limit == -1) {
224 //don't limit the data
225 if ($index > $this->get_total_rows()) {
226 return NULL;
227 } else {
228 return $this->_data[$index];
229 }
230 } else {
231 $left_to_show = $limit - ($index - $offset);
232 if ($left_to_show > 0) {
233 return $this->_data[$index];
234 } else {
235 return NULL;
236 }
237 }
238 }
239
240
241
242
243 /**
244 * This file trys to get the Object Hash Keys.
245 *
246 */
247 function propagate_schema() {
248 $this->debug("propagate_schema", "PROPAGATE SCHEMA!<br>");
249 /*
250 // FIXME: patch to make php not croak if result is empty at this stage
251 if (!is_array($this->_memory[0])) {
252 //$this->add_data_row( array( 'empty' => '[empty]') );
253 //$this->_memory[0] = array( '' => 'empty' );
254 $this->_errors[] = array("warning", "header metadata was empty");
255 print "no header!<br/>";
256 return;
257 }
258 */
259 foreach($this->_schema[labels] as $value) {
260 $this->debug("propagate_schema", "label: $value");
261 array_push($this->_data_keys, $value);
262 }
263 }
264
265
266 function O_get_header() {
267 $this->_read_schema();
268 return $this->_data_keys;
269 }
270
271 function get_labels() {
272 return $this->_data_keys;
273 }
274
275 function set_labels($labels_or_entry) {
276 if (php::is_hash($labels_or_entry)) {
277 if ($this->_debug[notice]) {
278 $this->debug('set_labels', 'hash was passed in (is_hash == true): just using keys');
279 }
280 $labels_or_entry = array_keys($labels_or_entry);
281 }
282 $this->trace('set_labels', $labels_or_entry);
283 $this->_schema[labels] = $labels_or_entry;
284 }
285
286 /*
287 function get_data_source() {
288
289 }
290 */
291
292
293 /**
294 * This function adds a row of data
295 * if necesarry to the data array
296 *
297 */
298 function add_data_row($row) {
299
300 //print "add_row!<br/>";
301
302 if ($row != NULL) {
303 if ($this->get_searchby_value()) {
304 //user wants to search the data
305 if ($this->_find_data($row) ) {
306 $this->_data[] = $row;
307 return TRUE;
308 } else {
309 return FALSE;
310 }
311 } else {
312 $this->_data[] = $row;
313 return TRUE;
314 }
315 } else {
316 return FALSE;
317 }
318 }
319
320 function sort() {
321 $sortby = $this->get_orderby();
322 //ok we need to sort the data by the
323 //column in the table
324 if ($this->_is_column_sortable($sortby)) {
325 //looks like we can sort this column
326 usort($this->_data, array($this, "cmp"));
327 }
328 }
329
330 function cmp($data1, $data2) {
331 $sortby = $this->get_orderby();
332 $ret = strnatcmp( $data1[$sortby], $data2[$sortby]);
333 if ($this->get_reverseorder() == "true") {
334 $ret *= -1;
335 }
336 return $ret;
337 }
338
339 function _find_data( $row_data ) {
340 //look for the 'needle' in the 'haystack'
341
342 $needle = $this->get_searchby_value();
343 $haystack = $row_data[$this->get_searchby()];
344
345 switch ($this->get_simplesearch_modifier()) {
346 case "BEGINS":
347 if (strncasecmp($needle, $haystack, strlen($needle)) == 0) {
348 return TRUE;
349 } else {
350 return FALSE;
351 }
352 break;
353
354 case "CONTAINS":
355 if (stristr($haystack, $needle)) {
356 return TRUE;
357 } else {
358 return FALSE;
359 }
360 break;
361 case "EXACT":
362 if (strcasecmp($needle, $haystack) == 0) {
363 return TRUE;
364 } else {
365 return FALSE;
366 }
367 break;
368 case "ENDS":
369 $needle_len = strlen( $needle );
370 $haystack_len = strlen( $haystack );
371 if ($needle_len > $haystack_len) {
372 return FALSE;
373 } else if ($needle_len == $haystack_len) {
374 $tmp = $haystack;
375 } else {
376 $tmp = substr($haystack, $haystack_len - $needle_len);
377 }
378 if (strncasecmp($needle, $tmp, $needle_len) == 0) {
379 return TRUE;
380 } else {
381 return FALSE;
382 }
383 break;
384 }
385 }
386
387 }
388 ?>

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