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