#!/usr/bin/perl -w use strict; use warnings; BEGIN { use FindBin qw($RealBin); # warn $RealBin; use lib ( '/home/raj/perl5/lib/perl5', $RealBin . '/../../lib', ); # use CGI::Carp qw(fatalsToBrowser); # use for devel use CGI::HTMLError (trace => 1); # use in production } use CGI; use Template; use Data::Dumper; use DBIx::Simple; use HTML::FillInForm; use Data::FormValidator; use Data::FormValidator::Constraints qw(:closures); use LIMS::Model::Email; use LIMS::Local::Debug; # DEBUG() use LIMS::Local::Config; my $config = LIMS::Local::Config->instance; my $dbix = DBIx::Simple->connect(@{ $config->{dbh_params} }); # template src: use constant TMPL_DIR => $RealBin . '/../../templates'; use constant TMPL => 'user/application_form.tt'; my $q = CGI->new(); # DEBUG($q); # get form submission params: my $vars = $q->Vars(); # DEBUG($vars); my $content; # if submitted form: if ($vars->{register}) { my $dfv_profile = _dfv_profile(); # DEBUG($dfv_profile); my $dfv = _dfv(); my $results = $dfv->check($q, $dfv_profile); # DEBUG($results); if ( $results->has_invalid or $results->has_missing ) { # die Dumper $results; $content = { errs => $results->msgs }; } else { } } my $view = render_view($content); print $q->header(), $view; sub render_view { my $content = shift; { # user locations: my $sql = q!select location_name,id from user_locations where active = 'yes'!; my $user_locations = $dbix->query($sql)->map; $content->{user_locations} = $user_locations; } { # user roles: my $roles = $dbix->query('select distinct(designation) from users')->flat; $content->{roles} = $roles; } # get TT object: my $t = Template->new({ INCLUDE_PATH => TMPL_DIR }); my $template_output; $t->process(TMPL, $content, \$template_output) || die "Template process failed: ", $t->error(), "\n"; my $output = HTML::FillInForm->fill( \$template_output, $vars ); return $output; } sub _dfv_profile { return { required => [ qw(last_name first_name contact email email2 tel dept) ], optional => [ qw(location_id other_source role other_role)], dependencies => { location_id => { OTHER => 'other_source' }, # require 'other_source' if location_id == OTHER designation => { OTHER => 'other_role' }, # require 'other_role' if designation == OTHER }, require_some => { # require one from this group location => [ 1, qw(location_id other_source) ], role => [ 1, qw(designation other_role) ], }, constraint_methods => { email => email(), }, msgs => { constraints => { location => 'Not a valid time format', }, }, } } sub _dfv { my $defaults = $config->{dfv_defaults}; # DEBUG($defaults); my $dfv = Data::FormValidator->new({}, $defaults); return $dfv; }