Bug 8338: (QA follow-up) Clean up warning
[koha.git] / tools / newHolidays.pl
blobf13e52447b525ea76e5d83c75b9795a557220b6c
1 #!/usr/bin/perl
2 #FIXME: perltidy this file
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public Lic# along with Koha; if not, see <http://www.gnu.org/licenses>.
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Output;
27 use Koha::Caches;
29 use C4::Calendar;
30 use DateTime;
31 use Koha::DateUtils;
33 my $input = new CGI;
34 my $dbh = C4::Context->dbh();
36 checkauth($input, 0, {tools=> 'edit_calendar'}, 'intranet');
38 our $branchcode = $input->param('newBranchName');
39 my $originalbranchcode = $branchcode;
40 our $weekday = $input->param('newWeekday');
41 our $day = $input->param('newDay');
42 our $month = $input->param('newMonth');
43 our $year = $input->param('newYear');
44 my $dateofrange = $input->param('dateofrange');
45 our $title = $input->param('newTitle');
46 our $description = $input->param('newDescription');
47 our $newoperation = $input->param('newOperation');
48 my $allbranches = $input->param('allBranches');
51 my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
52 my $end_dt = eval { dt_from_string( $dateofrange ); };
54 my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } );
56 $title || ($title = '');
57 if ($description) {
58 $description =~ s/\r/\\r/g;
59 $description =~ s/\n/\\n/g;
60 } else {
61 $description = '';
64 # We make an array with holiday's days
65 our @holiday_list;
66 if ($end_dt){
67 for (my $dt = $first_dt->clone();
68 $dt <= $end_dt;
69 $dt->add(days => 1) )
71 push @holiday_list, $dt->clone();
75 if($allbranches) {
76 my $libraries = Koha::Libraries->search;
77 while ( my $library = $libraries->next ) {
78 add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description);
80 } else {
81 add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
84 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
86 #FIXME: move add_holiday() to a better place
87 sub add_holiday {
88 ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;
89 my $calendar = C4::Calendar->new(branchcode => $branchcode);
91 if ($newoperation eq 'weekday') {
92 unless ( $weekday && ($weekday ne '') ) {
93 # was dow calculated by javascript? original code implies it was supposed to be.
94 # if not, we need it.
95 $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
97 unless($calendar->isHoliday($day, $month, $year)) {
98 $calendar->insert_week_day_holiday(weekday => $weekday,
99 title => $title,
100 description => $description);
102 } elsif ($newoperation eq 'repeatable') {
103 unless($calendar->isHoliday($day, $month, $year)) {
104 $calendar->insert_day_month_holiday(day => $day,
105 month => $month,
106 title => $title,
107 description => $description);
109 } elsif ($newoperation eq 'holiday') {
110 unless($calendar->isHoliday($day, $month, $year)) {
111 $calendar->insert_single_holiday(day => $day,
112 month => $month,
113 year => $year,
114 title => $title,
115 description => $description);
118 } elsif ( $newoperation eq 'holidayrange' ) {
119 if (@holiday_list){
120 foreach my $date (@holiday_list){
121 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
122 $calendar->insert_single_holiday(
123 day => $date->{local_c}->{day},
124 month => $date->{local_c}->{month},
125 year => $date->{local_c}->{year},
126 title => $title,
127 description => $description
132 } elsif ( $newoperation eq 'holidayrangerepeat' ) {
133 if (@holiday_list){
134 foreach my $date (@holiday_list){
135 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
136 $calendar->insert_day_month_holiday(
137 day => $date->{local_c}->{day},
138 month => $date->{local_c}->{month},
139 title => $title,
140 description => $description
146 # we updated the single_holidays table, so wipe its cache
147 my $cache = Koha::Caches->get_instance();
148 $cache->clear_from_cache( 'single_holidays') ;