1 |
<?php |
2 |
|
3 |
/** |
4 |
* This file contains the Base DataListSource |
5 |
* class which abstracts the data fetching/sorting |
6 |
* from the DataList. |
7 |
* |
8 |
* |
9 |
* $Id: DataListSource.inc,v 1.1.1.1 2003/01/30 03:29:46 jonen Exp $ |
10 |
* |
11 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
12 |
* @package phpHtmlLib |
13 |
*/ |
14 |
|
15 |
|
16 |
|
17 |
/** |
18 |
* |
19 |
* This is the base class for managing data for |
20 |
* the DataList class. This abstracts away the |
21 |
* underlying data layer from the DataList, so |
22 |
* the data can come from multiple sources. |
23 |
* Most of the time the data will come from a |
24 |
* data base such as Mysql or Oracle. This |
25 |
* abstraction enables the data to also come from |
26 |
* a tab delimited file, xml, php array |
27 |
* |
28 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
29 |
* @package phpHtmlLib |
30 |
*/ |
31 |
class DataListSource { |
32 |
|
33 |
/** |
34 |
* The column descriptions |
35 |
* for the data we are working on |
36 |
* |
37 |
* @var array |
38 |
*/ |
39 |
var $_columns = array(); |
40 |
|
41 |
/** |
42 |
* This is the message displayed when no data |
43 |
* was retrieved from the database |
44 |
*/ |
45 |
var $_not_found_message = "No data was found"; |
46 |
|
47 |
|
48 |
/** |
49 |
* This holds various parameters relating |
50 |
* to the query of the data |
51 |
* |
52 |
*/ |
53 |
var $_query_params = array("num_total_rows" => 0, |
54 |
"offset" => 0, |
55 |
"limit" => -1, |
56 |
"orderby" => '', |
57 |
"reverseorder" => '', |
58 |
"searchby" => '', |
59 |
"searchvalue" => '', |
60 |
"searchmodifier" => '', |
61 |
"searchtype" => 'simple'); |
62 |
|
63 |
/** |
64 |
* Holds the index into the array of data |
65 |
* so we can keep track of where we are |
66 |
* when we are walking the array |
67 |
* (only usefull for non DB children) |
68 |
* |
69 |
*/ |
70 |
var $_data_index = 0; |
71 |
|
72 |
/** |
73 |
* A placeholder for data that is read/built |
74 |
* and stored locally. Not all data sources |
75 |
* have to use this. |
76 |
* Each entry in the array corresponds to 1 row |
77 |
* of data. |
78 |
*/ |
79 |
var $_data = array(); |
80 |
|
81 |
/** |
82 |
* |
83 |
* The constructor |
84 |
*/ |
85 |
function DataListSource() { |
86 |
|
87 |
} |
88 |
|
89 |
/** |
90 |
* The main external API methods |
91 |
* that are used by the DataList |
92 |
* Class |
93 |
* |
94 |
*/ |
95 |
|
96 |
/** |
97 |
* Add a column of data to manage |
98 |
* |
99 |
* @param string - the title of the column |
100 |
* @param string - the data value name |
101 |
* @param boolean - is the column sortable? |
102 |
* default: FALSE |
103 |
* @param boolean - is the column searchable |
104 |
* default: FALSE |
105 |
* @param string - the sort order (ASC, DESC) |
106 |
* default: ASC |
107 |
*/ |
108 |
function add_column( $title, $data_name, $sortable=FALSE, |
109 |
$searchable=FALSE, $sortorder="ASC") { |
110 |
$col = array(); |
111 |
$col["data_name"] = $data_name; |
112 |
$col["sortable"] = $sortable; |
113 |
$col["searchable"] = $searchable; |
114 |
$col["sortorder"] = $sortorder; |
115 |
|
116 |
$this->_columns[$title] = $col; |
117 |
} |
118 |
|
119 |
|
120 |
/** |
121 |
* The main Query function. |
122 |
* This function is responsible for doing |
123 |
* any data prefetching from a db,file |
124 |
* and doing any sorting and searching |
125 |
* on it depending on the values passed |
126 |
* in from the DataList object |
127 |
* |
128 |
* @param int - the offset into the data set |
129 |
* @param int - the # of rows to get |
130 |
* @param string - the column to order the data by |
131 |
* @param string - order in asc or reverse? |
132 |
* @param string - the column in the dataset |
133 |
* to search by |
134 |
* @param string - the value to look for |
135 |
* @param string - the simple search modifier. |
136 |
* |
137 |
*/ |
138 |
function query($offset, $limit, $orderby, $reverseorder, |
139 |
$searchby, $searchby_value, |
140 |
$simplesearch_modifier, $search_type ) { |
141 |
$this->set_offset($offset); |
142 |
$this->set_limit($limit); |
143 |
$this->set_orderby($orderby); |
144 |
$this->set_reverseorder($reverseorder); |
145 |
$this->set_searchby($searchby); |
146 |
$this->set_searchby_value($searchby_value); |
147 |
$this->set_simplesearch_modifier( $simplesearch_modifier ); |
148 |
$this->set_search_type( $search_type ); |
149 |
|
150 |
//now call child methods |
151 |
|
152 |
//do any pre query setup |
153 |
//This depends on the source of the data |
154 |
//but an example would be to build a |
155 |
//sql query string if the source is a database |
156 |
//or to validate/open a file from disk if the |
157 |
//source is a file. |
158 |
//or to do any checks on a memory cache |
159 |
//if the data source is a sysvshm segment. |
160 |
$this->do_prequery(); |
161 |
|
162 |
//do the actual data fetching/sorting |
163 |
$this->do_query(); |
164 |
} |
165 |
|
166 |
/** |
167 |
* This function gets the next data row |
168 |
* from the query() |
169 |
* |
170 |
* @return array() |
171 |
*/ |
172 |
function get_next_data_row() { |
173 |
user_error("DataListSource::get_next_data_row() - Child must override"); |
174 |
} |
175 |
|
176 |
|
177 |
/** |
178 |
* This is a method that should be defined by the |
179 |
* child class to do any pre-query type of things. |
180 |
* Such as building a sql query string for a DB, |
181 |
* or checking to make sure the file on disk exists |
182 |
* if the source is a file on disk. |
183 |
* |
184 |
*/ |
185 |
function do_prequery() { |
186 |
user_error("DataListSource::do_prequery() - Child must override"); |
187 |
} |
188 |
|
189 |
|
190 |
/** |
191 |
* This is the function that does the data fetching, |
192 |
* and sorting if needed. |
193 |
* If the source is a sql database, this is where the |
194 |
* query gets called. This function doesn't actually read the |
195 |
* data from the DB yet. That is what get_next_data_row() |
196 |
* does. |
197 |
* |
198 |
*/ |
199 |
function do_query() { |
200 |
user_error("DataListSource::do_query() - Child must override"); |
201 |
} |
202 |
|
203 |
/** |
204 |
* A generic method API that can be used at the bottom |
205 |
* half of the do_query() method to sort data that is |
206 |
* stored locally. This is only needed when the source |
207 |
* is a non database. |
208 |
* |
209 |
* It should operate on the $this->_data array |
210 |
*/ |
211 |
function sort() { |
212 |
} |
213 |
|
214 |
|
215 |
/** |
216 |
* This function is used to set the |
217 |
* message displayed when no data is found |
218 |
* |
219 |
* @param string |
220 |
*/ |
221 |
function set_not_found_message( $mesg ) { |
222 |
$this->_not_found_message = $mesg; |
223 |
} |
224 |
|
225 |
/** |
226 |
* This function is used to get the |
227 |
* message displayed when no data is found |
228 |
* |
229 |
* @return string |
230 |
*/ |
231 |
function get_not_found_message() { |
232 |
return $this->_not_found__message; |
233 |
} |
234 |
|
235 |
/** |
236 |
* This returns the total number of rows |
237 |
* in our entire data set |
238 |
* |
239 |
* @return int |
240 |
*/ |
241 |
function get_total_rows() { |
242 |
return $this->_query_params["num_total_rows"]; |
243 |
} |
244 |
|
245 |
/** |
246 |
* This is used to set the total # of |
247 |
* rows we have in our data set |
248 |
* |
249 |
* @param int |
250 |
*/ |
251 |
function set_total_rows($num) { |
252 |
$this->_query_params["num_total_rows"] = $num; |
253 |
} |
254 |
|
255 |
|
256 |
/** |
257 |
* This sets the offset value |
258 |
* and resets the index into the data |
259 |
* array (in non DB children) |
260 |
* |
261 |
* @param int offset |
262 |
*/ |
263 |
function set_offset($offset) { |
264 |
$this->_query_params["offset"] = $offset; |
265 |
$this->_data_index = $offset; |
266 |
} |
267 |
|
268 |
/** |
269 |
* This function returns the value of the |
270 |
* offset |
271 |
* |
272 |
* @return int |
273 |
*/ |
274 |
function get_offset() { |
275 |
return $this->_query_params["offset"]; |
276 |
} |
277 |
|
278 |
/** |
279 |
* This sets the orderby column name. |
280 |
* This corresponds to the column |
281 |
* that wants to be sorted/ordered, but |
282 |
* not the actual direction (asc, desc) |
283 |
* |
284 |
* @param int offset |
285 |
*/ |
286 |
function set_orderby($orderby) { |
287 |
$this->_query_params["orderby"] = $orderby; |
288 |
} |
289 |
|
290 |
/** |
291 |
* This function returns the value of the |
292 |
* orderby |
293 |
* |
294 |
* @return int |
295 |
*/ |
296 |
function get_orderby() { |
297 |
return $this->_query_params["orderby"]; |
298 |
} |
299 |
|
300 |
/** |
301 |
* This sets the flag that tells us the |
302 |
* direction in which to order the orderby |
303 |
* column. |
304 |
* |
305 |
* @param int offset |
306 |
*/ |
307 |
function set_reverseorder($order) { |
308 |
$this->_query_params["reverseorder"] = $order; |
309 |
} |
310 |
|
311 |
/** |
312 |
* This function returns the value of the |
313 |
* reverseorder |
314 |
* |
315 |
* @return int |
316 |
*/ |
317 |
function get_reverseorder() { |
318 |
return $this->_query_params["reverseorder"]; |
319 |
} |
320 |
|
321 |
/** |
322 |
* This sets the column that we want to search |
323 |
* from. |
324 |
* |
325 |
* @param int offset |
326 |
*/ |
327 |
function set_searchby($search_col) { |
328 |
$this->_query_params["searchby"] = $search_col; |
329 |
} |
330 |
|
331 |
/** |
332 |
* This function returns the value of the |
333 |
* searchby |
334 |
* |
335 |
* @return int |
336 |
*/ |
337 |
function get_searchby() { |
338 |
return $this->_query_params["searchby"]; |
339 |
} |
340 |
|
341 |
/** |
342 |
* This sets the data that we want to search |
343 |
* for. |
344 |
* |
345 |
* @param int offset |
346 |
*/ |
347 |
function set_searchby_value($search_value) { |
348 |
$this->_query_params["searchvalue"] = $search_value; |
349 |
} |
350 |
|
351 |
/** |
352 |
* This function returns the value of the |
353 |
* search value |
354 |
* |
355 |
* @return int |
356 |
*/ |
357 |
function get_searchby_value() { |
358 |
return $this->_query_params["searchvalue"]; |
359 |
} |
360 |
|
361 |
/** |
362 |
* This sets the simple search modifier |
363 |
* |
364 |
* |
365 |
* @param int offset |
366 |
*/ |
367 |
function set_simplesearch_modifier($search_modifier) { |
368 |
$this->_query_params["searchmodifier"] = $search_modifier; |
369 |
} |
370 |
|
371 |
/** |
372 |
* This function returns the value of the |
373 |
* search value |
374 |
* |
375 |
* @return int |
376 |
*/ |
377 |
function get_simplesearch_modifier() { |
378 |
return $this->_query_params["searchmodifier"]; |
379 |
} |
380 |
|
381 |
/** |
382 |
* This function is used to set |
383 |
* the limit value, which limits |
384 |
* the # of rows of data to allow |
385 |
* to return |
386 |
*/ |
387 |
function set_limit( $limit ) { |
388 |
$this->_query_params["limit"] = $limit; |
389 |
} |
390 |
|
391 |
/** |
392 |
* This function gets the current |
393 |
* value of the limit value |
394 |
* |
395 |
* @return int |
396 |
*/ |
397 |
function get_limit() { |
398 |
return $this->_query_params["limit"]; |
399 |
} |
400 |
|
401 |
|
402 |
/** |
403 |
* This function sets the search type |
404 |
* (simple or advanced) |
405 |
* |
406 |
* @param string |
407 |
*/ |
408 |
function set_search_type( $search_type ) { |
409 |
$this->_query_params["searchtype"] = $search_type; |
410 |
} |
411 |
|
412 |
/** |
413 |
* this function returns the current search type |
414 |
* for the DataList query |
415 |
* |
416 |
* @return string |
417 |
*/ |
418 |
function get_search_type() { |
419 |
return $this->_query_params["searchtype"]; |
420 |
} |
421 |
|
422 |
/** |
423 |
* This function returns the |
424 |
* data_index value and increments it |
425 |
* |
426 |
* @return int |
427 |
*/ |
428 |
function get_data_index() { |
429 |
$this->_data_index++; |
430 |
return $this->_data_index-1; |
431 |
} |
432 |
|
433 |
|
434 |
/** |
435 |
* This function returns the |
436 |
* data_index value only |
437 |
* |
438 |
* @return int |
439 |
*/ |
440 |
function get_cur_data_index() { |
441 |
return $this->_data_index; |
442 |
} |
443 |
|
444 |
|
445 |
/** |
446 |
* This function determines if the column |
447 |
* associated w/ a data_name is sortable |
448 |
* or not |
449 |
* |
450 |
* @param string - the data_name filed in the |
451 |
* _columns array to look for |
452 |
* @return boolean - is that column sortable? |
453 |
*/ |
454 |
function _is_column_sortable( $data_name ) { |
455 |
foreach( $this->_columns as $name => $data ) { |
456 |
if ($data["data_name"] == $data_name && |
457 |
$data["sortable"] == TRUE) { |
458 |
return TRUE; |
459 |
} |
460 |
} |
461 |
return FALSE; |
462 |
} |
463 |
} |
464 |
?> |