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' ) {
54 library => Koha::Libraries->find($branchcode),
55 smtp_servers => Koha::SMTP::Servers->search,
57 } elsif ( $branchcode && $op eq 'view' ) {
58 my $library = Koha::Libraries->find($branchcode);
62 } elsif ( $op eq 'cud-add_validate' ) {
88 my $is_a_modif = $input->param('is_a_modif');
91 my $library = Koha::Libraries->find($branchcode);
92 for my $field (@fields) {
93 if( $field =~ /^(pickup_location|public)$/ ) {
94 # Don't fallback to undef/NULL, default is 1 in DB
95 $library->$field( scalar $input->param($field) );
97 $library->$field( scalar $input->param($field) || undef );
102 Koha::Database->new->schema->txn_do(
104 $library->store->discard_changes;
106 # Deal with SMTP server
107 my $smtp_server_id = $input->param('smtp_server');
109 if ($smtp_server_id) {
110 if ( $smtp_server_id eq '*' ) {
111 $library->smtp_server( { smtp_server => undef } );
113 my $smtp_server = Koha::SMTP::Servers->find($smtp_server_id);
114 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
116 $library->smtp_server( { smtp_server => $smtp_server } );
120 # Deal with opening hours
121 my @days = $input->multi_param("day");
122 my @open_times = $input->multi_param("open_time");
123 my @close_times = $input->multi_param("close_time");
126 foreach my $day (@days) {
127 if ( $open_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
128 $open_times[$index] = undef;
130 if ( $close_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
131 $close_times[$index] = undef;
134 my $openday = Koha::Library::Hours->find( { library_id => $branchcode, day => $day } );
137 { open_time => $open_times[$index], close_time => $close_times[$index] } );
139 $openday = Koha::Library::Hour->new(
141 library_id => $branchcode, day => $day, open_time => $open_times[$index],
142 close_time => $close_times[$index]
149 push @messages, { type => 'message', code => 'success_on_update' };
154 push @messages, { type => 'alert', code => 'error_on_update' };
157 $branchcode =~ s|\s||g;
158 my $library = Koha::Library->new(
160 branchcode => $branchcode,
163 /^(pickup_location|public)$/ # Don't fallback to undef for those fields
164 ? ( $_ => scalar $input->param($_) )
165 : ( $_ => scalar $input->param($_) || undef )
172 Koha::Database->new->schema->txn_do(
174 $library->store->discard_changes;
176 my $smtp_server_id = $input->param('smtp_server');
178 # Deal with SMTP server
179 if ($smtp_server_id) {
180 if ( $smtp_server_id ne '*' ) {
181 my $smtp_server = Koha::SMTP::Servers->find($smtp_server_id);
182 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
184 $library->smtp_server( { smtp_server => $smtp_server } );
188 # Deal with opening hours
189 my @days = $input->multi_param("day");
190 my @open_times = $input->multi_param("open_time");
191 my @close_times = $input->multi_param("close_time");
194 foreach my $day (@days) {
195 if ( $open_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
196 $open_times[$index] = undef;
198 if ( $close_times[$index] !~ /([0-9]{2}:[0-9]{2})/ ) {
199 $close_times[$index] = undef;
202 my $openday = Koha::Library::Hour->new(
204 library_id => $branchcode, day => $day, open_time => $open_times[$index],
205 close_time => $close_times[$index]
211 push @messages, { type => 'message', code => 'success_on_insert' };
216 push @messages, { type => 'alert', code => 'error_on_insert' };
220 } elsif ( $op eq 'delete_confirm' ) {
221 my $library = Koha::Libraries->find($branchcode);
222 my $items_count = Koha::Items->search(
224 holdingbranch => $branchcode,
225 homebranch => $branchcode
229 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
231 if ( $items_count or $patrons_count ) {
234 code => 'cannot_delete_library',
236 items_count => $items_count,
237 patrons_count => $patrons_count,
244 items_count => $items_count,
245 patrons_count => $patrons_count,
248 } elsif ( $op eq 'cud-delete_confirmed' ) {
249 my $library = Koha::Libraries->find($branchcode);
251 my $deleted = eval { $library->delete; };
253 if ( $@ or not $deleted ) {
254 push @messages, { type => 'alert', code => 'error_on_delete' };
256 push @messages, { type => 'message', code => 'success_on_delete' };
263 $template->param( libraries_count => Koha::Libraries->search->count )
267 messages => \@messages,
271 output_html_with_http_headers $input, $cookie, $template->output;