Bug 15758: Koha::Libraries - Remove GetBranches
[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 strict;
20 use warnings;
21
22 use CGI qw ( -utf8 );
23
24 use C4::Auth;
25 use C4::Output;
26
27 use C4::Calendar;
28 use Koha::DateUtils;
29
30 my $input = new CGI;
31
32 my $dbh = C4::Context->dbh();
33 # Get the template to use
34 my ($template, $loggedinuser, $cookie)
35     = get_template_and_user({template_name => "tools/holidays.tt",
36                              type => "intranet",
37                              query => $input,
38                              authnotrequired => 0,
39                              flagsrequired => {tools => 'edit_calendar'},
40                              debug => 1,
41                            });
42
43 # calendardate - date passed in url for human readability (syspref)
44 # if the url has an invalid date default to 'now.'
45 my $calendarinput_dt = eval { dt_from_string( scalar $input->param('calendardate') ); } || dt_from_string;
46 my $calendardate = output_pref( { dt => $calendarinput_dt, dateonly => 1 } );
47
48 # keydate - date passed to calendar.js.  calendar.js does not process dashes within a date.
49 my $keydate = output_pref( { dt => $calendarinput_dt, dateonly => 1, dateformat => 'iso' } );
50 $keydate =~ s/-/\//g;
51
52 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
53
54 # Get all the holidays
55
56 my $calendar = C4::Calendar->new(branchcode => $branch);
57 my $week_days_holidays = $calendar->get_week_days_holidays();
58 my @week_days;
59 foreach my $weekday (keys %$week_days_holidays) {
60 # warn "WEEK DAY : $weekday";
61     my %week_day;
62     %week_day = (KEY => $weekday,
63                  TITLE => $week_days_holidays->{$weekday}{title},
64                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
65     push @week_days, \%week_day;
66 }
67
68 my $day_month_holidays = $calendar->get_day_month_holidays();
69 my @day_month_holidays;
70 foreach my $monthDay (keys %$day_month_holidays) {
71     # Determine date format on month and day.
72     my $day_monthdate;
73     my $day_monthdate_sort;
74     if (C4::Context->preference("dateformat") eq "metric") {
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 "dmydot") {
78       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}.$day_month_holidays->{$monthDay}{day}";
79       $day_monthdate = "$day_month_holidays->{$monthDay}{day}.$day_month_holidays->{$monthDay}{month}";
80     }elsif (C4::Context->preference("dateformat") eq "us") {
81       $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
82       $day_monthdate_sort = $day_monthdate;
83     } else {
84       $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
85       $day_monthdate_sort = $day_monthdate;
86     }
87     my %day_month;
88     %day_month = (KEY => $monthDay,
89                   DATE_SORT => $day_monthdate_sort,
90                   DATE => $day_monthdate,
91                   TITLE => $day_month_holidays->{$monthDay}{title},
92                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
93     push @day_month_holidays, \%day_month;
94 }
95
96 my $exception_holidays = $calendar->get_exception_holidays();
97 my @exception_holidays;
98 foreach my $yearMonthDay (keys %$exception_holidays) {
99     my $exceptiondate = eval { dt_from_string( $exception_holidays->{$yearMonthDay}{date} ) };
100     my %exception_holiday;
101     %exception_holiday = (KEY => $yearMonthDay,
102                           DATE_SORT => $exception_holidays->{$yearMonthDay}{date},
103                           DATE => output_pref( { dt => $exceptiondate, dateonly => 1 } ),
104                           TITLE => $exception_holidays->{$yearMonthDay}{title},
105                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
106     push @exception_holidays, \%exception_holiday;
107 }
108
109 my $single_holidays = $calendar->get_single_holidays();
110 my @holidays;
111 foreach my $yearMonthDay (keys %$single_holidays) {
112     my $holidaydate_dt = eval { dt_from_string( $single_holidays->{$yearMonthDay}{date} ) };
113     my %holiday;
114     %holiday = (KEY => $yearMonthDay,
115                 DATE_SORT => $single_holidays->{$yearMonthDay}{date},
116                 DATE => output_pref( { dt => $holidaydate_dt, dateonly => 1 } ),
117                 TITLE => $single_holidays->{$yearMonthDay}{title},
118                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
119     push @holidays, \%holiday;
120 }
121
122 $template->param(
123     WEEK_DAYS_LOOP           => \@week_days,
124     HOLIDAYS_LOOP            => \@holidays,
125     EXCEPTION_HOLIDAYS_LOOP  => \@exception_holidays,
126     DAY_MONTH_HOLIDAYS_LOOP  => \@day_month_holidays,
127     calendardate             => $calendardate,
128     keydate                  => $keydate,
129     branch                   => $branch,
130 );
131
132 # Shows the template with the real values replaced
133 output_html_with_http_headers $input, $cookie, $template->output;