package Handy::Dandy::CGItools;
use strict;
use Exporter;
# use Time::HiRes qw( time );
$Handy::Dandy::CGItools::VERSION    = 1.00_4;      # 10/1/02, 1:17 pm
@Handy::Dandy::CGItools::ISA        = qw( Exporter );
@Handy::Dandy::CGItools::EXPORT_OK  = qw
   (
      cgi_error   cgi_header
      params_defined   time_spent
   );
%Handy::Dandy::CGItools::EXPORT_TAGS =
   (
      'all' => [ @Handy::Dandy::CGItools::EXPORT_OK ]
   );


=pod

   AUTHOR
      Tommy Butler <tommy @ atrixnet.com>
      phone: (817)-468-7716
      6711 Forest Park Dr
      Arlington, TX
           76001-8403

   COPYRIGHT   Tommy Butler. All rights reserved
   LISCENCE    This software is free, use/distribute under the GNU GPL.
   BUGS TO     Tommy Butler <perlmod @ atrixnet.com>

   HISTORY
      1.00_3      9/17/02, 9:33 am
                  Fixed bug that caused warning about an uninitialized value
                  to be output every time method 'cgi_error' was called

      1.00_4      10/1/02, 1:17 pm
                  Method 'cgi_header' now passes all of its arguments to
                  method 'CGI::header', instead of the first argument only.

=cut


# --------------------------------------------------------
# Handy::Dandy::CGItools::cgi_header()
# --------------------------------------------------------
sub cgi_header {

   return($$main::pcom{'CGI'}->header(@_))
      unless
         ($$main::pcom{'sent CGI header'});

   ++$$main::pcom{'sent CGI header'}; ''
}



# --------------------------------------------------------
# Handy::Dandy::CGItools::cgi_error()
# --------------------------------------------------------
sub cgi_error {

   my($error) = '';

   foreach (@_) {

      $_ = '' unless defined($_);

      $error .= qq[<div>$_ </div>\012]
   }

   $$main::pcom{'Template::Parser'}->set_token('error' => $error);

   print
      (
         &cgi_header(),
         $$main::pcom{'Template::Parser'}->parse_file
            (
               $$main::pcom{'DIR'}{'templates'} . '/error.tmpl'
            )
      );

   exit(1)
}


# --------------------------------------------------------
# Handy::Dandy::CGItools::params_defined()
# --------------------------------------------------------
sub params_defined {

   my(@errors)    = ();
   my($bullet)    = q[<strong>&#183;</strong>];
   my($required)  = $$main::pcom{'Futils'}->coerce_array(@_);

   foreach (keys(%{$required})) {

      my($req)    = $_;
      my($alias)  = $required->{$_};
      my($val)    = $$main::pcom{'CGI'}->param($req)||'';

      if (length($val) == 0) {

         push(@errors, qq[<div class="indent20">$bullet $alias</div>])
      }
   }

   return(1) unless (@errors > 0);

   &cgi_error(<<__error__,@errors)

<p>It appears you left some of the form fields blank just now.</p>

<p>Please go back and fill in the form fields that were left empty:</p>

__error__

}


# --------------------------------------------------------
# sub Handy::Dandy::CGItools::timespent
# --------------------------------------------------------
sub time_spent { sprintf('%0.2f', (time - $^T)) }


# --------------------------------------------------------
# Handy::Dandy::CGItools::DESTROY(), AUTOLOAD()
# --------------------------------------------------------
sub DESTROY {} sub AUTOLOAD {}


# --------------------------------------------------------
# end Handy::Dandy::CGItools Class, return true on import
# --------------------------------------------------------

1;