| 1 | # | ||
| 2 | # $Id: Output.pm,v 1.5 2005/01/27 23:30:50 scottb Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | package CGI::CRUD::Output; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use CGI::AutoForm; | ||
| 9 | |||
| 10 | |||
| 11 | =pod | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | CGI::CRUD::Output - Virtual base class liaison between CGI::CRUD and a web server | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | Virtual base class liaison between CGI::CRUD and a web server. C<output> method must | ||
| 20 | be overridden. C<form_attrs> will be called during initialization of the $form object | ||
| 21 | so set custom $form attributes in that method (see L<CGI::AutoForm>). | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | # ctor | ||
| 26 | sub new | ||
| 27 | { | ||
| 28 | my $caller = shift; | ||
| 29 | |||
| 30 | my $class = ref($caller) || $caller; | ||
| 31 | my $self = bless({},$class); | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub dbh | ||
| 37 | { | ||
| 38 | my ($self) = @_; | ||
| 39 | return $self->{dbh}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub perror | ||
| 43 | { | ||
| 44 | my ($self,$msg) = @_; | ||
| 45 | return $self->output(qq[<H2>Error:</H2>$msg]); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub param | ||
| 49 | { | ||
| 50 | my ($self,$name) = @_; | ||
| 51 | return $self->{q}{$name}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub query | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | return $self->{q}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub user | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | return $self->{user}; | ||
| 64 | } | ||
| 65 | |||
| 66 | # MUST BE OVERRIDDEN | ||
| 67 | # subclass should set $self->{tpl_vars}{BODY} and any others which will get passed to the template | ||
| 68 | sub output | ||
| 69 | { | ||
| 70 | } | ||
| 71 | |||
| 72 | sub server_error | ||
| 73 | { | ||
| 74 | my ($self) = @_; | ||
| 75 | return $self->perror("<EM>Internal Server Error. </EM><BR>Please contact your server administrator"); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub graceful_db_fields | ||
| 79 | { | ||
| 80 | my ($self,$form,$table_name,$usage) = @_; | ||
| 81 | shift,shift; | ||
| 82 | my $rv = $form->db_fields(@_); | ||
| 83 | unless ($rv > 0) | ||
| 84 | { | ||
| 85 | return $self->_ret($rv,$table_name); | ||
| 86 | } | ||
| 87 | return $rv; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub graceful_add_form_group | ||
| 91 | { | ||
| 92 | my ($self,$form,$usage,$table_name) = @_; | ||
| 93 | shift,shift; | ||
| 94 | my $rv = $form->add_group(@_); | ||
| 95 | unless ($rv > 0) | ||
| 96 | { | ||
| 97 | return $self->_ret($rv,$table_name); | ||
| 98 | } | ||
| 99 | return $rv; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _ret | ||
| 103 | { | ||
| 104 | my ($self,$rv,$table_name) = @_; | ||
| 105 | if (!defined($rv)) | ||
| 106 | { | ||
| 107 | $self->server_error(); | ||
| 108 | } | ||
| 109 | elsif ($rv == 0) | ||
| 110 | { | ||
| 111 | $self->perror("Table [$table_name] does not exist and does not have fields defined in UI_TABLE_COLUMN"); | ||
| 112 | } | ||
| 113 | elsif ($rv == -1) | ||
| 114 | { | ||
| 115 | $self->perror("At least 1 field in [$table_name] requested a SELECT input control but no mask list was found"); | ||
| 116 | } | ||
| 117 | elsif ($rv == -2) | ||
| 118 | { | ||
| 119 | $self->perror("Can't remember what this error was"); | ||
| 120 | } | ||
| 121 | elsif ($rv == -3) | ||
| 122 | { | ||
| 123 | $self->perror("$table_name (or one of its columns) not defined in the database"); | ||
| 124 | } | ||
| 125 | else | ||
| 126 | { | ||
| 127 | $self->server_error(); | ||
| 128 | } | ||
| 129 | return undef; | ||
| 130 | } | ||
| 131 | |||
| 132 | # Class or instance method | ||
| 133 | sub form | ||
| 134 | { | ||
| 135 | my $caller = shift; | ||
| 136 | my $form = new CGI::AutoForm(@_); | ||
| 137 | $caller->form_attrs($form); | ||
| 138 | return $form; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub form_attrs | ||
| 142 | { | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; | ||
| 146 | |||
| 147 | __END__ |