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;
38 my $branchcode = $input->param('branchcode');
39 my $categorycode = $input->param('categorycode');
40 my $op = $input->param('op') || 'list';
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 { template_name => "admin/branches.tt",
47 flagsrequired => { parameters => 'manage_libraries' },
51 if ( $op eq 'add_form' ) {
53 library => Koha::Libraries->find($branchcode),
54 smtp_servers => Koha::SMTP::Servers->search,
56 } elsif ( $branchcode && $op eq 'view' ) {
57 my $library = Koha::Libraries->find($branchcode);
61 } elsif ( $op eq 'add_validate' ) {
87 my $is_a_modif = $input->param('is_a_modif');
90 my $library = Koha::Libraries->find($branchcode);
91 for my $field (@fields) {
92 if( $field =~ /^(pickup_location|public)$/ ) {
93 # Don't fallback to undef/NULL, default is 1 in DB
94 $library->$field( scalar $input->param($field) );
96 $library->$field( scalar $input->param($field) || undef );
101 Koha::Database->new->schema->txn_do(
103 $library->store->discard_changes;
104 # Deal with SMTP server
105 my $smtp_server_id = $input->param('smtp_server');
107 if ( $smtp_server_id ) {
108 if ( $smtp_server_id eq '*' ) {
109 $library->smtp_server({ smtp_server => undef });
112 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
113 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
115 $library->smtp_server({ smtp_server => $smtp_server });
119 push @messages, { type => 'message', code => 'success_on_update' };
124 push @messages, { type => 'alert', code => 'error_on_update' };
127 $branchcode =~ s|\s||g;
128 my $library = Koha::Library->new(
130 branchcode => $branchcode,
133 /^(pickup_location|public)$/ # Don't fallback to undef for those fields
134 ? ( $_ => scalar $input->param($_) )
135 : ( $_ => scalar $input->param($_) || undef )
142 Koha::Database->new->schema->txn_do(
144 $library->store->discard_changes;
146 my $smtp_server_id = $input->param('smtp_server');
148 if ( $smtp_server_id ) {
149 if ( $smtp_server_id ne '*' ) {
150 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
151 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
153 $library->smtp_server({ smtp_server => $smtp_server });
157 push @messages, { type => 'message', code => 'success_on_insert' };
162 push @messages, { type => 'alert', code => 'error_on_insert' };
166 } elsif ( $op eq 'delete_confirm' ) {
167 my $library = Koha::Libraries->find($branchcode);
168 my $items_count = Koha::Items->search(
170 holdingbranch => $branchcode,
171 homebranch => $branchcode
175 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
177 if ( $items_count or $patrons_count ) {
180 code => 'cannot_delete_library',
182 items_count => $items_count,
183 patrons_count => $patrons_count,
190 items_count => $items_count,
191 patrons_count => $patrons_count,
194 } elsif ( $op eq 'delete_confirmed' ) {
195 my $library = Koha::Libraries->find($branchcode);
197 my $deleted = eval { $library->delete; };
199 if ( $@ or not $deleted ) {
200 push @messages, { type => 'alert', code => 'error_on_delete' };
202 push @messages, { type => 'message', code => 'success_on_delete' };
209 $template->param( libraries_count => Koha::Libraries->search->count )
213 messages => \@messages,
217 output_html_with_http_headers $input, $cookie, $template->output;