Bug 4985: Edit a calendar entry for all branches
[koha.git] / tools / exceptionHolidays.pl
blob680fe17130b90c5b60e6a63f0fc144be595ab866
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use CGI qw ( -utf8 );
7 use C4::Auth;
8 use C4::Output;
9 use DateTime;
11 use C4::Calendar;
12 use Koha::DateUtils;
14 my $input = new CGI;
15 my $dbh = C4::Context->dbh();
17 checkauth($input, 0, {tools=> 'edit_calendar'}, 'intranet');
20 my $branchcode = $input->param('showBranchName');
21 my $originalbranchcode = $branchcode;
22 my $weekday = $input->param('showWeekday');
23 my $day = $input->param('showDay');
24 my $month = $input->param('showMonth');
25 my $year = $input->param('showYear');
26 my $title = $input->param('showTitle');
27 my $description = $input->param('showDescription');
28 my $holidaytype = $input->param('showHolidayType');
29 my $datecancelrange_dt = eval { dt_from_string( scalar $input->param('datecancelrange') ) };
30 my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
31 my $showoperation = $input->param('showOperation');
32 my $allbranches = $input->param('allBranches');
34 $title || ($title = '');
35 if ($description) {
36 $description =~ s/\r/\\r/g;
37 $description =~ s/\n/\\n/g;
38 } else {
39 $description = '';
42 # We make an array with holiday's days
43 my @holiday_list;
44 if ($datecancelrange_dt){
45 my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
47 for (my $dt = $first_dt->clone();
48 $dt <= $datecancelrange_dt;
49 $dt->add(days => 1) )
51 push @holiday_list, $dt->clone();
55 if($allbranches) {
56 my $libraries = Koha::Libraries->search;
57 while ( my $library = $libraries->next ) {
58 edit_holiday($showoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype);
60 } else {
61 edit_holiday($showoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype);
64 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
66 sub edit_holiday {
67 ($showoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype) = @_;
68 my $calendar = C4::Calendar->new(branchcode => $branchcode);
70 if ($showoperation eq 'exception') {
71 $calendar->insert_exception_holiday(day => $day,
72 month => $month,
73 year => $year,
74 title => $title,
75 description => $description);
76 } elsif ($showoperation eq 'exceptionrange' ) {
77 if (@holiday_list){
78 foreach my $date (@holiday_list){
79 $calendar->insert_exception_holiday(
80 day => $date->{local_c}->{day},
81 month => $date->{local_c}->{month},
82 year => $date->{local_c}->{year},
83 title => $title,
84 description => $description
88 } elsif ($showoperation eq 'edit') {
89 if($holidaytype eq 'weekday') {
90 $calendar->ModWeekdayholiday(weekday => $weekday,
91 title => $title,
92 description => $description);
93 } elsif ($holidaytype eq 'daymonth') {
94 $calendar->ModDaymonthholiday(day => $day,
95 month => $month,
96 title => $title,
97 description => $description);
98 } elsif ($holidaytype eq 'ymd') {
99 $calendar->ModSingleholiday(day => $day,
100 month => $month,
101 year => $year,
102 title => $title,
103 description => $description);
104 } elsif ($holidaytype eq 'exception') {
105 $calendar->ModExceptionholiday(day => $day,
106 month => $month,
107 year => $year,
108 title => $title,
109 description => $description);
111 } elsif ($showoperation eq 'delete') {
112 $calendar->delete_holiday(weekday => $weekday,
113 day => $day,
114 month => $month,
115 year => $year);
116 }elsif ($showoperation eq 'deleterange') {
117 if (@holiday_list){
118 foreach my $date (@holiday_list){
119 $calendar->delete_holiday_range(weekday => $weekday,
120 day => $date->{local_c}->{day},
121 month => $date->{local_c}->{month},
122 year => $date->{local_c}->{year});
125 }elsif ($showoperation eq 'deleterangerepeat') {
126 if (@holiday_list){
127 foreach my $date (@holiday_list){
128 $calendar->delete_holiday_range_repeatable(weekday => $weekday,
129 day => $date->{local_c}->{day},
130 month => $date->{local_c}->{month});
133 }elsif ($showoperation eq 'deleterangerepeatexcept') {
134 if (@holiday_list){
135 foreach my $date (@holiday_list){
136 $calendar->delete_exception_holiday_range(weekday => $weekday,
137 day => $date->{local_c}->{day},
138 month => $date->{local_c}->{month},
139 year => $date->{local_c}->{year});