Finalized XML version for intranet
[koha.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 $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', -id=>'branch',
30                                                -values => \@listValues,
31                                                    -labels => \%list,
32                                                    -size => 1,
33                                          -default => [$branch],
34                                                    -multiple => 0,
35                                          -onChange => "changeBranch()");
36
37 $branches->finish;
38
39 # Get all the holidays
40 my $calendar = C4::Calendar::Calendar->new(branchcode => $branch);
41 my $week_days_holidays = $calendar->get_week_days_holidays();
42 my @week_days;
43 foreach my $weekday (keys %$week_days_holidays) {
44         my %week_day;
45         %week_day = (KEY => $weekday,
46                          TITLE => $week_days_holidays->{$weekday}{title},
47                          DESCRIPTION => $week_days_holidays->{$weekday}{description});
48         push @week_days, \%week_day;
49 }
50
51 my $day_month_holidays = $calendar->get_day_month_holidays();
52 my @day_month_holidays;
53 foreach my $monthDay (keys %$day_month_holidays) {
54         my %day_month;
55         %day_month = (KEY => $monthDay,
56                           TITLE => $day_month_holidays->{$monthDay}{title},
57                           DESCRIPTION => $day_month_holidays->{$monthDay}{description});
58         push @day_month_holidays, \%day_month;
59 }
60
61 my $exception_holidays = $calendar->get_exception_holidays();
62 my @exception_holidays;
63 foreach my $yearMonthDay (keys %$exception_holidays) {
64         my %exception_holiday;
65         %exception_holiday = (KEY => $yearMonthDay,
66                                   TITLE => $exception_holidays->{$yearMonthDay}{title},
67                                   DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
68         push @exception_holidays, \%exception_holiday;
69 }
70
71 my $single_holidays = $calendar->get_single_holidays();
72 my @holidays;
73 foreach my $yearMonthDay (keys %$single_holidays) {
74         my %holiday;
75         %holiday = (KEY => $yearMonthDay,
76                         TITLE => $single_holidays->{$yearMonthDay}{title},
77                         DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
78         push @holidays, \%holiday;
79 }
80
81 # Get the template to use
82 my ($template, $loggedinuser, $cookie)
83     = get_template_and_user({template_name => "tools/holidays.tmpl",
84                                          type => "intranet",
85                                          query => $input,
86                                          authnotrequired => 0,
87                                          flagsrequired => {parameters => 1},
88                                                  debug => 1,
89                                        });
90
91 # Replace the template values with the real ones
92 $template->param(BRANCHES => $branchesList);
93 $template->param(WEEK_DAYS_LOOP => \@week_days);
94 $template->param(HOLIDAYS_LOOP => \@holidays);
95 $template->param(EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays);
96 $template->param(DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays);
97 $template->param(branch => $branch);
98
99 # Shows the template with the real values replaced
100 output_html_with_http_headers $input, $cookie, $template->output;