granular permissions - updated 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 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
26 use C4::Calendar;
27
28 my $input = new CGI;
29
30 my $branch=C4::Context->preference('defaultbranch') || $input->param('branch');
31
32
33
34 my $dbh = C4::Context->dbh();
35 # Get the template to use
36 my ($template, $loggedinuser, $cookie)
37     = get_template_and_user({template_name => "tools/holidays.tmpl",
38                              type => "intranet",
39                              query => $input,
40                              authnotrequired => 0,
41                              flagsrequired => {tools => 'edit_calendar'},
42                              debug => 1,
43                            });
44
45 # Set all the branches.
46 my $branches = $dbh->prepare("select branchcode, branchname from branches");
47 $branches->execute;
48 # It creates a list of branches
49 my %list;
50 while (my ($branchcode, $branchname) = $branches->fetchrow) {
51     $list{$branchcode} = $branchname;
52 }
53 my @listValues = keys(%list);
54 if (!defined($branch)) {
55     $branch =$listValues[4];
56 }
57 my $branchesList = CGI::scrolling_list(-name => 'branch',
58                                        -values => \@listValues,
59                                        -labels => \%list,
60                                        -size => 1,
61                                        -default => [$branch],
62                                        -multiple => 0,
63                                        -id => "branch");
64
65 $branches->finish;
66
67 if ( C4::Context->preference("IndependantBranches") ) { 
68     $branch = C4::Context->userenv->{'branch'};
69 }
70 # Get all the holidays
71
72 my $calendar = C4::Calendar->new(branchcode => $branch);
73 my $week_days_holidays = $calendar->get_week_days_holidays();
74 my @week_days;
75 foreach my $weekday (keys %$week_days_holidays) {
76 # warn "WEEK DAY : $weekday";
77     my %week_day;
78     %week_day = (KEY => $weekday,
79                  TITLE => $week_days_holidays->{$weekday}{title},
80                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
81     push @week_days, \%week_day;
82 }
83
84 my $day_month_holidays = $calendar->get_day_month_holidays();
85 my @day_month_holidays;
86 foreach my $monthDay (keys %$day_month_holidays) {
87     my %day_month;
88     %day_month = (KEY => $monthDay,
89                   TITLE => $day_month_holidays->{$monthDay}{title},
90                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
91     push @day_month_holidays, \%day_month;
92 }
93
94 my $exception_holidays = $calendar->get_exception_holidays();
95 my @exception_holidays;
96 foreach my $yearMonthDay (keys %$exception_holidays) {
97     my %exception_holiday;
98     %exception_holiday = (KEY => $yearMonthDay,
99                           TITLE => $exception_holidays->{$yearMonthDay}{title},
100                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
101     push @exception_holidays, \%exception_holiday;
102 }
103
104 my $single_holidays = $calendar->get_single_holidays();
105 my @holidays;
106 foreach my $yearMonthDay (keys %$single_holidays) {
107     my %holiday;
108     %holiday = (KEY => $yearMonthDay,
109                 TITLE => $single_holidays->{$yearMonthDay}{title},
110                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
111     push @holidays, \%holiday;
112 }
113
114 # Replace the template values with the real ones
115 # If we have independent branches on we need to only let the user set holidays for their branch
116 # (except if the user is superlibrarian, in which case he can choose the branch anyway)
117 if ( C4::Context->preference("IndependantBranches") && !(C4::Context->userenv->{'flags'} % 2) ) { 
118         $template->param(BRANCHES => C4::Context->userenv->{'branchname'}."<input type='hidden' id='branch' value='".C4::Context->userenv->{'branch'}."'>");
119 }
120 else {
121         $template->param(BRANCHES => $branchesList);
122 }
123 $template->param(WEEK_DAYS_LOOP => \@week_days);
124 $template->param(HOLIDAYS_LOOP => \@holidays);
125 $template->param(EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays);
126 $template->param(DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays);
127 $template->param(branch => $branch);
128
129 # Shows the template with the real values replaced
130 output_html_with_http_headers $input, $cookie, $template->output;