Bug 15005: Fix pagination_bar calls
[koha.git] / tools / newHolidays.pl
blobdec4b3b7be3a7060209890bece96140ef3d6bde2
1 #!/usr/bin/perl
2 #FIXME: add a license
3 #FIXME: perltidy this file
5 use strict;
6 use warnings;
8 use CGI qw ( -utf8 );
10 use C4::Auth;
11 use C4::Output;
13 use Koha::Cache;
15 use C4::Dates;
16 use C4::Calendar;
17 use DateTime;
19 my $input = new CGI;
20 my $dbh = C4::Context->dbh();
22 our $branchcode = $input->param('newBranchName');
23 my $originalbranchcode = $branchcode;
24 our $weekday = $input->param('newWeekday');
25 our $day = $input->param('newDay');
26 our $month = $input->param('newMonth');
27 our $year = $input->param('newYear');
28 my $day1;
29 my $month1;
30 my $year1;
31 my $dateofrange = $input->param('dateofrange');
32 our $title = $input->param('newTitle');
33 our $description = $input->param('newDescription');
34 our $newoperation = $input->param('newOperation');
35 my $allbranches = $input->param('allBranches');
37 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
38 my $isodate = C4::Dates->new($calendardate, 'iso');
39 $calendardate = $isodate->output('syspref');
41 my @dateend = split(/[\/-]/, $dateofrange);
42 if (C4::Context->preference("dateformat") eq "metric") {
43 $day1 = $dateend[0];
44 $month1 = $dateend[1];
45 $year1 = $dateend[2];
46 }elsif (C4::Context->preference("dateformat") eq "us") {
47 $month1 = $dateend[0];
48 $day1 = $dateend[1];
49 $year1 = $dateend[2];
50 } else {
51 $year1 = $dateend[0];
52 $month1 = $dateend[1];
53 $day1 = $dateend[2];
55 $title || ($title = '');
56 if ($description) {
57 $description =~ s/\r/\\r/g;
58 $description =~ s/\n/\\n/g;
59 } else {
60 $description = '';
63 # We make an array with holiday's days
64 our @holiday_list;
65 if ($year1 && $month1 && $day1){
66 my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
67 my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1);
69 for (my $dt = $first_dt->clone();
70 $dt <= $end_dt;
71 $dt->add(days => 1) )
73 push @holiday_list, $dt->clone();
77 if($allbranches) {
78 my $branch;
79 my @branchcodes = split(/\|/, $input->param('branchCodes'));
80 foreach $branch (@branchcodes) {
81 add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description);
83 } else {
84 add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
87 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
89 #FIXME: move add_holiday() to a better place
90 sub add_holiday {
91 ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;
92 my $calendar = C4::Calendar->new(branchcode => $branchcode);
94 if ($newoperation eq 'weekday') {
95 unless ( $weekday && ($weekday ne '') ) {
96 # was dow calculated by javascript? original code implies it was supposed to be.
97 # if not, we need it.
98 $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
100 unless($calendar->isHoliday($day, $month, $year)) {
101 $calendar->insert_week_day_holiday(weekday => $weekday,
102 title => $title,
103 description => $description);
105 } elsif ($newoperation eq 'repeatable') {
106 unless($calendar->isHoliday($day, $month, $year)) {
107 $calendar->insert_day_month_holiday(day => $day,
108 month => $month,
109 title => $title,
110 description => $description);
112 } elsif ($newoperation eq 'holiday') {
113 unless($calendar->isHoliday($day, $month, $year)) {
114 $calendar->insert_single_holiday(day => $day,
115 month => $month,
116 year => $year,
117 title => $title,
118 description => $description);
121 } elsif ( $newoperation eq 'holidayrange' ) {
122 if (@holiday_list){
123 foreach my $date (@holiday_list){
124 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
125 $calendar->insert_single_holiday(
126 day => $date->{local_c}->{day},
127 month => $date->{local_c}->{month},
128 year => $date->{local_c}->{year},
129 title => $title,
130 description => $description
135 } elsif ( $newoperation eq 'holidayrangerepeat' ) {
136 if (@holiday_list){
137 foreach my $date (@holiday_list){
138 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
139 $calendar->insert_day_month_holiday(
140 day => $date->{local_c}->{day},
141 month => $date->{local_c}->{month},
142 title => $title,
143 description => $description
149 # we updated the single_holidays table, so wipe its cache
150 my $cache = Koha::Cache->get_instance();
151 $cache->clear_from_cache( 'single_holidays') ;