Koha/tools/exceptionHolidays.pl
Emmi Takkinen 83589f0317
Bug 33667: Allow copying holidays to all libraries when editing
When editing an existing holiday and checking the
"copy to all libraires" checkbox, the other calendars won't
get updates. Allow this by first checking if holiday exists
in target calendar and if not, add it.

To test:
1. Add unique holiday to branch A.
2. Don't check checkbox "Copy to all libraries".
3. Save.
4. Verify the holidays shows on all calendars as
a green box.
5. Edit the holiday, now check "Copy to all libraries"
and save.
=> Verify nothing has changed in other calendars:
only the green box, no holiday in list on the right
6. Edit again, make a change to description,
check checkbox, save.
=> Verify it's still not showing in the other
calendars.
9. Apply this patch.
10. Edit holiday again, check "Copy to all libraries"
and save.
=> Verify holiday is now added to other calendars.
11. Edit again, this time do not copy and save.
=> Verify holiday was edited just in branch A.
12. Again edit, check and save.
=> Verify holiday was edited in all libraries.

Sponsored-by: Koha-Suomi Oy
Signed-off-by: Sam Lau <samalau@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2023-07-06 10:08:45 -03:00

199 lines
7.4 KiB
Perl
Executable file

#!/usr/bin/perl
use Modern::Perl;
use CGI qw ( -utf8 );
use C4::Auth qw( checkauth );
use C4::Output;
use DateTime;
use C4::Calendar;
use Koha::DateUtils qw( dt_from_string );
my $input = CGI->new;
my $dbh = C4::Context->dbh();
checkauth($input, 0, {tools=> 'edit_calendar'}, 'intranet');
our $branchcode = $input->param('showBranchName');
my $originalbranchcode = $branchcode;
our $weekday = $input->param('showWeekday');
our $day = $input->param('showDay');
our $month = $input->param('showMonth');
our $year = $input->param('showYear');
our $title = $input->param('showTitle');
our $description = $input->param('showDescription');
our $holidaytype = $input->param('showHolidayType');
my $datecancelrange_dt = eval { dt_from_string( scalar $input->param('datecancelrange') ) };
my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day);
our $showoperation = $input->param('showOperation');
my $allbranches = $input->param('allBranches');
$title || ($title = '');
if ($description) {
$description =~ s/\r/\\r/g;
$description =~ s/\n/\\n/g;
} else {
$description = '';
}
# We make an array with holiday's days
our @holiday_list;
if ($datecancelrange_dt){
my $first_dt = DateTime->new(year => $year, month => $month, day => $day);
for (my $dt = $first_dt->clone();
$dt <= $datecancelrange_dt;
$dt->add(days => 1) )
{
push @holiday_list, $dt->clone();
}
}
if($allbranches) {
my $libraries = Koha::Libraries->search;
while ( my $library = $libraries->next ) {
edit_holiday($showoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype, @holiday_list);
}
} else {
edit_holiday($showoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype, @holiday_list);
}
print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate");
sub edit_holiday {
($showoperation, $branchcode, $weekday, $day, $month, $year, $title, $description, $holidaytype, @holiday_list) = @_;
my $calendar = C4::Calendar->new(branchcode => $branchcode);
if ($showoperation eq 'exception') {
$calendar->insert_exception_holiday(day => $day,
month => $month,
year => $year,
title => $title,
description => $description);
} elsif ($showoperation eq 'exceptionrange' ) {
if (@holiday_list){
foreach my $date (@holiday_list){
$calendar->insert_exception_holiday(
day => $date->{local_c}->{day},
month => $date->{local_c}->{month},
year => $date->{local_c}->{year},
title => $title,
description => $description
);
}
}
} elsif ($showoperation eq 'edit') {
if ( $holidaytype eq 'weekday' ) {
my $isHoliday = $calendar->isHoliday( $day, $month, $year );
if ($isHoliday) {
$calendar->ModWeekdayholiday(
weekday => $weekday,
title => $title,
description => $description
);
}
else {
$calendar->insert_week_day_holiday(
weekday => $weekday,
title => $title,
description => $description
);
}
}
elsif ( $holidaytype eq 'daymonth' ) {
my $isHoliday = $calendar->isHoliday( $day, $month, $year );
if ($isHoliday) {
$calendar->ModDaymonthholiday(
day => $day,
month => $month,
title => $title,
description => $description
);
}
else {
$calendar->insert_day_month_holiday(
day => $day,
month => $month,
title => $title,
description => $description
);
}
}
elsif ( $holidaytype eq 'ymd' ) {
my $isHoliday = $calendar->isHoliday( $day, $month, $year );
if ($isHoliday) {
$calendar->ModSingleholiday(
day => $day,
month => $month,
year => $year,
title => $title,
description => $description
);
}
else {
$calendar->insert_single_holiday(
day => $day,
month => $month,
year => $year,
title => $title,
description => $description
);
}
}
elsif ( $holidaytype eq 'exception' ) {
my $isHoliday = $calendar->isHoliday( $day, $month, $year );
if ($isHoliday) {
$calendar->ModExceptionholiday(
day => $day,
month => $month,
year => $year,
title => $title,
description => $description
);
}
else {
$calendar->insert_exception_holiday(
day => $day,
month => $month,
year => $year,
title => $title,
description => $description
);
}
}
} elsif ($showoperation eq 'delete') {
$calendar->delete_holiday(weekday => $weekday,
day => $day,
month => $month,
year => $year);
}elsif ($showoperation eq 'deleterange') {
if (@holiday_list){
foreach my $date (@holiday_list){
$calendar->delete_holiday_range(weekday => $weekday,
day => $date->{local_c}->{day},
month => $date->{local_c}->{month},
year => $date->{local_c}->{year});
}
}
}elsif ($showoperation eq 'deleterangerepeat') {
if (@holiday_list){
foreach my $date (@holiday_list){
$calendar->delete_holiday_range_repeatable(weekday => $weekday,
day => $date->{local_c}->{day},
month => $date->{local_c}->{month});
}
}
}elsif ($showoperation eq 'deleterangerepeatexcept') {
if (@holiday_list){
foreach my $date (@holiday_list){
$calendar->delete_exception_holiday_range(weekday => $weekday,
day => $date->{local_c}->{day},
month => $date->{local_c}->{month},
year => $date->{local_c}->{year});
}
}
}
}