Bug 18693: Translatability: Get rid of exposing a [%% FOREACH loop in translation...
[koha.git] / tools / newHolidays.pl
blobeb948c708e5d996e1cc5bb84809710820f529cd2
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::Caches;
15 use C4::Calendar;
16 use DateTime;
17 use Koha::DateUtils;
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 $dateofrange = $input->param('dateofrange');
29 our $title = $input->param('newTitle');
30 our $description = $input->param('newDescription');
31 our $newoperation = $input->param('newOperation');
32 my $allbranches = $input->param('allBranches');
35 my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
36 my $end_dt = eval { dt_from_string( $dateofrange ); };
38 my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } );
40 $title || ($title = '');
41 if ($description) {
42 $description =~ s/\r/\\r/g;
43 $description =~ s/\n/\\n/g;
44 } else {
45 $description = '';
48 # We make an array with holiday's days
49 our @holiday_list;
50 if ($end_dt){
51 for (my $dt = $first_dt->clone();
52 $dt <= $end_dt;
53 $dt->add(days => 1) )
55 push @holiday_list, $dt->clone();
59 if($allbranches) {
60 my $libraries = Koha::Libraries->search;
61 while ( my $library = $libraries->next ) {
62 add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description);
64 } else {
65 add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description);
68 print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
70 #FIXME: move add_holiday() to a better place
71 sub add_holiday {
72 ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_;
73 my $calendar = C4::Calendar->new(branchcode => $branchcode);
75 if ($newoperation eq 'weekday') {
76 unless ( $weekday && ($weekday ne '') ) {
77 # was dow calculated by javascript? original code implies it was supposed to be.
78 # if not, we need it.
79 $weekday = &Date::Calc::Day_of_Week($year, $month, $day) % 7 unless($weekday);
81 unless($calendar->isHoliday($day, $month, $year)) {
82 $calendar->insert_week_day_holiday(weekday => $weekday,
83 title => $title,
84 description => $description);
86 } elsif ($newoperation eq 'repeatable') {
87 unless($calendar->isHoliday($day, $month, $year)) {
88 $calendar->insert_day_month_holiday(day => $day,
89 month => $month,
90 title => $title,
91 description => $description);
93 } elsif ($newoperation eq 'holiday') {
94 unless($calendar->isHoliday($day, $month, $year)) {
95 $calendar->insert_single_holiday(day => $day,
96 month => $month,
97 year => $year,
98 title => $title,
99 description => $description);
102 } elsif ( $newoperation eq 'holidayrange' ) {
103 if (@holiday_list){
104 foreach my $date (@holiday_list){
105 unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) {
106 $calendar->insert_single_holiday(
107 day => $date->{local_c}->{day},
108 month => $date->{local_c}->{month},
109 year => $date->{local_c}->{year},
110 title => $title,
111 description => $description
116 } elsif ( $newoperation eq 'holidayrangerepeat' ) {
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_day_month_holiday(
121 day => $date->{local_c}->{day},
122 month => $date->{local_c}->{month},
123 title => $title,
124 description => $description
130 # we updated the single_holidays table, so wipe its cache
131 my $cache = Koha::Caches->get_instance();
132 $cache->clear_from_cache( 'single_holidays') ;