Bug 15332: Sent DateTime object to DBIx::Class, not formatted dates
[koha.git] / tools / newHolidays.pl
1 #!/usr/bin/perl
2 #FIXME: add a license
3 #FIXME: perltidy this file
4
5 use strict;
6 use warnings;
7
8 use CGI qw ( -utf8 );
9
10 use C4::Auth;
11 use C4::Output;
12
13 use Koha::Cache;
14
15 use C4::Calendar;
16 use DateTime;
17 use Koha::DateUtils;
18
19 my $input               = new CGI;
20 my $dbh                 = C4::Context->dbh();
21
22 our $branchcode          = $input->param('newBranchName');
23 my $originalbranchcode  = $branchcode;
24 our $weekday             = $input->param('newWeekday');
25 our $day                 = $input->param('newDay');
26 our $month               = $input->param('newMonth');
27 our $year                = $input->param('newYear');
28 my $dateofrange         = $input->param('dateofrange');
29 our $title               = $input->param('newTitle');
30 our $description         = $input->param('newDescription');
31 our $newoperation        = $input->param('newOperation');
32 my $allbranches         = $input->param('allBranches');
33
34
35 my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
36 my $end_dt   = eval { dt_from_string( $dateofrange ); };
37
38 my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } );
39
40 $title || ($title = '');
41 if ($description) {
42         $description =~ s/\r/\\r/g;
43         $description =~ s/\n/\\n/g;
44 } else {
45         $description = '';
46 }
47
48 # We make an array with holiday's days
49 our @holiday_list;
50 if ($end_dt){
51     for (my $dt = $first_dt->clone();
52     $dt <= $end_dt;
53     $dt->add(days => 1) )
54     {
55         push @holiday_list, $dt->clone();
56     }
57 }
58
59 if($allbranches) {
60         my $branch;
61         my @branchcodes = split(/\|/, $input->param('branchCodes')); 
62         foreach $branch (@branchcodes) {
63                 add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description);
64         }
65 } else {
66         add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
67 }
68
69 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
70
71 #FIXME: move add_holiday() to a better place
72 sub add_holiday {
73         ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
74         my $calendar = C4::Calendar->new(branchcode => $branchcode);
75
76         if ($newoperation eq 'weekday') {
77                 unless ( $weekday && ($weekday ne '') ) { 
78                         # was dow calculated by javascript?  original code implies it was supposed to be.
79                         # if not, we need it.
80                         $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
81                 }
82                 unless($calendar->isHoliday($day, $month, $year)) {
83                         $calendar->insert_week_day_holiday(weekday => $weekday,
84                                                                    title => $title,
85                                                                    description => $description);
86                 }
87         } elsif ($newoperation eq 'repeatable') {
88                 unless($calendar->isHoliday($day, $month, $year)) {
89                         $calendar->insert_day_month_holiday(day => $day,
90                                             month => $month,
91                                                                     title => $title,
92                                                                     description => $description);
93                 }
94         } elsif ($newoperation eq 'holiday') {
95                 unless($calendar->isHoliday($day, $month, $year)) {
96                         $calendar->insert_single_holiday(day => $day,
97                                          month => $month,
98                                                              year => $year,
99                                                              title => $title,
100                                                              description => $description);
101                 }
102
103         } elsif ( $newoperation eq 'holidayrange' ) {
104         if (@holiday_list){
105             foreach my $date (@holiday_list){
106                 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
107                     $calendar->insert_single_holiday(
108                         day         => $date->{local_c}->{day},
109                         month       => $date->{local_c}->{month},
110                         year        => $date->{local_c}->{year},
111                         title       => $title,
112                         description => $description
113                     );
114                 }
115             }
116         }
117     } elsif ( $newoperation eq 'holidayrangerepeat' ) {
118         if (@holiday_list){
119             foreach my $date (@holiday_list){
120                 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
121                     $calendar->insert_day_month_holiday(
122                         day         => $date->{local_c}->{day},
123                         month       => $date->{local_c}->{month},
124                         title       => $title,
125                         description => $description
126                     );
127                 }
128             }
129         }
130     }
131     # we updated the single_holidays table, so wipe its cache
132     my $cache = Koha::Cache->get_instance();
133     $cache->clear_from_cache( 'single_holidays') ;
134 }