Bug 7460: Add template toolkit's FILTER html_line_break on opac details description...
[koha.git] / tools / newHolidays.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7
8 use C4::Auth;
9 use C4::Output;
10
11
12 use C4::Calendar;
13 use DateTime;
14
15 my $input               = new CGI;
16 my $dbh                 = C4::Context->dbh();
17
18 my $branchcode          = $input->param('newBranchName');
19 my $originalbranchcode  = $branchcode;
20 my $weekday             = $input->param('newWeekday');
21 my $day                 = $input->param('newDay');
22 my $month               = $input->param('newMonth');
23 my $year                = $input->param('newYear');
24 my $day1;
25 my $month1;
26 my $year1;
27 my $dateofrange         = $input->param('dateofrange');
28 my $title               = $input->param('newTitle');
29 my $description         = $input->param('newDescription');
30 my $newoperation        = $input->param('newOperation');
31 my $allbranches         = $input->param('allBranches');
32
33 my $calendardate        = sprintf("%04d-%02d-%02d", $year, $month, $day);
34 my $isodate             = C4::Dates->new($calendardate, 'iso');
35 $calendardate           = $isodate->output('syspref');
36
37 my @dateend = split(/[\/-]/, $dateofrange);
38 if (C4::Context->preference("dateformat") eq "metric") {
39     $day1 = $dateend[0];
40     $month1 = $dateend[1];
41     $year1 = $dateend[2];
42 }elsif (C4::Context->preference("dateformat") eq "us") {
43     $month1 = $dateend[0];
44     $day1 = $dateend[1];
45     $year1 = $dateend[2];
46 } else {
47     $year1 = $dateend[0];
48     $month1 = $dateend[1];
49     $day1 = $dateend[2];
50 }
51 $title || ($title = '');
52 if ($description) {
53         $description =~ s/\r/\\r/g;
54         $description =~ s/\n/\\n/g;
55 } else {
56         $description = '';
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 sub add_holiday {
72         ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
73         my $calendar = C4::Calendar->new(branchcode => $branchcode);
74
75         if ($newoperation eq 'weekday') {
76                 unless ( $weekday && ($weekday ne '') ) { 
77                         # was dow calculated by javascript?  original code implies it was supposed to be.
78                         # if not, we need it.
79                         $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
80                 }
81                 unless($calendar->isHoliday($day, $month, $year)) {
82                         $calendar->insert_week_day_holiday(weekday => $weekday,
83                                                                    title => $title,
84                                                                    description => $description);
85                 }
86         } elsif ($newoperation eq 'repeatable') {
87                 unless($calendar->isHoliday($day, $month, $year)) {
88                         $calendar->insert_day_month_holiday(day => $day,
89                                             month => $month,
90                                                                     title => $title,
91                                                                     description => $description);
92                 }
93         } elsif ($newoperation eq 'holiday') {
94                 unless($calendar->isHoliday($day, $month, $year)) {
95                         $calendar->insert_single_holiday(day => $day,
96                                          month => $month,
97                                                              year => $year,
98                                                              title => $title,
99                                                              description => $description);
100                 }
101
102         } elsif ( $newoperation eq 'holidayrange' ) {
103         #Make an array with holiday's days
104         my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
105         my $end_dt   = DateTime->new(year => $year1, month  => $month1,  day => $day1);
106         my @holiday_list = ();
107
108         for (my $dt = $first_dt->clone();
109             $dt <= $end_dt;
110             $dt->add(days => 1) )
111             {
112             push @holiday_list, $dt->clone();
113             }
114
115         foreach my $date (@holiday_list){
116             unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
117             $calendar->insert_single_holiday(
118                 day         => $date->{local_c}->{day},
119                 month       => $date->{local_c}->{month},
120                 year        => $date->{local_c}->{year},
121                 title       => $title,
122                 description => $description
123                 );
124             }
125         }
126     } elsif ( $newoperation eq 'holidayrangerepeat' ) {
127         #Make an array with holiday's days
128         my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
129         my $end_dt   = DateTime->new(year => $year1, month  => $month1,  day => $day1);
130         my @holiday_list = ();
131
132         for (my $dt = $first_dt->clone();
133             $dt <= $end_dt;
134             $dt->add(days => 1) )
135             {
136             push @holiday_list, $dt->clone();
137             }
138
139         foreach my $date (@holiday_list){
140             unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
141             $calendar->insert_day_month_holiday(
142                 day         => $date->{local_c}->{day},
143                 month       => $date->{local_c}->{month},
144                 title       => $title,
145                 description => $description
146                 );
147             }
148         }
149     }
150 }