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' ) {
81 my $is_a_modif = $input->param('is_a_modif');
84 for my $category ( Koha::LibraryCategories->search ) {
85 push @categories, $category
86 if $input->param( "selected_categorycode_" . $category->categorycode );
89 my $library = Koha::Libraries->find($branchcode);
90 for my $field (@fields) {
91 $library->$field( scalar $input->param($field) );
93 $library->update_categories( \@categories );
95 eval { $library->store; };
97 push @messages, { type => 'alert', code => 'error_on_update' };
99 push @messages, { type => 'message', code => 'success_on_update' };
102 $branchcode =~ s|\s||g;
103 my $library = Koha::Library->new(
104 { branchcode => $branchcode,
105 ( map { $_ => scalar $input->param($_) || undef } @fields )
108 eval { $library->store; };
109 $library->add_to_categories( \@categories );
111 push @messages, { type => 'alert', code => 'error_on_insert' };
113 push @messages, { type => 'message', code => 'success_on_insert' };
117 } elsif ( $op eq 'delete_confirm' ) {
118 my $library = Koha::Libraries->find($branchcode);
119 my $items_count = Koha::Items->search(
121 holdingbranch => $branchcode,
122 homebranch => $branchcode
126 my $patrons_count = Koha::Patrons->search( { branchcode => $branchcode, } )->count;
128 if ( $items_count or $patrons_count ) {
131 code => 'cannot_delete_library',
133 items_count => $items_count,
134 patrons_count => $patrons_count,
141 items_count => $items_count,
142 patrons_count => $patrons_count,
145 } elsif ( $op eq 'delete_confirmed' ) {
146 my $library = Koha::Libraries->find($branchcode);
148 my $deleted = eval { $library->delete; };
150 if ( $@ or not $deleted ) {
151 push @messages, { type => 'alert', code => 'error_on_delete' };
153 push @messages, { type => 'message', code => 'success_on_delete' };
156 } elsif ( $op eq 'add_form_category' ) {
159 $category = Koha::LibraryCategories->find($categorycode);
161 $template->param( category => $category, );
162 } elsif ( $op eq 'add_validate_category' ) {
163 my $is_a_modif = $input->param('is_a_modif');
170 my $category = Koha::LibraryCategories->find($categorycode);
171 for my $field (@fields) {
172 $category->$field( scalar $input->param($field) );
174 $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
175 eval { $category->store; };
177 push @messages, { type => 'alert', code => 'error_on_update_category' };
179 push @messages, { type => 'message', code => 'success_on_update_category' };
182 my $category = Koha::LibraryCategory->new(
183 { categorycode => $categorycode,
184 ( map { $_ => scalar $input->param($_) || undef } @fields )
187 $category->show_in_pulldown( scalar $input->param('show_in_pulldown') eq 'on' );
188 eval { $category->store; };
190 push @messages, { type => 'alert', code => 'error_on_insert_category' };
192 push @messages, { type => 'message', code => 'success_on_insert_category' };
196 } elsif ( $op eq 'delete_confirm_category' ) {
197 my $category = Koha::LibraryCategories->find($categorycode);
198 if ( my $libraries_count = $category->libraries->count ) {
201 code => 'cannot_delete_category',
202 data => { libraries_count => $libraries_count, },
206 $template->param( category => $category );
208 } elsif ( $op eq 'delete_confirmed_category' ) {
209 my $category = Koha::LibraryCategories->find($categorycode);
210 my $deleted = eval { $category->delete; };
212 if ( $@ or not $deleted ) {
213 push @messages, { type => 'alert', code => 'error_on_delete_category' };
215 push @messages, { type => 'message', code => 'success_on_delete_category' };
222 if ( $op eq 'list' ) {
223 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
225 libraries => $libraries,
227 { categorytype => 'searchdomain',
228 categories => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ],
230 { categorytype => 'properties',
231 categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ],
238 messages => \@messages,
242 output_html_with_http_headers $input, $cookie, $template->output;