1 |
<?php |
<?php |
2 |
/** |
/** |
3 |
*-------------------------------------------------------- |
* This file contains the MemoryDataSource child class |
4 |
* This file contains the DataListSource class |
* that inherits from the DataListSource. |
5 |
* that gets the data as an hash. |
* |
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$ |
* $Id$ |
22 |
*-------------------------------------------------------- |
* |
23 |
* $Log$ |
* $Log$ |
24 |
|
* Revision 1.3 2003/03/05 10:57:10 joko |
25 |
|
* established here (again?) |
26 |
|
* |
27 |
|
* Revision 1.3 2003/03/03 21:24:45 joko |
28 |
|
* updated todo |
29 |
|
* |
30 |
|
* Revision 1.2 2003/03/01 15:34:26 joko |
31 |
|
* + introduced new 'label'-mechanism, purged old 'header'-mechanism |
32 |
|
* + reworked internal data-structure and introduced new methods 'propagate_xyz' |
33 |
|
* + added little debugging helper methods |
34 |
|
* |
35 |
|
* Revision 1.1 2003/03/01 03:09:22 joko |
36 |
|
* + moved here from data_list/MemoryDataListSource.inc |
37 |
|
* |
38 |
|
* |
39 |
* Revision 1.1 2003/02/28 17:17:59 jonen |
* Revision 1.1 2003/02/28 17:17:59 jonen |
40 |
* + renamed from flibRPCDataListSource.inc |
* + renamed from flibRPCDataListSource.inc |
41 |
* |
* |
42 |
|
* Revision 1.0 2003/02/xx 00:00:00 jonen |
43 |
|
* + copied from CSVFILEDataListSource.inc |
44 |
* |
* |
|
* |
|
|
*-------------------------------------------------------- |
|
45 |
*/ |
*/ |
46 |
|
|
|
|
|
|
|
|
47 |
/** |
/** |
48 |
|
* MemoryDataSource provides a basic framework for handling |
49 |
|
* arbitrary data, that is: items, lists and trees. |
50 |
|
* It assumes the "list" being *not* the most abstract, but the most |
51 |
|
* multipurpose thing to be able to represent/handle all the others. |
52 |
|
* (regarding convenience and code-reuse: inherits from DataListSource) |
53 |
|
* |
54 |
|
* This could be pictured by looking at a list being either |
55 |
|
* a part (tree) or a container (item) of another thing. |
56 |
|
* e.g.: |
57 |
|
* item = part of list (a item is a part of a list) |
58 |
|
* list = list (a list is a list) |
59 |
|
* tree = nested lists (a part of a tree is a list) |
60 |
* |
* |
61 |
* This DataListSource child class gets the date from an hash, |
* In fact, MemoryDataSource just doesn't care a bit |
62 |
* eg an hashed remote object |
* about the type of the actual data, so perfectly fits |
63 |
|
* as an abstract base for further hackings. |
64 |
|
* e.g.: look at ProxyDataSource.inc ..... |
65 |
* |
* |
66 |
|
*/ |
67 |
|
|
68 |
|
/** |
69 |
|
* Todo: |
70 |
|
* |
71 |
|
* o introduce (g|s)etlabels to set either |
72 |
|
* a) columns (for list columns) or |
73 |
|
* b) captions (for object attributes) |
74 |
|
* o maybe use shared memory as an alternative handler |
75 |
|
* look at the php Shared Memory extension at (e.g.): |
76 |
|
* http://www.zend.com/manual/ref.shmop.php |
77 |
* |
* |
78 |
*/ |
*/ |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
// !!!!! THIS HAS TO BE BROKEN AS WELL !!!!! |
83 |
|
// !!!! compare DataSource::GenericDataSource !!!! |
84 |
|
// !!!! has to be independent of DataListSource !!!! |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
//class MemoryDataSource extends DataListSource { |
89 |
class MemoryDataListSource extends DataListSource { |
class MemoryDataListSource extends DataListSource { |
90 |
|
|
91 |
|
// it's free now! |
92 |
|
//class DataSource_Proxy_Memory { |
93 |
|
|
94 |
|
|
95 |
/** |
/** |
96 |
* given data hash |
* These hold data |
97 |
* |
* |
98 |
*/ |
*/ |
99 |
var $_data_list = array(); |
var $_memory = array(); |
100 |
|
var $_result = array(); |
|
|
|
101 |
|
|
102 |
/** |
/** |
103 |
* this holds the headers read |
* this holds the headers read |
104 |
* array keys |
* array keys |
105 |
*/ |
*/ |
106 |
var $_data_keys = array(); |
var $_data_keys = array(); |
107 |
|
|
108 |
|
/** |
109 |
|
* This holds metadata |
110 |
|
* |
111 |
|
*/ |
112 |
|
var $_schema = array(); |
113 |
|
|
114 |
|
/** |
115 |
|
* This holds some information about the tracing level. |
116 |
|
* |
117 |
|
*/ |
118 |
|
var $_debug = array( |
119 |
|
notice => 0, |
120 |
|
trace => 0, |
121 |
|
payload => 0, |
122 |
|
); |
123 |
|
|
124 |
|
/** |
125 |
|
* This accumulates errors occouring while processing. |
126 |
|
* $error = array($level, $message); |
127 |
|
* |
128 |
|
*/ |
129 |
|
var $_errors = array(); |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
/** |
/** |
134 |
* The constructor. |
* The constructor. |
136 |
* @param hash - a hashed data object |
* @param hash - a hashed data object |
137 |
* |
* |
138 |
*/ |
*/ |
139 |
function MemoryDataListSource( $data_list ) { |
function MemoryDataListSource( &$payload ) { |
140 |
$this->_data_list = $data_list; |
|
141 |
//print_r($object_list); |
// tracing |
142 |
|
$this->trace_payload('MemoryDataSource', $payload); |
143 |
|
|
144 |
|
// transfer data to "memory" |
145 |
|
$this->_memory = &$payload; |
146 |
|
|
147 |
} |
} |
148 |
|
|
149 |
|
function read_labels_from_result() { |
150 |
|
$this->set_labels($this->_result[0]); |
151 |
|
$this->propagate_schema(); |
152 |
|
} |
153 |
|
|
154 |
|
function debug($method, $message) { |
155 |
|
if ($this->_debug[notice]) { |
156 |
|
if ($method) { $prefix = get_class($this) . "->" . $method . ": "; } |
157 |
|
print $prefix . $message . "<br/>" . "\n"; |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
|
function trace($method, $var) { |
162 |
|
if ($this->_debug[trace]) { |
163 |
|
print get_class($this) . "->" . $method . ": " . Dumper($var) . "<br/>" . "\n"; |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
function trace_payload($method, $data) { |
168 |
|
if ($this->_debug[payload]) { |
169 |
|
$this->trace($method, $data); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
/** |
/** |
174 |
* The prequery. We use this to read the file |
* The prequery. We use this to read the file |
175 |
* into memory so we can do operations on the data |
* into memory so we can do operations on the data |
176 |
* (search, sort, etc.) |
* (search, sort, etc.) |
177 |
*/ |
*/ |
178 |
function do_prequery() { |
function O_do_prequery() { |
179 |
$this->_get_header(); |
$this->_read_schema(); |
180 |
} |
} |
181 |
|
function O_do_prequery() { |
182 |
|
} |
183 |
|
|
184 |
/** |
/** |
185 |
* This function does the query |
* This function does the query |
186 |
* and search/sort |
* and search/sort |
187 |
*/ |
*/ |
188 |
function do_query() { |
function do_query() { |
189 |
foreach ($this->_data_list as $value) { |
// short-circuit (default behaviour, maybe intercepted by inheriting and overwriting 'do_query') |
190 |
|
$this->_result = $this->_memory; |
191 |
|
$this->handle_result(); |
192 |
|
} |
193 |
|
|
194 |
|
function handle_result() { |
195 |
|
$this->debug("handle_result", "HANDLE RESULT!<br>"); |
196 |
|
$this->propagate_result(); |
197 |
|
} |
198 |
|
|
199 |
|
function propagate_result() { |
200 |
|
$this->debug('propagate_result', "PROPAGATE RESULT!"); |
201 |
|
foreach ($this->_result as $value) { |
202 |
|
$this->trace_payload("propagate_result", $value); |
203 |
if ($this->add_data_row( $value)) { |
if ($this->add_data_row( $value)) { |
204 |
$count++; |
$count++; |
205 |
} |
} |
206 |
} |
} |
|
|
|
207 |
$this->set_total_rows( $count ); |
$this->set_total_rows( $count ); |
208 |
$this->sort(); |
$this->sort(); |
209 |
} |
} |
210 |
|
|
211 |
/** |
/** |
212 |
* This function returns the next row of |
* This function returns the next row of |
214 |
* |
* |
215 |
*/ |
*/ |
216 |
function get_next_data_row() { |
function get_next_data_row() { |
217 |
|
|
218 |
|
//print "get_row!<br/>"; |
219 |
|
|
220 |
|
//print Dumper($this->_proxy->_result); |
221 |
|
|
222 |
$index = $this->get_data_index(); |
$index = $this->get_data_index(); |
223 |
$limit = $this->get_limit(); |
$limit = $this->get_limit(); |
224 |
$offset = $this->get_offset(); |
$offset = $this->get_offset(); |
247 |
* This file trys to get the Object Hash Keys. |
* This file trys to get the Object Hash Keys. |
248 |
* |
* |
249 |
*/ |
*/ |
250 |
function _get_header() { |
function propagate_schema() { |
251 |
foreach($this->_data_list[0] as $key => $value) { |
$this->debug("propagate_schema", "PROPAGATE SCHEMA!<br>"); |
252 |
array_push($this->_data_keys, $key); |
/* |
253 |
|
// FIXME: patch to make php not croak if result is empty at this stage |
254 |
|
if (!is_array($this->_memory[0])) { |
255 |
|
//$this->add_data_row( array( 'empty' => '[empty]') ); |
256 |
|
//$this->_memory[0] = array( '' => 'empty' ); |
257 |
|
$this->_errors[] = array("warning", "header metadata was empty"); |
258 |
|
print "no header!<br/>"; |
259 |
|
return; |
260 |
|
} |
261 |
|
*/ |
262 |
|
foreach($this->_schema[labels] as $value) { |
263 |
|
$this->debug("propagate_schema", "label: $value"); |
264 |
|
array_push($this->_data_keys, $value); |
265 |
} |
} |
266 |
} |
} |
267 |
|
|
268 |
|
|
269 |
function get_header() { |
function O_get_header() { |
270 |
$this->_get_header(); |
$this->_read_schema(); |
271 |
return $this->_data_keys; |
return $this->_data_keys; |
272 |
} |
} |
273 |
|
|
274 |
|
function get_labels() { |
275 |
|
return $this->_data_keys; |
276 |
|
} |
277 |
|
|
278 |
|
function set_labels($labels_or_entry) { |
279 |
|
if (php::is_hash($labels_or_entry)) { |
280 |
|
if ($this->_debug[notice]) { |
281 |
|
$this->debug('set_labels', 'hash was passed in (is_hash == true): just using keys'); |
282 |
|
} |
283 |
|
$labels_or_entry = array_keys($labels_or_entry); |
284 |
|
} |
285 |
|
$this->trace('set_labels', $labels_or_entry); |
286 |
|
$this->_schema[labels] = $labels_or_entry; |
287 |
|
} |
288 |
|
|
289 |
|
/* |
290 |
function get_data_source() { |
function get_data_source() { |
291 |
|
|
292 |
} |
} |
293 |
|
*/ |
294 |
|
|
295 |
|
|
296 |
/** |
/** |
299 |
* |
* |
300 |
*/ |
*/ |
301 |
function add_data_row($row) { |
function add_data_row($row) { |
302 |
|
|
303 |
|
//print "add_row!<br/>"; |
304 |
|
|
305 |
if ($row != NULL) { |
if ($row != NULL) { |
306 |
if ($this->get_searchby_value()) { |
if ($this->get_searchby_value()) { |
307 |
//user wants to search the data |
//user wants to search the data |