Bug 22721: Remove frameworkcode parameter in GetMarcFromKohaField calls
[koha.git] / tools / exceptionHolidays.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use CGI qw ( -utf8 );
6
7 use C4::Auth;
8 use C4::Output;
9 use DateTime;
10
11 use C4::Calendar;
12 use Koha::DateUtils;
13
14 my $input = new CGI;
15 my $dbh = C4::Context->dbh();
16
17 my $branchcode = $input->param('showBranchName');
18 my $weekday = $input->param('showWeekday');
19 my $day = $input->param('showDay');
20 my $month = $input->param('showMonth');
21 my $year = $input->param('showYear');
22 my $title = $input->param('showTitle');
23 my $description = $input->param('showDescription');
24 my $holidaytype = $input->param('showHolidayType');
25 my $datecancelrange_dt = eval { dt_from_string( scalar $input->param('datecancelrange') ) };
26 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
27
28 my $calendar = C4::Calendar->new(branchcode => $branchcode);
29
30 $title || ($title = '');
31 if ($description) {
32     $description =~ s/\r/\\r/g;
33     $description =~ s/\n/\\n/g;
34 } else {
35     $description = '';
36 }   
37
38 # We make an array with holiday's days
39 my @holiday_list;
40 if ($datecancelrange_dt){
41             my $first_dt = DateTime->new(year => $year, month  => $month,  day => $day);
42
43             for (my $dt = $first_dt->clone();
44                 $dt <= $datecancelrange_dt;
45                 $dt->add(days => 1) )
46                 {
47                 push @holiday_list, $dt->clone();
48                 }
49 }
50 if ($input->param('showOperation') eq 'exception') {
51         $calendar->insert_exception_holiday(day => $day,
52                                                                                 month => $month,
53                                                                             year => $year,
54                                                                 title => $title,
55                                                                 description => $description);
56 } elsif ($input->param('showOperation') eq 'exceptionrange' ) {
57         if (@holiday_list){
58             foreach my $date (@holiday_list){
59                 $calendar->insert_exception_holiday(
60                     day         => $date->{local_c}->{day},
61                     month       => $date->{local_c}->{month},
62                     year       => $date->{local_c}->{year},
63                     title       => $title,
64                     description => $description
65                     );
66             }
67         }
68 } elsif ($input->param('showOperation') eq 'edit') {
69     if($holidaytype eq 'weekday') {
70       $calendar->ModWeekdayholiday(weekday => $weekday,
71                                    title => $title,
72                                    description => $description);
73     } elsif ($holidaytype eq 'daymonth') {
74       $calendar->ModDaymonthholiday(day => $day,
75                                     month => $month,
76                                     title => $title,
77                                     description => $description);
78     } elsif ($holidaytype eq 'ymd') {
79       $calendar->ModSingleholiday(day => $day,
80                                   month => $month,
81                                   year => $year,
82                                   title => $title,
83                                   description => $description);
84     } elsif ($holidaytype eq 'exception') {
85       $calendar->ModExceptionholiday(day => $day,
86                                   month => $month,
87                                   year => $year,
88                                   title => $title,
89                                   description => $description);
90     }
91 } elsif ($input->param('showOperation') eq 'delete') {
92         $calendar->delete_holiday(weekday => $weekday,
93                                   day => $day,
94                                   month => $month,
95                                               year => $year);
96 }elsif ($input->param('showOperation') eq 'deleterange') {
97     if (@holiday_list){
98         foreach my $date (@holiday_list){
99             $calendar->delete_holiday_range(weekday => $weekday,
100                                             day => $date->{local_c}->{day},
101                                             month => $date->{local_c}->{month},
102                                             year => $date->{local_c}->{year});
103             }
104     }
105 }elsif ($input->param('showOperation') eq 'deleterangerepeat') {
106     if (@holiday_list){
107         foreach my $date (@holiday_list){
108            $calendar->delete_holiday_range_repeatable(weekday => $weekday,
109                                          day => $date->{local_c}->{day},
110                                          month => $date->{local_c}->{month});
111         }
112     }
113 }elsif ($input->param('showOperation') eq 'deleterangerepeatexcept') {
114     if (@holiday_list){
115         foreach my $date (@holiday_list){
116            $calendar->delete_exception_holiday_range(weekday => $weekday,
117                                          day => $date->{local_c}->{day},
118                                          month => $date->{local_c}->{month},
119                                          year => $date->{local_c}->{year});
120         }
121     }
122 }
123 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$branchcode&calendardate=$calendardate");