| 1 | # | ||
| 2 | # $Id: CGIOutputFastTemplate.pm,v 1.5 2005/01/27 23:30:50 scottb Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | package CGI::CRUD::CGIOutputFastTemplate; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use CGI; | ||
| 9 | use DBI; | ||
| 10 | use CGI::AutoForm; | ||
| 11 | use CGI::FastTemplate; | ||
| 12 | use CGI::CRUD::Output; | ||
| 13 | |||
| 14 | @CGI::CRUD::CGIOutputFastTemplate::ISA = qw(CGI::CRUD::Output); | ||
| 15 | |||
| 16 | # CGI::FastTemplate path for templates | ||
| 17 | # May override by setting CRUDDY_FAST_TEMPLATE_PATH environment | ||
| 18 | # variable (e.g. SetEnv) | ||
| 19 | my $DEFAULT_TPL_PATH = '/var/www/tpl'; | ||
| 20 | |||
| 21 | # CGI::FastTemplate 'main' template | ||
| 22 | # May override by setting CRUDDY_FAST_TEMPLATE_MAIN environment | ||
| 23 | # variable (e.g. SetEnv) | ||
| 24 | # -or- | ||
| 25 | # as a second argument to the C<output> method call | ||
| 26 | my $DEFAULT_TPL_MAIN = 'cruddy.tpl'; | ||
| 27 | |||
| 28 | |||
| 29 | # May return undef if db connect error | ||
| 30 | sub new | ||
| 31 | { | ||
| 32 | my $caller = shift; | ||
| 33 | my ($q,$defaults) = @_; | ||
| 34 | |||
| 35 | my $self = $caller->SUPER::new(@_); | ||
| 36 | |||
| 37 | # Make sure to define env DBI_DSN, DBI_USER, etc (see man DBI) | ||
| 38 | unless ($self->{dbh} = DBI->connect(undef,undef,undef,{ PrintError => 1, RaiseError => 0, AutoCommit => 1, })) | ||
| 39 | { | ||
| 40 | $self->perror("Database server not responding, contact your administrator"); | ||
| 41 | warn("Can't connect to db, check DBI_DSN, DBI_USER, etc env."); | ||
| 42 | return undef; | ||
| 43 | } | ||
| 44 | |||
| 45 | $self->{cgi} = $q; | ||
| 46 | $self->{user} = $q->remote_user(); | ||
| 47 | |||
| 48 | my %params = $q->Vars; | ||
| 49 | |||
| 50 | my ($key,$val); | ||
| 51 | while (($key,$val) = each(%params)) | ||
| 52 | { | ||
| 53 | my @vals = split("\0",$params{$key}); | ||
| 54 | $params{$key} = \@vals if @vals > 1; | ||
| 55 | } | ||
| 56 | |||
| 57 | $self->{q} = \%params; | ||
| 58 | |||
| 59 | $self->{tpl_vars} = $defaults; | ||
| 60 | |||
| 61 | return $self; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub form_attrs | ||
| 65 | { | ||
| 66 | my ($caller,$form) = @_; | ||
| 67 | $caller->SUPER::form_attrs($form); | ||
| 68 | $form->{GT} = qq[WIDTH="80%" CELLPADDING="5" CELLSPACING="0" BORDER="0"]; | ||
| 69 | $form->{VFL} = qq[WIDTH="40%" ALIGN="RIGHT"]; | ||
| 70 | $form->{submit_button_attrs} = qq[class="formbutton" onmouseout="javascript:this.style.color='black';" onmouseover="javascript:this.style.color='red';"]; | ||
| 71 | return $form; | ||
| 72 | } | ||
| 73 | |||
| 74 | ##at should somehow cache templates | ||
| 75 | ##at Accept scalar references | ||
| 76 | sub output | ||
| 77 | { | ||
| 78 | my ($self,$out,$tplf) = @_; | ||
| 79 | $tplf = $ENV{CRUDDY_FAST_TEMPLATE_MAIN} || $DEFAULT_TPL_MAIN unless $tplf; | ||
| 80 | |||
| 81 | my $tpl = new CGI::FastTemplate($ENV{CRUDDY_FAST_TEMPLATE_PATH} || $DEFAULT_TPL_PATH); | ||
| 82 | ##at should check return value because an OS call was done | ||
| 83 | $tpl->define(main => $tplf); | ||
| 84 | $self->{tpl_vars}{BODY} = (ref($out) ? $$out : $out) if defined($out); | ||
| 85 | $tpl->assign($self->{tpl_vars}); | ||
| 86 | $tpl->parse(CONTENT => 'main'); | ||
| 87 | my $gob = $tpl->fetch('CONTENT'); | ||
| 88 | |||
| 89 | print $self->{cgi}->header, $$gob; | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; |