MT 2146 :Adding index on issn
[koha.git] / tools / holidays.pl
bloba6afcaa20c32adee7486a3e12caf35fe89cbe5d4
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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
18 #####Sets holiday periods for each branch. Datedues will be extended if branch is closed -TG
19 use strict;
20 use warnings;
22 use CGI;
24 use C4::Auth;
25 use C4::Output;
27 use C4::Branch; # GetBranches
28 use C4::Calendar;
30 my $input = new CGI;
32 my $dbh = C4::Context->dbh();
33 # Get the template to use
34 my ($template, $loggedinuser, $cookie)
35 = get_template_and_user({template_name => "tools/holidays.tmpl",
36 type => "intranet",
37 query => $input,
38 authnotrequired => 0,
39 flagsrequired => {tools => 'edit_calendar'},
40 debug => 1,
41 });
43 # keydate - date passed to calendar.js. calendar.js does not process dashes within a date.
44 my $keydate;
45 # calendardate - date passed in url for human readability (syspref)
46 my $calendardate;
47 my $today = C4::Dates->new();
48 my $calendarinput = C4::Dates->new($input->param('calendardate')) || $today;
49 # if the url has an invalid date default to 'now.'
50 unless($calendardate = $calendarinput->output('syspref')) {
51 $calendardate = $today->output('syspref');
53 unless($keydate = $calendarinput->output('iso')) {
54 $keydate = $today->output('iso');
56 $keydate =~ s/-/\//g;
58 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
59 # Set all the branches.
60 my $onlymine=(C4::Context->preference('IndependantBranches') &&
61 C4::Context->userenv &&
62 C4::Context->userenv->{flags} % 2 !=1 &&
63 C4::Context->userenv->{branch}?1:0);
64 if ( $onlymine ) {
65 $branch = C4::Context->userenv->{'branch'};
67 my $branches = GetBranches($onlymine);
68 my @branchloop;
69 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
70 my $selected = 1 if $thisbranch eq $branch;
71 my %row =(value => $thisbranch,
72 selected => $selected,
73 branchname => $branches->{$thisbranch}->{'branchname'},
75 push @branchloop, \%row;
77 # branches calculated - put branch codes in a single string so they can be passed in a form
78 my $branchcodes = join("|", keys %$branches);
80 # Get all the holidays
82 my $calendar = C4::Calendar->new(branchcode => $branch);
83 my $week_days_holidays = $calendar->get_week_days_holidays();
84 my @week_days;
85 foreach my $weekday (keys %$week_days_holidays) {
86 # warn "WEEK DAY : $weekday";
87 my %week_day;
88 %week_day = (KEY => $weekday,
89 TITLE => $week_days_holidays->{$weekday}{title},
90 DESCRIPTION => $week_days_holidays->{$weekday}{description});
91 push @week_days, \%week_day;
94 my $day_month_holidays = $calendar->get_day_month_holidays();
95 my @day_month_holidays;
96 foreach my $monthDay (keys %$day_month_holidays) {
97 # Determine date format on month and day.
98 my $day_monthdate;
99 if (C4::Context->preference("dateformat") eq "metric") {
100 $day_monthdate = "$day_month_holidays->{$monthDay}{day}/$day_month_holidays->{$monthDay}{month}";
101 } elsif (C4::Context->preference("dateformat") eq "us") {
102 $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
103 } else {
104 $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
106 my %day_month;
107 %day_month = (KEY => $monthDay,
108 DATE => $day_monthdate,
109 TITLE => $day_month_holidays->{$monthDay}{title},
110 DESCRIPTION => $day_month_holidays->{$monthDay}{description});
111 push @day_month_holidays, \%day_month;
114 my $exception_holidays = $calendar->get_exception_holidays();
115 my @exception_holidays;
116 foreach my $yearMonthDay (keys %$exception_holidays) {
117 my $exceptiondate = C4::Dates->new($exception_holidays->{$yearMonthDay}{date}, "iso");
118 my %exception_holiday;
119 %exception_holiday = (KEY => $yearMonthDay,
120 DATE => $exceptiondate->output("syspref"),
121 TITLE => $exception_holidays->{$yearMonthDay}{title},
122 DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
123 push @exception_holidays, \%exception_holiday;
126 my $single_holidays = $calendar->get_single_holidays();
127 my @holidays;
128 foreach my $yearMonthDay (keys %$single_holidays) {
129 my $holidaydate = C4::Dates->new($single_holidays->{$yearMonthDay}{date}, "iso");
130 my %holiday;
131 %holiday = (KEY => $yearMonthDay,
132 DATE => $holidaydate->output("syspref"),
133 TITLE => $single_holidays->{$yearMonthDay}{title},
134 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
135 push @holidays, \%holiday;
138 $template->param(WEEK_DAYS_LOOP => \@week_days,
139 branchloop => \@branchloop,
140 HOLIDAYS_LOOP => \@holidays,
141 EXCEPTION_HOLIDAYS_LOOP => \@exception_holidays,
142 DAY_MONTH_HOLIDAYS_LOOP => \@day_month_holidays,
143 calendardate => $calendardate,
144 keydate => $keydate,
145 branchcodes => $branchcodes,
146 branch => $branch
149 # Shows the template with the real values replaced
150 output_html_with_http_headers $input, $cookie, $template->output;