e8561d8b13a0a8e39c535e3bf935721f35d10443
[koha-equinox.git] / tools / holidays.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5
6 use C4::Auth;
7
8 use C4::Interface::CGI::Output;
9
10 use C4::Calendar::Calendar;
11
12 my $input = new CGI;
13 my $branch = $input->param('branch');
14 my $branch=C4::Context->preference('defaultbranch') unless $branch;
15 my $dbh = C4::Context->dbh();
16
17 # Set all the branches.
18 my $branches = $dbh->prepare("select branchcode, branchname from branches");
19 $branches->execute;
20 # It creates a list of branches
21 my %list;
22 while (my ($branchcode, $branchname) = $branches->fetchrow) {
23         $list{$branchcode} = $branchname;
24 }
25 my @listValues = keys(%list);
26 if (!defined($branch)) {
27         $branch =$listValues[4];
28 }
29 my $branchesList = CGI::scrolling_list(-name => 'branch',
30                                                -values => \@listValues,
31                                                    -labels => \%list,
32                                                    -size => 1,
33                                                                            -default => [$branch],
34                                                    -multiple => 0,
35                                                                            -id => "branch",
36                                                                            -onChange => "changeBranch()");
37
38 $branches->finish;
39
40 # Get all the holidays
41 my $calendar = C4::Calendar::Calendar->new(branchcode => $branch);
42 my $week_days_holidays = $calendar->get_week_days_holidays();
43 my @week_days;
44 foreach my $weekday (keys %$week_days_holidays) {
45         my %week_day;
46         %week_day = (KEY => $weekday,
47                          TITLE => $week_days_holidays->{$weekday}{title},
48                          DESCRIPTION => $week_days_holidays->{$weekday}{description});
49         push @week_days, \%week_day;
50 }
51
52 my $day_month_holidays = $calendar->get_day_month_holidays();
53 my @day_month_holidays;
54 foreach my $monthDay (keys %$day_month_holidays) {
55         my %day_month;
56         %day_month = (KEY => $monthDay,
57                           TITLE => $day_month_holidays->{$monthDay}{title},
58                           DESCRIPTION => $day_month_holidays->{$monthDay}{description});
59         push @day_month_holidays, \%day_month;
60 }
61
62 my $exception_holidays = $calendar->get_exception_holidays();
63 my @exception_holidays;
64 foreach my $yearMonthDay (keys %$exception_holidays) {
65         my %exception_holiday;
66         %exception_holiday = (KEY => $yearMonthDay,
67                                   TITLE => $exception_holidays->{$yearMonthDay}{title},
68                                   DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
69         push @exception_holidays, \%exception_holiday;
70 }
71
72 my $single_holidays = $calendar->get_single_holidays();
73 my @holidays;
74 foreach my $yearMonthDay (keys %$single_holidays) {
75         my %holiday;
76         %holiday = (KEY => $yearMonthDay,
77                         TITLE => $single_holidays->{$yearMonthDay}{title},
78                         DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
79         push @holidays, \%holiday;
80 }
81
82 # Get the template to use
83 my ($template, $loggedinuser, $cookie)
84     = get_template_and_user({template_name => "tools/holidays.tmpl",
85                                          type => "intranet",
86                                          query => $input,
87                                          authnotrequired => 0,
88                                          flagsrequired => {parameters => 1},
89                                                  debug => 1,
90                                        });
91
92 # Replace the template values with the real ones
93 $template->param(BRANCHES => $branchesList);
94 $template->param(WEEK_DAYS_LOOP => \@week_days);
95 $template->param(HOLIDAYS_LOOP => \@holidays);
96 $template->param(EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays);
97 $template->param(DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays);
98 $template->param(branch => $branch);
99
100 # Shows the template with the real values replaced
101 output_html_with_http_headers $input, $cookie, $template->output;