Chromium.pm untested

filter: Statementstmt 0.0% 0 / 989 Branchbran 0.0% 0 / 558 Conditioncond 0.0% 0 / 460 MC/DCmcdc 0.0% 0 / 238 Subroutinesub 0.0% 0 / 100 Podpod 0.0% 0 / 77 totaltotal 0.0% 0 / 2422 CC 280 SCAR 59.9
CC
280
Cov
0%
flow
1892.0
get_data
1332.0
save_data
240.0
CRAP 399.8
1 # BingoX::Chromium
2 # -----------------
3 # $Revision: 2.36 $
4 # $Date: 2001/11/14 23:12:26 $
5 # ---------------------------------------------------------
6  
7 =head1 NAME
8  
9 BingoX::Chromium - Generic BingoX Admin module
10  
11 =head1 SYNOPSIS
12  
13 use BingoX::Chromium;
14  
15 # $BR - Blessed Reference
16 # $SV - Scalar Value
17 # @AV - Array Value
18 # $HR - Hash Ref
19 # $AR - Array Ref
20 # $SR - Stream Ref
21  
22 # $proto - BingoX::Chromium object OR sub-class
23 # $object - BingoX::Chromium object
24  
25 CONSTRUCTORS
26  
27 $BR = $proto->new( $r, [ $conf ] );
28  
29 DISPLAY METHODS
30  
31 $SV = $proto->postmodify_handler();
32 $SV = $proto->postadd_handler();
33 $SV = $object->display_list();
34 $SV = $object->display_view();
35 $SV = $object->display_modify();
36 $SV = $object->display_search(); - Not Implemented Yet!
37 $SV = $object->display_row( $field );
38 $SV = $object->display_list_buttons();
39 $SV = $object->display_modify_buttons();
40 $SV = $object->displat_start_html();
41 $SV = $object->hidden_fields();
42  
43 DATA METHODS
44  
45 $SV = $object->save_data();
46 $HR = $object->get_data( [ $data, ] [ $fields ] );
47 $SV = $object->sanity();
48 $BR = $proto->dbh();
49 $BR = $object->db_obj();
50 $HR = $object->get_list_hash();
51  
52 CLASS VARIABLE METHODS
53  
54  
55 $SV = $proto->data_class();
56 $SV = $proto->data_class_name();
57 $SV = $proto->adminuri();
58 $SV = $proto->classdesc();
59 $SV = $proto->qfd();
60 $SV = $proto->pkd();
61 $SV = $proto->prefix();
62 $SV = $proto->cpkey_params($pcpkey);
63 $AR = $proto->fieldlist();
64 $HR = $proto->ui();
65 $AR = $proto->parents();
66 $AR = $proto->children();
67 $HR = $proto->fields();
68 $SV = $proto->fieldname( $field );
69 $SV = $proto->fieldtype( $field );
70 $HR = $proto->fieldhtmloptions( $field );
71 $SV = $proto->fieldrelclass( $field );
72 $SV = $proto->fieldrelclasstype( $field );
73 $HR = $proto->fieldoptions( $field );
74 $HR = $proto->fieldsanity( $field );
75  
76 OBJECT METHODS
77  
78 $SV = $object->flow();
79 $SV = $object->cpkey();
80 $SV = $object->pcpkey();
81 $SV = $object->parent_class();
82 $BR = $object->cgi();
83 $SV = $object->uri();
84 $SV = $object->displaymode();
85 $SV = $object->section();
86 $BR = $object->conf();
87 $BR = $object->r();
88  
89 HTML DISPLAY METHODS
90  
91 $SV = $object->HTML_date( $fieldname );
92 $SV = $object->HTML_view( $fieldname );
93 $SV = $object->HTML_hidden( $fieldname );
94 $SV = $object->HTML_text( $fieldname );
95 $SV = $object->HTML_textarea( $fieldname );
96 $SV = $object->HTML_popup( $fieldname ); - Not Finished!
97 $SV = $object->HTML_scrolling( $fieldname );
98 $SV = $object->HTML_checkbox( $fieldname );
99 $SV = $object->HTML_file( $fieldname );
100  
101 =head1 REQUIRES
102  
103 Time::Object, Apache, CGI, Carp
104  
105 =head1 EXPORTS
106  
107 Nothing
108  
109 =head1 DESCRIPTION
110  
111 BingoX::Chromium provides the generic API for BingoX admin classes.
112 BingoX::Chromium uses admin objects that wrap Carbon data objects
113  
114 =head1 CLASS VARIABLES
115  
116 Classes that inherit from BingoX::Chromium should have the following class variables:
117  
118 =over 4
119  
120 =item * @fieldlist
121  
122 The order in which to display fields (if one so chose to display them ;)
123  
124 =item * %fields
125  
126 A hash whose keys are columns (same as in the fieldlist array) and whose values
127 are complex arrays. Each array index is described below.
128  
129 =over 4
130  
131 =item [0] Descriptive Title
132  
133 A string describing the field
134  
135 =item [1] HTML Entity Type
136  
137 A string that contains one of the following HTML etity types:
138  
139 view
140 text
141 textarea
142 datetime
143 popup
144 reference
145 FIXME: more!!!
146  
147 =item [2] HTML Options
148  
149 A hash reference containing options for creating the HTML form field for
150 this field.
151  
152 =item [3] Related Classes
153  
154 FIXME: I dunno
155  
156 =item [4] Field Options
157  
158 A hash reference containing special options for this field. The options include:
159  
160 not_null - this field cannot be NULL
161  
162 =item [5] Sanity Methods
163  
164 This is a list reference. Each item in the list is either a string with a
165 sanity method name, or a list ref containing the method name, then any parameters
166 that need to be passed to it. An example:
167  
168 [
169 'sane_foo',
170 ['sane_bar', 'baz'],
171 ]
172  
173 For more info, see L<"SANITY METHODS">.
174  
175 =back
176  
177 =item * $adminuri
178  
179 An optional class variable, corresponds to the C<adminuri()> method.
180 FIXME: Can someone who knows more about this elaborate?
181  
182 =item * $classdesc
183  
184 A simple one- or two-word description of the class being administered.
185  
186 =back
187  
188 =item * $data_class
189  
190 The name of the data class that corresponds to the admin class. If this
191 value is not defined, it will default to the name of the admin class, with
192 the first instance of "::Admin::" changed to "::Data::".
193  
194 =back
195  
196 =head1 METHODS
197  
198 =over 4
199  
200 =cut
201  
202 package BingoX::Chromium;
203  
204 use Apache;
205 use Apache::Constants qw(:response);
206 use BingoX::Time;
207  
208 use Carp;
209 use strict;
210 use vars qw($AUTOLOAD $debug);
211  
212 BEGIN {
213 $BingoX::Chromium::REVISION = (qw$Revision: 2.36 $)[-1];
214 $BingoX::Chromium::VERSION = '1.92';
215  
216 $debug = undef;
217  
218 if ($debug) {
219 require Data::Dumper;
220 }
221 }
222  
223 =item C<handler> ( $r )
224  
225 Apache handler gets a new display object object of the class B<it was called
226 as> and calls flow against it.
227  
228 =cut
229  
230 sub handler ($$) {
231 warn "\n******************** BEGIN CLICK ************************\n\n" if ($debug);
232  
233 my $class = shift;
234 my $r = shift;
235  
236 # Prepare request handler for uncached HTML response
237 $r->content_type('text/html');
238 $r->no_cache(1);
239  
240 my $self = $class->new( $r );
241 my $response = $self->flow;
242  
243 warn "\n******************** END CLICK **************************\n\n" if ($debug);
244 return $response;
245 } # END sub handler
246  
247 =back
248  
249 =head2 CONSTRUCTORS
250  
251 =over 4
252  
253 =item C<new> ( $r [, $conf [, $mode ] ] )
254  
255 Given an apache request object, returns an Admin object of the class
256 B<it was called as>. It also sets the data_class, data_class_name, cgi,
257 uri, displaymode (from $q), and section (from $q).
258  
259 =cut
260  
261 sub new {
262 my ($class, $r, $conf, $mode, $cgi) = @_;
263 my $q = $cgi || new CGI;
264 my $self = {
265 _data_class => $class->data_class(),
266 _data_class_name => $class->data_class_name(),
267 _cgi => $q,
268 _conf => $conf || undef,
269 _uri => $r->uri,
270 _r => $r, # Apache request object
271 _displaymode => lc($q->param('displaymode')) || $mode || '',
272 _errors => { },
273 _section => $q->param('section') || '',
274 };
275 if ($q->param('parent_pcpkey')) {
276 my $parentclass = "${1}::Admin::" . substr($q->param('parent_pcpkey'),0,index($q->param('parent_pcpkey'),$class->qfd));
277 $self->{'_parent_class'} = $parentclass;
278 $self->{'_selection'} = $parentclass->cpkey_params($q->param('parent_pcpkey'));
279 }
280 bless $self, $class;
281 return $self
282 } # END sub new
283  
284  
285 =back
286  
287 =head2 FLOW
288  
289 =over 4
290  
291 =item C<flow> ( )
292  
293 Decides what display method (or save_data) to call based on the displaymode,
294 and submit_type (found in the query object).
295  
296 ?? Is this the best order to check ??
297  
298 =cut
299  
300 sub flow {
301 my $self = shift;
302 return $self->failure('Method flow called as static.') unless ref($self);
303 my $class = ref($self);
304 my $q = $self->cgi;
305  
306  
307 my $other_class;
308 if ($other_class = (grep { /^child##(.+)$/ } ($q->param))[0]) {
309 $q->delete($other_class);
310 $other_class = substr($other_class,7);
311  
312 warn "flow - displaymode child\n" if ($debug > 1);
313  
314 my $pcpkey = $q->param('cpkey');
315 $q->param('parent_pcpkey',$pcpkey);
316 my $new_class = $class;
317 substr($new_class,rindex($new_class,':')+1) = $other_class;
318  
319 my $new_admin = $new_class->new($self->r,undef,undef,$q);
320 $new_admin->flow;
321  
322 } elsif ($other_class = (grep { /^parent##(.+)$/ } ($q->param))[0]) {
323 $q->delete($other_class);
324 $other_class = substr($other_class,8); warn "flow - displaymode parent\n" if ($debug > 1);
325  
326 my $new_class = $class;
327 substr($new_class,rindex($new_class,':')+1) = $other_class;
328 $q->delete('parent_pcpkey');
329 my $new_admin = $new_class->new($self->r,undef,undef,$q);
330 $new_admin->flow;
331  
332 } elsif ($self->displaymode eq 'modify') {
333 $self->{'_displaymode'} = 'modify';
334 warn "flow - displaymode modify\n" if ($debug > 1);
335 if ($q->param('submit_type') eq 'Save') {
336 if ($self->save_data) {
337 warn "flow modify modify after MODIFY\n" if ($debug > 1);
338 $self->{'_displaymode'} = 'view';
339 $self->postmodify_handler('display_view');
340 } else {
341 warn "flow modify modify MODIFY FAILED\n" if ($debug > 1);
342 $self->display_modify;
343 }
344 } elsif ($q->param('submit_type') eq 'Cancel') {
345 $self->{'_displaymode'} = 'view';
346 $self->postmodify_handler('display_view');
347 } else {
348 $self->failure('Not a valid submit type.');
349 }
350  
351 } elsif ($self->displaymode eq 'view') {
352 warn "flow - displaymode view\n" if ($debug > 1);
353 if ($q->param('submit_type') eq 'Modify') {
354 $self->{'_displaymode'} = 'modify';
355 $self->display_modify;
356 } elsif ($q->param('submit_type') eq 'Remove') {
357 if ($self->db_obj->rm) {
358 $self->postmodify_handler('display_list');
359 } else {
360 $self->{'_errors'}->{'General Database Error'} = $self->dbh->errstr;
361 $self->display_modify;
362 }
363 } elsif ($q->param('submit_type') eq 'Return to List') {
364 $self->postmodify_handler('display_list');
365 } else {
366 $self->failure('Not a valid submit type.');
367 }
368  
369 } elsif ($self->displaymode eq 'add') {
370 warn "flow - displaymode add\n" if ($debug > 1);
371 if ($q->param('submit_type') eq 'Save') {
372 if ($self->save_data) {
373 warn "flow add add after ADD\n" if ($debug > 1);
374 $self->{'_displaymode'} = 'view';
375 $self->postadd_handler('display_view');
376 } else {
377 warn "flow add add ADD FAILED\n" if ($debug > 1);
378 $self->display_modify;
379 }
380 } else {
381 $self->postadd_handler('display_list');
382 }
383  
384 } else {
385 warn "flow - displaymode none\n" if ($debug > 1);
386  
387 if ($q->param('submit_type') eq 'View') {
388 warn "flow - displaymode none - submit_type view\n" if ($debug > 1);
389 if (ref $self->db_obj) {
390 warn "flow - displaymode none - submit_type view - we have db_obj\n" if ($debug > 1);
391 $self->{'_displaymode'} = 'view';
392 $self->display_view;
393 } else {
394 warn "no db_obj\n" if ($debug > 1);
395 $self->{'_displaymode'} = 'list';
396 $self->display_list;
397 }
398 } elsif ($q->param('submit_type') eq 'Remove') {
399 warn "flow - displaymode none - submit_type Remove\n" if ($debug > 1);
400 if ($self->db_obj->rm) {
401 warn "flow - displaymode none - submit_type Remove SUCCEED\n" if ($debug > 1);
402 $self->{'_displaymode'} = 'list';
403 $self->display_list;
404 } else {
405 warn "flow - displaymode none - submit_type Remove FAIL\n" if ($debug > 1);
406 $self->{'_errors'}->{'General Database Error'} = $self->dbh->errstr;
407 $self->{'_displaymode'} = 'list';
408 $self->display_list;
409 }
410 } elsif ($q->param('submit_type') eq 'Modify') {
411 warn "flow - displaymode none - submit_type modify\n" if ($debug > 1);
412 if (ref $self->db_obj) {
413 warn "flow - displaymode none - submit_type modify - we have db_obj\n" if ($debug > 1);
414 $self->{'_displaymode'} = 'modify';
415 $self->display_modify;
416 } else {
417 warn "no db_obj\n" if ($debug > 1);
418 $self->{'_displaymode'} = 'list';
419 $self->display_list;
420 }
421 } elsif ($q->param('submit_type') eq 'Add') {
422 warn "flow - displaymode none - submit type: add\n" if ($debug > 1);
423 $q->delete('cpkey'); #sometimes it sticks around if users select something in "display_list" and hit "new".
424 $self->{'_displaymode'} = 'add';
425 $self->display_modify;
426 } else {
427 $self->display_list;
428 }
429 }
430 } # END sub flow
431  
432  
433 sub failure {
434 my $self = shift;
435 return SERVER_ERROR unless ref($self);
436 my $message = shift || return SERVER_ERROR;
437 my $q = $self->cgi;
438 my $content = $q->starthtml(-title=>'Chromium Error')
439 . "<H2 ALIGN=center>Chromium Error</H2><BR>\n"
440 . "Error in Chromium-based Admin class: <FONT COLOR='blue'>" . ref($self) . "</FONT><BR>\n"
441 . "<FONT COLOR='red'>" . $message . "</FONT><BR>\n"
442 . $q->endhtml . "\n";
443 $self->r->custom_response(SERVER_ERROR => $content);
444 return SERVER_ERROR;
445 }
446  
447 =item C<postmodify_handler> ( $method_name, [ $method_name_args ] )
448  
449 This method decides what happens after you leave the "Modify"
450 screen. It normally works by passing a method that gets called, but
451 it's meant to be overloaded as a hook when flow() finishes saving data
452 (or if you hit Cancel). That way you can save something and go
453 somewhere else besides display_view(). It currently will always call
454 $method against $self, so no static methods, please...
455  
456 =cut
457  
458 sub postmodify_handler {
459 my $self = shift;
460 my $method = shift || 'display_view';
461 $self->$method( @_ );
462 } # END sub postmodify_handler
463  
464  
465 =item C<postadd_handler> ( $method_name, [ $method_name_args ] )
466  
467 Behaves exactly like postmodify_handler() (see above), but is called after
468 the user exits the "Add a new <whatever>" screen.
469  
470 =cut
471  
472 sub postadd_handler {
473 my $self = shift;
474 my $method = shift || 'display_view';
475 $self->$method( @_ );
476 } # END sub postadd_handler
477  
478  
479 =back
480  
481 =head2 DISPLAY METHODS
482  
483 =over 4
484  
485 =item C<display_list> ( )
486  
487 Prints an HTML page meant for listing all objects in class.
488  
489 =cut
490  
491 sub display_list {
492 my $self = shift;
493 my $q = $self->cgi;
494 $self->{'_displaymode'} = 'list';
495 my $r = $self->r;
496 my $ui = $self->ui;
497  
498 unless (ref $r) {
499 print $q->header;
500 }
501  
502 print $self->display_start_html({ -TITLE => 'Admin '. $self->classdesc,
503 -BGCOLOR => $ui->{'page_bg'}
504 });
505  
506 my $hashref = $self->get_list_hash;
507 print $q->startform( -ACTION => $self->adminuri || $self->uri,
508 -METHOD =>'POST'
509 )
510 . $q->p . "\n"
511 . $q->center
512 . $q->start_table({ -BORDER => 0,
513 -CELLSPACING => 0,
514 -CELLPADDING => 1,
515 -WIDTH => 500,
516 -BGCOLOR => $ui->{'table_border'}
517 })
518 . $q->start_Tr
519 . $q->start_td
520 . $q->start_table({ -BORDER => 0,
521 -CELLSPACING => 1,
522 -CELLPADDING => 4,
523 -WIDTH => 500,
524 -BGCOLOR => 'white'
525 })
526 . $q->start_Tr
527 . $q->start_td({ -ALIGN => 'center', -BGCOLOR => $ui->{'table_header' }})
528 . $q->start_b
529 . $q->start_font({
530 -FACE => $ui->{'font_header'}->{'-face'},
531 -SIZE => 5
532 })
533 . $self->display_admin_name . "List "
534 . $self->classdesc . "<BR>\n";
535  
536 if ($q->param('parent_pcpkey')) {
537 my $parent_class = $self->parent_class;
538 my $pclass_tfield = $parent_class->data_class->title_field;
539 print $q->font( { -SIZE => 4 },
540 "For " . $parent_class->classdesc . ": "
541 . $q->font( { -COLOR => 'blue' },
542 $parent_class->data_class->get(
543 $self->dbh,
544 $parent_class->cpkey_params(
545 $q->param('parent_pcpkey')
546 )
547 )->$pclass_tfield()
548 )
549 ) . "<BR>\n";
550 }
551 print $q->end_font . $q->br . $q->a(
552 { -HREF => $self->main_index },
553 [ 'Back to Main Index' ]
554 ) . "<BR>\n"
555 . $q->end_b
556 . $q->end_td
557 . $q->end_Tr
558  
559 . $q->start_Tr
560 . $q->start_td({ -ALIGN => 'center',
561 -BGCOLOR => $ui->{'row_value'}
562 }) . "\n"
563 . $q->p . "\n"
564 . $q->scrolling_list( -NAME =>'cpkey',
565 -VALUES => [ sort { $hashref->{$a} cmp $hashref->{$b} } keys %$hashref ],
566 -LABELS => $hashref,
567 -SIZE => 15, # should be setable
568 -MULTIPLE => 'true', # should be setable (why true?)
569 -OVERRIDE => 1
570 ) . "\n"
571 . $q->br
572 . $q->font($ui->{'font_footer'}, "Select an item and press View. <BR>Or press Add to add a new item\n")
573  
574 . $q->end_td
575 . $q->end_Tr
576  
577 . $q->start_Tr
578 . $q->start_td({ -ALIGN => 'center',
579 -BGCOLOR => $ui->{'table_footer'}
580 })
581  
582 . $q->start_table({ -WIDTH => '100%',
583 -BORDER => 0,
584 -CELLPADDING => 0,
585 -CELLSPACING => 0
586 })
587 . $q->start_Tr
588 . $q->start_td({ -WIDTH => '35%',
589 -ALIGN => 'left'
590
591 })
592 . $q->end_td
593 . $q->start_td({ -WIDTH => '30%',
594 -ALIGN => 'center'
595 })
596  
597 . $self->display_list_buttons
598  
599 . $q->end_td
600 . $q->start_td({ -WIDTH => '35%',
601 -ALIGN =>'right',
602 -VALIGN =>'bottom'
603 })
604 . $q->font({ -FACE => 'verdana,arial,helvetica',
605 -SIZE => 1
606 },
607 'Powered by: BingoX')
608  
609 . $q->end_td
610 . $q->end_Tr
611 . $q->end_table
612  
613 . $q->end_td
614 . $q->end_Tr
615 . $q->end_table
616  
617 . $q->end_td
618 . $q->end_Tr
619 . $q->end_table
620 . $q->hidden( -NAME => 'displaymode',
621 -VALUE => $self->displaymode,
622 -OVERRIDE => 1
623 ) . "\n"
624 . $q->hidden( -NAME => 'section',
625 -VALUE => $self->section,
626 -OVERRIDE => 1
627 ) . "\n"
628 . $q->hidden( -NAME => 'parent_pcpkey')
629 . $self->hidden_fields
630 . $q->endform . "\n"
631 . $q->end_center
632 . $q->end_html . "\n";
633  
634 return OK;
635 } # END sub display_list
636  
637  
638 =item C<display_list_buttons> ( )
639  
640 Displays the buttons for display_list().
641 Easy to overload if you want more buttons!
642  
643 =cut
644  
645 sub display_list_buttons {
646 my $self = shift;
647 return undef unless (ref $self);
648 my $q = $self->cgi;
649 my $html = "<CENTER>";
650 if ($self->parents) {
651 foreach (@{ $self->parents }) {
652 $html .= $q->submit(-NAME => "parent##$_", -VALUE => "Back to ${_}");
653 }
654 $html .= '<BR>';
655 }
656 if ($self->children) {
657 foreach (@{ $self->children }) {
658 $html .= $q->submit(-NAME => "child##$_", -VALUE => "Show ${_}");
659 }
660 $html .= '<BR>';
661 }
662  
663 return $html
664 . $q->submit( -NAME => 'submit_type',
665 -VALUE => 'Remove',
666 -onClick => "if (confirm('Are you sure you want to remove this record?')) { return true } else { return false }"
667 )
668 . $q->submit( -NAME => 'submit_type',
669 -VALUE => 'Add'
670 )
671 . $q->submit( -NAME => 'submit_type',
672 -VALUE => 'View'
673 )
674 . $q->submit( -NAME => 'submit_type',
675 -VALUE => 'Modify'
676 )
677 . "</CENTER>\n"
678 } # END sub display_list_buttons
679  
680  
681 =item C<display_modify_buttons> ( )
682  
683 Displays the buttons for display_modify().
684 Easy to overload if you want more buttons!
685 Just like display_list buttons.
686  
687 =cut
688  
689 sub display_modify_buttons {
690 my $self = shift;
691 # return undef unless ref($self);
692 my $q = $self->cgi;
693 return "<CENTER>"
694 . $q->submit( -NAME => 'submit_type',
695 -VALUE => 'Cancel'
696 )
697 . $q->submit( -NAME => 'submit_type',
698 -VALUE => 'Save'
699 )
700 . "</CENTER>\n"
701 } # END sub display_modify_buttons
702  
703  
704 =item C<display_view> ( )
705  
706 Prints an HTML page meant for viewing an object. Itterates through fieldlist
707 calling display_row for each element.
708  
709 =cut
710  
711 sub display_view {
712 my $self = shift;
713 my $q = $self->cgi;
714 my $fields = $self->fields;
715 my $r = $self->r;
716 my $ui = $self->ui;
717 $self->{'_displaymode'} = 'view';
718  
719 unless (ref $r) {
720 print $q->header;
721 }
722  
723 print $self->display_start_html({ -TITLE => 'Admin '. $self->classdesc,
724 -BGCOLOR => $ui->{'page_bg'}
725 })
726 . $q->startform( -ACTION => $self->adminuri || $self->uri,
727 -METHOD => 'POST',
728 -NAME => 'displayform'
729 )
730 . $q->start_center
731 . $q->start_table({ -BORDER => 0,
732 -CELLSPACING => 0,
733 -CELLPADDING => 1,
734 -WIDTH => 500,
735 -BGCOLOR => $ui->{'table_border'}
736 })
737 . $q->start_Tr
738 . $q->start_td
739 . $q->start_table({ -BORDER => 0,
740 -CELLSPACING => 1,
741 -CELLPADDING => 4,
742 -WIDTH => 500,
743 -BGCOLOR => 'white'
744 })
745 . $q->start_Tr
746 . $q->start_td({ -COLSPAN => 2,
747 -BGCOLOR => $ui->{'table_header'}
748 })
749 . $q->b($q->font($ui->{'font_header'},$q->center($self->display_admin_name . "View " . $self->classdesc. "<BR>\n")))
750 . $q->end_td
751 . $q->end_Tr;
752  
753 foreach (@{ $self->fieldlist }) {
754 $self->display_row( $_ ); # here's where the magic happens
755 }
756  
757 print $q->start_Tr
758 . $q->start_td({ -COLSPAN => 2,
759 -ALIGN => 'center',
760 -BGCOLOR => $ui->{'table_footer'}
761 })
762  
763 . $q->start_table({ -WIDTH => '100%',
764 -BORDER => 0,
765 -CELLPADDING => 0,
766 -CELLSPACING => 0
767 })
768  
769 . $q->start_Tr
770 . $q->start_td({ -WIDTH =>'35%',
771 -ALIGN => 'left'
772 })
773 . $q->end_td
774 . $q->start_td({ -WIDTH => '30%',
775 -ALIGN => 'center'
776 })
777 . $self->display_view_buttons()
778  
779 . $q->end_td
780 . $q->start_td({ -WIDTH => '35%',
781 -ALIGN => 'right',
782 -VALIGN => 'bottom'
783 })
784  
785 . $q->font({ -FACE => 'verdana,arial,helvetica',
786 -SIZE => 1
787 },
788 'Powered by: BingoX')
789
790 . $q->end_td
791 . $q->end_Tr
792 . $q->end_table
793  
794 . $q->end_td
795 . $q->end_Tr
796 . $q->end_table
797  
798 . $q->end_td
799 . $q->end_Tr
800 . $q->end_table
801
802 . $q->hidden( -NAME => 'section',
803 -VALUE => $self->section,
804 -OVERRIDE => 1
805 )
806 . $q->hidden( -NAME => 'cpkey',
807 -VALUE => $self->pcpkey,
808 -OVERRIDE => 1
809 )
810 . $q->hidden( -NAME => 'displaymode',
811 -VALUE => 'view',
812 -OVERRIDE => 1
813 )
814 . $q->hidden( -NAME => 'parent_pcpkey')
815 . $self->hidden_fields
816 . $q->endform . "\n"
817 . $q->end_center
818 . $q->end_html . "\n";
819  
820 return OK;
821 } # END sub display_view
822  
823  
824 =item C<display_view_buttons> ( )
825  
826 Displays the buttons for display_view().
827 Easy to overload if you want more buttons!
828 Just like display_list buttons.
829  
830 =cut
831  
832 sub display_view_buttons {
833 my $self = shift;
834 # return undef unless ref($self);
835 my $q = $self->cgi;
836 return "<CENTER>"
837 . $q->submit( -NAME => 'submit_type',
838 -VALUE => 'Remove',
839 -onClick => "if (confirm('Are you sure you want to remove this record?')) { return true } else { return false }"
840 )
841 . $q->submit( -NAME => 'submit_type',
842 -VALUE => 'Modify'
843 )
844 . $q->br
845 . $q->submit( -NAME => 'submit_type',
846 -VALUE => 'Return to List'
847 )
848 . "</CENTER>\n"
849 } # END sub display_view_buttons
850  
851
852 =item C<display_modify> ( )
853  
854 Prints an HTML page meant for modifying an object. Itterates through fieldlist
855 calling display_row for each element.
856  
857 =cut
858  
859 sub display_modify {
860 my $self = shift;
861 my $q = $self->cgi;
862 my $errors = shift;
863 my $db_errors = $self->db_obj ? $self->db_obj->errors : '';
864 my $r = $self->r;
865 my $ui = $self->ui;
866  
867 unless (ref $r) {
868 print $q->header;
869 }
870  
871 print $self->display_start_html({ -TITLE => 'Admin '. $self->classdesc,
872 -BGCOLOR => $ui->{'page_bg'}
873 })
874 . $q->start_multipart_form( -ACTION => $self->adminuri || $self->uri,
875 -METHOD => 'POST')
876 . $q->start_center
877 . $q->start_table({ -BORDER => 0,
878 -CELLSPACING => 0,
879 -CELLPADDING => 1,
880 -WIDTH => 500,
881 -BGCOLOR => $ui->{'table_border'}
882 })
883 . $q->start_Tr
884 . $q->start_td
885 . $q->start_table({ -BORDER => 0,
886 -CELLSPACING => 1,
887 -CELLPADDING => 4,
888 -WIDTH => 500,
889 -BGCOLOR => 'white'
890 })
891 . $q->start_Tr
892 . $q->start_td({ -COLSPAN => 2,
893 -BGCOLOR => $ui->{'table_header'}
894 })
895 . $q->b(
896 $q->font(
897 $ui->{'font_header'},
898 $q->center(
899 $self->display_admin_name . (
900 ($self->displaymode eq 'add')
901 ? "Add "
902 : "Modify "
903 ) . $self->classdesc. "<BR>\n"
904 )
905 )
906 );
907  
908 if (%{ $self->{'_errors'} }) {
909 print $q->p . $q->font($ui->{'font_key'},"There are errors in the following fields:\n")
910 . '<BR><UL>';
911 if (ref $self->{'_errors'} eq 'HASH') {
912 foreach (keys %{ $self->{'_errors'} }) {
913 print $q->li($q->font($ui->{'font_error'}, ($self->fieldname($_) || $_) . ': ' . $self->{'_errors'}->{$_}));
914 }
915 } else {
916 print $q->li($q->font($ui->{'font_error'},$self->fieldname($_) . ': ' . $self->{'_errors'}));
917 }
918 # map { print "<LI>" . $_ . "\n"} keys %{$self->{'_errors'}};
919 print "</UL><br>\n";
920 } elsif (ref $db_errors) {
921 print $q->p . $q->font($ui->{'font_key'},"The following errors occured:\n")
922 . '<BR><UL>';
923 foreach (keys %{ $db_errors }) {
924 print $q->li . $q->font($ui->{'font_error'},$_ . ': ' . $db_errors->{$_} . "\n");
925 }
926 print "</UL><br>\n";
927 } else {
928 print $q->font($ui->{'font_key'},'<P ALIGN="CENTER">An asterisk ("<B>*</B>") denotes a required field.</P>');
929 }
930  
931 print $q->end_td
932 . $q->end_Tr;
933  
934 foreach (@{ $self->fieldlist }) {
935 next if (($self->displaymode eq 'add') && ($_ eq $self->data_class->primary_keys->[0]));
936 $self->display_row( $_ );
937 }
938  
939 print $q->start_Tr
940 . $q->start_td({ -COLSPAN => 2,
941 -ALIGN => 'center',
942 -BGCOLOR => $ui->{'table_footer'}
943 })
944  
945 . $q->start_table({ -WIDTH => '100%',
946 -BORDER => 0,
947 -CELLPADDING => 0,
948 -CELLSPACING => 0
949 })
950 . $q->start_Tr
951 . $q->start_td({ -WIDTH => '35%',
952 -ALIGN => 'center'
953 })
954 . $q->end_td
955 . $q->start_td({ -WIDTH => '30%',
956 -ALIGN => 'center'
957 })
958  
959 . $self->display_modify_buttons
960  
961 . $q->end_td
962 . $q->start_td({ -WIDTH => '35%',
963 -ALIGN => 'right',
964 -VALIGN => 'bottom'
965 })
966 . $q->font({ -FACE => 'verdana,arial,helvetica',
967 -SIZE => 1
968 },
969 'Powered by: BingoX')
970  
971 . $q->end_td
972 . $q->end_Tr
973 . $q->end_table
974  
975 . $q->end_td
976 . $q->end_Tr
977 . $q->end_table
978  
979 . $q->end_td
980 . $q->end_Tr
981 . $q->end_table
982 . $q->hidden( -NAME => 'cpkey',
983 -VALUE => $self->pcpkey,
984 -OVERRIDE => 1
985 )
986 . $q->hidden( -NAME => 'section',
987 -VALUE => $self->section,
988 -OVERRIDE => 1
989 )
990 . $q->hidden( -NAME => 'displaymode',
991 -VALUE => $self->displaymode,
992 -OVERRIDE => 1
993 )
994 . $q->hidden( -NAME => 'parent_pcpkey')
995 . $self->hidden_fields
996 . $q->endform . "\n"
997 . $q->end_center
998 . $q->end_html . "\n";
999  
1000 return OK;
1001 } # END sub display_modify
1002  
1003  
1004 =item C<display_admin_name> ( )
1005  
1006 Returns class defined Class description as a string.
1007  
1008 =cut
1009  
1010 sub display_admin_name {
1011 no strict 'refs';
1012 my $self = shift;
1013 my $class = ref($self) || $self;
1014 my $q = $self->cgi;
1015 warn "display_admin_name ==> " . $class->admin_name . "\n" if ($debug > 3);
1016 return defined($class->admin_name) ? $class->admin_name . $q->br : undef;
1017 } # END sub admin_name
1018  
1019  
1020 =item C<hidden_fields> ( )
1021  
1022 A stub called by display_view() and display_modify()
1023 that you can overload in your subclass if you want
1024 the view or modify/add screens to have a custom title.
1025  
1026 =cut
1027  
1028 sub admin_name { }
1029  
1030  
1031 =item C<hidden_fields> ( \%fields )
1032  
1033 A stub called by display_view() and display_modify()
1034 that you can overload in your subclass if you want
1035 the view or modify/add screens to have extra hidden fields.
1036  
1037 Or you can pass it a hash ref of hidden fields,
1038 were key = field name and value = field value
1039  
1040 =cut
1041  
1042 sub hidden_fields {
1043 my $self = shift;
1044 my $fields = shift;
1045 ref($fields) || return undef;
1046 my $string;
1047 foreach (keys %$fields) {
1048 $string .= $self->cgi->hidden( -NAME => $_,
1049 -VALUE => $fields->{$_});
1050 }
1051 return $string;
1052 } # END sub admin_name
1053  
1054  
1055 =item display_start_html( \%params )
1056  
1057 Accessor method to $self->cgi->start_html();
1058 Put into params exactly what you'd put in CGI::start_html().
1059 Overload this in your admin class if you want to set any special
1060 BG,text,link colors, or anything else you want to pass to start_html()
1061  
1062 =cut
1063  
1064 sub display_start_html {
1065 my $self = shift;
1066 my $options = shift || { };
1067 my $ui = $self->ui;
1068 $options->{'-bgcolor'} ||= $ui->{'page_bg'};
1069 $options->{'-text'} ||= $ui->{'text_color'};
1070 $options->{'-link'} ||= $ui->{'link_color'};
1071 $options->{'-vlink'} ||= $ui->{'vlink_color'};
1072 $options->{'-alink'} ||= $ui->{'alink_color'};
1073 return $self->cgi->start_html(%{ $options });
1074 } # END sub display_start_html
1075  
1076  
1077 =item C<display_search> ( )
1078  
1079 Prints an HTML page meant for limiting what appears on the display_list page.
1080  
1081 Not Implimented Yet.
1082  
1083 =cut
1084  
1085 sub display_search {
1086 warn "display_search method not implimented yet!";
1087 return undef;
1088 } # END sub display_search
1089  
1090  
1091 =item C<display_row> ( $field )
1092  
1093 Takes a column (field) name and prints a 2 columned table row where the left
1094 column has the fields descriptive name ($self->fieldname($field)) and the right
1095 column has the output of that fields method ($self->$field)
1096  
1097 =cut
1098  
1099 sub display_row {
1100 my $self = shift;
1101 return undef unless ref $self;
1102 my $q = $self->cgi;
1103 my $field = shift;
1104 my $ui = $self->ui;
1105  
1106 return undef if ($self->fieldtype($field) eq 'hidden');
1107  
1108 ## Is this field required? (i.e. not null?) ##
1109 my $req = %{$self->fieldoptions($field) || { }}->{not_null} || 0;
1110  
1111 if (($self->fieldtype($field) eq 'row')) {
1112 print $self->$field();
1113 } elsif ($self->fieldtype($field) eq 'textarea') {
1114 print $q->Tr({ },
1115 $q->td({ -VALIGN => 'top',
1116 -COLSPAN => 2,
1117 -ALIGN => 'center',
1118 -WIDTH => 100,
1119 -BGCOLOR => $ui->{'row_key'}
1120 },
1121 $q->font(
1122 $ui->{'font_key'},
1123 $q->b(
1124 ($req ? '* ' : '') . $self->fieldname( $field ) . ':'
1125 )
1126 )
1127 )
1128 )."\n"
1129 . $q->Tr({ },
1130 $q->td({ -VALIGN => 'top',
1131 -COLSPAN => 2,
1132 -BGCOLOR => $ui->{'row_value'}
1133 },
1134 $q->font(
1135 $ui->{'font_value'},
1136 ($self->$field() ? $self->$field() : '&nbsp;')
1137 )
1138 )
1139 ) . "\n";
1140 } elsif (($self->fieldtype($field) eq 'password') && ($self->displaymode() ne 'view')) {
1141 # Once to enter
1142 print $q->Tr({ },
1143 $q->td({ -VALIGN => 'top',
1144 -ALIGN => 'right',
1145 -WIDTH => 100,
1146 -BGCOLOR => $ui->{'row_key'}
1147 },
1148 $q->font(
1149 $ui->{'font_key'},
1150 $q->b(
1151 ($req ? '* ' : '') . $self->fieldname( $field ) . ':'
1152 )
1153 )
1154 )
1155 . $q->td({ -VALIGN => 'top',
1156 -WIDTH => 400,
1157 -BGCOLOR => $ui->{'row_value'}
1158 },
1159 $q->font(
1160 $ui->{'font_value'},
1161 $self->$field()
1162 )
1163 )
1164 ) . "\n"
1165 # again to verify
1166 . $q->Tr({ },
1167 $q->td({ -VALIGN => 'top',
1168 -ALIGN => 'right',
1169 -WIDTH => 100,
1170 -BGCOLOR => $ui->{'row_key'}
1171 },
1172 $q->font(
1173 $ui->{'font_key'},
1174 $q->b(
1175 ($req ? '* ' : '') . $self->fieldname( $field ) . ':'
1176 )
1177 . '<br/>(for verification)'
1178 )
1179 )
1180 . $q->td({ -VALIGN => 'top',
1181 -WIDTH => 400,
1182 -BGCOLOR => $ui->{'row_value'}
1183 },
1184 $q->font(
1185 $ui->{'font_value'},
1186 $self->$field()
1187 )
1188 )
1189 ) . "\n"
1190 } else {
1191 print $q->Tr({ },
1192 $q->td({ -VALIGN => 'top',
1193 -ALIGN => 'right',
1194 -WIDTH => 100,
1195 -BGCOLOR => $ui->{'row_key'}
1196 },
1197 $q->font(
1198 $ui->{'font_key'},
1199 $q->b(
1200 ($req ? '* ' : '') . $self->fieldname( $field ) . ':'
1201 )
1202 )
1203 )
1204 . $q->td({ -VALIGN => 'top',
1205 -WIDTH => 400,
1206 -BGCOLOR => $ui->{'row_value'}
1207 },
1208 $q->font(
1209 $ui->{'font_value'},
1210 ($self->$field() ? $self->$field() : '&nbsp;')
1211 )
1212 )
1213 ) . "\n";
1214 }
1215 } # END sub display_row
1216  
1217  
1218 =back
1219  
1220 =head2 DATA METHODS
1221  
1222 =over 4
1223  
1224 =item C<save_data> ( [ \%data ] )
1225  
1226 Goes through the process of calling sanity, then get_data (to get the data out
1227 of the query object) and then db_obj->modify.
1228 Optionally takes data hashref as returned by get_data(), otherwise calls get_data() itself. (This makes extension of save_data() possible without having to call get_data() twice.)
1229  
1230 =cut
1231  
1232 sub save_data {
1233 my $self = shift;
1234 return undef unless ref $self;
1235 my $dbh = $self->dbh();
1236 my $data = shift || $self->get_data();
1237  
1238 warn 'save_data after get_data data: ' . Data::Dumper::Dumper($data) . "\n" if ($debug > 1);
1239 return undef unless (ref($data) eq 'HASH');
1240 return undef unless ($self->sanity($data));
1241  
1242 my $newself;
1243 warn 'save data==> ' . Data::Dumper::Dumper($data) . "\n" if ($debug);
1244 if ($self->displaymode eq 'add') {
1245 if ($newself = $self->data_class->new( $dbh, $data )) {
1246 warn "save_data - new succeeded\n" if ($debug > 1);
1247 $self->{'_db_obj'} = $newself;
1248 warn 'save_data - after new - new self ==> ' . Data::Dumper::Dumper($self->{'_db_obj'}) if ($debug > 1);
1249 } else {
1250 warn "save_date - new failed\n" if ($debug > 1);
1251 $self->{'_errors'}->{'General Database Error'} = $dbh->errstr || $self->data_class->errors;
1252 return undef;
1253 }
1254 } else {
1255 if ($newself = $self->db_obj->modify( $data )) {
1256 warn "save_data - modify succeeded\n" if ($debug > 1);
1257 $self->{'_db_obj'} = $newself;
1258 warn 'save_data - after modify - new self ==> ' . Data::Dumper::Dumper($self->{'_db_obj'}) if ($debug > 1);
1259 } else {
1260 warn "save_date - modify failed\n" if ($debug > 1);
1261 $self->{'_errors'}->{'General Database Error'} = $dbh->errstr || $self->db_obj->errors;
1262 return undef;
1263 }
1264 }
1265 return 1;
1266 } # END sub save_data
1267  
1268  
1269 =item C<get_data> ( [ $data, ] [ $fields ] )
1270  
1271 Takes the fields hash (returned by $self->fields()) and the CGI object and
1272 returns a data hashref which can be sent to Carbon's new or modify
1273 You can optionaly pass a $data hash and a $fields hash which it will use.
1274  
1275 B<OPTIMIZE>
1276  
1277 =cut
1278  
1279 sub get_data {
1280 my $self = shift;
1281 return undef unless ref $self;
1282 my $data = shift || { };
1283 my $fields = shift || $self->fields;
1284 my $q = $self->cgi;
1285 my $db_obj = $self->db_obj;
1286 warn 'Admin:get_data q ==> ' . Data::Dumper::Dumper($q) . "\n" if ($debug > 2);
1287 warn 'Admin:get_data fields ==> ' . Data::Dumper::Dumper($fields) . "\n" if ($debug > 2);
1288 warn 'Admin:get_data db_obj ==> ' . Data::Dumper::Dumper($db_obj) . "\n" if ($debug > 2);
1289  
1290 foreach (keys %$fields) {
1291 my $qfieldname = $self->qfieldname( $_ );
1292 my $foptions = $self->fieldoptions( $_ );
1293 my $qoptions = $self->fieldhtmloptions( $_ ) || { };
1294 # if (!$foptions->{not_null} && ($q->param( $qfieldname ) =~ /^NULL$/i)) {
1295 # warn "Null option selected.";
1296 # $q->param( $qfieldname, undef );
1297 # last;
1298 # }
1299 if ($self->fieldrelclass( $_ )) {
1300 warn "reclass - $_ ==> " . $self->fieldrelclass($_) . " qfieldname ==> $qfieldname - q ==> " . Data::Dumper::Dumper($q->param( $qfieldname )) ."\n" if ($debug > 1);
1301 if ($self->fieldrelclasstype($_)) {
1302 $data->{$self->fieldrelclass($_)} = [ $q->param( $qfieldname ) ];
1303 } else {
1304 warn "no classtype $_\n" if ($debug > 1);
1305 if ($q->param( $qfieldname ) eq 'NULL') {
1306 $data->{$_} = undef;
1307 } else {
1308 $data->{$_} = $q->param( $qfieldname ) unless (ref $db_obj && ($db_obj->$_() eq $q->param( $qfieldname )));
1309 warn Data::Dumper::Dumper( $data->{$_} ) if ($debug > 1);
1310 }
1311 }
1312 } elsif ($self->fieldtype($_) eq 'checkbox') {
1313 unless (defined $q->param( $qfieldname )) {
1314 $q->param(-NAME => $qfieldname, -VALUE => '0');
1315 }
1316 $data->{$_} = $q->param( $qfieldname )
1317 unless (ref $db_obj && ($db_obj->$_() eq $q->param( $qfieldname )));
1318 } elsif ($self->fieldtype($_) eq 'date') {
1319 next unless ($q->param( $qfieldname.'_year' ));
1320
1321 my $date = BingoX::Time->new;
1322 warn "date field => $qfieldname\n" if ($debug > 1);
1323  
1324 ## Year ##
1325 my $year = $q->param( $qfieldname.'_year' );
1326 if (length($year) <= 2) {
1327 $self->{'_errors'}->{$_} = "Invalid Date Entered: be compliant and the use full year";
1328 return undef;
1329 }
1330  
1331 ## Month ##
1332 my $month;
1333 my $mon = $q->param( $qfieldname.'_mon' );
1334 if ($mon =~ /\D/) {
1335 $self->{'_errors'}->{$_} = "Invalid Date Entered: must be of the value [ 1 .. 12 ]";
1336 return undef;
1337 } else {
1338 $month = $date->months->{ $mon };
1339 }
1340  
1341 ## Day ##
1342 my $day = $q->param( $qfieldname.'_day' );
1343 my $lday = $date->last_days->{ $mon - 1 };
1344 if ($day < 1 || $day > 31) {
1345 $self->{'_errors'}->{$_} = "Invalid Date Entered: day is out of range";
1346 return undef;
1347 } elsif ($day =~ /\D/) {
1348 $self->{'_errors'}->{$_} = "Invalid Date Entered: must be of the value [ 1 .. last day ]";
1349 return undef;
1350 } elsif ($day > $lday) {
1351 $day = $lday;
1352 }
1353  
1354 ## Hour ##
1355 my $hour = $q->param( $qfieldname.'_hour' );
1356 my $am_pm = $q->param( $qfieldname.'_ampm' );
1357 unless ($qoptions->{'-SHOW_24HOURS'}) {
1358 if ($am_pm eq 'PM') {
1359 $hour += 12 unless ($hour == 12);
1360 } else {
1361 $hour = 0 if ($hour == 12);
1362 }
1363 }
1364  
1365 ## Minute ##
1366 my $min = $q->param( $qfieldname.'_min' ) || '00';
1367  
1368 ## Jun 04 1998 21:09:55 ##
1369 my $string = sprintf("%s %2d %4d %02d:%02d:00", $month, $day, $year, $hour, $min);
1370 my $timelocal = $self->data_class->str2time( $string );
1371 my $new_date = BingoX::Time->new( $timelocal );
1372 $data->{$_} = $new_date->strftime( $self->data_class->date_format )
1373 unless (ref $db_obj && ($db_obj->$_() eq $new_date));
1374  
1375 warn 'get_data date ==> ' . Data::Dumper::Dumper($data->{$_}) . "\n" if ($debug > 1);
1376 } else {
1377 warn "qfieldname ==> $qfieldname\n" if ($debug > 1);
1378 next if ($self->fieldtype( $_ ) eq 'view');
1379  
1380 ## Verify that password fields match verification
1381 if ($self->fieldtype($_) eq 'password') {
1382 my @pw_values = $q->param( $qfieldname );
1383 if ($pw_values[0] ne $pw_values[1]) {
1384 $q->param( $qfieldname, '' );
1385 $self->{'_errors'}->{$_} = 'passwords do not match.';
1386 }
1387 }
1388 if ($q->param( $qfieldname ) eq 'NULL') {
1389 $data->{$_} = undef;
1390 } else {
1391 $data->{$_} = $q->param( $qfieldname )
1392 unless (ref $db_obj && ($db_obj->$_() eq $q->param( $qfieldname )));
1393 }
1394 }
1395 }
1396 warn 'get_data ==> ' . Data::Dumper::Dumper($data) ."\n" if ($debug > 2);
1397 return $data;
1398 } # END sub get_data
1399  
1400  
1401 =item C<sanity> ( )
1402  
1403 Populates the _errors data instance in the case that the data does not conform
1404 to what is allowed to be entered into the database.
1405  
1406 =cut
1407  
1408 sub sanity {
1409 my $self = shift;
1410 my $data_hash = shift;
1411 ref($data_hash) || return undef;
1412 my $q = $self->cgi;
1413 my $qfield;
1414  
1415 ## Step through each field, calling all the necessary sanity methods ##
1416 foreach my $field (@{ $self->fieldlist }) {
1417 my $sanity = $self->fieldsanity( $field );
1418 my $req = %{ $self->fieldoptions( $field ) || { } }->{'not_null'} || 0;
1419 unless (ref($sanity) eq 'ARRAY' || $req) {
1420 warn "BingoX::Chromium::fieldsanity('$field') is not an array ref\n" if ($debug);
1421 next;
1422 }
1423  
1424 $qfield = $self->qfieldname( $field );
1425  
1426 my @errors = ( );
1427 my $data = (exists $data_hash->{ $field })
1428 ? $data_hash->{$field}
1429 : ( $self->displaymode eq 'add'
1430 ? ''
1431 : $self->db_obj->$field());
1432 ## Is this a required field? If it's empty, ##
1433 ## we can complain here and move on... ##
1434 if ($req && !$data) {
1435 $self->{'_errors'}{$field} ||= 'This field is required.';
1436 next;
1437 }
1438  
1439 ## Check each sanity method for this field ##
1440 foreach my $sane (@$sanity) {
1441 if (ref $sane) { # listref, call the method, passing paramters
1442 my $method = shift @$sane;
1443 my $err = $self->$method($data, @$sane);
1444 warn("SANITY ERROR: $err") if ($err && $debug);
1445 push(@errors, $err) if $err;
1446 } else { # not a ref, call the method only
1447 my $err = $self->$sane( $data );
1448 warn("SANITY ERROR: $err") if ($err && $debug);
1449 push(@errors, $err) if $err;
1450 }
1451 }
1452 $self->{'_errors'}{$field} = join('<BR>', @errors) if @errors;
1453 }
1454  
1455 ## Parlance -- true result means "sane" ##
1456 (%{ $self->{'_errors'} }) ? 0 : 1;
1457 } # END sub sanity
1458  
1459 sub qfd { return "#" }
1460 sub pkd { return $_[0]->data_class->pkd }
1461 sub prefix { return $_[0]->data_class_name . $_[0]->qfd }
1462  
1463  
1464 =item C<cpkey> ( [ $db_obj ] )
1465  
1466 Returns a string representing a single composite primary key joined by $self->qfd.
1467  
1468 =cut
1469  
1470 sub cpkey {
1471 my $self = shift;
1472 my $obj = shift || $self->db_obj;
1473 return undef unless (ref($self) && ref($obj));
1474 return $obj->cpkey;
1475 } # END sub cpkey
1476  
1477  
1478 =item C<cpkey_params> ($cpkey)
1479  
1480 Returns a params hash from the cpkey string passed.
1481  
1482 =cut
1483  
1484 sub cpkey_params {
1485 my $self = shift;
1486 my $cpkey = shift || return undef;
1487 $cpkey = (split( $self->qfd,$cpkey ))[-1];
1488 return $self->data_class->cpkey_params( $cpkey );
1489 } # END sub cpkey_param
1490  
1491  
1492 =item C<pcpkey> ( )
1493  
1494 Returns a cpkey with a class name + $qfd in front of it.
1495  
1496 =cut
1497  
1498 sub pcpkey {
1499 my $self = shift;
1500 my $obj = shift || $self->db_obj;
1501 return undef unless (ref($self) && ref($obj));
1502 return $self->prefix . $obj->cpkey;
1503 } # END sub pcpkey
1504  
1505  
1506 =item C<dbh> ( )
1507  
1508 Returns the object's database handle.
1509  
1510 B<OPTIMIZE> Needs work. Doesn't appear to use Carboniums dbh method thus
1511 thus doesn't use cached dbh.
1512  
1513 =cut
1514  
1515 sub dbh {
1516 my $self = shift;
1517 return $self->{'_dbh'} if (ref($self) && ref($self->{'_dbh'}));
1518 my $data_class = $self->data_class;
1519 my $dbh = $data_class->dbh;
1520 $self->{'_dbh'} = $dbh if (ref $self);
1521 my $r = $self->r;
1522 if (ref $r) {
1523 $r->register_cleanup(sub { $data_class->purge_dbh }) unless ($r->notes('chromium_cleanup'));
1524 $r->notes('chromium_cleanup', 1);
1525 }
1526 return $dbh;
1527 } # END sub dbh
1528  
1529  
1530 =item C<get_list_hash> ( [ $selection ] )
1531  
1532 *** NEEDS TO BE REMOVED ***
1533  
1534 Returns a hash ref of all the objects in the class it was called against.
1535 The hash is built from the C<pcpkey>, and the data class' C<title_field>,
1536 substr()'d to the data class' C<title_size> or by default 80 chars.
1537  
1538 B<OPTIMIZE>
1539  
1540 =cut
1541  
1542 sub get_list_hash {
1543 my $self = shift;
1544 my $class = ref($self) || $self;
1545 my $selection = shift || $self->selection;
1546 my $title_size = $self->data_class->title_size || 80;
1547 my $title_field = $self->data_class->title_field;
1548 my $fields = $self->data_class->primary_keys;
1549 my $sort = [ ];
1550 unless ($self->data_class->content_fields->{ $title_field }) {
1551 push(@$fields, $title_field);
1552 $sort = [ $title_field ];
1553 }
1554  
1555 my $stream = $self->data_class->stream_obj(
1556 $self->dbh,
1557 $selection,
1558 $fields,
1559 $sort
1560 );
1561  
1562 my $hash = { };
1563 return undef unless ref $stream;
1564 while (my $obj = $stream->()) {
1565 $hash->{ $self->pcpkey( $obj ) }
1566 = length($obj->$title_field()) > $title_size
1567 ? substr($obj->$title_field(), 0, ($title_size - 3)) . '...'
1568 : $obj->$title_field();
1569 }
1570  
1571 warn Data::Dumper::Dumper( $hash ) if ($debug > 2);
1572 return $hash;
1573 } # END sub get_list_hash
1574  
1575  
1576 =item C<db_obj> ( )
1577  
1578 Retrieves and caches encapsolated DATA object based on whats in the query
1579 obect. Looks for pcpkey query param first and then for each primary
1580 key individually.
1581  
1582 B<OPTIMIZE>
1583  
1584 =cut
1585  
1586 sub db_obj {
1587 my $self = shift;
1588 return undef unless ref($self);
1589 return $self->{'_db_obj'} if ref($self->{'_db_obj'});
1590 my $q = $self->cgi;
1591 my $params = { };
1592  
1593 warn "CGI ==> " . Data::Dumper::Dumper( $q ) . "\n" if ($debug > 3);
1594 if ($q->param('cpkey')) { # should be here most of the time
1595 warn "pkey ==> " . $q->param('cpkey') . "\n" if ($debug > 3);
1596 $params = $self->cpkey_params( $q->param('cpkey') );
1597 warn 'params ==> ' . Data::Dumper::Dumper( $params ) . "\n" if ($debug > 3);
1598 } elsif ($q->param('ID')) { # BAD BAD BAD DOG! Just in case.
1599 $params->{ $self->data_class->primary_keys->[0] } = $q->param('ID');
1600 } else { # If the primary keys are specified individually.
1601 foreach (@{ $self->data_class->primary_keys }) {
1602 return undef unless $q->param($_);
1603 my $prefix = $self->prefix;
1604 $q->param($_) =~ /^$prefix(.*)/;
1605 $params->{$_} = $1;
1606 }
1607 }
1608  
1609 ## get it from the database. Should we be using get? ##
1610 $self->{'_db_obj'} = $self->data_class->get( $self->dbh, $params );
1611 } # END sub db_obj
1612  
1613  
1614 =back
1615  
1616 =head2 CLASS VARIABLE METHODS
1617  
1618 =over 4
1619  
1620 =item C<data_class> ( )
1621 =item C<db_class> ( )
1622  
1623 Returns the data class for the current display class (from the class
1624 variable C<$data_class>).
1625  
1626 =cut
1627  
1628 sub data_class {
1629 my $self = shift;
1630 return $self->{'_data_class'} if (ref $self);
1631 my $class = ref($self) || $self;
1632 no strict 'refs';
1633 my $dc = ${"${class}::data_class"};
1634 unless (defined $dc) {
1635 ($dc = $class) =~ s/::Admin::/::Data::/;
1636 ${"${class}::data_class"} = $dc;
1637 }
1638 return $dc;
1639 } # END of data_class
1640 *db_class = \&data_class; # Backward compatibility
1641  
1642  
1643 =item C<data_class_name> ( )
1644 =item C<db_class_name> ( )
1645  
1646 Returns the rightmost part of the db_class name (thats the text right of the ::)
1647  
1648 =cut
1649  
1650 sub data_class_name {
1651 my $self = shift;
1652 return $self->{'_data_class_name'} if (ref $self);
1653 $self->data_class =~ /^.*:(.*)/;
1654 return $1;
1655 } # END sub data_class_name
1656 *db_class_name = \&data_class_name; # Backward compatibility
1657  
1658  
1659 =item C<ui> ( )
1660  
1661 This method is a fallback method for the user interface for BingoX::Chromium.
1662 ui() contains the default colors for the forms created by BingoX::Chromium.
1663 To create a custom color scheme for a specific class or entire admin area
1664 create a ui() method in either the Admin class or the subclass
1665 and modify the details to your preference.
1666  
1667 =cut
1668  
1669 sub ui {
1670 my $self = shift;
1671 return $self->{'_ui'} if (ref($self) && ref($self->{'_ui'}));
1672 my $options = shift || { };
1673 $options->{'text_color'} ||= '#000000';
1674 $options->{'link_color'} ||= '#0000FF';
1675 $options->{'vlink_color'} ||= '#660099';
1676 $options->{'alink_color'} ||= '#FF0000';
1677 $options->{'page_bg'} ||= '#FFFFFF';
1678 $options->{'table_border'} ||= '#000000';
1679 $options->{'row_key'} ||= '#EEEEDE';
1680 $options->{'row_value'} ||= '#FFFFFF';
1681 $options->{'table_header'} ||= '#FFFFEE';
1682 $options->{'table_footer'} ||= '#FFFFEE';
1683 $options->{'font_header'} ||= { -FACE => 'verdana,arial,helvetica', -SIZE => '5' };
1684 $options->{'font_footer'} ||= { -FACE => 'verdana,arial,helvetica', -SIZE => '1' };
1685 $options->{'font_error'} ||= { -FACE => 'verdana,arial,helvetica', -SIZE => '2', -COLOR => "#FF0000" };
1686 $options->{'font_key'} ||= { -FACE => 'verdana,arial,helvetica', -SIZE => '2' };
1687 $options->{'font_value'} ||= { -FACE => 'times', -SIZE => '3' };
1688 return (ref($self) ? $self->{'_ui'} = $options : $options);
1689 } # END sub ui
1690  
1691  
1692 =item C<children> ( )
1693  
1694 Returns class defined children as an arrayref.
1695  
1696 =cut
1697  
1698 sub children {
1699 no strict 'refs';
1700 my $self = shift;
1701 my $class = ref($self) || $self;
1702 return [ @{"${class}::children"} ] || undef;
1703 } # END sub children
1704  
1705  
1706 =item C<parents> ( )
1707  
1708 Returns class defined parents as an arrayref.
1709  
1710 =cut
1711  
1712 sub parents {
1713 no strict 'refs';
1714 my $self = shift;
1715 my $class = ref($self) || $self;
1716 return [ @{"${class}::parents"} ] || undef;
1717 } # END sub children
1718  
1719  
1720 =item C<parent_class> ( )
1721  
1722 Returns cached parent class
1723  
1724 =cut
1725  
1726 sub parent_class {
1727 return $_[0]->{'_parent_class'};
1728 } # END of parent_class
1729  
1730  
1731 =item C<adminuri> ( )
1732  
1733 Returns class defined URI as a string.
1734  
1735 =cut
1736  
1737 sub adminuri {
1738 no strict 'refs';
1739 my $self = shift;
1740 my $class = ref($self) || $self;
1741 warn 'adminuri ==> ' . ${"${class}::adminuri"} . "\n" if ($debug > 3);
1742 return ${"${class}::adminuri"} || undef;
1743 } # END sub adminuri
1744  
1745  
1746 =item C<classdesc> ( )
1747  
1748 Returns class defined Class description as a string.
1749  
1750 =cut
1751  
1752 sub classdesc {
1753 no strict 'refs';
1754 my $self = shift;
1755 my $class = ref($self) || $self;
1756 warn 'classdesc ==> ' . ${"${class}::classdesc"} . "\n" if ($debug > 3);
1757 return ${"${class}::classdesc"} || undef;
1758 } # END sub classdesc
1759  
1760  
1761 =item C<adminclass> ( )
1762  
1763 NEEDS POD
1764  
1765 =cut
1766  
1767 sub adminclass {
1768 my $proto = shift;
1769 my $class = shift;
1770 my $lclass = $class;
1771 no strict 'refs';
1772 my @ISA = @{ "${class}::ISA" };
1773 while ($ISA[0] ne __PACKAGE__) {
1774 $lclass = shift(@ISA);
1775 unshift( @ISA, @{ "${lclass}::ISA" } );
1776 return undef unless (@ISA);
1777 }
1778 return $lclass;
1779 } # END sub adminclass
1780  
1781  
1782 =item C<fieldlist> ( )
1783  
1784 Returns the class defined fieldlist as an arrayref.
1785  
1786 =cut
1787  
1788 sub fieldlist {
1789 no strict 'refs';
1790 my $self = shift;
1791 my $class = ref($self) || $self;
1792 return \@{"${class}::fieldlist"};
1793 } # END sub fieldlist
1794  
1795  
1796 =item C<fields> ( )
1797  
1798 Returns class defined fields hashref.
1799  
1800 =cut
1801  
1802 sub fields {
1803 no strict 'refs';
1804 my $self = shift;
1805 my $class = ref($self) || $self;
1806 return \%{"${class}::fields"};
1807 } # END sub fields
1808  
1809  
1810 =item C<fieldname> ( $field )
1811  
1812 Takes a column name and returns a string with that field's pretty
1813 name as defined in the class defined field hash. This is the [0]
1814 element of that keys array value.
1815  
1816 =cut
1817  
1818 sub fieldname {
1819 no strict 'refs';
1820 my $self = shift;
1821 my $class = ref($self) || $self;
1822 my $field = shift;
1823 my $fields = $class->fields();
1824 return undef unless (defined %$fields);
1825 return $fields->{$field}[0];
1826 } # END sub fieldname
1827  
1828  
1829 =item C<fieldtype> ( $field )
1830  
1831 Takes a column name and returns a string with that field's HTML
1832 type as defined in the class defined field hash. This is the [1]
1833 element of that keys array value.
1834  
1835 =cut
1836  
1837 sub fieldtype {
1838 no strict 'refs';
1839 my $self = shift;
1840 my $class = ref($self) || $self;
1841 my $field = shift;
1842 my $fields = $class->fields();
1843 return undef unless (defined %$fields);
1844 return $fields->{$field}[1];
1845 } # END sub fieldtype
1846  
1847  
1848 =item C<fieldhtmloptions> ( $field )
1849  
1850 Takes a column name and returns a hashref with that field's HTML
1851 options as defined in the class defined field hash. This is the
1852 [2] element of that keys array value.
1853  
1854 =cut
1855  
1856 sub fieldhtmloptions {
1857 no strict 'refs';
1858 my $self = shift;
1859 my $class = ref($self) || $self;
1860 my $field = shift;
1861 my $fields = $class->fields();
1862 return undef unless (defined %$fields);
1863 return $fields->{$field}[2];
1864 } # END sub fieldhtmloptions
1865  
1866  
1867 =item C<fieldrelclass> ( $field )
1868  
1869 Takes a column name and returns a string with that field's related
1870 class (if it exists) as defined in the class defined field hash.
1871 This is the [3] element of that keys array value.
1872  
1873 =cut
1874  
1875 sub fieldrelclass {
1876 no strict 'refs';
1877 my $self = shift;
1878 my $class = ref($self) || $self;
1879 my $field = shift;
1880 my $fields = $class->fields();
1881 return undef unless (defined(%$fields) && exists($fields->{$field}[3]));
1882 return $fields->{$field}[3][0];
1883 } # END sub fieldrelclass
1884  
1885 =item C<fieldrelclasstype> ( $field )
1886  
1887 Takes a column name and returns a string with that field's related
1888 class (if it exists) as defined in the class defined field hash.
1889 This is the [3] element of that keys array value.
1890  
1891 =cut
1892  
1893 sub fieldrelclasstype {
1894 no strict 'refs';
1895 my $self = shift;
1896 my $class = ref($self) || $self;
1897 my $field = shift;
1898 my $fields = $class->fields();
1899 return undef unless (defined(%$fields) && exists($fields->{$field}[3]));
1900 return $fields->{$field}[3][1];
1901 } # END sub fieldrelclasstype
1902  
1903  
1904 =item C<fieldoptions> ( $field )
1905  
1906 Takes a column name and returns a hashref with that field's options
1907 information as defined in the class defined field hash.
1908 This is the [4] element of that keys array value.
1909  
1910 =cut
1911  
1912 sub fieldoptions {
1913 no strict 'refs';
1914 my $self = shift;
1915 my $class = ref($self) || $self;
1916 my $field = shift;
1917 my $fields = $class->fields();
1918 return undef unless (defined %$fields);
1919 return $fields->{$field}[4];
1920 } # END sub fieldoptions
1921  
1922  
1923 =item C<fieldsanity> ( $field )
1924  
1925 Takes a column name and returns a listref with that field's sanity
1926 information as defined in the class defined field hash.
1927 This is the [5] element of that keys array value.
1928  
1929 =cut
1930  
1931 sub fieldsanity {
1932 no strict 'refs';
1933 my $self = shift;
1934 my $class = ref($self) || $self;
1935 my $field = shift;
1936 my $fields = $class->fields();
1937 return undef unless (defined %$fields);
1938 return $fields->{$field}[5];
1939 } # END sub fieldsanity
1940  
1941  
1942 =back
1943  
1944 =head2 OBJECT METHODS
1945  
1946 =over 4
1947  
1948 =item C<uri> ( )
1949  
1950 Returns the cached uri object.
1951  
1952 =item C<cgi> ( )
1953  
1954 Returns the cached cgi object.
1955  
1956 =item C<conf> ( )
1957  
1958 Returns the cached conf object (set in new).
1959  
1960 =item C<section> ( )
1961  
1962 Returns the cached section (set in new).
1963  
1964 =item C<displaymode> ( )
1965  
1966 Returns the cached uri displaymode (set in new).
1967  
1968 =cut
1969  
1970 sub uri { $_[0]->{'_uri'} }
1971 sub cgi { $_[0]->{'_cgi'} }
1972 sub conf { $_[0]->{'_conf'} }
1973 sub section { $_[0]->{'_section'} }
1974 sub displaymode { $_[0]->{'_displaymode'} }
1975  
1976  
1977 =item C<r> ( )
1978  
1979 Object Method:
1980 Returns Apache Request object.
1981  
1982 =cut
1983  
1984 sub r {
1985 return undef unless (defined $ENV{'MOD_PERL'} && ref $_[0]);
1986 $_[0]->{'_r'} ||= Apache->request;
1987 } # END sub r
1988  
1989  
1990 =item C<selection> ( [ \%hash ] )
1991  
1992 Returns a hash reference containing the parameters that specify the current
1993 selection. If a new value is passed, it sets the selection to that value.
1994  
1995 =cut
1996  
1997 sub selection {
1998 my $self = shift;
1999 my $value = shift;
2000 if (ref $self) {
2001 $self->{'_selection'} = $value if (defined $value);
2002 return $self->{'_selection'} || { };
2003 } else {
2004 return { };
2005 }
2006 } # END sub selection
2007  
2008  
2009 =back
2010  
2011 =head2 HTML DISPLAY METHODS
2012  
2013 =over 4
2014  
2015  
2016 =item C<qfieldname> ( $fieldname )
2017  
2018 Object Method:
2019
2020 Returns the fieldname to be used in the FORM INPUT NAME field. When
2021 overloading Administration field variables, use this to get the INPUT TYPE NAME.
2022  
2023 ie. <INPUT TYPE="text" NAME="$self->qfieldname('username')">
2024  
2025 =cut
2026  
2027 sub qfieldname {
2028 my $self = shift;
2029 return undef unless ref($self);
2030 my $fieldname = shift;
2031 my $qfieldname = '';
2032 $qfieldname = $self->cpkey . $self->qfd if ($self->displaymode eq 'modify');
2033 $qfieldname .= $self->prefix . $fieldname;
2034 return $qfieldname;
2035 } # END of qfieldname
2036  
2037  
2038 =item C<main_index> ( )
2039  
2040 Object Method:
2041
2042 Returns the Main Index Path.
2043  
2044 =cut
2045  
2046 sub main_index {
2047 my $self = shift;
2048 my $path = $self->r->dir_config('AdminMainIndex') || '/';
2049 return $path;
2050 } # END of main_index
2051  
2052  
2053 =item C<HTML_time> ( $fieldname [, $qoptions ] )
2054  
2055 Object Method:
2056
2057 Generic Hours Form Tag. Called by AUTOLOAD.
2058 Gets the default field params based on the fieldname and returns a
2059 set of date form fields or in viewable format if the displaymode is 'view'.
2060  
2061 =cut
2062  
2063 sub HTML_time {
2064 my $self = shift;
2065 return undef unless ref($self);
2066 my $fieldname = shift;
2067 my $qoptions = shift || $self->fieldhtmloptions( $fieldname ) || { };
2068 my $q = $self->cgi;
2069 my $qfieldname = $self->qfieldname( $fieldname );
2070  
2071 my $hr24 = $qoptions->{'-SHOW_24HOURS'};
2072  
2073 my $date_obj;
2074 $date_obj = $self->db_obj->$fieldname() unless ($self->displaymode eq 'add');
2075 if (!ref($date_obj)) {
2076 return undef if ($self->displaymode eq 'view');
2077 $date_obj = BingoX::Time->new();
2078 }
2079  
2080 my ($hour, $minute, $ampm);
2081 ## get hour from the date object ##
2082 $hour = $date_obj->hour;
2083 ## get minute from the date object ##
2084 $minute = $date_obj->min;
2085 unless ($hr24) {
2086 $ampm = ($hour >= 12 ? 'PM' : 'AM');
2087 $hour -= 12 if ($hour > 12);
2088 $hour = 12 if ($hour == 0);
2089 }
2090  
2091 return ($hour . ':' . $minute . ' ' . $ampm ,
2092 $q->hidden(-NAME => $qfieldname.'_hour', -VALUE => $hour)
2093 . $q->hidden(-NAME => $qfieldname.'_min', -VALUE => $minute)
2094 . $q->hidden(-NAME => $qfieldname.'_ampm', -VALUE => $ampm))
2095 if ($self->displaymode eq 'view' || $qoptions->{'-TYPE'} =~ /view/i);
2096
2097 if ($qoptions->{'-TYPE'} =~/view/io) {
2098 return $q->hidden( -NAME => $qfieldname.'_hour',
2099 -VALUE => $hour)
2100 . $q->hidden( -NAME => $qfieldname.'_min',
2101 -VALUE => $minute)
2102 . $q->hidden( -NAME => $qfieldname.'_ampm',
2103 -VALUE => $ampm);
2104 } elsif ($qoptions->{'-TYPE'} =~/text/io) {
2105 return $q->textfield( -NAME => $qfieldname.'_hour',
2106 -SIZE => $qoptions->{'-HR_SIZE'} || 2,
2107 -MAXLENGTH => $qoptions->{'-HR_MAXLENGTH'} || 2,
2108 -OVERRIDE => $qoptions->{'-HR_OVERRIDE'} || 1,
2109 -DEFAULT => sprintf("%02d", $q->param( $qfieldname.'_hour' ) || $hour)
2110 )
2111 . ' : '
2112 . $q->textfield( -NAME => $qfieldname.'_min',
2113 -SIZE => $qoptions->{'-MIN_SIZE'} || 2,
2114 -MAXLENGTH => $qoptions->{'-MIN_MAXLENGTH'} || 2,
2115 -OVERRIDE => $qoptions->{'-MIN_OVERRIDE'} || 1,
2116 -DEFAULT => sprintf("%02d", $q->param( $qfieldname.'_min' ) || $minute)
2117 )
2118 . ($hr24 ? '' :
2119 '&nbsp;' . $q->popup_menu( -NAME => $qfieldname.'_ampm',
2120 -VALUES => [ 'AM', 'PM' ],
2121 -DEFAULT => ($q->param( $qfieldname.'_ampm' ) || $ampm),
2122 -OVERRIDE => $qoptions->{'-AMPM_OVERRIDE'} || 1
2123 ));
2124 } else {
2125 return $q->popup_menu( -NAME => $qfieldname.'_hour',
2126 -VALUES => ($hr24 ? $date_obj->hours24 : $date_obj->hours),
2127 -DEFAULT => sprintf("%02d", $hour),
2128 -OVERRIDE => $qoptions->{'-HR_OVERRIDE'} || 1
2129 )
2130 . '&nbsp;:&nbsp;'
2131 . $q->popup_menu( -NAME => $qfieldname.'_min',
2132 -VALUES => $date_obj->minutes,
2133 -DEFAULT => sprintf("%02d", $q->param( $qfieldname.'_min' ) || $minute),
2134 -OVERRIDE => $qoptions->{'-MIN_OVERRIDE'} || 1
2135 )
2136 . ($hr24 ? '' :
2137 '&nbsp;' . $q->popup_menu( -NAME => $qfieldname.'_ampm',
2138 -VALUES => [ 'AM', 'PM' ],
2139 -DEFAULT => ($q->param( $qfieldname.'_ampm' ) || $ampm),
2140 -OVERRIDE => $qoptions->{'-AMPM_OVERRIDE'} || 1
2141 ));
2142 }
2143 } # END sub HTML_time
2144  
2145  
2146 =item C<HTML_day> ( $fieldname [, $qoptions ] )
2147  
2148 Object Method:
2149  
2150 Generic Day Form Tag. Called by AUTOLOAD.
2151 Gets the default field params based on the fieldname and returns a
2152 set of date form fields or in viewable format if the displaymode is 'view'.
2153  
2154 =cut
2155  
2156 sub HTML_day {
2157 my $self = shift;
2158 return undef unless ref($self);
2159 my $fieldname = shift;
2160 my $qoptions = shift || $self->fieldhtmloptions( $fieldname ) || { };
2161 my $q = $self->cgi;
2162 my $qfieldname = $self->qfieldname( $fieldname );
2163
2164 my $date_obj;
2165 $date_obj = $self->db_obj->$fieldname() unless ($self->displaymode eq 'add');
2166 if (!ref($date_obj)) {
2167 return undef if ($self->displaymode eq 'view');
2168 $date_obj = BingoX::Time->new();
2169 }
2170  
2171 return $date_obj->mday if ($self->displaymode eq 'view');
2172  
2173 if ($qoptions->{'-TYPE'} =~/view/io) {
2174 return ( $date_obj->mday
2175 ,$q->hidden( -NAME => $qfieldname.'_day',
2176 -VALUE => $date_obj->mday));
2177 } elsif ($qoptions->{'-TYPE'} =~/text/io) {
2178 return $q->textfield( -NAME => $qfieldname.'_day',
2179 -SIZE => $qoptions->{'-DAY_SIZE'} || 2,
2180 -MAXLENGTH => $qoptions->{'-DAY_MAXLENGTH'} || 2,
2181 -OVERRIDE => $qoptions->{'-DAY_OVERRIDE'} || 1,
2182 -DEFAULT => $q->param( $qfieldname.'_day' ) || $date_obj->mday
2183 );
2184 } else {
2185 return $q->popup_menu( -NAME => $qfieldname.'_day',
2186 -VALUES => [ 1 .. 31 ],
2187 -OVERRIDE => $qoptions->{'-DAY_OVERRIDE'} || 1,
2188 -DEFAULT => $q->param( $qfieldname.'_day' ) || $date_obj->mday
2189 );
2190 }
2191 } # END sub HTML_day
2192  
2193  
2194 =item C<HTML_month> ( $fieldname [, $qoptions ] )
2195  
2196 Object Method:
2197  
2198 Generic Month Form Tag. Called by AUTOLOAD.
2199 Gets the default field params based on the fieldname and returns a
2200 set of date form fields or in viewable format if the displaymode is 'view'.
2201  
2202 =cut
2203  
2204 sub HTML_month {
2205 my $self = shift;
2206 return undef unless ref($self);
2207 my $fieldname = shift;
2208 my $qoptions = shift || $self->fieldhtmloptions( $fieldname ) || { };
2209 my $q = $self->cgi;
2210 my $qfieldname = $self->qfieldname( $fieldname );
2211  
2212 my $format = $qoptions->{'-FORMAT'} || "%B %e %Y";
2213 my $date_obj;
2214 $date_obj = $self->db_obj->$fieldname() unless ($self->displaymode eq 'add');
2215 if (!ref($date_obj)) {
2216 return undef if ($self->displaymode eq 'view');
2217 $date_obj = BingoX::Time->new();
2218 }
2219  
2220 return $date_obj->mon if ($self->displaymode eq 'view');
2221  
2222 if ($qoptions->{'-TYPE'} =~/view/io) {
2223 return ($date_obj->mon,
2224 $q->hidden( -NAME => $qfieldname.'_mon',
2225 -VALUE => $date_obj->mon));
2226 } elsif ($qoptions->{'-TYPE'} =~/text/io) {
2227 return $q->textfield( -NAME => $qfieldname.'_mon',
2228 -SIZE => $qoptions->{'-MON_SIZE'} || 2,
2229 -MAXLENGTH => $qoptions->{'-MON_MAXLENGTH'} || 2,
2230 -OVERRIDE => $qoptions->{'-MON_OVERRIDE'} || 1,
2231 -DEFAULT => $q->param( $qfieldname.'_mon' ) || $date_obj->mon
2232 );
2233 } else {
2234 return $q->popup_menu( -NAME => $qfieldname . '_mon',
2235 -VALUES => [ 1 .. 12 ],
2236 -LABELS => ($format =~ /\%B/o ? $date_obj->months_full : $date_obj->months),
2237 -OVERRIDE => $qoptions->{'-MON_OVERRIDE'} || 1,
2238 -DEFAULT => $q->param( $qfieldname.'_mon' ) || $date_obj->mon
2239 );
2240 }
2241 } # END sub HTML_month
2242  
2243  
2244 =item C<HTML_year> ( $fieldname [, $qoptions ] )
2245  
2246 Object Method:
2247  
2248 Generic Year Form Tag. Called by AUTOLOAD.
2249 Gets the default field params based on the fieldname and returns a
2250 set of date form fields or in viewable format if the displaymode is 'view'.
2251  
2252 =cut
2253  
2254 sub HTML_year {
2255 my $self = shift;
2256 return undef unless ref($self);
2257 my $fieldname = shift;
2258 my $qoptions = shift || $self->fieldhtmloptions( $fieldname ) || { };
2259 my $q = $self->cgi;
2260 my $qfieldname = $self->qfieldname( $fieldname );
2261  
2262 my $format = $qoptions->{'-FORMAT'} || "%B %e %Y";
2263 my $date_obj;
2264 $date_obj = $self->db_obj->$fieldname() unless ($self->displaymode eq 'add');
2265 if (!ref($date_obj)) {
2266 return undef if ($self->displaymode eq 'view');
2267 $date_obj = BingoX::Time->new();
2268 }
2269 my $start = $qoptions->{'-YEAR_START'} || 20;
2270 my $end = $qoptions->{'-YEAR_END'} || 20;
2271 my ($values, $year, $size, $max);
2272 if ($format =~ /\%Y/o) {
2273 $year = $date_obj->year;
2274 $values = [ ($year - $start) .. ($year + $end) ];
2275 $size = 4;
2276 $max = 4;
2277 } else {
2278 ### THERE IS NO REASON TO USE NON-COMPLIANT YEARS ###
2279 $size = 2;
2280 $max = 2;
2281 $year = substr($date_obj->year, -2, 2);
2282 my $yr = $year;
2283 $values = [ $year ];
2284 $start = 49 if ($start >= 50); # can only display 100 years at a time in two digit format
2285 foreach (1 .. $start) {
2286 $yr = 100 if ($yr == 0); # can't have negative years
2287 unshift(@$values, sprintf("%02d", --$yr));
2288 }
2289 $yr = $year;
2290 foreach (1 .. $start + 1) {
2291 push(@$values, sprintf("%02d", ++$yr));
2292 }
2293 }
2294  
2295 return $year if ($self->displaymode eq 'view');
2296  
2297 if ($qoptions->{'-TYPE'} =~/view/io) {
2298 return ($year ,$q->hidden( -NAME => $qfieldname.'_year',
2299 -VALUE => $year));
2300 } elsif ($qoptions->{'-TYPE'} =~/text/io) {
2301 return $q->textfield( -NAME => $qfieldname.'_year',
2302 -SIZE => $size,
2303 -MAXLENGTH => $max,
2304 -OVERRIDE => $qoptions->{'-YEAR_OVERRIDE'} || 1,
2305 -DEFAULT => $q->param( $qfieldname.'_year' ) || $year
2306 );
2307 } else {
2308 return $q->popup_menu( -NAME => $qfieldname.'_year',
2309 -VALUES => $values,
2310 -OVERRIDE => $qoptions->{'-YEAR_OVERRIDE'} || 1,
2311 -DEFAULT => $q->param( $qfieldname.'_year' ) || $year
2312 );
2313 }
2314 } # END sub HTML_year
2315  
2316  
2317 =item C<HTML_date> ( $fieldname [, $qoptions ] )
2318  
2319 Object Method:
2320  
2321 Generic form field method called by AUTOLOAD.
2322 Gets the default field params based on the fieldname and returns a
2323 set of date form fields or in viewable format if the displaymode is 'view'.
2324  
2325 B<Needs to be less Sybase Dependant and handle Time>
2326  
2327 =cut
2328  
2329 sub HTML_date {
2330 my $self = shift;
2331 return undef unless ref($self);
2332 my $fieldname = shift;
2333 my $qoptions = shift || $self->fieldhtmloptions( $fieldname ) || { };
2334 my $q = $self->cgi;
2335 my $qfieldname = $self->qfieldname( $fieldname );
2336  
2337 my $showhrs = $qoptions->{'-SHOW_HOURS'} || $qoptions->{'-SHOW_24HOURS'}; # Show the hours flag
2338 my $format = $qoptions->{'-FORMAT'} || "%B %e %Y"; # format for date strings
2339 my $delim = $qoptions->{'-DELIMITER'} || '/'; # delimiter for text dates
2340  
2341 my ($day, $dhidden) = $self->HTML_day( $fieldname, $qoptions );
2342 my ($mon, $mhidden) = $self->HTML_month($fieldname, $qoptions );
2343 my ($year, $yhidden) = $self->HTML_year( $fieldname, $qoptions );
2344 my ($time, $thidden) = $self->HTML_time( $fieldname, $qoptions );
2345  
2346 my $date_obj;
2347 $date_obj = $self->db_obj->$fieldname() unless ($self->displaymode eq 'add');
2348 if (!ref($date_obj)) {
2349 return undef if ($self->displaymode eq 'view');
2350 $date_obj = BingoX::Time->new();
2351 }
2352  
2353 ## if in display view mode get the date object from the scalar values ##
2354 if ($self->displaymode eq 'view') {
2355 if ($qoptions->{'-FORMAT'}) {
2356 return $date_obj->strftime( $format ) . $dhidden . $mhidden . $yhidden . $thidden;
2357 } elsif ($qoptions->{'-DELIMITER'}) {
2358 return "$mon $delim $day $delim $year" . ($showhrs ? " $time" : '');
2359 } else {
2360 return "$mon $day $year" . ($showhrs ? " $time" : '');
2361 }
2362 }
2363  
2364 my $html;
2365 if ($qoptions->{'-TYPE'} =~/view/io) {
2366 $html = "$mon $day $year";
2367 } elsif ($qoptions->{'-TYPE'} =~/text/io) {
2368 $html = "$mon $delim $day $delim $year";
2369 } else {
2370 $html = $mon . $day . $year;
2371 }
2372 $html .= "<BR>$time" if ($showhrs);
2373  
2374 ## should take care of time here. ##
2375 return $html;
2376 } # END sub HTML_date
2377  
2378  
2379 =item C<HTML_view> ( $fieldname )
2380  
2381 Object Method:
2382  
2383 Generic form field method called by AUTOLOAD.
2384 Gets the default field params based on the fieldname and returns the value
2385 in viewable format.
2386  
2387 =cut
2388  
2389 sub HTML_view {
2390 my $self = shift;
2391 return undef unless ref($self);
2392 my $fieldname = shift;
2393 my $q = $self->cgi;
2394 my $qfieldname = $self->qfieldname( $fieldname );
2395 (defined($q->param( $qfieldname ))
2396 ? $q->param( $qfieldname )
2397 : (($self->displaymode ne 'add') ? $self->db_obj->$fieldname() : ''))
2398 . "\n";
2399 } # END sub HTML_view
2400  
2401 =item C<HTML_hidden> ( $fieldname [, $qoptions ] )
2402  
2403 Object Method:
2404  
2405 Generic form field method called by AUTOLOAD.
2406 Gets the default field params based on the fieldname and returns the value
2407 as a hidden input field.
2408  
2409 =cut
2410  
2411 sub HTML_hidden {
2412 my $self = shift;
2413 return undef unless ref($self);
2414 my $fieldname = shift;
2415 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2416 my $q = $self->cgi;
2417 my $qfieldname = $self->qfieldname( $fieldname );
2418 $qfieldname = $fieldname unless (defined $q->param( $qfieldname ));
2419 ($self->displaymode eq 'view')
2420 ? $self->db_obj->$fieldname()
2421 : $q->hidden( -NAME => $qfieldname,
2422 -OVERRIDE => $qoptions->{'-OVERRIDE'} || 1,
2423 -DEFAULT => (defined($q->param( $qfieldname ))
2424 ? $q->param( $qfieldname )
2425 : (($self->displaymode eq 'modify')
2426 ? $self->db_obj->$fieldname()
2427 : ($qoptions->{'-DEFAULT'} || '')))
2428 ) . "\n";
2429 } # END sub HTML_hidden
2430  
2431  
2432 =item C<HTML_text> ( $fieldname [, $qoptions ] )
2433  
2434 Object Method:
2435  
2436 Generic form field method called by AUTOLOAD.
2437 Gets the default field params based on the fieldname and returns the value
2438 in a text input field or in viewable format if the displaymode is 'view'.
2439  
2440 =cut
2441  
2442 sub HTML_text {
2443 my $self = shift;
2444 return undef unless ref($self);
2445 my $fieldname = shift;
2446 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2447 my $q = $self->cgi;
2448 my $qfieldname = $self->qfieldname( $fieldname );
2449 ($self->displaymode eq 'view')
2450 ? $self->db_obj->$fieldname()
2451 : $q->textfield( -NAME => $qfieldname,
2452 -SIZE => $qoptions->{'-SIZE'} || 50,
2453 -MAXLENGTH => $qoptions->{'-MAXLENGTH'}|| 200,
2454 -OVERRIDE => $qoptions->{'-OVERRIDE'} || 1,
2455 -DEFAULT => (defined($q->param( $qfieldname ))
2456 ? $q->param( $qfieldname )
2457 : (($self->displaymode eq 'modify')
2458 ? $self->db_obj->$fieldname()
2459 : ($qoptions->{'-DEFAULT'} || '')))
2460 ) . "\n";
2461 } # END sub HTML_text
2462  
2463 =item C<HTML_password> ( $fieldname [, $qoptions ] )
2464  
2465 Object Method:
2466  
2467 Generic form field method called by AUTOLOAD.
2468 Gets the default field params based on the fieldname and returns the value
2469 in a password input field with a corresponding or in an obscured format if the displaymode is 'view'.
2470  
2471 =cut
2472  
2473 sub HTML_password {
2474 my $self = shift;
2475 return undef unless ref($self);
2476 my $fieldname = shift;
2477 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2478 my $q = $self->cgi;
2479 my $qfieldname = $self->qfieldname( $fieldname );
2480 ($self->displaymode eq 'view')
2481 ? '&lt;NOT DISPLAYED&gt;'
2482 : $q->password_field(-NAME => $qfieldname,
2483 -SIZE => $qoptions->{'-SIZE'} || 50,
2484 -MAXLENGTH => $qoptions->{'-MAXLENGTH'}|| 200,
2485 -OVERRIDE => $qoptions->{'-OVERRIDE'} || 1,
2486 -DEFAULT => (defined($q->param( $qfieldname ))
2487 ? $q->param( $qfieldname )
2488 : (($self->displaymode eq 'modify')
2489 ? $self->db_obj->$fieldname()
2490 : ($qoptions->{'-DEFAULT'} || '')))
2491 ) . "\n";
2492 } # END sub HTML_password
2493  
2494  
2495 =item C<HTML_textarea> ( $fieldname [, $qoptions ] )
2496  
2497 Object Method:
2498  
2499 Generic form field method called by AUTOLOAD.
2500 Gets the default field params based on the fieldname and returns
2501 the value in a text input field or in viewable format if the
2502 displaymode is 'view'.
2503  
2504 =cut
2505  
2506 sub HTML_textarea {
2507 my $self = shift;
2508 return undef unless ref($self);
2509 my $fieldname = shift;
2510 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2511 my $q = $self->cgi;
2512 my $qfieldname = $self->qfieldname( $fieldname );
2513  
2514 if ($self->{'_displaymode'} eq 'view') {
2515 my $content = $self->db_obj->$fieldname();
2516 $content =~ s/(\r\n)|[\n\r]/<BR>/go;
2517 return $content;
2518 }
2519 $q->textarea( -NAME => $qfieldname,
2520 -ROWS => $qoptions->{'-ROWS'} || 6,
2521 -COLS => $qoptions->{'-COLS'} || 50,
2522 -WRAP => $qoptions->{'-WRAP'} || 'VIRTUAL',
2523 -OVERRIDE => $qoptions->{'-OVERRIDE'} || 1,
2524 -DEFAULT => (defined($q->param( $qfieldname ))
2525 ? $q->param( $qfieldname )
2526 : (($self->displaymode eq 'modify')
2527 ? $self->db_obj->$fieldname()
2528 : ($qoptions->{'-DEFAULT'} || '')))
2529 ) . "\n";
2530 } # END sub HTML_textarea
2531  
2532  
2533 =item C<HTML_popup> ( $fieldname [, $qoptions ] )
2534  
2535 Object Method:
2536  
2537 Generic form field method called by AUTOLOAD.
2538 Gets the default field params based on the fieldname and returns the value
2539 in a popup field or in viewable format if the displaymode is 'view'.
2540  
2541 Note: Not used yet because its not easy to populate the
2542 vaules & labels fields. USually always overloaded in the subclasses.
2543  
2544 B<OPTIMIZE>
2545  
2546 =cut
2547  
2548 sub HTML_popup {
2549 my $self = shift;
2550 my $class = ref($self) || return undef;
2551 my $fieldname = shift;
2552 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2553 my $foptions = $self->fieldoptions( $fieldname );
2554 my $q = $self->cgi;
2555  
2556 ## if modify then grab the actual values of the primary keys of the obect ##
2557 ## and prepend the fieldname with them. This will allow people to modify multiple ##
2558 ## objects on the same page. Always put the classname and the fieldname at the end ##
2559 ## of the fieldname. ##
2560 my $qfieldname = $self->qfieldname( $fieldname );
2561 my $relclass = $class->fieldrelclass( $fieldname );
2562 my $relclass_title_field = $relclass->title_field;
2563 $relclass =~ /(.+)::Data::(.+)$/;
2564 my $reladminclass = "${1}::Admin::${2}";
2565  
2566 ## show all of the objects as text. ##
2567 if ($self->displaymode eq 'view') {
2568 my $obj = @{ $relclass->list_obj(
2569 $self->dbh,
2570 {
2571 $relclass->primary_keys->[0] => $self->db_obj->$fieldname()
2572 }
2573 ) || [ ] }->[0];
2574 (ref($obj) ? return $obj->$relclass_title_field() : return 'None Selected') . "\n";
2575  
2576 ## get all of the objects with get_list_hash. ##
2577 } else {
2578 my $hashref = $relclass->get_list_hash( $self->dbh );
2579  
2580 ## Allows null value for popups ##
2581 if (!$foptions->{'not_null'} && $foptions->{'null_label'}) {
2582 $hashref->{'NULL'} = $foptions->{'null_label'};
2583 }
2584  
2585 # ## if the displaymode isn't add, then highlite the already related objects. ##
2586 # my $selected = ($self->displaymode eq 'add')
2587 # ? [ ]
2588 # : $self->db_obj->list_related( $relclass );
2589 # warn "Too Many Related" if ($selected->[1] && $debug);
2590  
2591 ## default - if there's already data in the query object, highlite those objects, ##
2592 ## otherwise if the displaymode if modify, show the data in the scrolling list where the ##
2593 ## keys are composit primary keys seperated by $self->pkd and the ##
2594 ## values are the title_fields of the objects, else don't highlite anything. ##
2595 $q->popup_menu( -NAME => $qfieldname,
2596 -VALUES => [ sort { $hashref->{$a} cmp $hashref->{$b} } keys %$hashref ], # need to fill in info later
2597 -LABELS => $hashref, # need to fill in info later
2598 -DEFAULT => (defined($q->param( $qfieldname ))
2599 ? $q->param( $qfieldname )
2600 : (($self->displaymode eq 'modify')
2601 ? (defined($self->db_obj->$fieldname())
2602 ? $self->db_obj->$fieldname()
2603 : ($foptions->{'null_label'}
2604 ? 'NULL'
2605 : $qoptions->{'-DEFAULT'} || ''))
2606 ## The trinary below is for parent child relationships. Basically, if ##
2607 ## we're adding a new object and this pop-up represents a list of our ##
2608 ## parent fields, we should default to the parent we came through. ##
2609 : ((($self->displaymode eq 'add') && ($reladminclass eq $self->parent_class) && ($q->param('parent_pcpkey')))
2610 ? (substr($q->param('parent_pcpkey'),rindex($q->param('parent_pcpkey'),$self->qfd)+1))
2611 : ($qoptions->{'-DEFAULT'} || ''))))
2612 ) . "\n";
2613 }
2614 } # END sub HTML_popup
2615  
2616  
2617 =item C<HTML_radio> ( $fieldname [, $qoptions ] )
2618  
2619 Object Method:
2620  
2621 Generic form field method called by AUTOLOAD.
2622 Gets the default field params based on the fieldname and returns the value
2623 in a group of radio buttons or in viewable format if the displaymode is 'view'.
2624  
2625 =cut
2626  
2627 sub HTML_radio {
2628 my $self = shift;
2629 my $class = ref($self) || return undef;
2630 my $fieldname = shift;
2631 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2632 my $q = $self->cgi;
2633 my $qfieldname = $self->qfieldname( $fieldname );
2634  
2635 my $relclass = $class->fieldrelclass( $fieldname );
2636 my $relclass_title_field = $relclass->title_field;
2637  
2638 ## show all of the objects as text. ##
2639 if ($self->displaymode eq 'view') {
2640 my $obj = @{ $relclass->list_obj( $self->dbh,
2641 {
2642 $relclass->primary_keys->[0] => $self->db_obj->$fieldname()
2643 }
2644 ) || [ ] }->[0];
2645 (ref($obj) ? return $obj->$relclass_title_field() : return 'None Selected') . "\n";
2646  
2647 ## get all of the objects with get_list_hash. ##
2648 } else {
2649 my $hashref = $relclass->get_list_hash( $self->dbh );
2650
2651 ## if the displaymode isn't add, then highlite the already related objects. ##
2652 my $selected = ($self->displaymode eq 'add')
2653 ? [ ]
2654 : $self->db_obj->list_related( $relclass );
2655  
2656 warn "Too Many Related" if ($selected->[1] && $debug);
2657  
2658 $q->autoEscape(0) if ($qoptions->{'-NOESCAPE'});
2659 my $val = $q->radio_group(
2660 -NAME => $qfieldname,
2661 -VALUES => [ keys %$hashref ], # need to fill in info later
2662 -LABELS => $hashref, # need to fill in info later
2663 -LINEBREAK => $qoptions->{'-LINEBREAK'} || 'true',
2664 -DEFAULT => (defined($q->param( $qfieldname ))
2665 ? $q->param( $qfieldname )
2666 : (($self->displaymode eq 'modify')
2667 ? $self->db_obj->$fieldname()
2668 : ($qoptions->{'-DEFAULT'} || '')))
2669 );
2670 $q->autoEscape(1) if ($qoptions->{'-NOESCAPE'});
2671 return $val;
2672 }
2673 } # END sub HTML_radio
2674  
2675  
2676 =item C<HTML_scrolling> ( $fieldname [, $qoptions ] )
2677  
2678 Object Method:
2679  
2680 Generic form field method called by AUTOLOAD.
2681 Gets the default field params based on the fieldname and returns the value
2682 in a scrolling list field or in viewable format if the displaymode is 'view'.
2683  
2684 Note: Not used yet because its not easy to populate the
2685 vaules & labels fields. USually always overloaded in the subclasses.
2686  
2687 B<OPTIMIZE>
2688  
2689 =cut
2690  
2691 sub HTML_scrolling {
2692 my $self = shift;
2693 return undef unless ref($self);
2694 my $fieldname = shift;
2695 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2696 my $q = $self->cgi;
2697  
2698 ## if modify then grab the actual values of the primary keys of the obect ##
2699 ## and prepend the fieldname with them. This will allow people to modify multiple ##
2700 ## objects on the same page. Always put the classname and the fieldname at the end ##
2701 ## of the fieldname. ##
2702 my $qfieldname = $self->qfieldname( $fieldname );
2703  
2704 my $relclass = $self->fieldrelclass( $fieldname );
2705 my $relclass_title_field = $relclass->title_field;
2706  
2707 ## show all of the objects as text. ##
2708 if ($self->displaymode eq 'view') {
2709 my $code = $self->db_obj->stream_related( $relclass );
2710 my $list;
2711 if (ref $code) {
2712 while (my $obj = $code->()) {
2713 $list .= $obj->$relclass_title_field() . "<BR>\n";
2714 }
2715 }
2716 ## NEED AN ELSE AS AN ERROR CHECK! ##
2717 ($list ? return $list : return 'None Selected') . "\n";
2718 ## get all of the objects with get_list_hash. ##
2719 } else {
2720 my $hashref = $relclass->get_list_hash( $self->dbh );
2721  
2722 ## if the displaymode isn't add, then highlite the already related objects. ##
2723 my $selected = ($self->displaymode eq 'add')
2724 ? [ ]
2725 : $self->db_obj->list_related( $relclass );
2726  
2727 ## default - if there's already data in the query object, highlite those objects, ##
2728 ## otherwise if the displaymode if modify, show the data in the scrolling list where the ##
2729 ## keys are composit primary keys seperated by $self->pkd and the ##
2730 ## values are the title_fields of the objects, else don't highlite anything. ##
2731 return $q->scrolling_list(
2732 -NAME => $qfieldname,
2733 -OVERRIDE => $qoptions->{'-OVERRIDE'} || 1,
2734 -SIZE => $qoptions->{'-SIZE'} || 6,
2735 -MULTIPLE => $qoptions->{'-MULTIPLE'} || 'true',
2736 -VALUES => [ keys %{$hashref} ],
2737 -LABELS => $hashref,
2738 -DEFAULT => ($q->param( $qfieldname )
2739 ? [ $q->param( $qfieldname ) ]
2740 : (($self->displaymode eq 'modify')
2741 ? [
2742 map {
2743 $_->cpkey()
2744 } @$selected
2745 ]
2746 : ($qoptions->{'-DEFAULT'} || [ ])))
2747 ) . "\n";
2748 }
2749 } # END sub HTML_scrolling
2750  
2751  
2752 =item C<HTML_checkbox> ( $fieldname [, $qoptions ] )
2753  
2754 Object Method:
2755  
2756 Generic form field method called by AUTOLOAD.
2757 Gets the default field params based on the fieldname and returns the value
2758 in a checkbox field or in viewable format if the displaymode is 'view'.
2759  
2760 Note: Not used yet because its not easy to populate the
2761 vaules & labels fields. USually always overloaded in the subclasses.
2762  
2763 =cut
2764  
2765 sub HTML_checkbox {
2766 my $self = shift;
2767 return undef unless ref($self);
2768 my $fieldname = shift;
2769 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2770 my $q = $self->cgi;
2771 my $qfieldname = $self->qfieldname( $fieldname );
2772 ($self->displaymode eq 'view')
2773 ? $self->db_obj->$fieldname()
2774 : $q->checkbox(
2775 -NAME => $qfieldname,
2776 -CHECKED => $self->displaymode eq 'modify'
2777 ? $self->db_obj->$fieldname()
2778 : $qoptions->{'-CHECKED'},
2779 -VALUE => $qoptions->{'-VALUE'} || '1',
2780 -LABEL => $qoptions->{'-LABEL'} || ''
2781 );
2782 } # END sub HTML_checkbox
2783  
2784  
2785 =item C<HTML_file> ( $fieldname [, $qoptions ] )
2786  
2787 Object Method:
2788  
2789 Generic form field method called by AUTOLOAD.
2790 Gets the default field params based on the fieldname and returns the value
2791 in a form file upload field or in viewable format if the displaymode is 'view'.
2792  
2793 =cut
2794  
2795 sub HTML_file {
2796 my $self = shift;
2797 return undef unless ref($self);
2798 my $fieldname = shift;
2799 my $qoptions = shift || $self->fieldhtmloptions( $fieldname );
2800 my $q = $self->cgi;
2801 my $qfieldname = $self->qfieldname( $fieldname );
2802 ($self->displaymode eq 'view')
2803 ? $self->db_obj->$fieldname()
2804 : $q->filefield(
2805 -NAME => $qfieldname,
2806 -SIZE => $qoptions->{'-SIZE'} || 40,
2807 -MAXLENGTH => $qoptions->{'-MAXLENGTH'} || 200,
2808 -OVERRIDE => 1,
2809 -DEFAULT => '' # browsers null defaults in upload fields anyway
2810 );
2811 } #END sub HTML_file
2812  
2813  
2814 =back
2815  
2816 =head2 SANITY METHODS
2817  
2818 These methods can be overridden at any level to provide customized sanity
2819 checking. These methods always return an error message on failure, and an
2820 empty string on success.
2821  
2822 =over 4
2823  
2824 =item C<sane_regex> ( $data, $regex [, $error ] )
2825  
2826 If C<$data> matches the regular expression in C<$regex>, returns an empty
2827 string. Otherwise, returns C<$error> or a default error message.
2828  
2829 =cut
2830  
2831 sub sane_regex {
2832 my $self = shift;
2833 my $data = shift;
2834 my $regex = shift;
2835 my $error = shift || 'Not correctly formatted.';
2836 warn("sane_regex('$data', '$regex', '$error')\n") if $debug > 2;
2837 return $error unless $data =~ /$regex/;
2838 return "";
2839 } #END sub sane_regex
2840  
2841  
2842 =item C<sane_maxlength> ( $data, $length )
2843  
2844 Makes sure that C<$data> is no more than C<$length> characters long. Returns
2845 an error message on failure, an empty string on success.
2846  
2847 =cut
2848  
2849 sub sane_maxlength {
2850 my $self = shift;
2851 my $data = shift;
2852 my $length = shift;
2853 warn("sane_maxlength('$data', $length)\n") if $debug > 2;
2854 return "Exceeds $length characters in length." unless (length($data) <= $length);
2855 return "";
2856 } #END sub sane_maxlength
2857  
2858  
2859 =item C<sane_minlength> ( $data, $length )
2860  
2861 Makes sure that C<$data> is at least C<$length> characters long. Returns an
2862 error message on failure, an empty string on success.
2863  
2864 =cut
2865  
2866 sub sane_minlength {
2867 my $self = shift;
2868 my $data = shift;
2869 my $length = shift;
2870 warn("sane_minlength('$data', $length)\n") if $debug > 2;
2871 return "Must be at least $length characters." unless (length($data) >= $length);
2872 return "";
2873 } #END sub sane_minlength
2874  
2875  
2876 =item C<AUTOLOAD> (?????)
2877  
2878 AUTOLOAD method - Figures out what method was being called by stripping the
2879 fully qualified portion of $AUTOLOAD out as $name. This method is expected to be
2880 a column name (an element of fieldlist). It then figures out what that
2881 columns fieldtype is from fieldtype($name). It then calls that type
2882 of HTML display method and passed $name to it.
2883  
2884 =cut
2885  
2886 sub AUTOLOAD {
2887 return if $AUTOLOAD =~ /::DESTROY$/;
2888 my $self = shift;
2889 my $name = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1); # strip fully-qualified portion
2890
2891 return undef unless $self->fieldtype( $name ); # need better error
2892 my $meth = 'HTML_' . $self->fieldtype( $name );
2893 $self->$meth( $name ); # call one of the private fieldtype methods above
2894 } # END sub AUTOLOAD
2895  
2896  
2897 1;
2898  
2899 __END__