1 |
<? |
2 |
/* |
3 |
## ------------------------------------------------------------------------- |
4 |
## $Id: GenericGUIDataList.php,v 1.10 2003/04/18 18:40:02 jonen Exp $ |
5 |
## ------------------------------------------------------------------------- |
6 |
## $Log: GenericGUIDataList.php,v $ |
7 |
## Revision 1.10 2003/04/18 18:40:02 jonen |
8 |
## + fixed bugs at 'actionbar_cell' relating to html-form and js |
9 |
## |
10 |
## Revision 1.9 2003/04/11 00:44:46 joko |
11 |
## minor update: shortened seperators |
12 |
## |
13 |
## Revision 1.8 2003/04/09 07:54:42 joko |
14 |
## cleaned up decoding part |
15 |
## |
16 |
## Revision 1.7 2003/04/09 00:37:53 jonen |
17 |
## + added actionbar |
18 |
## + some changes according to request vars |
19 |
## |
20 |
## Revision 1.6 2003/04/08 17:48:31 jonen |
21 |
## + bugfix again |
22 |
## |
23 |
## Revision 1.5 2003/04/08 17:33:22 jonen |
24 |
## + fixed problems with hidden form fields(current base URL) at the 'simple search form' |
25 |
## |
26 |
## Revision 1.4 2003/04/06 01:41:07 jonen |
27 |
## - removed duplicated decode functions |
28 |
## |
29 |
## Revision 1.3 2003/04/04 21:26:06 joko |
30 |
## added comments regarding possible (future) refactoring using the Data::Lift-infrastructure |
31 |
## |
32 |
## Revision 1.2 2003/04/04 00:45:40 jonen |
33 |
## + reworked decode functions |
34 |
## |
35 |
## Revision 1.1 2003/03/27 01:17:31 jonen |
36 |
## + inital commit (was orginal 'ObjectList.inc) |
37 |
## |
38 |
## Revision 1.1 2003/03/20 04:17:28 jonen |
39 |
## + initial commit |
40 |
## |
41 |
## Revision 1.2 2003/03/02 04:32:22 cvsmax |
42 |
## + add decode_item functions |
43 |
## |
44 |
## Revision 1.1 2003/03/01 22:57:40 cvsmax |
45 |
## + inital commit |
46 |
## |
47 |
## ------------------------------------------------------------------------- |
48 |
*/ |
49 |
|
50 |
|
51 |
/** |
52 |
* Need to make sure we have the DefaultGUIDataList object |
53 |
*/ |
54 |
require_once($phphtmllib."/widgets/data_list/DefaultGUIDataList.inc"); |
55 |
|
56 |
|
57 |
class GenericGUIDataList extends DefaultGUIDataList { |
58 |
|
59 |
/** |
60 |
* Args needed for object list. |
61 |
* Current use: |
62 |
* - use some bits to create locator metadata (see below) |
63 |
* - use all options to propagate to an AdapterProxy (here: GenericDataSource) |
64 |
* as a query to receive a list of objects as response to a rpc-call. |
65 |
* Todo: glossary: ban "*list* of objects" - it's an object *set* !!! (see CPAN's Set::Object) |
66 |
* |
67 |
*/ |
68 |
var $_options = NULL; |
69 |
|
70 |
/** |
71 |
* Holds a list of hidden elements |
72 |
* |
73 |
*/ |
74 |
var $_hidden_elements = array(); |
75 |
|
76 |
|
77 |
/** |
78 |
* The constructor is used to pass in some additional |
79 |
* metadata informations as "options". |
80 |
* The "locator metadata hash" isn't passed in here |
81 |
* any more. Handling this one occours one level deeper |
82 |
* inside the "GenericDataSource" Proxy. |
83 |
* |
84 |
* It's still possible to set locator metadata directly by |
85 |
* using the optional key 'locator' inside your options hash. |
86 |
* This can be either a DataSource::Locator instance or a plain |
87 |
* hash. However - it gets merged to the master locator |
88 |
* created by GenericDataSource using 'array_join_merge'. |
89 |
* |
90 |
* @param old: LocatorMetadataHash array - layout: array( type => '', metadata => '', dsn => '' ) |
91 |
* @param old: Query array - layout: array( type => '', metadata => '', dsn => '' ) |
92 |
* |
93 |
* @param new: options array - layout: array( datasource_type => '', [locator => array()] ) |
94 |
* |
95 |
* |
96 |
*/ |
97 |
function GenericGUIDataList($title, $width = "100%", $default_orderby='', $default_reverseorder=FALSE, $options = array()) { |
98 |
if ($options) { $this->_options = $options; } |
99 |
|
100 |
// call parent constructor |
101 |
$parent = get_parent_class($this); |
102 |
$this->$parent($title, $width = "100%", $default_orderby='', $default_reverseorder=FALSE); |
103 |
} |
104 |
|
105 |
|
106 |
function get_data_source( ) { |
107 |
|
108 |
// finally: create phpHtmlLib source handle (XyzDataListSource) |
109 |
|
110 |
// V1: |
111 |
//$source = new GenericDataSource($this->_locator, $this->_options); |
112 |
//$this->set_data_source( &$source ); |
113 |
|
114 |
/* |
115 |
// V2: |
116 |
loadModule("DataSource::GenericDataSource"); |
117 |
$source = new DataSource_GenericDataSource($this->_locator, $this->_options); |
118 |
$this->set_data_source( &$source ); |
119 |
*/ |
120 |
|
121 |
// V3: |
122 |
/* |
123 |
$proxy = php::mkComponent('DataSource::GenericDataSource', $this->_locator, $this->_options); |
124 |
$source = &$proxy; |
125 |
$this->set_data_source( &$source ); |
126 |
*/ |
127 |
|
128 |
// V4: |
129 |
/* |
130 |
$proxy = php::mkComponent('DataSource::GenericDataSource', array( proxy => '', adapter => ''), $this->_options); |
131 |
$source = &$proxy; |
132 |
$this->set_data_source( &$source ); |
133 |
*/ |
134 |
|
135 |
// V5: |
136 |
//print "options: " . Dumper($this->_options); |
137 |
|
138 |
$initial_locator = php::mkComponent( 'DataSource::Locator', array( adapter_type => 'phpHtmlLib' ) ); |
139 |
//print "1st locator: " . Dumper($initial_locator); |
140 |
|
141 |
// OLD |
142 |
//$proxy = php::mkComponent('DataSource::Generic', $initial_locator, $this->_options); |
143 |
|
144 |
//print "Test Query:"; |
145 |
//print Dumper($this->_options['query']); |
146 |
$proxy = php::mkComponent('DataSource::Generic', $initial_locator, $this->_options['data_locator_meta']); |
147 |
|
148 |
// debug |
149 |
//print "Proxy: " . Dumper($proxy); |
150 |
|
151 |
// V1: |
152 |
//$source = &$proxy; |
153 |
|
154 |
// V2: |
155 |
//$source->make_adapter_transparent(); |
156 |
|
157 |
// V3: |
158 |
$source = $proxy->get_adapter(); |
159 |
|
160 |
// V4: |
161 |
//$source = $this->_datasource; |
162 |
|
163 |
//print "objectlist-source: " . Dumper($source); |
164 |
//exit; |
165 |
|
166 |
$this->set_data_source( &$source ); |
167 |
|
168 |
} |
169 |
|
170 |
function user_setup() { |
171 |
|
172 |
//return; |
173 |
|
174 |
$source = $this->_datasource; |
175 |
//if (!$source) { return; } |
176 |
|
177 |
//print Dumper($source); |
178 |
$headers = $source->get_header(); |
179 |
//print Dumper($headers); |
180 |
|
181 |
$this->set_title( $this->get_title() . ' - ' . $source->get_total_rows() . ' Einträge' ); |
182 |
|
183 |
foreach($headers as $val) { |
184 |
$this->add_header_item($val, "150", $val, SORTABLE, SEARCHABLE); |
185 |
} |
186 |
|
187 |
/* |
188 |
$this->add_header_item("Uid", "200", "uid", SORTABLE, SEARCHABLE); |
189 |
$this->add_header_item("Name", "200", "lastName", SORTABLE, SEARCHABLE, "center"); |
190 |
*/ |
191 |
|
192 |
} |
193 |
|
194 |
|
195 |
/** |
196 |
* This function ensures that the data we place |
197 |
* in a column is aligned according to what the |
198 |
* user wants. |
199 |
* |
200 |
* @param mixed - $obj - the data for the td. |
201 |
* @param string - $col_name - the name of the column header |
202 |
* for this row to render. |
203 |
* @param int - $odd_row - tells us if this cell lives in |
204 |
* an odd # row (for alternating row colors) |
205 |
* @param int - the column # we are working on. |
206 |
* @return TDtag object |
207 |
*/ |
208 |
function wrap_column_item($obj, $col_name, $row_data) { |
209 |
|
210 |
//make sure its set to something. |
211 |
if ($obj == '') { |
212 |
$obj = " "; |
213 |
} |
214 |
|
215 |
// decode item / mungle item data? |
216 |
// TODO: use Data::Lift here!!! (like already done in YAA::JobList and YAA::JobDetails) |
217 |
elseif ($this->_options['decode']) { |
218 |
|
219 |
// Proposal using Data::Lift: (see YAA::JobList and YAA::JobDetails) |
220 |
// This class will get shorter and better to maintain! |
221 |
|
222 |
// Picture Data::Lift as a container component handling |
223 |
// *specific* en-/decoding stuff for your very core components. |
224 |
// This way we will have almost all "specific mungling code" at one place. nice thing! |
225 |
|
226 |
/* |
227 |
|
228 |
// 1. expr: string -> object (phpHtmlLib-object: 'html_a'-tag) |
229 |
// returns a phpHtmlLib object of class 'Xyz' containing a http-/html-link |
230 |
// pointing to a View of a different - linked/referenced - object (reference-interpolation) |
231 |
$this->lift1 = php::mkComponent('Data::Lift', $obj, array( metatype => 'expression' ) ); |
232 |
$obj = $this->lift1->to('phpHtmlLib::html_a'); |
233 |
|
234 |
// 2. data: hash -> object (phpHtmlLib-object: 'container'-object containing form) |
235 |
// returns a phpHtmlLib object of class 'Container' containing a html-form made up of |
236 |
// phpHtmlLib form-tags wrapping all attribute-value pairs of the data hash (object-edit) |
237 |
$this->lift2 = php::mkComponent('Data::Lift', $obj, array( metatype => 'item' ) ); |
238 |
$obj = $this->lift2->to('phpHtmlLib::form'); |
239 |
|
240 |
*/ |
241 |
|
242 |
$utils = php::mkComponent('WebExplorer::utils'); |
243 |
$hidden = $this->_hidden_elements; |
244 |
$options = $this->_options['decode_args']; |
245 |
$options[label] = $col_name; |
246 |
$options[parent_guid] = $row_data[guid]; |
247 |
$options[parent_class] = $this->_options['parent']['class']; |
248 |
|
249 |
// a) Iterate through the attributes of the item and replace each |
250 |
// value with an appropriate link object if it has a certain format. |
251 |
if ($utils->decode_item_expr($obj, $hidden, $options)) { |
252 |
|
253 |
// b) Otherwise: If item is an Array we will replace it with a selection form object. |
254 |
} else { |
255 |
$utils->decode_item_array($obj, $hidden, $options); |
256 |
} |
257 |
|
258 |
} |
259 |
|
260 |
//make sure we don't put a right border on the last |
261 |
//column we are working on. |
262 |
//$style = "padding-left: 3px;padding-right:3px;border-top: 1px solid #dddddd;"; |
263 |
|
264 |
|
265 |
if ($this->_columns[$col_name]["data_name"] == $this->orderby()) { |
266 |
$style = "background-color: #f4f4f4;"; |
267 |
} else { |
268 |
$style = "background-color: #ffffff;"; |
269 |
} |
270 |
|
271 |
$align = $this->_columns[$col_name]["align"]; |
272 |
$td = new TDtag(array("align" => $align, |
273 |
"style" => $style, |
274 |
"class" => "datalist_data_cell")); |
275 |
|
276 |
if (is_object($obj) && $obj->_tag == "td") { |
277 |
return $obj; |
278 |
} else { |
279 |
$td->add( $obj ); |
280 |
} |
281 |
return $td; |
282 |
} |
283 |
|
284 |
function add_hidden_element($label, $value) { |
285 |
$this->_hidden_elements[$label] = $value; |
286 |
} |
287 |
|
288 |
|
289 |
|
290 |
/** |
291 |
* This function builds the simple search TD |
292 |
* |
293 |
* @return ContainerWidget |
294 |
*/ |
295 |
function _build_simple_search_form() { |
296 |
|
297 |
//if there is only 1 item enabled for search |
298 |
//then the search looks simple. |
299 |
$fields = $this->_get_searchable_fields(); |
300 |
$cnt = count($fields); |
301 |
if ($cnt == 0) { |
302 |
return NULL; |
303 |
} |
304 |
|
305 |
$container = new ContainerWidget; |
306 |
|
307 |
// OLD - needed hidden elements are now recognized at 'render()' |
308 |
/* |
309 |
// get current request vars and add them ad hidden items |
310 |
$url_vars = $this->_get_current_url_vars(); |
311 |
foreach($url_vars as $label => $value) { |
312 |
$container->push(form_hidden($label, $value)); |
313 |
} |
314 |
*/ |
315 |
|
316 |
if ($cnt == 1) { |
317 |
//user only has 1 field to show. |
318 |
list($name, $field) = each($fields); |
319 |
$container->push("Find ".$name." "); |
320 |
} else { |
321 |
//user has many fields to show. |
322 |
$container->push("Find ", |
323 |
form_select($this->_vars["search_fieldVar"], $fields, $this->search_field())); |
324 |
} |
325 |
|
326 |
if ($this->get_simple_search_modifier()) { |
327 |
//the user wants the search modifier turned on. |
328 |
$container->push($this->_build_simple_search_modifier()); |
329 |
} |
330 |
|
331 |
$container->push(form_text($this->_vars["search_valueVar"], $this->search_value_filter($this->search_value()), "20", "100", array("style"=>"vertical-align:middle;")), |
332 |
form_submit($this->get_form_name(), "Search", array("style"=>"vertical-align:middle;"))); |
333 |
|
334 |
if ($this->is_advanced_search_enabled()) { |
335 |
$span = html_span(html_a("ass","Advanced Search", "title")); |
336 |
$container->push(" ", $span); |
337 |
} |
338 |
|
339 |
if ($cnt == 1) { |
340 |
$container->push(form_hidden($this->_vars["search_fieldVar"], $field)); |
341 |
} |
342 |
|
343 |
return $container; |
344 |
} |
345 |
|
346 |
|
347 |
/** |
348 |
* This function builds the data cell action buttons or main actionbar |
349 |
* |
350 |
* @return TDtag; |
351 |
*/ |
352 |
function _build_actionbar_data_cell() { |
353 |
$td = new TDtag(array("class" => "datalist_title", |
354 |
"style" => "padding-left:5px;padding-right:5px;". |
355 |
"padding-top:5px;padding-bottom:5px") |
356 |
); |
357 |
|
358 |
$flag = FALSE; |
359 |
if ($this->_has_action_column("FIRST")) { |
360 |
$flag = TRUE; |
361 |
$td->set_tag_attribute("align", "left"); |
362 |
} |
363 |
elseif ($this->_has_action_column("LAST")) { |
364 |
$flag = TRUE; |
365 |
$td->set_tag_attribute("align", "right"); |
366 |
} |
367 |
|
368 |
if ($flag && $this->_datasource->get_total_rows()) { |
369 |
$td->push($this->actionbar_cell()); |
370 |
$td->push($this->actionbar_cell_new()); |
371 |
} |
372 |
else { |
373 |
$td->push(" "); |
374 |
$td->push($this->actionbar_cell_new()); |
375 |
} |
376 |
|
377 |
return $td; |
378 |
} |
379 |
|
380 |
|
381 |
/** |
382 |
* this is the method that builds |
383 |
* the contents for the middle actionbar |
384 |
* td cell. |
385 |
* |
386 |
* @return ContainerWidget object |
387 |
*/ |
388 |
function actionbar_cell() { |
389 |
$action_list = $this->_options['actionbar']['list']; |
390 |
$action_name = $this->_options['actionbar']['name']; |
391 |
|
392 |
if($action_name && is_array($action_list)) { |
393 |
$action_form = container("Select action", _HTML_SPACE, |
394 |
form_select($action_name, $action_list, $this->_options['actionbar']['selected']), |
395 |
form_submit("ok","OK" ) |
396 |
); |
397 |
return $action_form; |
398 |
} else { |
399 |
return _HTML_SPACE; |
400 |
} |
401 |
} |
402 |
|
403 |
|
404 |
/** |
405 |
* this is the method that builds |
406 |
* the contents for the 'create new' actionbar |
407 |
* td cell. |
408 |
* |
409 |
* @return ContainerWidget object |
410 |
*/ |
411 |
function actionbar_cell_new() { |
412 |
$action_create = $this->_options['actionbar']['create']; |
413 |
|
414 |
if(is_array($action_create)) { |
415 |
$action_form = container( |
416 |
_HTML_SPACE, |
417 |
form_submit($action_create['name'], $action_create['value']) |
418 |
); |
419 |
return $action_form; |
420 |
} else { |
421 |
return _HTML_SPACE; |
422 |
} |
423 |
} |
424 |
|
425 |
|
426 |
/* |
427 |
* OLD (depreciated) - this method returns the current requested variables |
428 |
* |
429 |
* @return array |
430 |
*/ |
431 |
function _get_current_url_vars() { |
432 |
$cur_url_vars = array(); |
433 |
|
434 |
// get new LinkBuilder instance |
435 |
$linkbuilder = new LinkBuilder(); |
436 |
|
437 |
$url_vars = php::array_join_merge($_GET, $_POST); |
438 |
if(is_array($url_vars)) { |
439 |
foreach($url_vars as $key => $value) { |
440 |
// if there is an LinkBuilder guid instead of variables, encode it |
441 |
if($key == 'lbid') { |
442 |
$lb_vars = $linkbuilder->load($value); |
443 |
foreach($lb_vars as $lbkey => $lbval) { |
444 |
$cur_url_vars[$lbkey] = $lbval; |
445 |
} |
446 |
} else { |
447 |
$cur_url_vars[$key] = $value; |
448 |
} |
449 |
} |
450 |
} |
451 |
return $cur_url_vars; |
452 |
} |
453 |
|
454 |
|
455 |
/** |
456 |
* This function renders the final |
457 |
* widget |
458 |
* |
459 |
*/ |
460 |
function render($indent_level, $output_debug) { |
461 |
|
462 |
//setup the columns in their sorts |
463 |
$this->setup_columns(); |
464 |
|
465 |
//do any data prefetch work |
466 |
//which may be child specific |
467 |
if ( $this->_show_results() ) { |
468 |
$this->data_prefetch(); |
469 |
} |
470 |
|
471 |
//This function gives the child class |
472 |
//a chance to build the tables/divs/containers |
473 |
//that will be responsible for the look/feel |
474 |
//of the DataList |
475 |
$this->gui_init(); |
476 |
|
477 |
//see if we need to build the search area. |
478 |
if ( $this->is_search_enabled() || $this->is_advanced_search_enabled() ) { |
479 |
$this->_search_table = $this->child_build_search_table(); |
480 |
if ( $this->_search_table ) { |
481 |
$this->set_form_render(TRUE); |
482 |
} |
483 |
} |
484 |
|
485 |
//see if form is needed by the actionbar |
486 |
if($this->_show_actionbar) { |
487 |
$this->set_form_render(TRUE); |
488 |
} |
489 |
|
490 |
if ( $this->get_form_render() ) { |
491 |
$form = new FORMtag( array("method" => $this->get_form_method(), |
492 |
"action" => $this->build_base_url(), |
493 |
"name" => $this->get_form_name(), |
494 |
"style" => "margin-top: 0px;margin-bottom:0px;") ); |
495 |
|
496 |
$target = $this->get_form_target(); |
497 |
if ( $target != NULL ) |
498 |
$form->set_tag_attribute("target",$target); |
499 |
|
500 |
$action = $this->get_form_action(); |
501 |
if ( $action != NULL ) |
502 |
$form->set_tag_attribute("action",$action); |
503 |
|
504 |
//now build the UI and return it |
505 |
$form->add( $this->build_gui() ); |
506 |
|
507 |
} else { |
508 |
$form = container(); |
509 |
|
510 |
//now build the UI and return it |
511 |
$form->add( $this->build_gui() ); |
512 |
} |
513 |
|
514 |
//add the hidden vars if we are a POST |
515 |
if ($this->get_form_method() == "POST") { |
516 |
$form->add( $this->_build_default_vars() ); |
517 |
|
518 |
//build hidden elements needed for data flow control |
519 |
$hidden = $this->_hidden_elements; |
520 |
//print "Hidden: " . Dumper($hidden); |
521 |
if($hidden) { |
522 |
foreach($hidden as $label => $value) { |
523 |
$form->add(form_hidden($label, $value)); |
524 |
} |
525 |
} |
526 |
|
527 |
} |
528 |
|
529 |
//add the save vars the user wants. |
530 |
$form->add( $this->_build_save_vars() ); |
531 |
|
532 |
//add any javascript required |
533 |
$container = container( $this->_javascript(), $form ); |
534 |
return $container->render( $indent_level, $output_debug ); |
535 |
} |
536 |
|
537 |
|
538 |
|
539 |
function child_add_row_cell($obj, $col_name, $last_in_row_flag, $row_data) { |
540 |
if (!$this->_cur_col_cntr) { |
541 |
$this->_cur_col_cntr = 1; |
542 |
//lets see if we need to add an action column |
543 |
//as the first column. |
544 |
if ($this->_has_action_column("FIRST")) { |
545 |
//looks like we have a FIRST column actionbar |
546 |
//lets add it |
547 |
$this->_data_row->add($this->_build_action_column("FIRST", FALSE, $row_data)); |
548 |
} |
549 |
$td = $this->wrap_column_item($obj, $col_name, $row_data); |
550 |
$this->_data_row->add( $td ); |
551 |
} else { |
552 |
//show the normal data |
553 |
$td = $this->wrap_column_item($obj, $col_name, $row_data); |
554 |
$this->_data_row->add( $td ); |
555 |
} |
556 |
|
557 |
|
558 |
if ($last_in_row_flag) { |
559 |
//lets see if we need to add an action column |
560 |
//as the first column. |
561 |
if ($this->_has_action_column("LAST")) { |
562 |
//looks like we have a LAST column actionbar |
563 |
//lets add it |
564 |
$this->_data_row->add($this->_build_action_column("LAST", FALSE, $row_data)); |
565 |
} |
566 |
|
567 |
|
568 |
|
569 |
$this->_data_table->add_row( $this->_data_row ); |
570 |
$this->_data_row = new TRtag; |
571 |
unset( $this->_cur_col_cntr ); |
572 |
} else { |
573 |
$this->_cur_col_cntr++; |
574 |
} |
575 |
} |
576 |
|
577 |
} |
578 |
?> |