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>.
30 use Koha::LibraryCategories;
33 my $branchcode = $input->param('branchcode');
34 my $categorycode = $input->param('categorycode');
35 my $op = $input->param('op') || 'list';
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39 { template_name => "admin/branches.tt",
43 flagsrequired => { parameters => 'parameters_remaining_permissions' },
48 if ( $op eq 'add_form' ) {
51 $library = Koha::Libraries->find($branchcode);
56 categories => [ Koha::LibraryCategories->search( {}, { order_by => [ 'categorytype', 'categoryname' ] } ) ],
57 $library ? ( selected_categorycodes => [ map { $_->categorycode } $library->get_categories ] ) : (),
59 } elsif ( $op eq 'add_validate' ) {
80 my $is_a_modif = $input->param('is_a_modif');
83 for my $category ( Koha::LibraryCategories->search ) {
84 push @categories, $category
85 if $input->param( "selected_categorycode_" . $category->categorycode );
88 my $library = Koha::Libraries->find($branchcode);
89 for my $field (@fields) {
90 $library->$field( $input->param($field) );
92 $library->update_categories( \@categories );
94 eval { $library->store; };
96 push @messages, { type => 'error', code => 'error_on_update' };
98 push @messages, { type => 'message', code => 'success_on_update' };
101 $branchcode =~ s|\s||g;
102 my $library = Koha::Library->new(
103 { branchcode => $branchcode,
104 ( map { $_ => $input->param($_) || undef } @fields )
107 eval { $library->store; };
108 $library->add_to_categories( \@categories );
110 push @messages, { type => 'error', code => 'error_on_insert' };
112 push @messages, { type => 'message', code => 'success_on_insert' };
116 } elsif ( $op eq 'delete_confirm' ) {
117 my $library = Koha::Libraries->find($branchcode);
118 my $items_count = Koha::Items->search(
120 holdingbranch => $branchcode,
121 homebranch => $branchcode
125 my $patrons_count = Koha::Borrowers->search( { branchcode => $branchcode, } )->count;
127 if ( $items_count or $patrons_count ) {
130 code => 'cannot_delete_library',
132 items_count => $items_count,
133 patrons_count => $patrons_count,
140 items_count => $items_count,
141 patrons_count => $patrons_count,
144 } elsif ( $op eq 'delete_confirmed' ) {
145 my $library = Koha::Libraries->find($branchcode);
147 my $deleted = eval { $library->delete; };
149 if ( $@ or not $deleted ) {
150 push @messages, { type => 'error', code => 'error_on_delete' };
152 push @messages, { type => 'message', code => 'success_on_delete' };
155 } elsif ( $op eq 'add_form_category' ) {
158 $category = Koha::LibraryCategories->find($categorycode);
160 $template->param( category => $category, );
161 } elsif ( $op eq 'add_validate_category' ) {
162 my $is_a_modif = $input->param('is_a_modif');
169 my $category = Koha::LibraryCategories->find($categorycode);
170 for my $field (@fields) {
171 $category->$field( $input->param($field) );
173 $category->show_in_pulldown( $input->param('show_in_pulldown') eq 'on' );
174 eval { $category->store; };
176 push @messages, { type => 'error', code => 'error_on_update_category' };
178 push @messages, { type => 'message', code => 'success_on_update_category' };
181 my $category = Koha::LibraryCategory->new(
182 { categorycode => $categorycode,
183 ( map { $_ => $input->param($_) || undef } @fields )
186 $category->show_in_pulldown( $input->param('show_in_pulldown') eq 'on' );
187 eval { $category->store; };
189 push @messages, { type => 'error', code => 'error_on_insert_category' };
191 push @messages, { type => 'message', code => 'success_on_insert_category' };
195 } elsif ( $op eq 'delete_confirm_category' ) {
196 my $category = Koha::LibraryCategories->find($categorycode);
197 if ( my $libraries_count = scalar( $category->branchcodes ) ) {
200 code => 'cannot_delete_category',
201 data => { libraries_count => $libraries_count, },
205 $template->param( category => $category );
207 } elsif ( $op eq 'delete_confirmed_category' ) {
208 my $category = Koha::LibraryCategories->find($categorycode);
209 my $deleted = eval { $category->delete; };
211 if ( $@ or not $deleted ) {
212 push @messages, { type => 'error', code => 'error_on_delete_category' };
214 push @messages, { type => 'message', code => 'success_on_delete_category' };
221 if ( $op eq 'list' ) {
222 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
224 libraries => $libraries,
226 { categorytype => 'searchdomain',
227 categories => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ],
229 { categorytype => 'properties',
230 categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ],
237 messages => \@messages,
241 output_html_with_http_headers $input, $cookie, $template->output;