Bug 6390: Implement 'see mine/my branches/all baskets'-syspref
[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
14 my $input = new CGI;
15 my $dbh = C4::Context->dbh();
16
17 my $branchcode = $input->param('newBranchName');
18 my $originalbranchcode = $branchcode;
19 my $weekday = $input->param('newWeekday');
20 my $day = $input->param('newDay');
21 my $month = $input->param('newMonth');
22 my $year = $input->param('newYear');
23 my $title = $input->param('newTitle');
24 my $description = $input->param('newDescription');
25 my $newoperation = $input->param('newOperation');
26 my $allbranches = $input->param('allBranches');
27
28 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
29 my $isodate = C4::Dates->new($calendardate, 'iso');
30 $calendardate = $isodate->output('syspref');
31
32 $title || ($title = '');
33 if ($description) {
34         $description =~ s/\r/\\r/g;
35         $description =~ s/\n/\\n/g;
36 } else {
37         $description = '';
38 }
39
40 if($allbranches) {
41         my $branch;
42         my @branchcodes = split(/\|/, $input->param('branchCodes')); 
43         foreach $branch (@branchcodes) {
44                 add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description);
45         }
46 } else {
47         add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
48 }
49
50 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
51
52 sub add_holiday {
53         ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;  
54         my $calendar = C4::Calendar->new(branchcode => $branchcode);
55
56         if ($newoperation eq 'weekday') {
57                 unless ( $weekday && ($weekday ne '') ) { 
58                         # was dow calculated by javascript?  original code implies it was supposed to be.
59                         # if not, we need it.
60                         $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
61                 }
62                 unless($calendar->isHoliday($day, $month, $year)) {
63                         $calendar->insert_week_day_holiday(weekday => $weekday,
64                                                                    title => $title,
65                                                                    description => $description);
66                 }
67         } elsif ($newoperation eq 'repeatable') {
68                 unless($calendar->isHoliday($day, $month, $year)) {
69                         $calendar->insert_day_month_holiday(day => $day,
70                                             month => $month,
71                                                                     title => $title,
72                                                                     description => $description);
73                 }
74         } elsif ($newoperation eq 'holiday') {
75                 unless($calendar->isHoliday($day, $month, $year)) {
76                         $calendar->insert_single_holiday(day => $day,
77                                          month => $month,
78                                                              year => $year,
79                                                              title => $title,
80                                                              description => $description);
81                 }
82
83         }
84 }