import of the authorized values fails on the step 3 of the web-installation (ru-RU)
[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} !=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
76
77 # Get all the holidays
78
79 my $calendar = C4::Calendar->new(branchcode => $branch);
80 my $week_days_holidays = $calendar->get_week_days_holidays();
81 my @week_days;
82 foreach my $weekday (keys %$week_days_holidays) {
83 # warn "WEEK DAY : $weekday";
84     my %week_day;
85     %week_day = (KEY => $weekday,
86                  TITLE => $week_days_holidays->{$weekday}{title},
87                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
88     push @week_days, \%week_day;
89 }
90
91 my $day_month_holidays = $calendar->get_day_month_holidays();
92 my @day_month_holidays;
93 foreach my $monthDay (keys %$day_month_holidays) {
94     # Determine date format on month and day.
95     my $day_monthdate;
96     if (C4::Context->preference("dateformat") eq "metric") {
97       $day_monthdate = "$day_month_holidays->{$monthDay}{day}/$day_month_holidays->{$monthDay}{month}";
98     } elsif (C4::Context->preference("dateformat") eq "us") {
99       $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
100     } else {
101       $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
102     }
103     my %day_month;
104     %day_month = (KEY => $monthDay,
105                   DATE => $day_monthdate,
106                   TITLE => $day_month_holidays->{$monthDay}{title},
107                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
108     push @day_month_holidays, \%day_month;
109 }
110
111 my $exception_holidays = $calendar->get_exception_holidays();
112 my @exception_holidays;
113 foreach my $yearMonthDay (keys %$exception_holidays) {
114     my $exceptiondate = C4::Dates->new($exception_holidays->{$yearMonthDay}{date}, "iso");
115     my %exception_holiday;
116     %exception_holiday = (KEY => $yearMonthDay,
117                           DATE => $exceptiondate->output("syspref"),
118                           TITLE => $exception_holidays->{$yearMonthDay}{title},
119                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
120     push @exception_holidays, \%exception_holiday;
121 }
122
123 my $single_holidays = $calendar->get_single_holidays();
124 my @holidays;
125 foreach my $yearMonthDay (keys %$single_holidays) {
126     my $holidaydate = C4::Dates->new($single_holidays->{$yearMonthDay}{date}, "iso");
127     my %holiday;
128     %holiday = (KEY => $yearMonthDay,
129                 DATE => $holidaydate->output("syspref"),
130                 TITLE => $single_holidays->{$yearMonthDay}{title},
131                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
132     push @holidays, \%holiday;
133 }
134
135 $template->param(WEEK_DAYS_LOOP => \@week_days,
136                                 branchloop => \@branchloop, 
137                                 HOLIDAYS_LOOP => \@holidays,
138                                 EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays,
139                                 DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays,
140                                 calendardate => $calendardate,
141                                 keydate => $keydate,
142                                 branch => $branch
143         );
144
145 # Shows the template with the real values replaced
146 output_html_with_http_headers $input, $cookie, $template->output;