Bug 17600: Standardize our EXPORT_OK
[koha.git] / tools / holidays.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 #####Sets holiday periods for each branch. Datedues will be extended if branch is closed -TG
19 use Modern::Perl;
20
21 use CGI qw ( -utf8 );
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25
26 use C4::Calendar;
27 use Koha::DateUtils qw( dt_from_string output_pref );
28
29 my $input = CGI->new;
30
31 my $dbh = C4::Context->dbh();
32 # Get the template to use
33 my ($template, $loggedinuser, $cookie)
34     = get_template_and_user({template_name => "tools/holidays.tt",
35                              type => "intranet",
36                              query => $input,
37                              flagsrequired => {tools => 'edit_calendar'},
38                            });
39
40 # calendardate - date passed in url for human readability (syspref)
41 # if the url has an invalid date default to 'now.'
42 my $calendarinput_dt = eval { dt_from_string( scalar $input->param('calendardate') ); } || dt_from_string;
43 my $calendardate = output_pref( { dt => $calendarinput_dt, dateonly => 1 } );
44
45 # keydate - date passed to calendar.js.  calendar.js does not process dashes within a date.
46 my $keydate = output_pref( { dt => $calendarinput_dt, dateonly => 1, dateformat => 'iso' } );
47 $keydate =~ s/-/\//g;
48
49 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
50
51 # Get all the holidays
52
53 my $calendar = C4::Calendar->new(branchcode => $branch);
54 my $week_days_holidays = $calendar->get_week_days_holidays();
55 my @week_days;
56 foreach my $weekday (keys %$week_days_holidays) {
57 # warn "WEEK DAY : $weekday";
58     my %week_day;
59     %week_day = (KEY => $weekday,
60                  TITLE => $week_days_holidays->{$weekday}{title},
61                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
62     push @week_days, \%week_day;
63 }
64
65 my $day_month_holidays = $calendar->get_day_month_holidays();
66 my @day_month_holidays;
67 foreach my $monthDay (keys %$day_month_holidays) {
68     # Determine date format on month and day.
69     my $day_monthdate;
70     my $day_monthdate_sort;
71     if (C4::Context->preference("dateformat") eq "metric") {
72       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
73       $day_monthdate = "$day_month_holidays->{$monthDay}{day}/$day_month_holidays->{$monthDay}{month}";
74     } elsif (C4::Context->preference("dateformat") eq "dmydot") {
75       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}.$day_month_holidays->{$monthDay}{day}";
76       $day_monthdate = "$day_month_holidays->{$monthDay}{day}.$day_month_holidays->{$monthDay}{month}";
77     }elsif (C4::Context->preference("dateformat") eq "us") {
78       $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
79       $day_monthdate_sort = $day_monthdate;
80     } else {
81       $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
82       $day_monthdate_sort = $day_monthdate;
83     }
84     my %day_month;
85     %day_month = (KEY => $monthDay,
86                   DATE_SORT => $day_monthdate_sort,
87                   DATE => $day_monthdate,
88                   TITLE => $day_month_holidays->{$monthDay}{title},
89                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
90     push @day_month_holidays, \%day_month;
91 }
92
93 my $exception_holidays = $calendar->get_exception_holidays();
94 my @exception_holidays;
95 foreach my $yearMonthDay (keys %$exception_holidays) {
96     my $exceptiondate = eval { dt_from_string( $exception_holidays->{$yearMonthDay}{date} ) };
97     my %exception_holiday;
98     %exception_holiday = (KEY => $yearMonthDay,
99                           DATE_SORT => $exception_holidays->{$yearMonthDay}{date},
100                           DATE => output_pref( { dt => $exceptiondate, dateonly => 1 } ),
101                           TITLE => $exception_holidays->{$yearMonthDay}{title},
102                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
103     push @exception_holidays, \%exception_holiday;
104 }
105
106 my $single_holidays = $calendar->get_single_holidays();
107 my @holidays;
108 foreach my $yearMonthDay (keys %$single_holidays) {
109     my $holidaydate_dt = eval { dt_from_string( $single_holidays->{$yearMonthDay}{date} ) };
110     my %holiday;
111     %holiday = (KEY => $yearMonthDay,
112                 DATE_SORT => $single_holidays->{$yearMonthDay}{date},
113                 DATE => output_pref( { dt => $holidaydate_dt, dateonly => 1 } ),
114                 TITLE => $single_holidays->{$yearMonthDay}{title},
115                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
116     push @holidays, \%holiday;
117 }
118
119 $template->param(
120     WEEK_DAYS_LOOP           => \@week_days,
121     HOLIDAYS_LOOP            => \@holidays,
122     EXCEPTION_HOLIDAYS_LOOP  => \@exception_holidays,
123     DAY_MONTH_HOLIDAYS_LOOP  => \@day_month_holidays,
124     calendardate             => $calendardate,
125     keydate                  => $keydate,
126     branch                   => $branch,
127 );
128
129 # Shows the template with the real values replaced
130 output_html_with_http_headers $input, $cookie, $template->output;