Bugfix #2630 2nd attempt
[koha.git] / C4 / Dates.pm
index f45fd79..b70b59e 100644 (file)
@@ -18,10 +18,12 @@ use strict;
 use warnings;
 use Carp;
 use C4::Context;
+use C4::Debug;
 use Exporter;
 use POSIX qw(strftime);
 use Date::Calc qw(check_date check_time);
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+use vars qw($debug $cgi_debug);
 
 BEGIN {
        $VERSION = 0.03;
@@ -29,8 +31,16 @@ BEGIN {
        @EXPORT_OK = qw(DHTMLcalendar format_date_in_iso format_date);
 }
 
-my $prefformat = C4::Context->preference('dateformat');
-my $debug = $ENV{'DEBUG'} || 0;
+my $prefformat;
+sub _prefformat {
+    unless (defined $prefformat) {
+        $prefformat = C4::Context->preference('dateformat');
+    }
+    return $prefformat;
+}
+
+# print STDERR " Dates :      \$debug is '$debug'\n";
+# print STDERR " Dates :  \$cgi_debug is '$cgi_debug'\n";
 
 our %format_map = ( 
          iso  => 'yyyy-mm-dd',
@@ -77,7 +87,7 @@ sub dmy_map ($$) {
                return  @{$aref}; 
        }
        # $debug and 
-       carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual() . "\n";
+       carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual();
        return 0;
 }
 
@@ -85,11 +95,11 @@ sub _check_date_and_time {
     my $chron_ref = shift;
     my ($year, $month, $day) = _chron_to_ymd($chron_ref);
     unless (check_date($year, $month, $day)) {
-        carp "Illegal date specified (year = $year, month = $month, day = $day)\n";
+        carp "Illegal date specified (year = $year, month = $month, day = $day)";
     }
     my ($hour, $minute, $second) = _chron_to_hms($chron_ref);
     unless (check_time($hour, $minute, $second)) {
-        carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)\n";
+        carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)";
     }
 }
 
@@ -113,33 +123,33 @@ sub new {
 sub init ($;$$) {
        my $self = shift;
        my $dformat;
-       $self->{'dateformat'} = $dformat = (scalar(@_) >= 2) ? $_[1] : $prefformat;
+       $self->{'dateformat'} = $dformat = (scalar(@_) >= 2) ? $_[1] : _prefformat();
        ($format_map{$dformat}) or croak 
                "Invalid date format '$dformat' from " . ((scalar(@_) >= 2) ? 'argument' : 'system preferences');
        $self->{'dmy_arrayref'} = [((@_) ? $self->dmy_map(shift) : localtime )] ;
-       $debug and print STDERR "(during init) \@\$self->{'dmy_arrayref'}: " . join(' ',@{$self->{'dmy_arrayref'}}) . "\n";
+       $debug and warn "(during init) \@\$self->{'dmy_arrayref'}: " . join(' ',@{$self->{'dmy_arrayref'}}) . "\n";
        return $self;
 }
 sub output ($;$) {
        my $self = shift;
-       my $newformat = (@_) ? _recognize_format(shift) : $prefformat;
+       my $newformat = (@_) ? _recognize_format(shift) : _prefformat();
        return (eval {POSIX::strftime($posix_map{$newformat}, @{$self->{'dmy_arrayref'}})} || undef);
 }
 sub today ($;$) {              # NOTE: sets date value to today (and returns it in the requested or current format)
        my $class = shift;
        $class = ref($class) || $class;
-       my $format = (@_) ? _recognize_format(shift) : $prefformat;
+       my $format = (@_) ? _recognize_format(shift) : _prefformat();
        return $class->new()->output($format);
 }
 sub _recognize_format($) {
        my $incoming = shift;
-       ($incoming eq 'syspref') and return $prefformat;
+       ($incoming eq 'syspref') and return _prefformat();
        (scalar grep (/^$incoming$/, keys %format_map) == 1) or croak "The format you asked for ('$incoming') is unrecognized.";
        return $incoming;
 }
 sub DHTMLcalendar ($;$) {      # interface to posix_map
        my $class = shift;
-       my $format = (@_) ? shift : $prefformat;
+       my $format = (@_) ? shift : _prefformat();
        return $posix_map{$format};     
 }
 sub format {   # get or set dateformat: iso, metric, us, etc.
@@ -152,16 +162,16 @@ sub visual {
        if (@_) {
                return $format_map{ _recognize_format(shift) };
        }
-       $self eq __PACKAGE__ and return $format_map{$prefformat};
-       return $format_map{ eval { $self->{'dateformat'} } || $prefformat} ;
+       $self eq __PACKAGE__ and return $format_map{_prefformat()};
+       return $format_map{ eval { $self->{'dateformat'} } || _prefformat()} ;
 }
 
 # like the functions from the old C4::Date.pm
 sub format_date {
-       return __PACKAGE__ -> new(shift,'iso')->output((@_) ? shift : $prefformat);
+       return __PACKAGE__ -> new(shift,'iso')->output((@_) ? shift : _prefformat());
 }
 sub format_date_in_iso {
-       return __PACKAGE__ -> new(shift,$prefformat)->output('iso');
+       return __PACKAGE__ -> new(shift,_prefformat())->output('iso');
 }
 
 1;
@@ -253,9 +263,9 @@ Or even:
 
                print C4::Dates->new($date_from_database,"iso")->output("syspref");
 
-If you just want to know what the <systempreferece> is, you can use:
+If you just want to know what the <systempreferece> is, a default Dates object can tell you:
 
-C4::Dates->
+               C4::Dates->new()->format();
 
 =head2 ->DHMTLcalendar([date_format])
 
@@ -279,7 +289,7 @@ To validate before creating a new object, use the regexp method of the class:
                $input =~ C4::Dates->regexp("iso") or deal_with_it("input ($input) invalid as iso format");
                my $date = C4::Dates->new($input,"iso");
 
-More verose debugging messages are sent in the presence of non-zero $ENV{"DEBUG"}.
+More verbose debugging messages are sent in the presence of non-zero $ENV{"DEBUG"}.
 
 =head3 TO DO
 
@@ -288,5 +298,16 @@ This kind of check should be centralized somewhere.  Probably not here, though.
 
 Notes: if the date in the db is null or empty, interpret null expiration to mean "never expires".
 
+=head3 _prefformat()
+
+This internal function is used to read the preferred date format
+from the system preference table.  It reads the preference once, 
+then caches it.
+
+This replaces using the package variable $prefformat directly, and
+specifically, doing a call to C4::Context->preference() during
+module initialization.  That way, C4::Dates no longer has a
+compile-time dependency on having a valid $dbh.
+
 =cut