Context.pm fatalsToBrowser now dependent on $ENV{USER_AGENT}.
authorJoe Atzberger <joe.atzberger@liblime.com>
Fri, 12 Oct 2007 22:35:20 +0000 (17:35 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Sat, 13 Oct 2007 03:19:42 +0000 (22:19 -0500)
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 <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/Context.pm
C4/Dates.pm
t/Dates.t

index 1dd33ff..98b63cb 100644 (file)
@@ -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 "
-                <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;
@@ -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;
 }
index 67430c9..bfee3a5 100644 (file)
@@ -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 <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
 
index 21e285b..734029c 100755 (executable)
--- 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'],