Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 #####Sets holiday periods for each branch. Datedues will be extended if branch is closed -TG
19 use Modern::Perl;
20
21 use CGI qw ( -utf8 );
22
23 use C4::Auth;
24 use C4::Output;
25
26 use C4::Calendar;
27 use Koha::DateUtils;
28
29 my $input = new CGI;
30
31 my $dbh = C4::Context->dbh();
32 # Get the template to use
33 my ($template, $loggedinuser, $cookie)
34     = get_template_and_user({template_name => "tools/holidays.tt",
35                              type => "intranet",
36                              query => $input,
37                              authnotrequired => 0,
38                              flagsrequired => {tools => 'edit_calendar'},
39                              debug => 1,
40                            });
41
42 # calendardate - date passed in url for human readability (syspref)
43 # if the url has an invalid date default to 'now.'
44 my $calendarinput_dt = eval { dt_from_string( scalar $input->param('calendardate') ); } || dt_from_string;
45 my $calendardate = output_pref( { dt => $calendarinput_dt, dateonly => 1 } );
46
47 # keydate - date passed to calendar.js.  calendar.js does not process dashes within a date.
48 my $keydate = output_pref( { dt => $calendarinput_dt, dateonly => 1, dateformat => 'iso' } );
49 $keydate =~ s/-/\//g;
50
51 my $branch= $input->param('branch') || C4::Context->userenv->{'branch'};
52
53 # Get all the holidays
54
55 my $calendar = C4::Calendar->new(branchcode => $branch);
56 my $week_days_holidays = $calendar->get_week_days_holidays();
57 my @week_days;
58 foreach my $weekday (keys %$week_days_holidays) {
59 # warn "WEEK DAY : $weekday";
60     my %week_day;
61     %week_day = (KEY => $weekday,
62                  TITLE => $week_days_holidays->{$weekday}{title},
63                  DESCRIPTION => $week_days_holidays->{$weekday}{description});
64     push @week_days, \%week_day;
65 }
66
67 my $day_month_holidays = $calendar->get_day_month_holidays();
68 my @day_month_holidays;
69 foreach my $monthDay (keys %$day_month_holidays) {
70     # Determine date format on month and day.
71     my $day_monthdate;
72     my $day_monthdate_sort;
73     if (C4::Context->preference("dateformat") eq "metric") {
74       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
75       $day_monthdate = "$day_month_holidays->{$monthDay}{day}/$day_month_holidays->{$monthDay}{month}";
76     } elsif (C4::Context->preference("dateformat") eq "dmydot") {
77       $day_monthdate_sort = "$day_month_holidays->{$monthDay}{month}.$day_month_holidays->{$monthDay}{day}";
78       $day_monthdate = "$day_month_holidays->{$monthDay}{day}.$day_month_holidays->{$monthDay}{month}";
79     }elsif (C4::Context->preference("dateformat") eq "us") {
80       $day_monthdate = "$day_month_holidays->{$monthDay}{month}/$day_month_holidays->{$monthDay}{day}";
81       $day_monthdate_sort = $day_monthdate;
82     } else {
83       $day_monthdate = "$day_month_holidays->{$monthDay}{month}-$day_month_holidays->{$monthDay}{day}";
84       $day_monthdate_sort = $day_monthdate;
85     }
86     my %day_month;
87     %day_month = (KEY => $monthDay,
88                   DATE_SORT => $day_monthdate_sort,
89                   DATE => $day_monthdate,
90                   TITLE => $day_month_holidays->{$monthDay}{title},
91                   DESCRIPTION => $day_month_holidays->{$monthDay}{description});
92     push @day_month_holidays, \%day_month;
93 }
94
95 my $exception_holidays = $calendar->get_exception_holidays();
96 my @exception_holidays;
97 foreach my $yearMonthDay (keys %$exception_holidays) {
98     my $exceptiondate = eval { dt_from_string( $exception_holidays->{$yearMonthDay}{date} ) };
99     my %exception_holiday;
100     %exception_holiday = (KEY => $yearMonthDay,
101                           DATE_SORT => $exception_holidays->{$yearMonthDay}{date},
102                           DATE => output_pref( { dt => $exceptiondate, dateonly => 1 } ),
103                           TITLE => $exception_holidays->{$yearMonthDay}{title},
104                           DESCRIPTION => $exception_holidays->{$yearMonthDay}{description});
105     push @exception_holidays, \%exception_holiday;
106 }
107
108 my $single_holidays = $calendar->get_single_holidays();
109 my @holidays;
110 foreach my $yearMonthDay (keys %$single_holidays) {
111     my $holidaydate_dt = eval { dt_from_string( $single_holidays->{$yearMonthDay}{date} ) };
112     my %holiday;
113     %holiday = (KEY => $yearMonthDay,
114                 DATE_SORT => $single_holidays->{$yearMonthDay}{date},
115                 DATE => output_pref( { dt => $holidaydate_dt, dateonly => 1 } ),
116                 TITLE => $single_holidays->{$yearMonthDay}{title},
117                 DESCRIPTION => $single_holidays->{$yearMonthDay}{description});
118     push @holidays, \%holiday;
119 }
120
121 $template->param(
122     WEEK_DAYS_LOOP           => \@week_days,
123     HOLIDAYS_LOOP            => \@holidays,
124     EXCEPTION_HOLIDAYS_LOOP  => \@exception_holidays,
125     DAY_MONTH_HOLIDAYS_LOOP  => \@day_month_holidays,
126     calendardate             => $calendardate,
127     keydate                  => $keydate,
128     branch                   => $branch,
129 );
130
131 # Shows the template with the real values replaced
132 output_html_with_http_headers $input, $cookie, $template->output;