| 1 | package Template::Flute::Filter::Date; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use DateTime; | ||
| 7 | use DateTime::Format::ISO8601; | ||
| 8 | use Scalar::Util qw/blessed/; | ||
| 9 | |||
| 10 | use base 'Template::Flute::Filter'; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Template::Flute::Filter::Date - Date filter | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Date filter based on L<DateTime>. | ||
| 19 | |||
| 20 | =head1 PREREQUISITES | ||
| 21 | |||
| 22 | L<DateTime> and L<DateTime::Format::ISO8601> modules. | ||
| 23 | |||
| 24 | =head1 METHODS | ||
| 25 | |||
| 26 | =head2 init | ||
| 27 | |||
| 28 | The init method allows you to set the following options: | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item format | ||
| 33 | |||
| 34 | Format string for L<DateTime>'s strftime method. Defaults to %c. | ||
| 35 | |||
| 36 | =item strict | ||
| 37 | |||
| 38 | Determines how strict the filter is with empty resp. invalid | ||
| 39 | dates. The default setting is to throw an error on both. | ||
| 40 | |||
| 41 | You can override this setting for empty and invalid dates | ||
| 42 | separately resulting in returning an empty string instead. | ||
| 43 | |||
| 44 | Example for accepting empty dates: | ||
| 45 | |||
| 46 | options => {strict => {empty => 0}} | ||
| 47 | |||
| 48 | =item date_text | ||
| 49 | |||
| 50 | This option can be used to supply strings for empty and/or | ||
| 51 | invalid dates. It also overrides the C<strict> option. | ||
| 52 | |||
| 53 | Example for empty dates: | ||
| 54 | |||
| 55 | options => {date_text => {empty => 'Not yet scheduled'}} | ||
| 56 | |||
| 57 | =back | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub init { | ||
| 62 | my ($self, %args) = @_; | ||
| 63 | |||
| 64 | $self->{format} = $args{options}->{format} || '%c'; | ||
| 65 | $self->{strict} = $args{options}->{strict} || {empty => 1, | ||
| 66 | invalid => 1}; | ||
| 67 | $self->{date_text} = $args{options}->{date_text} | ||
| 68 | || {empty => '', invalid => ''}; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 filter | ||
| 72 | |||
| 73 | Date filter. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub filter { | ||
| 78 | my ($self, $date, %args) = @_; | ||
| 79 | my ($dt, $fmt); | ||
| 80 | |||
| 81 | if ($args{format}) { | ||
| 82 | $fmt = $args{format}; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $fmt = $self->{format}; | ||
| 86 | } | ||
| 87 | |||
| 88 | if (! defined $date || $date !~ /\S/) { | ||
| 89 | if (my $date_text = $self->{date_text}->{empty}) { | ||
| 90 | return $date_text; | ||
| 91 | } | ||
| 92 | if (! $self->{strict}->{empty}) { | ||
| 93 | # accept empty strings for dates | ||
| 94 | return ''; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | die "Empty date."; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | if (defined blessed $date && $date->isa('DateTime')) { | ||
| 102 | $dt = $date; | ||
| 103 | } | ||
| 104 | else { | ||
| 105 | # parsing date | ||
| 106 | eval { | ||
| 107 | $dt = DateTime::Format::ISO8601->parse_datetime($date); | ||
| 108 | }; | ||
| 109 | |||
| 110 | if ($@) { | ||
| 111 | if (my $date_text = $self->{date_text}->{invalid}) { | ||
| 112 | return $date_text; | ||
| 113 | } | ||
| 114 | if ($self->{strict}->{invalid}) { | ||
| 115 | die $@; | ||
| 116 | } | ||
| 117 | else { | ||
| 118 | # replace invalid dates with empty string | ||
| 119 | return ''; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | return $dt->strftime($fmt); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | =head1 AUTHOR | ||
| 129 | |||
| 130 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 131 | |||
| 132 | =head1 LICENSE AND COPYRIGHT | ||
| 133 | |||
| 134 | Copyright 2011-2016 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 135 | |||
| 136 | This program is free software; you can redistribute it and/or modify it | ||
| 137 | under the terms of either: the GNU General Public License as published | ||
| 138 | by the Free Software Foundation; or the Artistic License. | ||
| 139 | |||
| 140 | See http://dev.perl.org/licenses/ for more information. | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 1; |