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