Bug 14141: Fix copy action
[koha.git] / tools / newHolidays.pl
blob646276c4efc05ddd814e712004872ff21d9c49dd
1 #!/usr/bin/perl
3 use strict;
4 use warnings;
6 use CGI;
8 use C4::Auth;
9 use C4::Output;
12 use C4::Calendar;
13 use DateTime;
15 my $input = new CGI;
16 my $dbh = C4::Context->dbh();
18 our $branchcode = $input->param('newBranchName');
19 my $originalbranchcode = $branchcode;
20 our $weekday = $input->param('newWeekday');
21 our $day = $input->param('newDay');
22 our $month = $input->param('newMonth');
23 our $year = $input->param('newYear');
24 my $day1;
25 my $month1;
26 my $year1;
27 my $dateofrange = $input->param('dateofrange');
28 our $title = $input->param('newTitle');
29 our $description = $input->param('newDescription');
30 our $newoperation = $input->param('newOperation');
31 my $allbranches = $input->param('allBranches');
33 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
34 my $isodate = C4::Dates->new($calendardate, 'iso');
35 $calendardate = $isodate->output('syspref');
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];
51 $title || ($title = '');
52 if ($description) {
53 $description =~ s/\r/\\r/g;
54 $description =~ s/\n/\\n/g;
55 } else {
56 $description = '';
59 # We make an array with holiday's days
60 our @holiday_list;
61 if ($year1 && $month1 && $day1){
62 my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
63 my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1);
65 for (my $dt = $first_dt->clone();
66 $dt <= $end_dt;
67 $dt->add(days => 1) )
69 push @holiday_list, $dt->clone();
73 if($allbranches) {
74 my $branch;
75 my @branchcodes = split(/\|/, $input->param('branchCodes'));
76 foreach $branch (@branchcodes) {
77 add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description);
79 } else {
80 add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
83 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
85 sub add_holiday {
86 ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;
87 my $calendar = C4::Calendar->new(branchcode => $branchcode);
89 if ($newoperation eq 'weekday') {
90 unless ( $weekday && ($weekday ne '') ) {
91 # was dow calculated by javascript? original code implies it was supposed to be.
92 # if not, we need it.
93 $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
95 unless($calendar->isHoliday($day, $month, $year)) {
96 $calendar->insert_week_day_holiday(weekday => $weekday,
97 title => $title,
98 description => $description);
100 } elsif ($newoperation eq 'repeatable') {
101 unless($calendar->isHoliday($day, $month, $year)) {
102 $calendar->insert_day_month_holiday(day => $day,
103 month => $month,
104 title => $title,
105 description => $description);
107 } elsif ($newoperation eq 'holiday') {
108 unless($calendar->isHoliday($day, $month, $year)) {
109 $calendar->insert_single_holiday(day => $day,
110 month => $month,
111 year => $year,
112 title => $title,
113 description => $description);
116 } elsif ( $newoperation eq 'holidayrange' ) {
117 if (@holiday_list){
118 foreach my $date (@holiday_list){
119 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
120 $calendar->insert_single_holiday(
121 day => $date->{local_c}->{day},
122 month => $date->{local_c}->{month},
123 year => $date->{local_c}->{year},
124 title => $title,
125 description => $description
130 } elsif ( $newoperation eq 'holidayrangerepeat' ) {
131 if (@holiday_list){
132 foreach my $date (@holiday_list){
133 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
134 $calendar->insert_day_month_holiday(
135 day => $date->{local_c}->{day},
136 month => $date->{local_c}->{month},
137 title => $title,
138 description => $description