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>.
32 my $branchcode = $input->param('branchcode');
33 my $categorycode = $input->param('categorycode');
34 my $op = $input->param('op') || 'list';
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38 { template_name => "admin/branches.tt",
41 flagsrequired => { parameters => 'manage_libraries' },
46 if ( $op eq 'add_form' ) {
49 $library = Koha::Libraries->find($branchcode);
55 } elsif ( $op eq 'add_validate' ) {
78 my $is_a_modif = $input->param('is_a_modif');
81 my $library = Koha::Libraries->find($branchcode);
82 for my $field (@fields) {
83 $library->$field( scalar $input->param($field) );
85 eval { $library->store; };
87 push @messages, { type => 'alert', code => 'error_on_update' };
89 push @messages, { type => 'message', code => 'success_on_update' };
92 $branchcode =~ s|\s||g;
93 my $library = Koha::Library->new(
94 { branchcode => $branchcode,
95 ( map { $_ => scalar $input->param($_) || undef } @fields )
98 eval { $library->store; };
100 push @messages, { type => 'alert', code => 'error_on_insert' };
102 push @messages, { type => 'message', code => 'success_on_insert' };
106 } elsif ( $op eq 'delete_confirm' ) {
107 my $library = Koha::Libraries->find($branchcode);
108 my $items_count = Koha::Items->search(
110 holdingbranch => $branchcode,
111 homebranch => $branchcode
115 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
117 if ( $items_count or $patrons_count ) {
120 code => 'cannot_delete_library',
122 items_count => $items_count,
123 patrons_count => $patrons_count,
130 items_count => $items_count,
131 patrons_count => $patrons_count,
134 } elsif ( $op eq 'delete_confirmed' ) {
135 my $library = Koha::Libraries->find($branchcode);
137 my $deleted = eval { $library->delete; };
139 if ( $@ or not $deleted ) {
140 push @messages, { type => 'alert', code => 'error_on_delete' };
142 push @messages, { type => 'message', code => 'success_on_delete' };
149 if ( $op eq 'list' ) {
150 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
151 $template->param( libraries => $libraries, );
155 messages => \@messages,
159 output_html_with_http_headers $input, $cookie, $template->output;