3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2015 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Try::Tiny qw( catch try );
26 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
35 use Koha::SMTP::Servers;
36 use Koha::Library::Hours;
39 my $branchcode = $input->param('branchcode');
40 my $categorycode = $input->param('categorycode');
41 my $op = $input->param('op') || 'list';
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45 { template_name => "admin/branches.tt",
48 flagsrequired => { parameters => 'manage_libraries' },
52 if ( $op eq 'add_form' ) {
53 my @opening_hours = Koha::Library::Hours->search({ library_id => $branchcode }, { order_by => { -asc => 'day' } })->as_list;
56 library => Koha::Libraries->find($branchcode),
57 smtp_servers => Koha::SMTP::Servers->search,
58 opening_hours => \@opening_hours
60 } elsif ( $branchcode && $op eq 'view' ) {
61 my $library = Koha::Libraries->find($branchcode);
65 } elsif ( $op eq 'cud-add_validate' ) {
91 my $is_a_modif = $input->param('is_a_modif');
94 my $library = Koha::Libraries->find($branchcode);
95 for my $field (@fields) {
96 if( $field =~ /^(pickup_location|public)$/ ) {
97 # Don't fallback to undef/NULL, default is 1 in DB
98 $library->$field( scalar $input->param($field) );
100 $library->$field( scalar $input->param($field) || undef );
105 Koha::Database->new->schema->txn_do(
107 $library->store->discard_changes;
108 # Deal with SMTP server
109 my $smtp_server_id = $input->param('smtp_server');
111 if ( $smtp_server_id ) {
112 if ( $smtp_server_id eq '*' ) {
113 $library->smtp_server({ smtp_server => undef });
116 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
117 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
119 $library->smtp_server({ smtp_server => $smtp_server });
123 my @days = $input->multi_param("day");
124 my @open_times = $input->multi_param("open_time");
125 my @close_times = $input->multi_param("close_time");
127 foreach my $day ( @days ) {
128 if ( $open_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
129 $open_times[$day] = undef;
131 if ( $close_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
132 $close_times[$day] = undef;
135 my $openday = Koha::Library::Hours->find({ library_id => $branchcode, day => $day })->update({ open_time => $open_times[$day], close_time => $close_times[$day] });
138 push @messages, { type => 'message', code => 'success_on_update' };
143 push @messages, { type => 'alert', code => 'error_on_update' };
146 $branchcode =~ s|\s||g;
147 my $library = Koha::Library->new(
149 branchcode => $branchcode,
152 /^(pickup_location|public)$/ # Don't fallback to undef for those fields
153 ? ( $_ => scalar $input->param($_) )
154 : ( $_ => scalar $input->param($_) || undef )
161 Koha::Database->new->schema->txn_do(
163 $library->store->discard_changes;
165 my $smtp_server_id = $input->param('smtp_server');
167 if ( $smtp_server_id ) {
168 if ( $smtp_server_id ne '*' ) {
169 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
170 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
172 $library->smtp_server({ smtp_server => $smtp_server });
176 my @days = $input->multi_param("day");
177 my @open_times = $input->multi_param("open_time");
178 my @close_times = $input->multi_param("close_time");
180 foreach my $day ( @days ) {
181 if ( $open_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
182 $open_times[$day] = undef;
184 if ( $close_times[$day] !~ /([0-9]{2}:[0-9]{2})/ ) {
185 $close_times[$day] = undef;
188 my $openday = Koha::Library::Hour->new({ library_id => $branchcode, day => $day, open_time => $open_times[$day], close_time => $close_times[$day] })->store;
191 push @messages, { type => 'message', code => 'success_on_insert' };
196 push @messages, { type => 'alert', code => 'error_on_insert' };
200 } elsif ( $op eq 'delete_confirm' ) {
201 my $library = Koha::Libraries->find($branchcode);
202 my $items_count = Koha::Items->search(
204 holdingbranch => $branchcode,
205 homebranch => $branchcode
209 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
211 if ( $items_count or $patrons_count ) {
214 code => 'cannot_delete_library',
216 items_count => $items_count,
217 patrons_count => $patrons_count,
224 items_count => $items_count,
225 patrons_count => $patrons_count,
228 } elsif ( $op eq 'cud-delete_confirmed' ) {
229 my $library = Koha::Libraries->find($branchcode);
231 my $deleted = eval { $library->delete; };
233 if ( $@ or not $deleted ) {
234 push @messages, { type => 'alert', code => 'error_on_delete' };
236 push @messages, { type => 'message', code => 'success_on_delete' };
243 $template->param( libraries_count => Koha::Libraries->search->count )
247 messages => \@messages,
251 output_html_with_http_headers $input, $cookie, $template->output;