Bug 25723: Update cache flushing calls
[koha-equinox.git] / tools / newHolidays.pl
1 #!/usr/bin/perl
2 #FIXME: perltidy this file
3
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
24 use C4::Auth;
25 use C4::Output;
26
27 use Koha::Caches;
28
29 use C4::Calendar;
30 use DateTime;
31 use Koha::DateUtils;
32
33 my $input               = new CGI;
34 my $dbh                 = C4::Context->dbh();
35
36 checkauth($input, 0, {tools=> 'edit_calendar'}, 'intranet');
37
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');
49
50
51 my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
52 my $end_dt   = eval { dt_from_string( $dateofrange ); };
53
54 my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } );
55
56 $title || ($title = '');
57 if ($description) {
58         $description =~ s/\r/\\r/g;
59         $description =~ s/\n/\\n/g;
60 } else {
61         $description = '';
62 }
63
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) )
70     {
71         push @holiday_list, $dt->clone();
72     }
73 }
74
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);
79     }
80 } else {
81     add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
82 }
83
84 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
85
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);
90
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);
96                 }
97                 unless($calendar->isHoliday($day, $month, $year)) {
98                         $calendar->insert_week_day_holiday(weekday => $weekday,
99                                                                    title => $title,
100                                                                    description => $description);
101                 }
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);
108                 }
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);
116                 }
117
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
128                     );
129                 }
130             }
131         }
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
141                     );
142                 }
143             }
144         }
145     }
146 }