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