Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 our $branchcode          = $input->param('newBranchName');
37 my $originalbranchcode  = $branchcode;
38 our $weekday             = $input->param('newWeekday');
39 our $day                 = $input->param('newDay');
40 our $month               = $input->param('newMonth');
41 our $year                = $input->param('newYear');
42 my $dateofrange         = $input->param('dateofrange');
43 our $title               = $input->param('newTitle');
44 our $description         = $input->param('newDescription');
45 our $newoperation        = $input->param('newOperation');
46 my $allbranches         = $input->param('allBranches');
47
48
49 my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
50 my $end_dt   = eval { dt_from_string( $dateofrange ); };
51
52 my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } );
53
54 $title || ($title = '');
55 if ($description) {
56         $description =~ s/\r/\\r/g;
57         $description =~ s/\n/\\n/g;
58 } else {
59         $description = '';
60 }
61
62 # We make an array with holiday's days
63 our @holiday_list;
64 if ($end_dt){
65     for (my $dt = $first_dt->clone();
66     $dt <= $end_dt;
67     $dt->add(days => 1) )
68     {
69         push @holiday_list, $dt->clone();
70     }
71 }
72
73 if($allbranches) {
74     my $libraries = Koha::Libraries->search;
75     while ( my $library = $libraries->next ) {
76         add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description);
77     }
78 } else {
79     add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
80 }
81
82 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
83
84 #FIXME: move add_holiday() to a better place
85 sub add_holiday {
86         ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
87         my $calendar = C4::Calendar->new(branchcode => $branchcode);
88
89         if ($newoperation eq 'weekday') {
90                 unless ( $weekday && ($weekday ne '') ) { 
91                         # was dow calculated by javascript?  original code implies it was supposed to be.
92                         # if not, we need it.
93                         $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
94                 }
95                 unless($calendar->isHoliday($day, $month, $year)) {
96                         $calendar->insert_week_day_holiday(weekday => $weekday,
97                                                                    title => $title,
98                                                                    description => $description);
99                 }
100         } elsif ($newoperation eq 'repeatable') {
101                 unless($calendar->isHoliday($day, $month, $year)) {
102                         $calendar->insert_day_month_holiday(day => $day,
103                                             month => $month,
104                                                                     title => $title,
105                                                                     description => $description);
106                 }
107         } elsif ($newoperation eq 'holiday') {
108                 unless($calendar->isHoliday($day, $month, $year)) {
109                         $calendar->insert_single_holiday(day => $day,
110                                          month => $month,
111                                                              year => $year,
112                                                              title => $title,
113                                                              description => $description);
114                 }
115
116         } elsif ( $newoperation eq 'holidayrange' ) {
117         if (@holiday_list){
118             foreach my $date (@holiday_list){
119                 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
120                     $calendar->insert_single_holiday(
121                         day         => $date->{local_c}->{day},
122                         month       => $date->{local_c}->{month},
123                         year        => $date->{local_c}->{year},
124                         title       => $title,
125                         description => $description
126                     );
127                 }
128             }
129         }
130     } elsif ( $newoperation eq 'holidayrangerepeat' ) {
131         if (@holiday_list){
132             foreach my $date (@holiday_list){
133                 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
134                     $calendar->insert_day_month_holiday(
135                         day         => $date->{local_c}->{day},
136                         month       => $date->{local_c}->{month},
137                         title       => $title,
138                         description => $description
139                     );
140                 }
141             }
142         }
143     }
144     # we updated the single_holidays table, so wipe its cache
145     my $cache = Koha::Caches->get_instance();
146     $cache->clear_from_cache( 'single_holidays') ;
147 }