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 ( $op eq 'add_validate' ) {
81 my $is_a_modif = $input->param('is_a_modif');
84 my $library = Koha::Libraries->find($branchcode);
85 for my $field (@fields) {
86 if( $field =~ /^(pickup_location|public)$/ ) {
87 # Don't fallback to undef/NULL, default is 1 in DB
88 $library->$field( scalar $input->param($field) );
90 $library->$field( scalar $input->param($field) || undef );
95 Koha::Database->new->schema->txn_do(
97 $library->store->discard_changes;
98 # Deal with SMTP server
99 my $smtp_server_id = $input->param('smtp_server');
101 if ( $smtp_server_id ) {
102 if ( $smtp_server_id eq '*' ) {
103 $library->smtp_server({ smtp_server => undef });
106 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
107 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
109 $library->smtp_server({ smtp_server => $smtp_server });
113 push @messages, { type => 'message', code => 'success_on_update' };
118 push @messages, { type => 'alert', code => 'error_on_update' };
121 $branchcode =~ s|\s||g;
122 my $library = Koha::Library->new(
124 branchcode => $branchcode,
127 /^(pickup_location|public)$/ # Don't fallback to undef for those fields
128 ? ( $_ => scalar $input->param($_) )
129 : ( $_ => scalar $input->param($_) || undef )
136 Koha::Database->new->schema->txn_do(
138 $library->store->discard_changes;
140 my $smtp_server_id = $input->param('smtp_server');
142 if ( $smtp_server_id ) {
143 if ( $smtp_server_id ne '*' ) {
144 my $smtp_server = Koha::SMTP::Servers->find( $smtp_server_id );
145 Koha::Exceptions::BadParameter->throw( parameter => 'smtp_server' )
147 $library->smtp_server({ smtp_server => $smtp_server });
151 push @messages, { type => 'message', code => 'success_on_insert' };
156 push @messages, { type => 'alert', code => 'error_on_insert' };
160 } elsif ( $op eq 'delete_confirm' ) {
161 my $library = Koha::Libraries->find($branchcode);
162 my $items_count = Koha::Items->search(
164 holdingbranch => $branchcode,
165 homebranch => $branchcode
169 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
171 if ( $items_count or $patrons_count ) {
174 code => 'cannot_delete_library',
176 items_count => $items_count,
177 patrons_count => $patrons_count,
184 items_count => $items_count,
185 patrons_count => $patrons_count,
188 } elsif ( $op eq 'delete_confirmed' ) {
189 my $library = Koha::Libraries->find($branchcode);
191 my $deleted = eval { $library->delete; };
193 if ( $@ or not $deleted ) {
194 push @messages, { type => 'alert', code => 'error_on_delete' };
196 push @messages, { type => 'message', code => 'success_on_delete' };
203 $template->param( libraries_count => Koha::Libraries->search->count )
207 messages => \@messages,
211 output_html_with_http_headers $input, $cookie, $template->output;