Bug 6796: Add library_hours table and set opening hours for library

Sponsored-by: Catalyst IT
Signed-off-by: Sam Lau <samalau@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Aleisha Amohia 2021-09-07 15:22:25 +12:00 committed by Katrin Fischer
parent b4ce5051c1
commit 7dac9eb7b5
Signed by: kfischer
GPG key ID: 0EF6E2C03357A834
6 changed files with 262 additions and 1 deletions

51
Koha/Library/Hour.pm Normal file
View file

@ -0,0 +1,51 @@
package Koha::Library::Hour;
# Copyright 2021 Aleisha Amohia <aleisha@catalyst.net.nz>
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::Database;
use base qw(Koha::Object);
=head1 NAME
Koha::Library::Hour - Koha Library Hour Object class
=head1 SYNOPSIS
use Koha::Library::Hour;
=head1 DESCRIPTION
Describes the open time and close time for a library on a given day of the week.
=head1 FUNCTIONS
=head2 Class Methods
=head3 _type
Return type of Object relating to Schema Resultset
=cut
sub _type {
return 'LibraryHour';
}
1;

56
Koha/Library/Hours.pm Normal file
View file

@ -0,0 +1,56 @@
package Koha::Library::Hours;
# Copyright 2021 Aleisha Amohia <aleisha@catalyst.net.nz>
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Koha::Database;
use Koha::Library::Hour;
use base qw(Koha::Objects);
=head1 NAME
Koha::Library::Hours - Koha Library Hours Object set class
=head1 API
=head2 Class Methods
=cut
=head3 _type
Return type of object, relating to Schema Resultset
=cut
sub _type {
return 'LibraryHour';
}
=head3 object_class
Return object class
=cut
sub object_class {
return 'Koha::Library::Hour';
}
1;

View file

@ -33,6 +33,7 @@ use Koha::Patrons;
use Koha::Items;
use Koha::Libraries;
use Koha::SMTP::Servers;
use Koha::Library::Hours;
my $input = CGI->new;
my $branchcode = $input->param('branchcode');
@ -49,9 +50,12 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
);
if ( $op eq 'add_form' ) {
my @opening_hours = Koha::Library::Hours->search({ library_id => $branchcode }, { order_by => { -asc => 'day' } })->as_list;
$template->param(
library => Koha::Libraries->find($branchcode),
smtp_servers => Koha::SMTP::Servers->search,
opening_hours => \@opening_hours
);
} elsif ( $branchcode && $op eq 'view' ) {
my $library = Koha::Libraries->find($branchcode);
@ -116,6 +120,21 @@ if ( $op eq 'add_form' ) {
}
}
my @days = $input->multi_param("day");
my @open_times = $input->multi_param("open_time");
my @close_times = $input->multi_param("close_time");
foreach my $day ( @days ) {
if ( $open_times[$day] and $open_times[$day] eq '' ) {
$open_times[$day] = undef;
}
if ( $close_times[$day] and $close_times[$day] eq '' ) {
$close_times[$day] = undef;
}
my $openday = Koha::Library::Hours->find({ library_id => $branchcode, day => $day })->update({ open_time => $open_times[$day], close_time => $close_times[$day] });
}
push @messages, { type => 'message', code => 'success_on_update' };
}
);
@ -154,6 +173,21 @@ if ( $op eq 'add_form' ) {
}
}
my @days = $input->multi_param("day");
my @open_times = $input->multi_param("open_time");
my @close_times = $input->multi_param("close_time");
foreach my $day ( @days ) {
if ( $open_times[$day] and $open_times[$day] eq '' ) {
$open_times[$day] = undef;
}
if ( $close_times[$day] and $close_times[$day] eq '' ) {
$close_times[$day] = undef;
}
my $openday = Koha::Library::Hour->new({ library_id => $branchcode, day => $day, open_time => $open_times[$day], close_time => $close_times[$day] })->store;
}
push @messages, { type => 'message', code => 'success_on_insert' };
}
);

View file

@ -0,0 +1,35 @@
use Modern::Perl;
return {
bug_number => "6796",
description => "Overnight checkouts taking into account opening and closing hours",
up => sub {
my ($args) = @_;
my ( $dbh, $out ) = @$args{qw(dbh out)};
unless ( TableExists('library_hours') ) {
$dbh->do(
q{
CREATE TABLE library_hours (
library_id varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
day enum('0','1','2','3','4','5','6') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
open_time time COLLATE utf8mb4_unicode_ci DEFAULT NULL,
close_time time COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (library_id, day),
CONSTRAINT library_hours FOREIGN KEY (library_id) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
}
);
my @indexes = ( 0 .. 6 );
for my $i (@indexes) {
$dbh->do(
q{ INSERT INTO library_hours (library_id, day) SELECT branchcode, ? FROM branches }, undef,
$i
);
}
say $out "Added table `library_hours`";
}
},
};

View file

@ -4270,6 +4270,19 @@ CREATE TABLE `letter` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `library_hours`
--
DROP TABLE IF EXISTS library_hours;
CREATE TABLE library_hours (
library_id varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
day enum('0','1','2','3','4','5','6') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
open_time time COLLATE utf8mb4_unicode_ci DEFAULT NULL,
close_time time COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (library_id, day),
CONSTRAINT library_hours_ibfk_1 FOREIGN KEY (library_id) REFERENCES branches (branchcode) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Table structure for table `library_groups`
--

View file

@ -290,6 +290,61 @@
</select>
<div class="hint">Set to 'yes' to show this library as a search option and on the libraries page in the OPAC.</div>
</li>
<li>
<label for="opening_hours">Opening hours: </label>
<table id="opening_hours_table">
<thead>
<tr>
<th>Day</th>
<th>Open time</th>
<th>Close time</th>
</tr>
</thead>
<tbody>
[% IF opening_hours # Existing library %]
[% daycount = 0 %]
[% FOREACH hr IN opening_hours %]
<tr id="hours_[% daycount %]">
<td>
[% PROCESS dayname day=daycount %]
<input type="hidden" value="[% daycount %]" name="day">
</td>
<td>
[% IF hr.day == daycount && hr.open_time %]
<input type="time" value="[% hr.open_time %]" name="open_time">
[% ELSE %]
<input type="time" value="" name="open_time">
[% END %]
</td>
<td>
[% IF hr.day == daycount && hr.close_time %]
<input type="time" value="[% hr.close_time %]" name="close_time">
[% ELSE %]
<input type="time" value="" name="close_time">
[% END %]
</td>
</tr>
[% daycount = daycount+1 %]
[% END %]
[% ELSE # New library %]
[% FOREACH daycount IN [0..6] %]
<tr id="hours_[% day %]">
<td>
[% PROCESS dayname day=daycount %]
<input type="hidden" value="[% daycount %]" name="day">
</td>
<td>
<input type="time" value="" name="open_time">
</td>
<td>
<input type="time" value="" name="close_time">
</td>
</tr>
[% END %]
[% END %]
</tbody>
</table>
</li>
<li>
<label for="opacuserjs">Specific OPAC JS: </label>
<div style="display:flex; flex-direction:column;">
@ -306,7 +361,6 @@
<a class="collapse-textarea" id="collapse_opacusercss" data-target="opacusercss" data-syntax="css" style="display:none" href="#">Collapse</br></a>
</div>
</li>
</li>
</ol>
</fieldset>
<fieldset class="action">
@ -316,6 +370,24 @@
</form>
[% END %]
[% BLOCK dayname %]
[% IF day == 0 %]
<span>Monday</span>
[% ELSIF day == 1 %]
<span>Tuesday</span>
[% ELSIF day == 2 %]
<span>Wednesday</span>
[% ELSIF day == 3 %]
<span>Thursday</span>
[% ELSIF day == 4 %]
<span>Friday</span>
[% ELSIF day == 5 %]
<span>Saturday</span>
[% ELSE %]
<span>Sunday</span>
[% END %]
[% END %]
[% IF op == 'delete_confirm' and not ( items_count or patrons_count )%]
<div class="dialog alert">
<form action="/cgi-bin/koha/admin/branches.pl" method="post">