Bug 2889: Removed toggle variable from roadtype.pl and .tmpl.
[koha.git] / tools / newHolidays.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5
6 use C4::Auth;
7 use C4::Output;
8
9
10 use C4::Calendar;
11
12 my $input = new CGI;
13 my $dbh = C4::Context->dbh();
14
15 my $branchcode = $input->param('newBranchName');
16 my $originalbranchcode = $branchcode;
17 my $weekday = $input->param('newWeekday');
18 my $day = $input->param('newDay');
19 my $month = $input->param('newMonth');
20 my $year = $input->param('newYear');
21 my $title = $input->param('newTitle');
22 my $description = $input->param('newDescription');
23 my $newoperation = $input->param('newOperation');
24 my $allbranches = $input->param('allBranches');
25
26 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
27 my $isodate = C4::Dates->new($calendardate, 'iso');
28 $calendardate = $isodate->output('syspref');
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 if($allbranches) {
39         my $branch;
40         my @branchcodes = split(/\|/, $input->param('branchCodes')); 
41         foreach $branch (@branchcodes) {
42                 add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description);
43         }
44 } else {
45         add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
46 }
47
48 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
49
50 sub add_holiday {
51         ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
52         my $calendar = C4::Calendar->new(branchcode => $branchcode);
53
54         if ($newoperation eq 'weekday') {
55                 unless ( $weekday && ($weekday ne '') ) { 
56                         # was dow calculated by javascript?  original code implies it was supposed to be.
57                         # if not, we need it.
58                         $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
59                 }
60                 unless($calendar->isHoliday($day, $month, $year)) {
61                         $calendar->insert_week_day_holiday(weekday => $weekday,
62                                                                    title => $title,
63                                                                    description => $description);
64                 }
65         } elsif ($newoperation eq 'repeatable') {
66                 unless($calendar->isHoliday($day, $month, $year)) {
67                         $calendar->insert_day_month_holiday(day => $day,
68                                             month => $month,
69                                                                     title => $title,
70                                                                     description => $description);
71                 }
72         } elsif ($newoperation eq 'holiday') {
73                 unless($calendar->isHoliday($day, $month, $year)) {
74                         $calendar->insert_single_holiday(day => $day,
75                                          month => $month,
76                                                              year => $year,
77                                                              title => $title,
78                                                              description => $description);
79                 }
80
81         }
82 }