From bd76a1fb0f9a07520f987646a3e1bf154d9861b1 Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Fri, 12 Oct 2007 17:35:20 -0500 Subject: [PATCH] Context.pm fatalsToBrowser now dependent on $ENV{USER_AGENT}. Minor adjustments to Dates.pm and associated test. You should be able to run a perl test that uses Context w/o getting fatalsToBrowser output. Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/Context.pm | 70 +++++++++++++++++++++++++-------------------------- C4/Dates.pm | 36 ++++++++++++++++++-------- t/Dates.t | 4 +-- 3 files changed, 62 insertions(+), 48 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 1dd33ffbeb..98b63cb40a 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -19,36 +19,39 @@ package C4::Context; # $Id$ use strict; -use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { - sub handle_errors { - my $msg = shift; - my $debug_level = C4::Context->preference("DebugLevel"); - - if ($debug_level eq "2"){ - # debug 2 , print extra info too. - my %versions = get_versions(); - -# a little example table with various version info"; - print " -

debug level $debug_level

-

Got an error: $msg

- - - - - - -
Apache $versions{apacheVersion}
Koha $versions{kohaVersion}
MySQL $versions{mysqlVersion}
OS $versions{osVersion}
Perl $versions{perlVersion}
"; - - } elsif ($debug_level eq "1"){ - print "

debug level $debug_level

"; - print "

Got an error: $msg

"; - } else { - print "production mode - trapped fatal"; - } - } - set_message(\&handle_errors); + if ($ENV{'USER_AGENT'}) { + require CGI::Carp; + import CGI::Carp qw(fatalsToBrowser); + sub handle_errors { + my $msg = shift; + my $debug_level = C4::Context->preference("DebugLevel"); + + if ($debug_level eq "2"){ + # debug 2 , print extra info too. + my %versions = get_versions(); + + # a little example table with various version info"; + print " +

debug level $debug_level

+

Got an error: $msg

+ + + + + + +
Apache $versions{apacheVersion}
Koha $versions{kohaVersion}
MySQL $versions{mysqlVersion}
OS $versions{osVersion}
Perl $versions{perlVersion}
"; + + } elsif ($debug_level eq "1"){ + print "

debug level $debug_level

"; + print "

Got an error: $msg

"; + } else { + print "production mode - trapped fatal"; + } + } + CGI::Carp->set_message(\&handle_errors); + } # else there is no browser to send fatals to! } use DBI; @@ -57,9 +60,7 @@ use XML::Simple; use C4::Boolean; -use vars qw($VERSION $AUTOLOAD), - qw($context), - qw(@context_stack); +use vars qw($VERSION $AUTOLOAD $context @context_stack); $VERSION = '3.00.00.005'; @@ -911,9 +912,8 @@ sub get_versions { $versions{perlVersion} = $]; $versions{mysqlVersion} = `mysql -V`; $versions{apacheVersion} = `httpd -v`; - $versions{apacheVersion} = `httpd2 -v` unless $versions{apacheVersion} ; - $versions{apacheVersion} = `apache2 -v` unless $versions{apacheVersion} -; + $versions{apacheVersion} = `httpd2 -v` unless $versions{apacheVersion} ; + $versions{apacheVersion} = `apache2 -v` unless $versions{apacheVersion} ; $versions{apacheVersion} = `/usr/sbin/apache2 -v` unless $versions{apacheVersion} ; return %versions; } diff --git a/C4/Dates.pm b/C4/Dates.pm index 67430c9a04..bfee3a563b 100644 --- a/C4/Dates.pm +++ b/C4/Dates.pm @@ -45,7 +45,7 @@ our %posix_map = ( ); our %dmy_subs = ( # strings to eval (after using regular expression returned by regexp below) - # make arrayrs for POSIX::strftime() + # make arrays for POSIX::strftime() iso => '[(0,0,0,$3, $2 - 1, $1 - 1900)]', metric => '[(0,0,0,$1, $2 - 1, $3 - 1900)]', us => '[(0,0,0,$2, $1 - 1, $3 - 1900)]', @@ -99,7 +99,7 @@ sub init ($;$$) { sub output ($;$) { my $self = shift; my $newformat = (@_) ? _recognize_format(shift) : $self->{'dateformat'} ; - return POSIX::strftime($posix_map{$newformat}, @self::dmy_array); + return (eval {POSIX::strftime($posix_map{$newformat}, @self::dmy_array)} || undef); } sub today ($;$) { # NOTE: sets date value to today (and returns it in the requested or current format) my $class = shift; @@ -126,9 +126,10 @@ sub format { # get or set dateformat: iso, metric, us, etc. sub visual { my $self = shift; if (@_) { - return $format_map{ shift }; + return $format_map{ _recognize_format(shift) }; } - return $format_map{$self->{'dateformat'} || $prefformat} ; + $self eq __PACKAGE__ and return $format_map{$prefformat}; + return $format_map{ eval { $self->{'dateformat'} } || $prefformat} ; } 1; @@ -204,17 +205,32 @@ Or even: print C4::Dates->new("21-09-1989", "metric")->output("iso"); -=head2 ->DHMTLCalendar([date_format]) +=head2 "syspref" -- System Preference(s) -Returns the format string for DHTML Calendar Display based on date_format. -If date_format is not supplied, the return is based on system preference. +Perhaps you want to force data obtained in a known format to display according to the user's system +preference, without necessarily knowing what that preference is. For this purpose, you can use the +psuedo-format argument "syspref". - C4::Dates->new()->DHTMLCalendar(); # e.g., returns "%m/%d/%Y" for 'us' system preference - -Format dates from database in ISO format into the format for display to user: +For example, to print an ISO date (from the database) in the format: my $date = C4::Dates->new($date_from_database,"iso"); my $datestring_for_display = $date->display("syspref"); + print $datestring_for_display; + +Or even: + + print C4::Dates->new($date_from_database,"iso")->display("syspref"); + +If you just want to know what the is, you can use: + +C4::Dates-> + +=head2 ->DHMTLcalendar([date_format]) + +Returns the format string for DHTML Calendar Display based on date_format. +If date_format is not supplied, the return is based on system preference. + + C4::Dates->DHTMLcalendar(); # e.g., returns "%m/%d/%Y" for 'us' system preference =head3 Error Handling diff --git a/t/Dates.t b/t/Dates.t index 21e285b484..734029cca2 100755 --- a/t/Dates.t +++ b/t/Dates.t @@ -1,11 +1,9 @@ #!/bin/perl -use Test::More tests => 83; +use Test::More tests => 82; BEGIN { use_ok('C4::Dates'); } -use_ok( CGI::Carp ); -CGI::Carp->fatalsToBrowser(0); my %thash = ( iso => ['2001-01-01','1989-09-21'], -- 2.39.5