| 1 | # | ||
| 2 | # $Id: Skipper.pm,v 1.3 2005/01/27 21:33:26 rsandberg Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | package CGI::CRUD::Skipper; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use CGI::CRUD::SkipperOutput; | ||
| 9 | use CGI::CRUD::TableIO; | ||
| 10 | use CGI::AutoForm; | ||
| 11 | use DBIx::IO::Table; | ||
| 12 | |||
| 13 | use constant OK => 0; | ||
| 14 | |||
| 15 | @CGI::CRUD::Skipper::ISA = (qw(CGI::CRUD::TableIO)); | ||
| 16 | |||
| 17 | my $table_field = { | ||
| 18 | FIELD_NAME => 'TABLE_NAME', | ||
| 19 | INPUT_CONTROL_TYPE => 'SELECT', | ||
| 20 | HEADING => 'Table Name', | ||
| 21 | DATATYPE => 'CHAR', | ||
| 22 | REQUIRED => 'Y', | ||
| 23 | INSERTABLE => 'Y', | ||
| 24 | }; | ||
| 25 | |||
| 26 | my $action_field = { | ||
| 27 | FIELD_NAME => 'ACTION', | ||
| 28 | INPUT_CONTROL_TYPE => 'RADIO', | ||
| 29 | DEFAULT_VALUE => 'IR', | ||
| 30 | HEADING => 'Operation', | ||
| 31 | DATATYPE => 'CHAR', | ||
| 32 | REQUIRED => 'Y', | ||
| 33 | INSERTABLE => 'Y', | ||
| 34 | }; | ||
| 35 | my $action_picklist = [ | ||
| 36 | { ID => 'IR', MASK => 'Insert', }, | ||
| 37 | { ID => 'SR', MASK => 'Search/Edit', }, | ||
| 38 | ]; | ||
| 39 | |||
| 40 | |||
| 41 | =pod | ||
| 42 | |||
| 43 | =head1 NAME | ||
| 44 | |||
| 45 | CGI::CRUD::Skipper - Generic implementation of CGI::CRUD::TableIO | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | A concrete subclass of CGI::CRUD::TableIO to provide a vanilla web front-end to your RDBMS. It performs auto-discovery of your schema | ||
| 50 | and data dictionary, just plug it in and go - you can customize and code later. | ||
| 51 | |||
| 52 | Naming conventions that bear any similarity to the famous TV star Muddy Mudskipper (having made several special appearances on "The Ren and Stimpy Show") are purely co-incidental. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | |||
| 57 | sub handler | ||
| 58 | { | ||
| 59 | my $r = new CGI::CRUD::SkipperOutput(@_) or return OK; | ||
| 60 | |||
| 61 | my $self = __PACKAGE__->new($r->dbh()); | ||
| 62 | |||
| 63 | $self->handle_req($r); | ||
| 64 | |||
| 65 | return OK; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub handle_req | ||
| 69 | { | ||
| 70 | my ($self,$r) = @_; | ||
| 71 | |||
| 72 | $self->{action} = $ENV{CRUDDY_URI_PREFIX}.$r->{apache}->uri() if $ENV{MOD_PERL}; | ||
| 73 | |||
| 74 | my $query = $r->query(); | ||
| 75 | #$query->{'__SDAT_TAB_ACTION.TABLE_NAME'} = uc($query->{'__SDAT_TAB_ACTION.TABLE_NAME'}) if exists($query->{'__SDAT_TAB_ACTION.TABLE_NAME'}); | ||
| 76 | my $action = $r->param('__SDAT_TAB_ACTION.ACTION') || ''; | ||
| 77 | |||
| 78 | # Set other vars to be substituted into CGI::FastTemplate | ||
| 79 | $r->{tpl_vars}{HOME_URL} = $self->{action}; | ||
| 80 | |||
| 81 | if ($r->param('__SDAT_TAB_ACTION.RESTART')) | ||
| 82 | { | ||
| 83 | $self->request_action($r); | ||
| 84 | } | ||
| 85 | elsif ($action eq 'IR') | ||
| 86 | { | ||
| 87 | $self->insert_req($r); | ||
| 88 | } | ||
| 89 | elsif ($action eq 'ID') | ||
| 90 | { | ||
| 91 | $self->insert_data($r); | ||
| 92 | } | ||
| 93 | elsif ($action eq 'SR') | ||
| 94 | { | ||
| 95 | $self->search_req($r); | ||
| 96 | } | ||
| 97 | elsif ($action eq 'SD') | ||
| 98 | { | ||
| 99 | $self->search_results($r); | ||
| 100 | } | ||
| 101 | elsif ($action eq 'UR') | ||
| 102 | { | ||
| 103 | $self->update_req($r); | ||
| 104 | } | ||
| 105 | elsif ($action eq 'UD') | ||
| 106 | { | ||
| 107 | $self->update_data($r); | ||
| 108 | } | ||
| 109 | elsif ($action eq 'DR') | ||
| 110 | { | ||
| 111 | $self->delete_req($r); | ||
| 112 | } | ||
| 113 | else | ||
| 114 | { | ||
| 115 | $self->request_action($r); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | sub request_action | ||
| 120 | { | ||
| 121 | my ($self,$r) = @_; | ||
| 122 | my $dbh = $r->dbh(); | ||
| 123 | my $form = $r->form($dbh); | ||
| 124 | $form->heading("Database Operations"); | ||
| 125 | $form->action($self->{action}); | ||
| 126 | $form->submit_value('Continue'); | ||
| 127 | $form->add_group('INSERTABLE',undef,'Choose a table and an operation to perform on that table','__SDAT_TAB_ACTION'); | ||
| 128 | |||
| 129 | unless (defined($self->{table_picklist})) | ||
| 130 | { | ||
| 131 | my $table_names = DBIx::IO::Table->existing_table_names($dbh); | ||
| 132 | ref($table_names) or ($r->server_error(),return undef); | ||
| 133 | my @tp = (); | ||
| 134 | foreach my $table_name (@$table_names) | ||
| 135 | { | ||
| 136 | push(@tp,{ID => $table_name, MASK => $table_name}); | ||
| 137 | } | ||
| 138 | $tp[0] = {ID => 'NO TABLES FOUND', MASK => 'NO TABLES FOUND'} unless @tp; | ||
| 139 | $self->{table_picklist} = \@tp; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $rv; | ||
| 143 | unless ($rv = $form->add_field($table_field,$self->{table_picklist})) | ||
| 144 | { | ||
| 145 | defined($rv) or ($r->server_error(),return undef); | ||
| 146 | $r->perror("Table list unavailable"); | ||
| 147 | return $rv; | ||
| 148 | } | ||
| 149 | $form->add_field($action_field,$action_picklist); | ||
| 150 | $r->output($form->prepare($r->query())); | ||
| 151 | } | ||
| 152 | |||
| 153 | 1; | ||
| 154 | |||
| 155 | __END__ |