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