# $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 "
- <h1>debug level $debug_level </h1>
- <p>Got an error: $msg</p>
- <table>
- <tr><th>Apache<td> $versions{apacheVersion}</tr>
- <tr><th>Koha<td> $versions{kohaVersion}</tr>
- <tr><th>MySQL<td> $versions{mysqlVersion}</tr>
- <tr><th>OS<td> $versions{osVersion}</tr>
- <tr><th>Perl<td> $versions{perlVersion}</tr>
- </table>";
-
- } elsif ($debug_level eq "1"){
- print "<h1>debug level $debug_level </h1>";
- print "<p>Got an error: $msg</p>";
- } 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 "
+ <h1>debug level $debug_level </h1>
+ <p>Got an error: $msg</p>
+ <table>
+ <tr><th>Apache<td> $versions{apacheVersion}</tr>
+ <tr><th>Koha<td> $versions{kohaVersion}</tr>
+ <tr><th>MySQL<td> $versions{mysqlVersion}</tr>
+ <tr><th>OS<td> $versions{osVersion}</tr>
+ <tr><th>Perl<td> $versions{perlVersion}</tr>
+ </table>";
+
+ } elsif ($debug_level eq "1"){
+ print "<h1>debug level $debug_level </h1>";
+ print "<p>Got an error: $msg</p>";
+ } else {
+ print "production mode - trapped fatal";
+ }
+ }
+ CGI::Carp->set_message(\&handle_errors);
+ } # else there is no browser to send fatals to!
}
use DBI;
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';
$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;
}
);
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)]',
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;
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;
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 <systempreference> format for display to user:
+For example, to print an ISO date (from the database) in the <systempreference> 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 <systempreferece> 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
#!/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'],