Fixed bug #2357
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 #####Sets holiday periods for each branch. Datedues will be extended if branch is closed -TG
19 use strict;
20 use CGI;
21
22 use C4::Auth;
23 use C4::Output;
24
25 use C4::Branch; # GetBranches
26 use C4::Calendar;
27
28 my $input = new CGI;
29
30 my $dbh = C4::Context->dbh();
31 # Get the template to use
32 my ($template, $loggedinuser, $cookie)
33     = get_template_and_user({template_name => "tools/holidays.tmpl",
34                              type => "intranet",
35                              query => $input,
36                              authnotrequired => 0,
37                              flagsrequired => {tools => 'edit_calendar'},
38                              debug => 1,
39                            });
40
41 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
42 # Set all the branches.
43 my $onlymine=(C4::Context->preference('IndependantBranches') &&
44               C4::Context->userenv &&
45               C4::Context->userenv->{flags} !=1  &&
46               C4::Context->userenv->{branch}?1:0);
47 if ( $onlymine ) { 
48     $branch = C4::Context->userenv->{'branch'};
49 }
50 my $branches = GetBranches($onlymine);
51 my @branchloop;
52 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
53     my $selected = 1 if $thisbranch eq $branch;
54     my %row =(value => $thisbranch,
55                 selected => $selected,
56                 branchname => $branches->{$thisbranch}->{'branchname'},
57             );
58     push @branchloop, \%row;
59 }
60
61
62 # Get all the holidays
63
64 my $calendar = C4::Calendar->new(branchcode => $branch);
65 my $week_days_holidays = $calendar->get_week_days_holidays();
66 my @week_days;
67 foreach my $weekday (keys %$week_days_holidays) {
68 # warn "WEEK DAY : $weekday";
69     my %week_day;
70     %week_day = (KEY => $weekday,
71                  TITLE => $week_days_holidays->{$weekday}{title},
72                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
73     push @week_days, \%week_day;
74 }
75
76 my $day_month_holidays = $calendar->get_day_month_holidays();
77 my @day_month_holidays;
78 foreach my $monthDay (keys %$day_month_holidays) {
79     my %day_month;
80     %day_month = (KEY => $monthDay,
81                   TITLE => $day_month_holidays->{$monthDay}{title},
82                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
83     push @day_month_holidays, \%day_month;
84 }
85
86 my $exception_holidays = $calendar->get_exception_holidays();
87 my @exception_holidays;
88 foreach my $yearMonthDay (keys %$exception_holidays) {
89     my %exception_holiday;
90     %exception_holiday = (KEY => $yearMonthDay,
91                           TITLE => $exception_holidays->{$yearMonthDay}{title},
92                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
93     push @exception_holidays, \%exception_holiday;
94 }
95
96 my $single_holidays = $calendar->get_single_holidays();
97 my @holidays;
98 foreach my $yearMonthDay (keys %$single_holidays) {
99     my %holiday;
100     %holiday = (KEY => $yearMonthDay,
101                 TITLE => $single_holidays->{$yearMonthDay}{title},
102                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
103     push @holidays, \%holiday;
104 }
105
106 $template->param(WEEK_DAYS_LOOP => \@week_days,
107                                 branchloop => \@branchloop, 
108                                 HOLIDAYS_LOOP => \@holidays,
109                                 EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays,
110                                 DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays,
111                                 branch => $branch
112         );
113
114 # Shows the template with the real values replaced
115 output_html_with_http_headers $input, $cookie, $template->output;