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