#!/Perl/bin/perl package CatcherInTheRye; use strict; use warnings; use base qw/CGI::Application/; use CGI::Application::Plugin::Forward; use CGI::Application::Plugin::Redirect; use CGI::Application::Plugin::ConfigAuto qw/cfg/; use CGI::Application::Plugin::ValidateRM; use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); #use CGI::Application::Plugin::Session; # for MessageStack #use CGI::Application::Plugin::MessageStack; # for development use CGI::Application::Plugin::DebugScreen; use Data::Dumper qw/Dumper/; =head1 NAME CatcherInTheRye - Perl extension for blah blah blah =head1 SYNOPSIS use CatcherInTheRye; blah blah blah =head1 DESCRIPTION Stub documentation for CatcherInTheRye, created by h2xs. It looks like the author of the extension was negligent enough to leave the stub unedited. This one uses a database with the following tabel in schema 'test': -- -- Definition of table `time_reports` -- DROP TABLE IF EXISTS `time_reports`; CREATE TABLE `time_reports` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `field` varchar(45) NOT NULL, `number` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; =head2 EXPORT None by default. =head1 METHODS =cut =head2 cgiapp_init() Open database connection, setup config files, etc. =cut sub cgiapp_init { my $self = shift; # -- Database configuration. #my $dbc = $self->cfg('db'); # todo: use a config file my $dbc = { 'dsn' => 'DBI:mysql:test:localhost', 'username' => 'Test', 'password' => 'test', 'db_attributes' => { 'PrintError' => 0, 'RaiseError' => 0, 'AutoCommit' => 1, }, }; my $data_source = $dbc->{dsn}; my $username = $dbc->{username}; my $auth = $dbc->{password}; my $attr_href = $dbc->{attributes}; $self->dbh_config($data_source, $username, $auth, $attr_href); # -- Set some defaults for DFV unless they already exist. $self->param('dfv_defaults') || $self->param('dfv_defaults', { missing_optional_valid => 1, filters => 'trim', msgs => { any_errors => 'some_errors', prefix => 'err_', invalid => 'Invalid', missing => 'Missing', format => '%s', }, }); } # /cgiapp_init =head2 setup() Defined runmodes, etc. =cut sub setup { my $self = shift; $self->start_mode('start'); $self->run_modes([qw/ start new_form new_validate /]); } # /setup =head2 cgiapp_prerun() We know what the runmode will be. Do we want to do anything about it? =over =item Authentication? =item Authorization? =item Redirection? =back =cut sub cgiapp_prerun { my $self = shift; } # /cgiapp_prerun =head2 cgiapp_get_query() Bote: uploads are disabled by default in CGI::Simple. =cut sub cgiapp_get_query { require CGI::Simple; my $q = new CGI::Simple; return $q; } # /cgiapp_get_query =head2 start( $errs ) Display the workspace with link to anything the user can do or data 'n stuff. =cut sub start { my $self = shift; my $errs = shift; # may be undef # -- get the data to display in the workspace, that is all records that # have been saved until now. my @all_records = (); my $sql = qq~ SELECT * FROM time_reports ~; my $dbh = $self->dbh(); my $sth = $dbh->prepare($sql) or return("Error preparing SQL. Unfortunately for you, I didn't implement any error handling."); my $rv = $sth->execute() or return("Error executing SQL. Unfortunately for you, I didn't implement any error handling."); while( my $row_href = $sth->fetchrow_hashref() ) { push @all_records, $row_href; # some magic here } my $t = $self->load_tmpl('workspace.tmpl', associate => $self); $t->param('c.query.url' => $self->query->url()); $t->param($errs) if $errs; $t->param(records => \@all_records) if @all_records; return $t->output(); } # /start =head2 new_validate() Validate user input. Unless valid, redirect to form, display errors. If valid, write things to DB or wherever you want to store it. =cut sub new_validate { my $self = shift; my $form_profile = { required => [qw/field number/], optional => [qw/rm submit/], constraints => { field => qr/^.{1,45}$/, number => qr/^\d+$/, } }; my $results = $self->check_rm('start', $form_profile) || return $self->check_rm_error_page(); # -- Results are valid, create new record in DB. # INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] # [INTO] tbl_name [(col_name,...)] # VALUES ({expr | DEFAULT},...),(...),... # [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] my $sql = qq~ INSERT INTO time_reports (field, number) VALUES (?,?) ~; my $dbh = $self->dbh(); my $sth = $dbh->prepare($sql) or return("Error preparing SQL. Unfortunately for you, I didn't implement any error handling."); my $rv = $sth->execute($results->valid('field'), $results->valid('number')) or return("Error executing SQL. Unfortunately for you, I didn't implement any error handling."); # -- todo: set up a confirmation message #$self->push_message( # -scope => 'mainpage', # -message => 'Your stuff has been written', # -classification => 'INFO', #); # -- Redirect to workspace my $url = $self->query->url(-absolute => 1,) . '?rm=start'; return $self->redirect($url); } # /new_validate =head2 cgiapp_postrun( $output ) Output manipulation: =over =item add common header/footer? =item Cleanup your HTML? =item Rewrite URLs? =back =cut sub cgiapp_postrun { my $self = shift; my $output = shift; } # /cgiapp_postrun =head2 teardown() Close database connections (if not persistant), flush out session storage, etc. =cut sub teardown { my $self = shift; } # /teardown =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR A. U. Thor, Ea.u.thor@a.galaxy.far.far.awayE =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by A. U. Thor This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut 1; use strict; use warnings; use FindBin qw/$Bin/; my $app = CatcherInTheRye->new( TMPL_PATH => $Bin . '/templates', ); $app->run();