| 1 | # BingoX::Cobalt | ||
| 2 | # ----------------- | ||
| 3 | # $Revision: 2.15 $ | ||
| 4 | # $Date: 2000/12/12 18:53:36 $ | ||
| 5 | # --------------------------------------------------------- | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | BingoX::Cobalt - Cobalt display parent class containing generic methods | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | use BingoX::Cobalt; | ||
| 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::Cobalt object OR sub-class | ||
| 23 | # $object - BingoX::Cobalt object | ||
| 24 | |||
| 25 | CONSTRUCTORS | ||
| 26 | |||
| 27 | $BR = $proto->new( [ $app, $dbh, ] $data_object ); | ||
| 28 | $BR = $proto->get( [ $dbh, ] \%params ); | ||
| 29 | |||
| 30 | STREAM CONSTRUCTOR METHODS | ||
| 31 | |||
| 32 | $SR = $proto->stream_obj( $app [, $dbh, ] @_ ); | ||
| 33 | $SR = $proto->stream_hash( [ $dbh, ] @_ ); | ||
| 34 | $SR = $proto->stream_array( [ $dbh, ] @_ ); | ||
| 35 | |||
| 36 | LIST CONSTRUCTOR METHODS | ||
| 37 | |||
| 38 | $AR = $proto->list_obj( $app [, $dbh, ] @_ ); | ||
| 39 | $AR = $proto->list_hash( [ $dbh, ] @_ ); | ||
| 40 | $AR = $proto->list_array( [ $dbh, ] @_ ); | ||
| 41 | |||
| 42 | RELATION METHODS | ||
| 43 | |||
| 44 | $SR = $object->stream_related( $rel_display_class [, \@fields] [, \@sort] [, $unary_rev_flag ] ); | ||
| 45 | $AR = $object->list_related( $rel_display_class [, \@fields] [, \@sort] [, $unary_rev_flag ] ); | ||
| 46 | |||
| 47 | OBJECT METHODS | ||
| 48 | |||
| 49 | $BR = $object->db_obj( ); | ||
| 50 | $BR = $object->dbh( ); | ||
| 51 | $BR = $object->app( ); | ||
| 52 | $BR = $proto->r( ); | ||
| 53 | $BR = $proto->cgi( ); | ||
| 54 | $BR = $proto->conf( ); | ||
| 55 | |||
| 56 | PARSER METHODS | ||
| 57 | |||
| 58 | $SV = $object->include( @_ ); | ||
| 59 | $SV = $object->xinclude( @_ ); | ||
| 60 | |||
| 61 | CLASS DATA ACCESSOR METHODS | ||
| 62 | |||
| 63 | $SV = $proto->data_class( ); | ||
| 64 | $HR = $proto->display_fields( ); | ||
| 65 | $AR = $proto->display_order( ); | ||
| 66 | |||
| 67 | =head1 REQUIRES | ||
| 68 | |||
| 69 | BingoX::Time, CGI, Carp, strict | ||
| 70 | |||
| 71 | =head1 EXPORTS | ||
| 72 | |||
| 73 | Nothing | ||
| 74 | |||
| 75 | =head1 DESCRIPTION | ||
| 76 | |||
| 77 | Cobalt provides the generic API for BingoX display classes. | ||
| 78 | Cobalt uses display objects that wrap Carbon data objects | ||
| 79 | (Cobalt mono Carbide?). | ||
| 80 | |||
| 81 | =head1 CLASS VARIABLES | ||
| 82 | |||
| 83 | Classes that inherit from Cobalt should have the following class variables: | ||
| 84 | |||
| 85 | =head2 REQUIRED | ||
| 86 | |||
| 87 | =over 4 | ||
| 88 | |||
| 89 | =item * @display_order | ||
| 90 | |||
| 91 | The order in which to display fields (if one so chose to display them ;) | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head2 OPTIONAL | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item * $data_class | ||
| 100 | |||
| 101 | The name of the data class that corresponds to the display class. If this | ||
| 102 | value is not defined, it will default to the name of the display class, with | ||
| 103 | the first instance of "::Display::" changed to "::Data::". | ||
| 104 | |||
| 105 | =item * %display_fields | ||
| 106 | |||
| 107 | A total mystery. If this variable is not defined, eack key is an field from the | ||
| 108 | C<@display_order> variable, and each value is 1. | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | package BingoX::Cobalt; | ||
| 117 | |||
| 118 | |||
| 119 | use CGI; | ||
| 120 | use Carp; | ||
| 121 | use strict; | ||
| 122 | use BingoX::Time; | ||
| 123 | use vars qw($AUTOLOAD $debug); | ||
| 124 | |||
| 125 | BEGIN { | ||
| 126 | $BingoX::Cobalt::REVISION = (qw$Revision: 2.15 $)[-1]; | ||
| 127 | $BingoX::Cobalt::VERSION = '1.92'; | ||
| 128 | |||
| 129 | $debug = undef; | ||
| 130 | } | ||
| 131 | |||
| 132 | =head2 CONSTRUCTORS | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item C<new> ( [ $app ] [, $dbh ], $data_obj ) | ||
| 137 | |||
| 138 | Given a data object, returns a Display object of the class B<it was called as>. | ||
| 139 | This means you could concievably provide a data object of a totally inapproriate | ||
| 140 | type, and this method will not compain about it. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | sub new { | ||
| 144 | my $self = shift; | ||
| 145 | my $class = ref($self) || $self; | ||
| 146 | my $app = ref($self) ? $self->app : shift; | ||
| 147 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 148 | my $data = shift; | ||
| 149 | |||
| 150 | bless({ | ||
| 151 | _app => $app, | ||
| 152 | _date => BingoX::Time->new, | ||
| 153 | _db_obj => $data, | ||
| 154 | _dbh => $dbh | ||
| 155 | }, $class); | ||
| 156 | } # END of new | ||
| 157 | |||
| 158 | |||
| 159 | =item C<get> ( [ $app ] [, $dbh, ] \%params ) | ||
| 160 | |||
| 161 | Returns a display object. | ||
| 162 | |||
| 163 | =cut | ||
| 164 | sub get { | ||
| 165 | my $self = shift; | ||
| 166 | my $class = ref($self) || $self; | ||
| 167 | my $app = ref($self) ? $self->app : shift; | ||
| 168 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 169 | my $params = shift || return undef; | ||
| 170 | |||
| 171 | my $code = $class->stream_obj( $app, $dbh, $params, @_ ) || return undef; | ||
| 172 | my $obj = $code->(); | ||
| 173 | $code->(1); # finish | ||
| 174 | undef $code; | ||
| 175 | return $obj; | ||
| 176 | } # END of get | ||
| 177 | |||
| 178 | |||
| 179 | =back | ||
| 180 | |||
| 181 | =head2 STREAM METHODS | ||
| 182 | |||
| 183 | =over 4 | ||
| 184 | |||
| 185 | =item C<stream_obj> ( $app [, $dbh ], @_ ) | ||
| 186 | |||
| 187 | Returns an stream (CODE ref) that will return objects (See list_obj) | ||
| 188 | If you don't know how to use streams, use list_obj() instead. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | sub stream_obj { | ||
| 192 | my $self = shift; | ||
| 193 | my $class = ref($self) || $self; | ||
| 194 | my $app = shift; | ||
| 195 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 196 | my $stream = $class->data_class->stream_obj( $dbh, @_ ); | ||
| 197 | return undef unless (ref $stream); | ||
| 198 | return bless(sub { | ||
| 199 | my $self = bless({ | ||
| 200 | _app => $app, | ||
| 201 | _db_obj => $stream->(@_), | ||
| 202 | _dbh => $dbh | ||
| 203 | }, $class); | ||
| 204 | return undef unless ref($self->{'_db_obj'}); | ||
| 205 | return $self; | ||
| 206 | }, 'BingoX::Cobalt::Stream'); | ||
| 207 | } # END of stream_obj | ||
| 208 | |||
| 209 | |||
| 210 | =item C<stream_hash> ( [ $dbh, ] @_ ) | ||
| 211 | |||
| 212 | Forwards to $class->data_class->stream_hash | ||
| 213 | |||
| 214 | =cut | ||
| 215 | sub stream_hash { | ||
| 216 | my $self = shift; | ||
| 217 | my $class = ref($self) || $self; | ||
| 218 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 219 | $class->data_class->stream_hash( $dbh, @_ ); | ||
| 220 | } # END of stream_hash | ||
| 221 | |||
| 222 | |||
| 223 | =item C<stream_array> ( [ $dbh, ] @_ ) | ||
| 224 | |||
| 225 | Forwards to $class->data_class->stream_array | ||
| 226 | |||
| 227 | =cut | ||
| 228 | sub stream_array { | ||
| 229 | my $self = shift; | ||
| 230 | my $class = ref($self) || $self; | ||
| 231 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 232 | $class->data_class->stream_array( $dbh, @_ ); | ||
| 233 | } # END of stream_array | ||
| 234 | |||
| 235 | |||
| 236 | =back | ||
| 237 | |||
| 238 | =head2 LIST METHODS | ||
| 239 | |||
| 240 | =over 4 | ||
| 241 | |||
| 242 | =item C<list_obj> ( $app [, $dbh, ] @_ ) | ||
| 243 | |||
| 244 | Returns an array ref of Display objects. Used mainly by Apache::XPP (the BingoX | ||
| 245 | default parser) coders. Everyone else is probably using $self->data_class->list_obj(). | ||
| 246 | |||
| 247 | =cut | ||
| 248 | sub list_obj { | ||
| 249 | my $self = shift; | ||
| 250 | my $class = ref($self) || $self; | ||
| 251 | my $app = shift; | ||
| 252 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 253 | |||
| 254 | my @data = ( | ||
| 255 | map { bless({ | ||
| 256 | _app => $app, | ||
| 257 | _db_obj => $_, | ||
| 258 | _dbh => $dbh | ||
| 259 | }, $class) | ||
| 260 | } @{ $class->data_class->list_obj( $dbh, @_ ) || return undef } | ||
| 261 | ); | ||
| 262 | return \@data; | ||
| 263 | } # END of list_obj | ||
| 264 | |||
| 265 | |||
| 266 | =item C<list_hash> ( [ $dbh, ] @_ ) | ||
| 267 | |||
| 268 | Forwards to $class->data_class->list_hash | ||
| 269 | |||
| 270 | =cut | ||
| 271 | sub list_hash { | ||
| 272 | my $self = shift; | ||
| 273 | my $class = ref($self) || $self; | ||
| 274 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 275 | return $class->data_class->list_hash( $dbh, @_ ); | ||
| 276 | } # END of list_hash | ||
| 277 | |||
| 278 | |||
| 279 | =item C<list_array> ( [ $dbh, ] @_ ) | ||
| 280 | |||
| 281 | Forwards to $class->data_class->list_array | ||
| 282 | |||
| 283 | =cut | ||
| 284 | sub list_array { | ||
| 285 | my $self = shift; | ||
| 286 | my $class = ref($self) || $self; | ||
| 287 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 288 | return $class->data_class->list_array( $dbh, @_ ); | ||
| 289 | } # END of list_array | ||
| 290 | |||
| 291 | |||
| 292 | =item C<show_obj> ( $app [, $dbh ] [, \%params ] [, $template ] ) | ||
| 293 | |||
| 294 | Behaves just like list_obj() or stream_obj() but will xinclude each | ||
| 295 | object in a passed $template. Normally called by AUTOLOAD (via a show_* call), | ||
| 296 | but you can call it explicitly if you like. Pass $app and $dbh if it's | ||
| 297 | called statically. | ||
| 298 | |||
| 299 | =cut | ||
| 300 | sub show_obj { | ||
| 301 | my $self = shift; | ||
| 302 | my $class = ref($self) || $self; | ||
| 303 | my $app = ref($self) ? $self->app : shift; | ||
| 304 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 305 | my $params = shift || { }; | ||
| 306 | my $template = shift || return undef; | ||
| 307 | my $code = $class->stream_obj( $app, $dbh, $params, @_ ) || warn "stream failed"; | ||
| 308 | while (my $obj = $code->()) { | ||
| 309 | $app->xinclude( $template, $obj ) || warn "Can't include object $obj in template $template"; | ||
| 310 | } | ||
| 311 | $code->(1); | ||
| 312 | return 1; | ||
| 313 | } # END of show_obj | ||
| 314 | |||
| 315 | |||
| 316 | =back | ||
| 317 | |||
| 318 | =head2 RELATION METHODS | ||
| 319 | |||
| 320 | =over 4 | ||
| 321 | |||
| 322 | =item C<stream_related> ( $rel_display_class [, \@fields] [, \@sort] [, $unary_rev_flag ] ) | ||
| 323 | |||
| 324 | Returns a stream of related display objects from the $rel_display_class class. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | sub stream_related { | ||
| 328 | my $self = shift; | ||
| 329 | my $class = ref($self) || $self; | ||
| 330 | my $app = ref($self) ? $self->app : shift; | ||
| 331 | my $dbh = ref($self) ? $self->dbh : shift; | ||
| 332 | my $rclass = shift; | ||
| 333 | my $rdclass = $rclass->data_class || $rclass; # get the display class's data class if they passed that instead. | ||
| 334 | my $stream = $self->db_obj->stream_related( $rdclass, @_ ) || return undef; | ||
| 335 | return bless(sub { | ||
| 336 | while (my $db_obj = $stream->()) { | ||
| 337 | return bless({ | ||
| 338 | _app => $app, | ||
| 339 | _db_obj => $db_obj, | ||
| 340 | _dbh => $dbh | ||
| 341 | }, $rclass); | ||
| 342 | } | ||
| 343 | }, 'BingoX::Cobalt::Stream'); | ||
| 344 | } # END of stream_related | ||
| 345 | |||
| 346 | |||
| 347 | =item list_related( $rel_display_class [, \@fields] [, \@sort] [, $unary_rev_flag ] ) | ||
| 348 | |||
| 349 | Returns an array ref of related display objects from the $rel_display_class class. | ||
| 350 | |||
| 351 | =cut | ||
| 352 | sub list_related { | ||
| 353 | my $self = shift; | ||
| 354 | return undef unless ref($self && $self->db_obj); | ||
| 355 | my $stream = $self->stream_related( @_ ); | ||
| 356 | return undef unless (ref $stream); | ||
| 357 | my @list; | ||
| 358 | push(@list, $a) while ($a = $stream->()); | ||
| 359 | return \@list; | ||
| 360 | } # END of list_related | ||
| 361 | |||
| 362 | |||
| 363 | =back | ||
| 364 | |||
| 365 | =head2 OBJECT METHODS | ||
| 366 | |||
| 367 | =over 4 | ||
| 368 | |||
| 369 | =item C<db_obj> ( ) | ||
| 370 | |||
| 371 | Returns the Data object for the Display Class that it was called | ||
| 372 | from, with the constraint being: map $q->param( $_ ) @{ $data_class->primary_keys } | ||
| 373 | |||
| 374 | =cut | ||
| 375 | sub db_obj { | ||
| 376 | my $self = shift; | ||
| 377 | return undef unless (ref $self); # needs to be called as an object method | ||
| 378 | return $self->{'_db_obj'} if (ref $self->{'_db_obj'}); # return cached object | ||
| 379 | my $q = $self->cgi; | ||
| 380 | my $params = { }; | ||
| 381 | foreach (@{ $self->data_class->primary_keys }) { | ||
| 382 | return undef unless (defined $q->param( $_ )); | ||
| 383 | $params->{ $_ } = $q->param( $_ ); | ||
| 384 | } | ||
| 385 | |||
| 386 | return undef unless (keys %$params); # primary key needs to exist | ||
| 387 | my $arrayref = $self->data_class->list_obj($self->dbh, $params); | ||
| 388 | $self->{'_db_obj'} = $arrayref->[0] if (ref $arrayref); | ||
| 389 | $self->{'_db_obj'}; | ||
| 390 | } # END of db_obj | ||
| 391 | |||
| 392 | |||
| 393 | ######################## Display Methods ######################## | ||
| 394 | |||
| 395 | |||
| 396 | =item C<hour_menu> ( $name [, $default ] [, $24hr ] ) | ||
| 397 | |||
| 398 | Returns a Popup Menu with Hours. | ||
| 399 | |||
| 400 | =cut | ||
| 401 | sub hour_menu { | ||
| 402 | my $self = shift; | ||
| 403 | my $name = shift; | ||
| 404 | my $default = shift || undef; | ||
| 405 | my $hr24 = shift || undef; | ||
| 406 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 407 | my $values; | ||
| 408 | |||
| 409 | if ($hr24) { | ||
| 410 | $default = $date->hour; | ||
| 411 | $values = $date->hours24; | ||
| 412 | } else { | ||
| 413 | $values = $date->hours; | ||
| 414 | if ($date->hour > 0 && $date->hour <= 12) { | ||
| 415 | $default = $date->hour; | ||
| 416 | } elsif ($date->hour == 0) { | ||
| 417 | $default = '12'; | ||
| 418 | } else { | ||
| 419 | $default = $date->hour - 12; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 424 | -VALUES => $values, | ||
| 425 | -DEFAULT => sprintf("%02d", $default), | ||
| 426 | -OVERRIDE => 1); | ||
| 427 | } # END of sub hour_menu | ||
| 428 | |||
| 429 | |||
| 430 | =item C<min_menu> ( $name [, $default ] ) | ||
| 431 | |||
| 432 | Returns a Popup Menu with Minutes. | ||
| 433 | |||
| 434 | =cut | ||
| 435 | sub min_menu { | ||
| 436 | my $self = shift; | ||
| 437 | my $name = shift; | ||
| 438 | my $default = shift || undef; | ||
| 439 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 440 | |||
| 441 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 442 | -VALUES => $date->minutes, | ||
| 443 | -DEFAULT => sprintf("%02d", $date->min), | ||
| 444 | -OVERRIDE => 1); | ||
| 445 | } # END of sub min_menu | ||
| 446 | |||
| 447 | |||
| 448 | =item C<am_pm_menu> ( $name [, $default ] ) | ||
| 449 | |||
| 450 | Returns a Popup Menu with AM/PM menus. | ||
| 451 | |||
| 452 | =cut | ||
| 453 | sub am_pm_menu { | ||
| 454 | my $self = shift; | ||
| 455 | my $name = shift; | ||
| 456 | my $default = shift || undef; | ||
| 457 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 458 | |||
| 459 | if ($date->hour >= 0 && $date->hour < 12) { | ||
| 460 | $default = 'AM'; | ||
| 461 | } else { | ||
| 462 | $default = 'PM'; | ||
| 463 | } | ||
| 464 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 465 | -VALUES => [ 'AM', 'PM' ], | ||
| 466 | -DEFAULT => $default || '', | ||
| 467 | -OVERRIDE => 1); | ||
| 468 | } # END of sub am_pm_menu | ||
| 469 | |||
| 470 | |||
| 471 | =item C<day_menu> ( $name [, $default ] ) | ||
| 472 | |||
| 473 | Returns a Popup Menu with the Days of the Month. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | sub day_menu { | ||
| 477 | my $self = shift; | ||
| 478 | my $name = shift; | ||
| 479 | my $default = shift || undef; | ||
| 480 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 481 | |||
| 482 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 483 | -VALUES => [ 1 .. 31 ], | ||
| 484 | -DEFAULT => $date->mday, | ||
| 485 | -OVERRIDE => 1); | ||
| 486 | } # END of sub day_menu | ||
| 487 | |||
| 488 | |||
| 489 | =item C<month_menu> ( $name [, $default ] ) | ||
| 490 | |||
| 491 | Returns a Popup Menu with the Months of the Year. | ||
| 492 | |||
| 493 | =cut | ||
| 494 | sub month_menu { | ||
| 495 | my $self = shift; | ||
| 496 | my $name = shift; | ||
| 497 | my $default = shift || undef; | ||
| 498 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 499 | |||
| 500 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 501 | -VALUES => [ 1 .. 12 ], | ||
| 502 | -LABELS => $date->months, | ||
| 503 | -DEFAULT => $date->mon, | ||
| 504 | -OVERRIDE => 1); | ||
| 505 | } # END of sub month_menu | ||
| 506 | |||
| 507 | |||
| 508 | =item C<year_menu> ( $name [, $default ] ) | ||
| 509 | |||
| 510 | Returns a Popup Menu with a list of Years up to 20 years from now. | ||
| 511 | |||
| 512 | =cut | ||
| 513 | sub year_menu { | ||
| 514 | my $self = shift; | ||
| 515 | my $name = shift; | ||
| 516 | my $default = shift || undef; | ||
| 517 | my $date = ref($default) ? $default : BingoX::Time->new; | ||
| 518 | my $start_year = $date->year; | ||
| 519 | |||
| 520 | return $self->cgi->popup_menu( -NAME => $name, | ||
| 521 | -VALUES => [ ($start_year - 20) .. ($start_year + 20) ], | ||
| 522 | -DEFAULT => $date->year, | ||
| 523 | -OVERRIDE => 1); | ||
| 524 | } # END of sub year_menu | ||
| 525 | |||
| 526 | |||
| 527 | =item C<errors_list> ( ) | ||
| 528 | |||
| 529 | Returns list of errors in list format. | ||
| 530 | |||
| 531 | =cut | ||
| 532 | sub errors_list { | ||
| 533 | my $self = shift; | ||
| 534 | my $list = shift; | ||
| 535 | my $q = $self->cgi; | ||
| 536 | my $errs = $self->{'_errors'}; | ||
| 537 | return '' unless (keys %$errs); | ||
| 538 | my $html = '<B>' . $self->conf->error_msg . "</B>\n" unless ($errs->{'success'}); | ||
| 539 | |||
| 540 | if ($list) { | ||
| 541 | $html .= "<" . $list . ">\n"; | ||
| 542 | foreach (sort keys %$errs) { $html .= "<LI>" . $errs->{$_} . "\n" } | ||
| 543 | $html .= "</" . $list . ">\n"; | ||
| 544 | } else { | ||
| 545 | foreach (keys %$errs) { $html .= $errs->{$_} . "\n" } | ||
| 546 | } | ||
| 547 | |||
| 548 | return $html; | ||
| 549 | } # END of errors_list | ||
| 550 | |||
| 551 | |||
| 552 | ######################## Form Methods ######################## | ||
| 553 | |||
| 554 | =item C<alink> ( \%params, $text | \@text ) | ||
| 555 | |||
| 556 | Returns a hyper link with the params passed to it, -HREF, -JAVASCRIPT. | ||
| 557 | |||
| 558 | =cut | ||
| 559 | sub alink { | ||
| 560 | my $self = shift; | ||
| 561 | return $self->cgi->a(@_); | ||
| 562 | } # END of sub alink | ||
| 563 | |||
| 564 | |||
| 565 | =item C<submit> ( $name, %params ) | ||
| 566 | |||
| 567 | Returns a Submit Button with the params passed to it, -NAME, -VALUE, -JAVASCRIPT. | ||
| 568 | |||
| 569 | =cut | ||
| 570 | sub submit { | ||
| 571 | my $self = shift; | ||
| 572 | my $name = shift; | ||
| 573 | return $self->cgi->submit(-NAME => $name, @_); | ||
| 574 | } # END of sub submit | ||
| 575 | |||
| 576 | |||
| 577 | =item C<image_button> ( $name, %params ) | ||
| 578 | |||
| 579 | Returns a hyper link with the params passed to it, -HREF, -SRC, | ||
| 580 | -BORDER, -ALT, -JAVASCRIPT. | ||
| 581 | |||
| 582 | =cut | ||
| 583 | sub image_button { | ||
| 584 | my $self = shift; | ||
| 585 | my $name = shift; | ||
| 586 | return $self->cgi->image_button(-NAME => $name, @_); | ||
| 587 | } # END of sub image_button | ||
| 588 | |||
| 589 | |||
| 590 | =item C<textfield> ( $name, %params ) | ||
| 591 | |||
| 592 | Returns a Text Field with the params passed to it, -VALUE, -SIZE, | ||
| 593 | -OVERRIDE. | ||
| 594 | |||
| 595 | =cut | ||
| 596 | sub textfield { | ||
| 597 | my $self = shift; | ||
| 598 | my $name = shift; | ||
| 599 | return $self->cgi->textfield(-NAME => $name, -DEFAULT => ($self->_get_default($name) || ''), @_); | ||
| 600 | } # END of sub textfield | ||
| 601 | |||
| 602 | |||
| 603 | =item C<hidden> ( $name, %params ) | ||
| 604 | |||
| 605 | Returns a Hidden Field with the params passed to it, -VALUE. | ||
| 606 | |||
| 607 | =cut | ||
| 608 | sub hidden { | ||
| 609 | my $self = shift; | ||
| 610 | my $name = shift; | ||
| 611 | return $self->cgi->hidden(-NAME => $name, @_); | ||
| 612 | } # END of sub hidden | ||
| 613 | |||
| 614 | |||
| 615 | =item C<popup_menu> ( $name, %params ) | ||
| 616 | |||
| 617 | Returns a Popup Menu with the params passed to it, -VALUES, -LABELS, | ||
| 618 | -OVERRIDE. | ||
| 619 | |||
| 620 | =cut | ||
| 621 | sub popup_menu { | ||
| 622 | my $self = shift; | ||
| 623 | my $name = shift; | ||
| 624 | return $self->cgi->popup_menu(-NAME => $name, -DEFAULT => ($self->_get_default($name) || ''), @_); | ||
| 625 | } # END of sub popup_menu | ||
| 626 | |||
| 627 | |||
| 628 | =item C<checkbox> ( $name, %params ) | ||
| 629 | |||
| 630 | Returns a Checkbox with the params passed to it, -VALUE, -LABEL, | ||
| 631 | -OVERRIDE. | ||
| 632 | |||
| 633 | =cut | ||
| 634 | sub checkbox { | ||
| 635 | my $self = shift; | ||
| 636 | my $name = shift; | ||
| 637 | return $self->cgi->checkbox(-NAME => $name, -DEFAULT => ($self->_get_default($name) || ''), @_); | ||
| 638 | } # END of sub checkbox | ||
| 639 | |||
| 640 | |||
| 641 | =item C<checkbox_group> ( $name, %params ) | ||
| 642 | |||
| 643 | Returns a Checkbox Button Group with the params passed to it, | ||
| 644 | -VALUES, -LABELS, OVERRIDE. | ||
| 645 | |||
| 646 | =cut | ||
| 647 | sub checkbox_group { | ||
| 648 | my $self = shift; | ||
| 649 | my $name = shift; | ||
| 650 | return $self->cgi->checkbox_group(-NAME => $name, -DEFAULT => ($self->_get_default($name) || ''), @_); | ||
| 651 | } # END of sub checkbox_group | ||
| 652 | |||
| 653 | |||
| 654 | =item C<radio_group> ( $name, %params ) | ||
| 655 | |||
| 656 | Returns a Radio Button Group with the params passed to it, | ||
| 657 | -NAME, -VALUES, -LABELS, -DEFAULT, -OVERRIDE. | ||
| 658 | |||
| 659 | =cut | ||
| 660 | sub radio_group { | ||
| 661 | my $self = shift; | ||
| 662 | my $name = shift; | ||
| 663 | return $self->cgi->radio_group(-NAME => $name, -DEFAULT => ($self->_get_default($name) || ''), @_); | ||
| 664 | } # END of sub radio_group | ||
| 665 | |||
| 666 | |||
| 667 | =item C<get_default> ( ) | ||
| 668 | |||
| 669 | Gets the default value for the field name requested. | ||
| 670 | |||
| 671 | =cut | ||
| 672 | sub get_default { | ||
| 673 | my $self = shift; | ||
| 674 | return $self->_get_default(@_); | ||
| 675 | } # END of get_default | ||
| 676 | |||
| 677 | |||
| 678 | ######################## Application Methods ######################## | ||
| 679 | |||
| 680 | |||
| 681 | =item C<app> ( ) | ||
| 682 | |||
| 683 | Returns the object's Application Object. | ||
| 684 | |||
| 685 | =cut | ||
| 686 | sub app { | ||
| 687 | return undef unless (ref $_[0]); | ||
| 688 | $_[0]->{'_app'}; | ||
| 689 | } # END of app | ||
| 690 | |||
| 691 | |||
| 692 | =item C<dbh> ( ) | ||
| 693 | |||
| 694 | Returns the object's database handle. | ||
| 695 | |||
| 696 | =cut | ||
| 697 | sub dbh { | ||
| 698 | my $self = shift; | ||
| 699 | return undef unless (ref $self); | ||
| 700 | if (ref $self->app) { | ||
| 701 | return $self->app->dbh if (ref $self->app->dbh); | ||
| 702 | } | ||
| 703 | $self->{'_dbh'} ||= $self->data_class->connectdb; | ||
| 704 | } # END of dbh | ||
| 705 | |||
| 706 | |||
| 707 | =item C<r> ( ) | ||
| 708 | |||
| 709 | Returns Apache Request object. | ||
| 710 | |||
| 711 | =cut | ||
| 712 | sub r { | ||
| 713 | my $self = shift; | ||
| 714 | return undef unless (ref $self); | ||
| 715 | if (ref $self->app && ref $self->app->r) { | ||
| 716 | return $self->app->r; | ||
| 717 | } | ||
| 718 | return $self->{'_r'} ||= Apache->request; | ||
| 719 | } # END of r | ||
| 720 | |||
| 721 | |||
| 722 | =item C<cgi> ( ) | ||
| 723 | |||
| 724 | Returns the display object's internal CGI object (or makes a new one) | ||
| 725 | |||
| 726 | =cut | ||
| 727 | sub cgi { | ||
| 728 | my $self = shift; | ||
| 729 | return undef unless (ref $self); | ||
| 730 | if (ref $self->app && ref $self->app->cgi) { | ||
| 731 | return $self->app->cgi; | ||
| 732 | } | ||
| 733 | return $self->{'_cgi'} ||= CGI->new; | ||
| 734 | } # END of cgi | ||
| 735 | |||
| 736 | |||
| 737 | =item C<conf> ( ) | ||
| 738 | |||
| 739 | Returns the conf object. | ||
| 740 | |||
| 741 | =cut | ||
| 742 | sub conf { | ||
| 743 | my $self = shift; | ||
| 744 | return undef unless (ref $self); | ||
| 745 | if (ref $self->app) { | ||
| 746 | return $self->app->conf if (ref $self->app->conf); | ||
| 747 | } | ||
| 748 | $self->{'_conf'}; | ||
| 749 | } # END of conf | ||
| 750 | |||
| 751 | |||
| 752 | =item C<date> ( ) | ||
| 753 | |||
| 754 | Returns the display object's internal date object (or makes a new one) | ||
| 755 | |||
| 756 | =cut | ||
| 757 | sub date { | ||
| 758 | my $self = shift; | ||
| 759 | return undef unless (ref $self); | ||
| 760 | if (ref $self->app && $self->app->date) { | ||
| 761 | return $self->app->date; | ||
| 762 | } | ||
| 763 | return $self->{'_date'} ||= BingoX::Time->new; | ||
| 764 | } # END of date | ||
| 765 | |||
| 766 | |||
| 767 | =back | ||
| 768 | |||
| 769 | =head2 PARSER METHODS | ||
| 770 | |||
| 771 | =over 4 | ||
| 772 | |||
| 773 | =item C< include > ( @_ ) | ||
| 774 | |||
| 775 | Forwards to $app->include | ||
| 776 | |||
| 777 | =cut | ||
| 778 | sub include { | ||
| 779 | my $self = shift; | ||
| 780 | return undef unless (ref $self); | ||
| 781 | $self->app->include( @_ ); | ||
| 782 | } # END of include | ||
| 783 | |||
| 784 | |||
| 785 | =item C< xinclude > ( @_ ) | ||
| 786 | |||
| 787 | Forwards to $app->xinclude | ||
| 788 | |||
| 789 | =cut | ||
| 790 | sub xinclude { | ||
| 791 | my $self = shift; | ||
| 792 | return undef unless (ref $self); | ||
| 793 | $self->app->xinclude( @_ ); | ||
| 794 | } # END of xinclude | ||
| 795 | |||
| 796 | |||
| 797 | =back | ||
| 798 | |||
| 799 | =head2 CLASS VARIABLE METHODS | ||
| 800 | |||
| 801 | =over 4 | ||
| 802 | |||
| 803 | |||
| 804 | =item C<adminuri> ( ) | ||
| 805 | |||
| 806 | Returns class defined URI as a string. | ||
| 807 | |||
| 808 | =cut | ||
| 809 | sub adminuri { | ||
| 810 | my $self = shift; | ||
| 811 | my $class = ref($self) || $self; | ||
| 812 | no strict 'refs'; | ||
| 813 | my $uri = ${"${class}::adminuri"}; | ||
| 814 | unless (defined $uri) { | ||
| 815 | $class =~ s/::Display::/::Admin::/; | ||
| 816 | $uri = $class->adminuri; | ||
| 817 | } | ||
| 818 | return $uri; | ||
| 819 | } # END sub adminuri | ||
| 820 | |||
| 821 | |||
| 822 | =item C<data_class> ( ) | ||
| 823 | |||
| 824 | Returns the data class for the current display class (from the class | ||
| 825 | variable C<$data_class>). | ||
| 826 | |||
| 827 | =cut | ||
| 828 | sub data_class { | ||
| 829 | my $self = shift; | ||
| 830 | my $class = ref($self) || $self; | ||
| 831 | no strict 'refs'; | ||
| 832 | my $dc = ${"${class}::data_class"}; | ||
| 833 | unless (defined $dc) { | ||
| 834 | ($dc = $class) =~ s/::Display::/::Data::/; | ||
| 835 | ${"${class}::data_class"} = $dc; | ||
| 836 | } | ||
| 837 | return $dc; | ||
| 838 | } # END of data_class | ||
| 839 | |||
| 840 | |||
| 841 | =item C<display_fields> ( ) | ||
| 842 | |||
| 843 | Returns the class variable %{"${class}::display_fields"} as a hash reference. | ||
| 844 | |||
| 845 | =cut | ||
| 846 | sub display_fields { | ||
| 847 | my $self = shift; | ||
| 848 | my $class = ref($self) || $self; | ||
| 849 | no strict 'refs'; | ||
| 850 | return \%{ "${class}::display_fields" } if (defined %{"${class}::display_fields"}); | ||
| 851 | return { %{ "${class}::display_fields" } = map { $_ => 1 } @{ $class->display_order || [ ] } }; | ||
| 852 | } # END of display_fields | ||
| 853 | |||
| 854 | |||
| 855 | =item C<display_order> ( ) | ||
| 856 | |||
| 857 | Returns the class variable @{"${class}::display_order"} as an array reference. | ||
| 858 | |||
| 859 | =cut | ||
| 860 | sub display_order { | ||
| 861 | my $self = shift; | ||
| 862 | my $class = ref($self) || $self; | ||
| 863 | no strict 'refs'; | ||
| 864 | return [ @{ "${class}::display_order" } ] || $self->error_handler("In ${class}::display_order() not defined"); | ||
| 865 | } # END of display_order | ||
| 866 | |||
| 867 | |||
| 868 | =item C<title_field> ( ) | ||
| 869 | |||
| 870 | Returns the title field for the current display class (from the class | ||
| 871 | variable C<data_class>). | ||
| 872 | |||
| 873 | =cut | ||
| 874 | sub title_field { | ||
| 875 | return $_[0]->data_class->title_field; | ||
| 876 | } # END of title_field | ||
| 877 | |||
| 878 | |||
| 879 | =item C<date_fields> ( ) | ||
| 880 | |||
| 881 | Returns the date fields for the current display class (from the class | ||
| 882 | variable C<data_class>). | ||
| 883 | |||
| 884 | =cut | ||
| 885 | sub date_fields { | ||
| 886 | return $_[0]->data_class->date_fields; | ||
| 887 | } # END of date_fields | ||
| 888 | |||
| 889 | |||
| 890 | =item C<content_fields> ( ) | ||
| 891 | |||
| 892 | Returns the content fields for the current display class (from the class | ||
| 893 | variable C<data_class>). | ||
| 894 | |||
| 895 | =cut | ||
| 896 | sub content_fields { | ||
| 897 | return $_[0]->data_class->content_fields; | ||
| 898 | } # END of content_fields | ||
| 899 | |||
| 900 | |||
| 901 | =item C<relations> ( ) | ||
| 902 | |||
| 903 | Returns the relations for the current display class (from the class | ||
| 904 | variable C<data_class>). | ||
| 905 | |||
| 906 | =cut | ||
| 907 | sub relations { | ||
| 908 | return $_[0]->data_class->relations; | ||
| 909 | } # END of relations | ||
| 910 | |||
| 911 | |||
| 912 | =item C<primary_keys> ( ) | ||
| 913 | |||
| 914 | Returns the primary keys for the current display class (from the class | ||
| 915 | variable C<data_class>). | ||
| 916 | |||
| 917 | =cut | ||
| 918 | sub primary_keys { | ||
| 919 | return $_[0]->data_class->primary_keys; | ||
| 920 | } # END of primary_keys | ||
| 921 | |||
| 922 | |||
| 923 | =item C<foreign_keys> ( ) | ||
| 924 | |||
| 925 | Returns the foreign keys for the current display class (from the class | ||
| 926 | variable C<data_class>). | ||
| 927 | |||
| 928 | =cut | ||
| 929 | sub foreign_keys { | ||
| 930 | return $_[0]->data_class->foreign_keys; | ||
| 931 | } # END of foreign_keys | ||
| 932 | |||
| 933 | |||
| 934 | =item C<cpkey> ( ) | ||
| 935 | |||
| 936 | Returns the cpkey for the current display class (from the class | ||
| 937 | variable C<db_obj>). | ||
| 938 | |||
| 939 | =cut | ||
| 940 | sub cpkey { | ||
| 941 | return undef unless (ref $_[0]); | ||
| 942 | return $_[0]->db_obj->cpkey; | ||
| 943 | } # END of cpkey | ||
| 944 | |||
| 945 | |||
| 946 | sub AUTOLOAD { | ||
| 947 | my $self = shift; | ||
| 948 | return if ($AUTOLOAD =~ /::DESTROY$/); | ||
| 949 | |||
| 950 | my $name = substr($AUTOLOAD, rindex($AUTOLOAD, ':') + 1); | ||
| 951 | |||
| 952 | my ($data, $obj); | ||
| 953 | if (ref($self) && ($obj = $self->db_obj) && ref($obj)) { | ||
| 954 | $data = $obj->$name(); | ||
| 955 | } else { | ||
| 956 | ## Pass @_ because of $dbh, etc. ## | ||
| 957 | $data = $self->data_class->$name( @_ ) if ($self::data_class); | ||
| 958 | } | ||
| 959 | return $data; | ||
| 960 | } # END of AUTOLOAD | ||
| 961 | |||
| 962 | |||
| 963 | sub _errors { | ||
| 964 | my $self = shift; | ||
| 965 | my $nam = shift; | ||
| 966 | my $val = shift; | ||
| 967 | my $errors; | ||
| 968 | |||
| 969 | if (ref $self->{'_errors'}) { | ||
| 970 | $errors = $self->{'_errors'}; | ||
| 971 | } else { | ||
| 972 | $errors = { }; | ||
| 973 | } | ||
| 974 | if ($nam && $val) { | ||
| 975 | $errors->{$nam} = $val; | ||
| 976 | $self->{'_errors'} = $errors; | ||
| 977 | } | ||
| 978 | |||
| 979 | return $errors; | ||
| 980 | } # End of _errors | ||
| 981 | |||
| 982 | |||
| 983 | =begin private api | ||
| 984 | |||
| 985 | =item _get_default ( $name ) | ||
| 986 | |||
| 987 | Returns the default (exitisting) value for a form field. | ||
| 988 | |||
| 989 | =end private api | ||
| 990 | |||
| 991 | =cut | ||
| 992 | sub _get_default { | ||
| 993 | my $self = shift; | ||
| 994 | my $name = shift; | ||
| 995 | if (ref($self->db_obj) && | ||
| 996 | $self->db_obj->deffields->{ $name } && | ||
| 997 | $self->db_obj->$name() ne 'NULL') { | ||
| 998 | return $self->db_obj->$name(); | ||
| 999 | } else { | ||
| 1000 | return $self->cgi->param( $name ) || undef; | ||
| 1001 | } | ||
| 1002 | } # END of _get_default | ||
| 1003 | |||
| 1004 | package BingoX::Cobalt::Stream; | ||
| 1005 | |||
| 1006 | # Objects are constructed in the BingoX::Cobalt::stream_obj method above. | ||
| 1007 | sub next { | ||
| 1008 | my $self = shift; | ||
| 1009 | return $self->(); | ||
| 1010 | } # END sub next | ||
| 1011 | |||
| 1012 | sub close { | ||
| 1013 | my $self = shift; | ||
| 1014 | return $self->(1); | ||
| 1015 | } # END sub close | ||
| 1016 | |||
| 1017 | 1; | ||
| 1018 | |||
| 1019 | __END__ |