Merge branch 'bug_7368' into 3.14-master
[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 warnings;
21
22 use CGI;
23
24 use C4::Auth;
25 use C4::Output;
26
27 use C4::Branch; # GetBranches
28 use C4::Calendar;
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.tmpl",
36                              type => "intranet",
37                              query => $input,
38                              authnotrequired => 0,
39                              flagsrequired => {tools => 'edit_calendar'},
40                              debug => 1,
41                            });
42
43 # keydate - date passed to calendar.js.  calendar.js does not process dashes within a date.
44 my $keydate;
45 # calendardate - date passed in url for human readability (syspref)
46 my $calendardate;
47 my $today = C4::Dates->new();
48 my $calendarinput = C4::Dates->new($input->param('calendardate')) || $today;
49 # if the url has an invalid date default to 'now.'
50 unless($calendardate = $calendarinput->output('syspref')) {
51   $calendardate = $today->output('syspref');
52 }
53 unless($keydate = $calendarinput->output('iso')) {
54   $keydate = $today->output('iso');
55 }
56 $keydate =~ s/-/\//g;
57
58 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
59 # Set all the branches.
60 my $onlymine=(C4::Context->preference('IndependantBranches') &&
61               C4::Context->userenv &&
62               C4::Context->userenv->{flags} % 2 !=1  &&
63               C4::Context->userenv->{branch}?1:0);
64 if ( $onlymine ) { 
65     $branch = C4::Context->userenv->{'branch'};
66 }
67 my $branchname = GetBranchName($branch);
68 my $branches   = GetBranches($onlymine);
69 my @branchloop;
70 for my $thisbranch (
71     sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} }
72     keys %{$branches} ) {
73     push @branchloop,
74       { value      => $thisbranch,
75         selected   => $thisbranch eq $branch,
76         branchname => $branches->{$thisbranch}->{'branchname'},
77       };
78 }
79
80 # branches calculated - put branch codes in a single string so they can be passed in a form
81 my $branchcodes = join '|', keys %{$branches};
82
83 # Get all the holidays
84
85 my $calendar = C4::Calendar->new(branchcode => $branch);
86 my $week_days_holidays = $calendar->get_week_days_holidays();
87 my @week_days;
88 foreach my $weekday (keys %$week_days_holidays) {
89 # warn "WEEK DAY : $weekday";
90     my %week_day;
91     %week_day = (KEY => $weekday,
92                  TITLE => $week_days_holidays->{$weekday}{title},
93                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
94     push @week_days, \%week_day;
95 }
96
97 my $day_month_holidays = $calendar->get_day_month_holidays();
98 my @day_month_holidays;
99 foreach my $monthDay (keys %$day_month_holidays) {
100     # Determine date format on month and day.
101     my $day_monthdate;
102     my $day_monthdate_sort;
103     if (C4::Context->preference("dateformat") eq "metric") {
104       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
105       $day_monthdate = "$day_month_holidays->{$monthDay}{day}/$day_month_holidays->{$monthDay}{month}";
106     } elsif (C4::Context->preference("dateformat") eq "us") {
107       $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
108       $day_monthdate_sort = $day_monthdate;
109     } else {
110       $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
111       $day_monthdate_sort = $day_monthdate;
112     }
113     my %day_month;
114     %day_month = (KEY => $monthDay,
115                   DATE_SORT => $day_monthdate_sort,
116                   DATE => $day_monthdate,
117                   TITLE => $day_month_holidays->{$monthDay}{title},
118                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
119     push @day_month_holidays, \%day_month;
120 }
121
122 my $exception_holidays = $calendar->get_exception_holidays();
123 my @exception_holidays;
124 foreach my $yearMonthDay (keys %$exception_holidays) {
125     my $exceptiondate = C4::Dates->new($exception_holidays->{$yearMonthDay}{date}, "iso");
126     my %exception_holiday;
127     %exception_holiday = (KEY => $yearMonthDay,
128                           DATE_SORT => $exception_holidays->{$yearMonthDay}{date},
129                           DATE => $exceptiondate->output("syspref"),
130                           TITLE => $exception_holidays->{$yearMonthDay}{title},
131                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
132     push @exception_holidays, \%exception_holiday;
133 }
134
135 my $single_holidays = $calendar->get_single_holidays();
136 my @holidays;
137 foreach my $yearMonthDay (keys %$single_holidays) {
138     my $holidaydate = C4::Dates->new($single_holidays->{$yearMonthDay}{date}, "iso");
139     my %holiday;
140     %holiday = (KEY => $yearMonthDay,
141                 DATE_SORT => $single_holidays->{$yearMonthDay}{date},
142                 DATE => $holidaydate->output("syspref"),
143                 TITLE => $single_holidays->{$yearMonthDay}{title},
144                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
145     push @holidays, \%holiday;
146 }
147
148 $template->param(
149     WEEK_DAYS_LOOP           => \@week_days,
150     branchloop               => \@branchloop,
151     HOLIDAYS_LOOP            => \@holidays,
152     EXCEPTION_HOLIDAYS_LOOP  => \@exception_holidays,
153     DAY_MONTH_HOLIDAYS_LOOP  => \@day_month_holidays,
154     calendardate             => $calendardate,
155     keydate                  => $keydate,
156     branchcodes              => $branchcodes,
157     branch                   => $branch,
158     branchname               => $branchname,
159     branch                   => $branch,
160 );
161
162 # Shows the template with the real values replaced
163 output_html_with_http_headers $input, $cookie, $template->output;